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.
 
 
 
 
 
 

137 lines
4.7 KiB

  1. /**
  2. * ### Massload plugin
  3. *
  4. * Adds massload functionality to jsTree, so that multiple nodes can be loaded in a single request (only useful with lazy loading).
  5. */
  6. /*globals jQuery, define, exports, require, document */
  7. (function (factory) {
  8. "use strict";
  9. if (typeof define === 'function' && define.amd) {
  10. define('jstree.massload', ['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.massload) { return; }
  21. /**
  22. * massload configuration
  23. *
  24. * It is possible to set this to a standard jQuery-like AJAX config.
  25. * In addition to the standard jQuery ajax options here you can supply functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node IDs need to be loaded, the return value of those functions will be used.
  26. *
  27. * You can also set this to a function, that function will receive the node IDs being loaded as argument and a second param which is a function (callback) which should be called with the result.
  28. *
  29. * Both the AJAX and the function approach rely on the same return value - an object where the keys are the node IDs, and the value is the children of that node as an array.
  30. *
  31. * {
  32. * "id1" : [{ "text" : "Child of ID1", "id" : "c1" }, { "text" : "Another child of ID1", "id" : "c2" }],
  33. * "id2" : [{ "text" : "Child of ID2", "id" : "c3" }]
  34. * }
  35. *
  36. * @name $.jstree.defaults.massload
  37. * @plugin massload
  38. */
  39. $.jstree.defaults.massload = null;
  40. $.jstree.plugins.massload = function (options, parent) {
  41. this.init = function (el, options) {
  42. this._data.massload = {};
  43. parent.init.call(this, el, options);
  44. };
  45. this._load_nodes = function (nodes, callback, is_callback, force_reload) {
  46. var s = this.settings.massload,
  47. nodesString = JSON.stringify(nodes),
  48. toLoad = [],
  49. m = this._model.data,
  50. i, j, dom;
  51. if (!is_callback) {
  52. for(i = 0, j = nodes.length; i < j; i++) {
  53. if(!m[nodes[i]] || ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || force_reload) ) {
  54. toLoad.push(nodes[i]);
  55. dom = this.get_node(nodes[i], true);
  56. if (dom && dom.length) {
  57. dom.addClass("jstree-loading").attr('aria-busy',true);
  58. }
  59. }
  60. }
  61. this._data.massload = {};
  62. if (toLoad.length) {
  63. if($.isFunction(s)) {
  64. return s.call(this, toLoad, $.proxy(function (data) {
  65. var i, j;
  66. if(data) {
  67. for(i in data) {
  68. if(data.hasOwnProperty(i)) {
  69. this._data.massload[i] = data[i];
  70. }
  71. }
  72. }
  73. for(i = 0, j = nodes.length; i < j; i++) {
  74. dom = this.get_node(nodes[i], true);
  75. if (dom && dom.length) {
  76. dom.removeClass("jstree-loading").attr('aria-busy',false);
  77. }
  78. }
  79. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  80. }, this));
  81. }
  82. if(typeof s === 'object' && s && s.url) {
  83. s = $.extend(true, {}, s);
  84. if($.isFunction(s.url)) {
  85. s.url = s.url.call(this, toLoad);
  86. }
  87. if($.isFunction(s.data)) {
  88. s.data = s.data.call(this, toLoad);
  89. }
  90. return $.ajax(s)
  91. .done($.proxy(function (data,t,x) {
  92. var i, j;
  93. if(data) {
  94. for(i in data) {
  95. if(data.hasOwnProperty(i)) {
  96. this._data.massload[i] = data[i];
  97. }
  98. }
  99. }
  100. for(i = 0, j = nodes.length; i < j; i++) {
  101. dom = this.get_node(nodes[i], true);
  102. if (dom && dom.length) {
  103. dom.removeClass("jstree-loading").attr('aria-busy',false);
  104. }
  105. }
  106. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  107. }, this))
  108. .fail($.proxy(function (f) {
  109. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  110. }, this));
  111. }
  112. }
  113. }
  114. return parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  115. };
  116. this._load_node = function (obj, callback) {
  117. var data = this._data.massload[obj.id],
  118. rslt = null, dom;
  119. if(data) {
  120. rslt = this[typeof data === 'string' ? '_append_html_data' : '_append_json_data'](
  121. obj,
  122. typeof data === 'string' ? $($.parseHTML(data)).filter(function () { return this.nodeType !== 3; }) : data,
  123. function (status) { callback.call(this, status); }
  124. );
  125. dom = this.get_node(obj.id, true);
  126. if (dom && dom.length) {
  127. dom.removeClass("jstree-loading").attr('aria-busy',false);
  128. }
  129. delete this._data.massload[obj.id];
  130. return rslt;
  131. }
  132. return parent._load_node.call(this, obj, callback);
  133. };
  134. };
  135. }));