You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jstree.state.js 4.5 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if($.jstree.plugins.state) { return; }
  21. var to = false;
  22. /**
  23. * stores all defaults for the state plugin
  24. * @name $.jstree.defaults.state
  25. * @plugin state
  26. */
  27. $.jstree.defaults.state = {
  28. /**
  29. * A string for the key to use when saving the current tree (change if using multiple trees in your project). Defaults to `jstree`.
  30. * @name $.jstree.defaults.state.key
  31. * @plugin state
  32. */
  33. key : 'jstree',
  34. /**
  35. * A space separated list of events that trigger a state save. Defaults to `changed.jstree open_node.jstree close_node.jstree`.
  36. * @name $.jstree.defaults.state.events
  37. * @plugin state
  38. */
  39. events : 'changed.jstree open_node.jstree close_node.jstree check_node.jstree uncheck_node.jstree',
  40. /**
  41. * Time in milliseconds after which the state will expire. Defaults to 'false' meaning - no expire.
  42. * @name $.jstree.defaults.state.ttl
  43. * @plugin state
  44. */
  45. ttl : false,
  46. /**
  47. * 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.
  48. * @name $.jstree.defaults.state.filter
  49. * @plugin state
  50. */
  51. filter : false,
  52. /**
  53. * 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`
  54. * @name $.jstree.defaults.state.preserve_loaded
  55. * @plugin state
  56. */
  57. preserve_loaded : false
  58. };
  59. $.jstree.plugins.state = function (options, parent) {
  60. this.bind = function () {
  61. parent.bind.call(this);
  62. var bind = $.proxy(function () {
  63. this.element.on(this.settings.state.events, $.proxy(function () {
  64. if(to) { clearTimeout(to); }
  65. to = setTimeout($.proxy(function () { this.save_state(); }, this), 100);
  66. }, this));
  67. /**
  68. * triggered when the state plugin is finished restoring the state (and immediately after ready if there is no state to restore).
  69. * @event
  70. * @name state_ready.jstree
  71. * @plugin state
  72. */
  73. this.trigger('state_ready');
  74. }, this);
  75. this.element
  76. .on("ready.jstree", $.proxy(function (e, data) {
  77. this.element.one("restore_state.jstree", bind);
  78. if(!this.restore_state()) { bind(); }
  79. }, this));
  80. };
  81. /**
  82. * save the state
  83. * @name save_state()
  84. * @plugin state
  85. */
  86. this.save_state = function () {
  87. var tm = this.get_state();
  88. if (!this.settings.state.preserve_loaded) {
  89. delete tm.core.loaded;
  90. }
  91. var st = { 'state' : tm, 'ttl' : this.settings.state.ttl, 'sec' : +(new Date()) };
  92. $.vakata.storage.set(this.settings.state.key, JSON.stringify(st));
  93. };
  94. /**
  95. * restore the state from the user's computer
  96. * @name restore_state()
  97. * @plugin state
  98. */
  99. this.restore_state = function () {
  100. var k = $.vakata.storage.get(this.settings.state.key);
  101. if(!!k) { try { k = JSON.parse(k); } catch(ex) { return false; } }
  102. if(!!k && k.ttl && k.sec && +(new Date()) - k.sec > k.ttl) { return false; }
  103. if(!!k && k.state) { k = k.state; }
  104. if(!!k && $.isFunction(this.settings.state.filter)) { k = this.settings.state.filter.call(this, k); }
  105. if(!!k) {
  106. if (!this.settings.state.preserve_loaded) {
  107. delete k.core.loaded;
  108. }
  109. this.element.one("set_state.jstree", function (e, data) { data.instance.trigger('restore_state', { 'state' : $.extend(true, {}, k) }); });
  110. this.set_state(k);
  111. return true;
  112. }
  113. return false;
  114. };
  115. /**
  116. * clear the state on the user's computer
  117. * @name clear_state()
  118. * @plugin state
  119. */
  120. this.clear_state = function () {
  121. return $.vakata.storage.del(this.settings.state.key);
  122. };
  123. };
  124. (function ($, undefined) {
  125. $.vakata.storage = {
  126. // simply specifying the functions in FF throws an error
  127. set : function (key, val) { return window.localStorage.setItem(key, val); },
  128. get : function (key) { return window.localStorage.getItem(key); },
  129. del : function (key) { return window.localStorage.removeItem(key); }
  130. };
  131. }($));
  132. // include the state plugin by default
  133. // $.jstree.defaults.plugins.push("state");
  134. }));