comparison vakata-jstree-3.3.5/src/jstree.massload.js @ 0:803f4888f36a draft

planemo upload commit 004a320fc0619c234164b44c64ba5dce205734e1
author mingchen0919
date Thu, 13 Dec 2018 22:46:23 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:803f4888f36a
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
21 if($.jstree.plugins.massload) { return; }
22
23 /**
24 * massload configuration
25 *
26 * It is possible to set this to a standard jQuery-like AJAX config.
27 * 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.
28 *
29 * 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.
30 *
31 * 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.
32 *
33 * {
34 * "id1" : [{ "text" : "Child of ID1", "id" : "c1" }, { "text" : "Another child of ID1", "id" : "c2" }],
35 * "id2" : [{ "text" : "Child of ID2", "id" : "c3" }]
36 * }
37 *
38 * @name $.jstree.defaults.massload
39 * @plugin massload
40 */
41 $.jstree.defaults.massload = null;
42 $.jstree.plugins.massload = function (options, parent) {
43 this.init = function (el, options) {
44 this._data.massload = {};
45 parent.init.call(this, el, options);
46 };
47 this._load_nodes = function (nodes, callback, is_callback, force_reload) {
48 var s = this.settings.massload,
49 nodesString = JSON.stringify(nodes),
50 toLoad = [],
51 m = this._model.data,
52 i, j, dom;
53 if (!is_callback) {
54 for(i = 0, j = nodes.length; i < j; i++) {
55 if(!m[nodes[i]] || ( (!m[nodes[i]].state.loaded && !m[nodes[i]].state.failed) || force_reload) ) {
56 toLoad.push(nodes[i]);
57 dom = this.get_node(nodes[i], true);
58 if (dom && dom.length) {
59 dom.addClass("jstree-loading").attr('aria-busy',true);
60 }
61 }
62 }
63 this._data.massload = {};
64 if (toLoad.length) {
65 if($.isFunction(s)) {
66 return s.call(this, toLoad, $.proxy(function (data) {
67 var i, j;
68 if(data) {
69 for(i in data) {
70 if(data.hasOwnProperty(i)) {
71 this._data.massload[i] = data[i];
72 }
73 }
74 }
75 for(i = 0, j = nodes.length; i < j; i++) {
76 dom = this.get_node(nodes[i], true);
77 if (dom && dom.length) {
78 dom.removeClass("jstree-loading").attr('aria-busy',false);
79 }
80 }
81 parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
82 }, this));
83 }
84 if(typeof s === 'object' && s && s.url) {
85 s = $.extend(true, {}, s);
86 if($.isFunction(s.url)) {
87 s.url = s.url.call(this, toLoad);
88 }
89 if($.isFunction(s.data)) {
90 s.data = s.data.call(this, toLoad);
91 }
92 return $.ajax(s)
93 .done($.proxy(function (data,t,x) {
94 var i, j;
95 if(data) {
96 for(i in data) {
97 if(data.hasOwnProperty(i)) {
98 this._data.massload[i] = data[i];
99 }
100 }
101 }
102 for(i = 0, j = nodes.length; i < j; i++) {
103 dom = this.get_node(nodes[i], true);
104 if (dom && dom.length) {
105 dom.removeClass("jstree-loading").attr('aria-busy',false);
106 }
107 }
108 parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
109 }, this))
110 .fail($.proxy(function (f) {
111 parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
112 }, this));
113 }
114 }
115 }
116 return parent._load_nodes.call(this, nodes, callback, is_callback, force_reload);
117 };
118 this._load_node = function (obj, callback) {
119 var data = this._data.massload[obj.id],
120 rslt = null, dom;
121 if(data) {
122 rslt = this[typeof data === 'string' ? '_append_html_data' : '_append_json_data'](
123 obj,
124 typeof data === 'string' ? $($.parseHTML(data)).filter(function () { return this.nodeType !== 3; }) : data,
125 function (status) { callback.call(this, status); }
126 );
127 dom = this.get_node(obj.id, true);
128 if (dom && dom.length) {
129 dom.removeClass("jstree-loading").attr('aria-busy',false);
130 }
131 delete this._data.massload[obj.id];
132 return rslt;
133 }
134 return parent._load_node.call(this, obj, callback);
135 };
136 };
137 }));