cirandas.net

ref: master

plugins/orders/public/javascripts/help.js


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// see also: HelpHelper methods

help = {
  selector: '.hideable-help',
  link_selector: '.hideable-help-link',

  cookie: {
    key: function(text) {
      return 'hide-help-'+text.hashCode()
    },
    get: function(text) {
      var hide = jQuery.cookie(this.key(text))
      return hide == 'true' ? true : false
    },
    set: function(text, hide) {
      hide = hide ? 'true' : 'false';
      return jQuery.cookie(this.key(text), hide)
    }
  },

  apply_all: function() {
    jQuery(this.selector).each(function (i, container) {
      container = jQuery(container)
      var hide = help.cookie.get(container.text())
      container.toggle(!hide)
      var link = container.siblings(this.link_selector)
      help.apply(link)
    });
  },

  apply: function(link) {
    link = jQuery(link)
    var container = link.siblings(this.selector)

    var isShown = container.is(':visible')
    link.text(link.attr(isShown ? 'data-hide' : 'data-show'))
    container.toggle(isShown);
  },

  toggle: function(link) {
    link = jQuery(link)
    var container = link.siblings(this.selector)

    container.toggle()
    var hide = !container.is(':visible')

    help.cookie.set(container.text(), hide)
    this.apply(link)
  },

};

jQuery(document).ready(function() {
  help.apply_all()
});

String.prototype.hashCode = function() {
  var hash = 0, i, chr, len;
  if (this.length == 0) return hash;
  for (i = 0, len = this.length; i < len; i++) {
    chr   = this.charCodeAt(i);
    hash  = ((hash << 5) - hash) + chr;
    hash |= 0; // Convert to 32bit integer
  }
  return hash;
};