view app/assets/javascripts/zp.hash_magic.js @ 301:2fb00d365745

Add set -x to view commands executed for test.
author edogawaconan <me@myconan.net>
date Wed, 12 Feb 2014 15:44:36 +0900
parents 77c157a9e645
children e855e58d137e
line wrap: on
line source

//= require zp.formatter
(function($) {
  window.zp.hash_magic = {
    load_hash: function() {
      var
        format = window.location.hash.slice(1),
        tab_link = function(fmt) {
                     return zp.formatter.links().filter("[href='#" + fmt + "']")
                   }
      if(!tab_link(format).length) format = "pl"
      tab_link(format).click()
    },
    set_hash: function(e) {
      if (typeof e !== "undefined") {
        if (zp.formatter.links().filter(e.target).length === 0) return
        else e.preventDefault()
      }
      var
        format = zp.formatter.current(),
        hash = "#"
      hash += format
      if (hash === "#pl") { hash = " " }
      history.replaceState(null, "", hash)
    },
    init: function() {
      if (!$("#paste").length) return
      this.load_hash()
      if (history.replaceState) {
        this.set_hash()
        $(document).on('click', $.proxy(this.set_hash, this))
      }
    }
  }
})(jQuery)