| 875 |
lars |
1 |
/**
|
|
|
2 |
* ### State plugin
|
|
|
3 |
*
|
|
|
4 |
* Saves the state of the tree (selected nodes, opened nodes) on the user's computer using available options (localStorage, cookies, etc)
|
|
|
5 |
*/
|
|
|
6 |
/*globals jQuery, define, exports, require */
|
|
|
7 |
(function (factory) {
|
|
|
8 |
"use strict";
|
|
|
9 |
if (typeof define === 'function' && define.amd) {
|
|
|
10 |
define('jstree.state', ['jquery','jstree'], factory);
|
|
|
11 |
}
|
|
|
12 |
else if(typeof exports === 'object') {
|
|
|
13 |
factory(require('jquery'), require('jstree'));
|
|
|
14 |
}
|
|
|
15 |
else {
|
|
|
16 |
factory(jQuery, jQuery.jstree);
|
|
|
17 |
}
|
|
|
18 |
}(function ($, jstree, undefined) {
|
|
|
19 |
"use strict";
|
|
|
20 |
|
|
|
21 |
if($.jstree.plugins.state) { return; }
|
|
|
22 |
|
|
|
23 |
var to = false;
|
|
|
24 |
/**
|
|
|
25 |
* stores all defaults for the state plugin
|
|
|
26 |
* @name $.jstree.defaults.state
|
|
|
27 |
* @plugin state
|
|
|
28 |
*/
|
|
|
29 |
$.jstree.defaults.state = {
|
|
|
30 |
/**
|
|
|
31 |
* A string for the key to use when saving the current tree (change if using multiple trees in your project). Defaults to `jstree`.
|
|
|
32 |
* @name $.jstree.defaults.state.key
|
|
|
33 |
* @plugin state
|
|
|
34 |
*/
|
|
|
35 |
key : 'jstree',
|
|
|
36 |
/**
|
|
|
37 |
* A space separated list of events that trigger a state save. Defaults to `changed.jstree open_node.jstree close_node.jstree`.
|
|
|
38 |
* @name $.jstree.defaults.state.events
|
|
|
39 |
* @plugin state
|
|
|
40 |
*/
|
|
|
41 |
events : 'changed.jstree open_node.jstree close_node.jstree check_node.jstree uncheck_node.jstree',
|
|
|
42 |
/**
|
|
|
43 |
* Time in milliseconds after which the state will expire. Defaults to 'false' meaning - no expire.
|
|
|
44 |
* @name $.jstree.defaults.state.ttl
|
|
|
45 |
* @plugin state
|
|
|
46 |
*/
|
|
|
47 |
ttl : false,
|
|
|
48 |
/**
|
|
|
49 |
* A function that will be executed prior to restoring state with one argument - the state object. Can be used to clear unwanted parts of the state.
|
|
|
50 |
* @name $.jstree.defaults.state.filter
|
|
|
51 |
* @plugin state
|
|
|
52 |
*/
|
|
|
53 |
filter : false
|
|
|
54 |
};
|
|
|
55 |
$.jstree.plugins.state = function (options, parent) {
|
|
|
56 |
this.bind = function () {
|
|
|
57 |
parent.bind.call(this);
|
|
|
58 |
var bind = $.proxy(function () {
|
|
|
59 |
this.element.on(this.settings.state.events, $.proxy(function () {
|
|
|
60 |
if(to) { clearTimeout(to); }
|
|
|
61 |
to = setTimeout($.proxy(function () { this.save_state(); }, this), 100);
|
|
|
62 |
}, this));
|
|
|
63 |
/**
|
|
|
64 |
* triggered when the state plugin is finished restoring the state (and immediately after ready if there is no state to restore).
|
|
|
65 |
* @event
|
|
|
66 |
* @name state_ready.jstree
|
|
|
67 |
* @plugin state
|
|
|
68 |
*/
|
|
|
69 |
this.trigger('state_ready');
|
|
|
70 |
}, this);
|
|
|
71 |
this.element
|
|
|
72 |
.on("ready.jstree", $.proxy(function (e, data) {
|
|
|
73 |
this.element.one("restore_state.jstree", bind);
|
|
|
74 |
if(!this.restore_state()) { bind(); }
|
|
|
75 |
}, this));
|
|
|
76 |
};
|
|
|
77 |
/**
|
|
|
78 |
* save the state
|
|
|
79 |
* @name save_state()
|
|
|
80 |
* @plugin state
|
|
|
81 |
*/
|
|
|
82 |
this.save_state = function () {
|
|
|
83 |
var st = { 'state' : this.get_state(), 'ttl' : this.settings.state.ttl, 'sec' : +(new Date()) };
|
|
|
84 |
$.vakata.storage.set(this.settings.state.key, JSON.stringify(st));
|
|
|
85 |
};
|
|
|
86 |
/**
|
|
|
87 |
* restore the state from the user's computer
|
|
|
88 |
* @name restore_state()
|
|
|
89 |
* @plugin state
|
|
|
90 |
*/
|
|
|
91 |
this.restore_state = function () {
|
|
|
92 |
var k = $.vakata.storage.get(this.settings.state.key);
|
|
|
93 |
if(!!k) { try { k = JSON.parse(k); } catch(ex) { return false; } }
|
|
|
94 |
if(!!k && k.ttl && k.sec && +(new Date()) - k.sec > k.ttl) { return false; }
|
|
|
95 |
if(!!k && k.state) { k = k.state; }
|
|
|
96 |
if(!!k && $.isFunction(this.settings.state.filter)) { k = this.settings.state.filter.call(this, k); }
|
|
|
97 |
if(!!k) {
|
|
|
98 |
this.element.one("set_state.jstree", function (e, data) { data.instance.trigger('restore_state', { 'state' : $.extend(true, {}, k) }); });
|
|
|
99 |
this.set_state(k);
|
|
|
100 |
return true;
|
|
|
101 |
}
|
|
|
102 |
return false;
|
|
|
103 |
};
|
|
|
104 |
/**
|
|
|
105 |
* clear the state on the user's computer
|
|
|
106 |
* @name clear_state()
|
|
|
107 |
* @plugin state
|
|
|
108 |
*/
|
|
|
109 |
this.clear_state = function () {
|
|
|
110 |
return $.vakata.storage.del(this.settings.state.key);
|
|
|
111 |
};
|
|
|
112 |
};
|
|
|
113 |
|
|
|
114 |
(function ($, undefined) {
|
|
|
115 |
$.vakata.storage = {
|
|
|
116 |
// simply specifying the functions in FF throws an error
|
|
|
117 |
set : function (key, val) { return window.localStorage.setItem(key, val); },
|
|
|
118 |
get : function (key) { return window.localStorage.getItem(key); },
|
|
|
119 |
del : function (key) { return window.localStorage.removeItem(key); }
|
|
|
120 |
};
|
|
|
121 |
}($));
|
|
|
122 |
|
|
|
123 |
// include the state plugin by default
|
|
|
124 |
// $.jstree.defaults.plugins.push("state");
|
|
|
125 |
}));
|