ultimix
jquery.tree.cookie.js
Go to the documentation of this file.
1 (function ($) {
2  if(typeof $.cookie == "undefined") throw "jsTree cookie: jQuery cookie plugin not included.";
3 
4  $.extend($.tree.plugins, {
5  "cookie" : {
6  defaults : {
7  prefix : "", // a prefix that will be used for all cookies for this tree
8  options : {
9  expires: false,
10  path: false,
11  domain: false,
12  secure: false
13  },
14  types : {
15  selected : true, // should we set the selected cookie
16  open : true // should we set the open cookie
17  },
18  keep_selected : false, // should we merge with the selected option or overwrite it
19  keep_opened : false // should we merge with the opened option or overwrite it
20  },
21  set_cookie : function (type) {
22  var opts = $.extend(true, {}, $.tree.plugins.cookie.defaults, this.settings.plugins.cookie);
23  if(opts.types[type] !== true) return false;
24  switch(type) {
25  case "selected":
26  if(this.settings.rules.multiple != false && this.selected_arr.length > 1) {
27  var val = Array();
28  $.each(this.selected_arr, function () {
29  if(this.attr("id")) { val.push(this.attr("id")); }
30  });
31  val = val.join(",");
32  }
33  else var val = this.selected ? this.selected.attr("id") : false;
34  $.cookie(opts.prefix + 'selected', val, opts.options);
35  break;
36  case "open":
37  var str = "";
38  this.container.find("li.open").each(function (i) { if(this.id) { str += this.id + ","; } });
39  $.cookie(opts.prefix + 'open', str.replace(/,$/ig,""), opts.options);
40  break;
41  }
42  },
43  callbacks : {
44  oninit : function (t) {
45  var opts = $.extend(true, {}, $.tree.plugins.cookie.defaults, this.settings.plugins.cookie);
46  var tmp = false;
47  tmp = $.cookie(opts.prefix + 'open');
48  if(tmp) {
49  tmp = tmp.split(",");
50  if(opts.keep_opened) this.settings.opened = $.unique($.merge(tmp, this.settings.opened));
51  else this.settings.opened = tmp;
52  }
53  tmp = $.cookie(opts.prefix + 'selected');
54  if(tmp) {
55  tmp = tmp.split(",");
56  if(opts.keep_selected) this.settings.selected = $.unique($.merge(tmp, this.settings.opened));
57  else this.settings.selected = tmp;
58  }
59  },
60  onchange : function() { $.tree.plugins.cookie.set_cookie.apply(this, ["selected"]); },
61  onopen : function() { $.tree.plugins.cookie.set_cookie.apply(this, ["open"]); },
62  onclose : function() { $.tree.plugins.cookie.set_cookie.apply(this, ["open"]); },
63  ondelete : function() { $.tree.plugins.cookie.set_cookie.apply(this, ["open"]); },
64  oncopy : function() { $.tree.plugins.cookie.set_cookie.apply(this, ["open"]); },
65  oncreate : function() { $.tree.plugins.cookie.set_cookie.apply(this, ["open"]); },
66  onmoved : function() { $.tree.plugins.cookie.set_cookie.apply(this, ["open"]); }
67  }
68  }
69  });
70 })(jQuery);