comparison vakata-jstree-3.3.5/src/jstree.state.js @ 5:aacd5f53ac99 draft

v2.0.0
author mingchen0919
date Wed, 18 Apr 2018 13:17:28 -0400
parents
children
comparison
equal deleted inserted replaced
4:0fdb0d5f53ce 5:aacd5f53ac99
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 * Should loaded nodes be restored (setting this to true means that it is possible that the whole tree will be loaded for some users - use with caution). Defaults to `false`
56 * @name $.jstree.defaults.state.preserve_loaded
57 * @plugin state
58 */
59 preserve_loaded : false
60 };
61 $.jstree.plugins.state = function (options, parent) {
62 this.bind = function () {
63 parent.bind.call(this);
64 var bind = $.proxy(function () {
65 this.element.on(this.settings.state.events, $.proxy(function () {
66 if(to) { clearTimeout(to); }
67 to = setTimeout($.proxy(function () { this.save_state(); }, this), 100);
68 }, this));
69 /**
70 * triggered when the state plugin is finished restoring the state (and immediately after ready if there is no state to restore).
71 * @event
72 * @name state_ready.jstree
73 * @plugin state
74 */
75 this.trigger('state_ready');
76 }, this);
77 this.element
78 .on("ready.jstree", $.proxy(function (e, data) {
79 this.element.one("restore_state.jstree", bind);
80 if(!this.restore_state()) { bind(); }
81 }, this));
82 };
83 /**
84 * save the state
85 * @name save_state()
86 * @plugin state
87 */
88 this.save_state = function () {
89 var tm = this.get_state();
90 if (!this.settings.state.preserve_loaded) {
91 delete tm.core.loaded;
92 }
93 var st = { 'state' : tm, 'ttl' : this.settings.state.ttl, 'sec' : +(new Date()) };
94 $.vakata.storage.set(this.settings.state.key, JSON.stringify(st));
95 };
96 /**
97 * restore the state from the user's computer
98 * @name restore_state()
99 * @plugin state
100 */
101 this.restore_state = function () {
102 var k = $.vakata.storage.get(this.settings.state.key);
103 if(!!k) { try { k = JSON.parse(k); } catch(ex) { return false; } }
104 if(!!k && k.ttl && k.sec && +(new Date()) - k.sec > k.ttl) { return false; }
105 if(!!k && k.state) { k = k.state; }
106 if(!!k && $.isFunction(this.settings.state.filter)) { k = this.settings.state.filter.call(this, k); }
107 if(!!k) {
108 if (!this.settings.state.preserve_loaded) {
109 delete k.core.loaded;
110 }
111 this.element.one("set_state.jstree", function (e, data) { data.instance.trigger('restore_state', { 'state' : $.extend(true, {}, k) }); });
112 this.set_state(k);
113 return true;
114 }
115 return false;
116 };
117 /**
118 * clear the state on the user's computer
119 * @name clear_state()
120 * @plugin state
121 */
122 this.clear_state = function () {
123 return $.vakata.storage.del(this.settings.state.key);
124 };
125 };
126
127 (function ($, undefined) {
128 $.vakata.storage = {
129 // simply specifying the functions in FF throws an error
130 set : function (key, val) { return window.localStorage.setItem(key, val); },
131 get : function (key) { return window.localStorage.getItem(key); },
132 del : function (key) { return window.localStorage.removeItem(key); }
133 };
134 }($));
135
136 // include the state plugin by default
137 // $.jstree.defaults.plugins.push("state");
138 }));