diff app/assets/javascripts/zp.hash_magic.js.coffee @ 349:f802099acd37

coffee: hash_magic.
author edogawaconan <me@myconan.net>
date Thu, 03 Jul 2014 15:03:34 +0900
parents app/assets/javascripts/zp.hash_magic.js@4b91c0c1938c
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/assets/javascripts/zp.hash_magic.js.coffee	Thu Jul 03 15:03:34 2014 +0900
@@ -0,0 +1,21 @@
+#= require zp.formatter
+window.zp.hash_magic =
+  load_hash: ->
+    format = window.location.hash.slice(1)
+    tab_link = (fmt) -> zp.formatter.links().filter("[href='##{fmt}']")
+
+    format = "pl" unless tab_link(format).length
+    tab_link(format).click()
+
+  set_hash: (e) ->
+    format = zp.formatter.current()
+    newpath = window.location.pathname
+    newpath += "##{format}" if (format != "pl")
+    history.replaceState null, "", newpath
+
+  init: ->
+    return unless $("#paste").length
+    @load_hash()
+    if (history.replaceState)
+      @set_hash()
+      zp.formatter.links().on("shown.bs.tab", this.set_hash)