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