jstree.massload.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.js'], factory);
  11. }
  12. else if(typeof exports === 'object') {
  13. factory(require('jquery'), require('./jstree.js'));
  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. toLoad = [],
  48. m = this._model.data,
  49. i, j, dom;
  50. if (!is_callback) {
  51. for(i = 0, j = nodes.length; i < j; i++) {
  52. if(!m[nodes[i]] || ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || force_reload) ) {
  53. toLoad.push(nodes[i]);
  54. dom = this.get_node(nodes[i], true);
  55. if (dom && dom.length) {
  56. dom.addClass("jstree-loading").attr('aria-busy',true);
  57. }
  58. }
  59. }
  60. this._data.massload = {};
  61. if (toLoad.length) {
  62. if($.vakata.is_function(s)) {
  63. return s.call(this, toLoad, function (data) {
  64. var i, j;
  65. if(data) {
  66. for(i in data) {
  67. if(data.hasOwnProperty(i)) {
  68. this._data.massload[i] = data[i];
  69. }
  70. }
  71. }
  72. for(i = 0, j = nodes.length; i < j; i++) {
  73. dom = this.get_node(nodes[i], true);
  74. if (dom && dom.length) {
  75. dom.removeClass("jstree-loading").attr('aria-busy',false);
  76. }
  77. }
  78. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  79. }.bind(this));
  80. }
  81. if(typeof s === 'object' && s && s.url) {
  82. s = $.extend(true, {}, s);
  83. if($.vakata.is_function(s.url)) {
  84. s.url = s.url.call(this, toLoad);
  85. }
  86. if($.vakata.is_function(s.data)) {
  87. s.data = s.data.call(this, toLoad);
  88. }
  89. return $.ajax(s)
  90. .done(function (data,t,x) {
  91. var i, j;
  92. if(data) {
  93. for(i in data) {
  94. if(data.hasOwnProperty(i)) {
  95. this._data.massload[i] = data[i];
  96. }
  97. }
  98. }
  99. for(i = 0, j = nodes.length; i < j; i++) {
  100. dom = this.get_node(nodes[i], true);
  101. if (dom && dom.length) {
  102. dom.removeClass("jstree-loading").attr('aria-busy',false);
  103. }
  104. }
  105. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  106. }.bind(this))
  107. .fail(function (f) {
  108. parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  109. }.bind(this));
  110. }
  111. }
  112. }
  113. return parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
  114. };
  115. this._load_node = function (obj, callback) {
  116. var data = this._data.massload[obj.id],
  117. rslt = null, dom;
  118. if(data) {
  119. rslt = this[typeof data === 'string' ? '_append_html_data' : '_append_json_data'](
  120. obj,
  121. typeof data === 'string' ? $($.parseHTML(data)).filter(function () { return this.nodeType !== 3; }) : data,
  122. function (status) { callback.call(this, status); }
  123. );
  124. dom = this.get_node(obj.id, true);
  125. if (dom && dom.length) {
  126. dom.removeClass("jstree-loading").attr('aria-busy',false);
  127. }
  128. delete this._data.massload[obj.id];
  129. return rslt;
  130. }
  131. return parent._load_node.call(this, obj, callback);
  132. };
  133. };
  134. }));