annotate vakata-jstree-3.3.5/src/jstree.massload.js @ 0:b8c76b06d724 draft

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