comparison vakata-jstree-3.3.5/src/misc.js @ 0:a42e58c71e5b draft default tip

planemo upload commit 841d8b22bf9f1aaed6bfe8344b60617f45b275b2-dirty
author mingchen0919
date Sun, 30 Dec 2018 12:55:49 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a42e58c71e5b
1 /* global jQuery */
2
3 // disable all events
4 (function ($, undefined) {
5 "use strict";
6 $.jstree.plugins.trigger = function (options, parent) {
7 this.init = function (el, options) {
8 // do not forget parent
9 parent.init.call(this, el, options);
10 this._data.trigger.disabled = false;
11 };
12 this.trigger = function (ev, data) {
13 if(!this._data.trigger.disabled) {
14 parent.trigger.call(this, ev, data);
15 }
16 };
17 this.disable_events = function () { this._data.trigger.disabled = true; };
18 this.enable_events = function () { this._data.trigger.disabled = false; };
19 };
20 })(jQuery);
21
22 // mapping
23 (function ($, undefined) {
24 "use strict";
25 // use this if you need any options
26 $.jstree.defaults.mapper = {
27 option_key : "option_value"
28 };
29 $.jstree.plugins.mapper = function () {
30 this._parse_model_from_json = function (d, p, ps) {
31 // d is the node from the server, it will be called recursively for children,
32 // so you do not need to process at once
33 /* // for example
34 for(var i in d) {
35 if(d.hasOwnProperty(i)) {
36 d[i.toLowerCase()] = d[i];
37 }
38 }
39 */
40 return parent._parse_model_from_json.call(this, d, p, ps);
41 };
42 };
43 })(jQuery);
44
45 // no hover
46 (function ($, undefined) {
47 "use strict";
48 $.jstree.plugins.nohover = function () {
49 this.hover_node = $.noop;
50 };
51 })(jQuery);
52
53 // force multiple select
54 (function ($, undefined) {
55 "use strict";
56 $.jstree.defaults.multiselect = {};
57 $.jstree.plugins.multiselect = function (options, parent) {
58 this.activate_node = function (obj, e) {
59 e.ctrlKey = true;
60 parent.activate_node.call(this, obj, e);
61 };
62 };
63 })(jQuery);
64
65 // real checkboxes
66 (function ($, undefined) {
67 "use strict";
68
69 var inp = document.createElement("INPUT");
70 inp.type = "checkbox";
71 inp.className = "jstree-checkbox jstree-realcheckbox";
72
73 $.jstree.defaults.realcheckboxes = {};
74
75 $.jstree.plugins.realcheckboxes = function (options, parent) {
76 this.bind = function () {
77 parent.bind.call(this);
78 this._data.realcheckboxes.uto = false;
79 this.element
80 .on('changed.jstree uncheck_node.jstree check_node.jstree uncheck_all.jstree check_all.jstree move_node.jstree copy_node.jstree redraw.jstree open_node.jstree ready.jstree loaded.jstree', $.proxy(function () {
81 // only if undetermined is in setting
82 if(this._data.realcheckboxes.uto) { clearTimeout(this._data.realcheckboxes.uto); }
83 this._data.realcheckboxes.uto = setTimeout($.proxy(this._realcheckboxes, this), 50);
84 }, this));
85 };
86 this.redraw_node = function(obj, deep, callback, force_draw) {
87 obj = parent.redraw_node.call(this, obj, deep, callback, force_draw);
88 if(obj) {
89 var i, j, tmp = null, chk = inp.cloneNode(true);
90 for(i = 0, j = obj.childNodes.length; i < j; i++) {
91 if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
92 tmp = obj.childNodes[i];
93 break;
94 }
95 }
96 if(tmp) {
97 for(i = 0, j = tmp.childNodes.length; i < j; i++) {
98 if(tmp.childNodes[i] && tmp.childNodes[i].className && tmp.childNodes[i].className.indexOf("jstree-checkbox") !== -1) {
99 tmp = tmp.childNodes[i];
100 break;
101 }
102 }
103 }
104 if(tmp && tmp.tagName === "I") {
105 tmp.style.backgroundColor = "transparent";
106 tmp.style.backgroundImage = "none";
107 tmp.appendChild(chk);
108 }
109 }
110 return obj;
111 };
112 this._realcheckboxes = function () {
113 var ts = this.settings.checkbox.tie_selection;
114 console.log(ts);
115 $('.jstree-realcheckbox').each(function () {
116 this.checked = (!ts && this.parentNode.parentNode.className.indexOf("jstree-checked") !== -1) || (ts && this.parentNode.parentNode.className.indexOf('jstree-clicked') !== -1);
117 this.indeterminate = this.parentNode.className.indexOf("jstree-undetermined") !== -1;
118 this.disabled = this.parentNode.parentNode.className.indexOf("disabled") !== -1;
119 });
120 };
121 };
122 })(jQuery);
123
124 // no state
125 (function ($, undefined) {
126 "use strict";
127 $.jstree.plugins.nostate = function () {
128 this.set_state = function (state, callback) {
129 if(callback) { callback.call(this); }
130 this.trigger('set_state');
131 };
132 };
133 })(jQuery);
134
135 // no selected in state
136 (function ($, undefined) {
137 "use strict";
138 $.jstree.plugins.noselectedstate = function (options, parent) {
139 this.get_state = function () {
140 var state = parent.get_state.call(this);
141 delete state.core.selected;
142 return state;
143 };
144 };
145 })(jQuery);
146
147 // additional icon on node (outside of anchor)
148 (function ($, undefined) {
149 "use strict";
150 var img = document.createElement('IMG');
151 //img.src = "http://www.dpcd.vic.gov.au/__data/assets/image/0004/30667/help.gif";
152 img.className = "jstree-questionmark";
153
154 $.jstree.defaults.questionmark = $.noop;
155 $.jstree.plugins.questionmark = function (options, parent) {
156 this.bind = function () {
157 parent.bind.call(this);
158 this.element
159 .on("click.jstree", ".jstree-questionmark", $.proxy(function (e) {
160 e.stopImmediatePropagation();
161 this.settings.questionmark.call(this, this.get_node(e.target));
162 }, this));
163 };
164 this.teardown = function () {
165 if(this.settings.questionmark) {
166 this.element.find(".jstree-questionmark").remove();
167 }
168 parent.teardown.call(this);
169 };
170 this.redraw_node = function(obj, deep, callback, force_draw) {
171 obj = parent.redraw_node.call(this, obj, deep, callback, force_draw);
172 if(obj) {
173 var tmp = img.cloneNode(true);
174 obj.insertBefore(tmp, obj.childNodes[2]);
175 }
176 return obj;
177 };
178 };
179 })(jQuery);
180
181 // auto numbering
182 (function ($, undefined) {
183 "use strict";
184 var span = document.createElement('SPAN');
185 span.className = "jstree-numbering";
186
187 $.jstree.defaults.numbering = {};
188 $.jstree.plugins.numbering = function (options, parent) {
189 this.teardown = function () {
190 if(this.settings.questionmark) {
191 this.element.find(".jstree-numbering").remove();
192 }
193 parent.teardown.call(this);
194 };
195 this.get_number = function (obj) {
196 obj = this.get_node(obj);
197 var ind = $.inArray(obj.id, this.get_node(obj.parent).children) + 1;
198 return obj.parent === '#' ? ind : this.get_number(obj.parent) + '.' + ind;
199 };
200 this.redraw_node = function(obj, deep, callback, force_draw) {
201 var i, j, tmp = null, elm = null, org = this.get_number(obj);
202 obj = parent.redraw_node.call(this, obj, deep, callback, force_draw);
203 if(obj) {
204 for(i = 0, j = obj.childNodes.length; i < j; i++) {
205 if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
206 tmp = obj.childNodes[i];
207 break;
208 }
209 }
210 if(tmp) {
211 elm = span.cloneNode(true);
212 elm.innerHTML = org + '. ';
213 tmp.insertBefore(elm, tmp.childNodes[tmp.childNodes.length - 1]);
214 }
215 }
216 return obj;
217 };
218 };
219 })(jQuery);
220
221 // additional icon on node (inside anchor)
222 (function ($, undefined) {
223 "use strict";
224 var _s = document.createElement('SPAN');
225 _s.className = 'fa-stack jstree-stackedicon';
226 var _i = document.createElement('I');
227 _i.className = 'jstree-icon';
228 _i.setAttribute('role', 'presentation');
229
230 $.jstree.plugins.stackedicon = function (options, parent) {
231 this.teardown = function () {
232 this.element.find(".jstree-stackedicon").remove();
233 parent.teardown.call(this);
234 };
235 this.redraw_node = function(obj, deep, is_callback, force_render) {
236 obj = parent.redraw_node.apply(this, arguments);
237 if(obj) {
238 var i, j, tmp = null, icon = null, temp = null;
239 for(i = 0, j = obj.childNodes.length; i < j; i++) {
240 if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
241 tmp = obj.childNodes[i];
242 break;
243 }
244 }
245 if(tmp) {
246 if(this._model.data[obj.id].state.icons && this._model.data[obj.id].state.icons.length) {
247 icon = _s.cloneNode(false);
248 for(i = 0, j = this._model.data[obj.id].state.icons.length; i < j; i++) {
249 temp = _i.cloneNode(false);
250 temp.className += ' ' + this._model.data[obj.id].state.icons[i];
251 icon.appendChild(temp);
252 }
253 tmp.insertBefore(icon, tmp.childNodes[0]);
254 }
255 }
256 }
257 return obj;
258 };
259 };
260 })(jQuery);
261
262 // selecting a node opens it
263 (function ($, undefined) {
264 "use strict";
265 $.jstree.plugins.selectopens = function (options, parent) {
266 this.bind = function () {
267 parent.bind.call(this);
268 this.element.on('select_node.jstree', function (e, data) { data.instance.open_node(data.node); });
269 };
270 };
271 })(jQuery);
272
273 // object as data
274 (function ($, undefined) {
275 "use strict";
276 $.jstree.defaults.datamodel = {};
277 $.jstree.plugins.datamodel = function (options, parent) {
278 this.init = function (el, options) {
279 this._data.datamodel = {};
280 parent.init.call(this, el, options);
281 };
282 this._datamodel = function (id, nodes, callback) {
283 var i = 0, j = nodes.length, tmp = [], obj = null;
284 for(; i < j; i++) {
285 this._data.datamodel[nodes[i].getID()] = nodes[i];
286 obj = {
287 id : nodes[i].getID(),
288 text : nodes[i].getText(),
289 children : nodes[i].hasChildren()
290 };
291 if(nodes[i].getExtra) {
292 obj = nodes[i].getExtra(obj); // icon, type
293 }
294 tmp.push(obj);
295 }
296 return this._append_json_data(id, tmp, $.proxy(function (status) {
297 callback.call(this, status);
298 }, this));
299 };
300 this._load_node = function (obj, callback) {
301 var id = obj.id;
302 var nd = obj.id === "#" ? this.settings.core.data : this._data.datamodel[obj.id].getChildren($.proxy(function (nodes) {
303 this._datamodel(id, nodes, callback);
304 }, this));
305 if($.isArray(nd)) {
306 this._datamodel(id, nd, callback);
307 }
308 };
309 };
310 })(jQuery);
311 /*
312 demo of the above
313 function treeNode(val) {
314 var id = ++treeNode.counter;
315 this.getID = function () {
316 return id;
317 };
318 this.getText = function () {
319 return val.toString();
320 };
321 this.getExtra = function (obj) {
322 obj.icon = false;
323 return obj;
324 };
325 this.hasChildren = function () {
326 return true;
327 };
328 this.getChildren = function () {
329 return [
330 new treeNode(Math.pow(val, 2)),
331 new treeNode(Math.sqrt(val)),
332 ];
333 };
334 }
335 treeNode.counter = 0;
336
337 $('#jstree').jstree({
338 'core': {
339 'data': [
340 new treeNode(2),
341 new treeNode(3),
342 new treeNode(4),
343 new treeNode(5)
344 ]
345 },
346 plugins : ['datamodel']
347 });
348 */
349
350 // untested sample plugin to keep all nodes in the DOM
351 (function ($, undefined) {
352 "use strict";
353 $.jstree.plugins.dom = function (options, parent) {
354 this.redraw_node = function (node, deep, is_callback, force_render) {
355 return parent.redraw_node.call(this, node, deep, is_callback, true);
356 };
357 this.close_node = function (obj, animation) {
358 var t1, t2, t, d;
359 if($.isArray(obj)) {
360 obj = obj.slice();
361 for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
362 this.close_node(obj[t1], animation);
363 }
364 return true;
365 }
366 obj = this.get_node(obj);
367 if(!obj || obj.id === $.jstree.root) {
368 return false;
369 }
370 if(this.is_closed(obj)) {
371 return false;
372 }
373 animation = animation === undefined ? this.settings.core.animation : animation;
374 t = this;
375 d = this.get_node(obj, true);
376 if(d.length) {
377 if(!animation) {
378 d[0].className = d[0].className.replace('jstree-open', 'jstree-closed');
379 d.attr("aria-expanded", false);
380 }
381 else {
382 d
383 .children(".jstree-children").attr("style","display:block !important").end()
384 .removeClass("jstree-open").addClass("jstree-closed").attr("aria-expanded", false)
385 .children(".jstree-children").stop(true, true).slideUp(animation, function () {
386 this.style.display = "";
387 t.trigger("after_close", { "node" : obj });
388 });
389 }
390 }
391 obj.state.opened = false;
392 this.trigger('close_node',{ "node" : obj });
393 if(!animation || !d.length) {
394 this.trigger("after_close", { "node" : obj });
395 }
396 };
397 };
398 })(jQuery);
399
400 // customize plugin by @Lusito
401 // https://github.com/Lusito/jstree/blob/node-customize/src/jstree-node-customize.js
402 /**
403 * ### Node Customize plugin
404 *
405 * Allows to customize nodes when they are drawn.
406 */
407 (function (factory) {
408 "use strict";
409 if (typeof define === 'function' && define.amd) {
410 define('jstree.node_customize', ['jquery','jstree'], factory);
411 }
412 else if(typeof exports === 'object') {
413 factory(require('jquery'), require('jstree'));
414 }
415 else {
416 factory(jQuery, jQuery.jstree);
417 }
418 }(function ($, jstree, undefined) {
419 "use strict";
420
421 if($.jstree.plugins.node_customize) { return; }
422
423 /**
424 * the settings object.
425 * key is the attribute name to select the customizer function from switch.
426 * switch is a key => function(el, node) map.
427 * default: function(el, node) will be called if the type could not be mapped
428 * @name $.jstree.defaults.node_customize
429 * @plugin node_customize
430 */
431 $.jstree.defaults.node_customize = {
432 "key": "type",
433 "switch": {},
434 "default": null
435 };
436
437 $.jstree.plugins.node_customize = function (options, parent) {
438 this.redraw_node = function (obj, deep, callback, force_draw) {
439 var node_id = obj;
440 var el = parent.redraw_node.apply(this, arguments);
441 if (el) {
442 var node = this._model.data[node_id];
443 var cfg = this.settings.node_customize;
444 var key = cfg.key;
445 var type = (node && node.original && node.original[key]);
446 var customizer = (type && cfg.switch[type]) || cfg.default;
447 if(customizer)
448 customizer(el, node);
449 }
450 return el;
451 };
452 }
453 }));
454
455
456 // parentsload plugin by @ashl1
457 /**
458 * ### Parentsload plugin
459 *
460 * Change load_node() functionality in jsTree, to possible load not yes downloaded node with all it parent in a single request (only useful with lazy loading).
461 *
462 * version 1.0.0 (Alexey Shildyakov - ashl1future@gmail.com)
463 * 2015: Compatible with jsTree-3.2.1
464 */
465 /*globals jQuery, define, exports, require, document */
466 (function (factory) {
467 "use strict";
468 if (typeof define === 'function' && define.amd) {
469 define('jstree.parentsload', ['jquery','jstree'], factory);
470 }
471 else if(typeof exports === 'object') {
472 factory(require('jquery'), require('jstree'));
473 }
474 else {
475 factory(jQuery, jQuery.jstree);
476 }
477 }(function ($, jstree, undefined) {
478 "use strict";
479
480 if($.jstree.plugins.parentsload) { return; }
481
482 /**
483 * parentsload configuration
484 *
485 * The configuration syntax is almost the same as for core.data option. You must set parenstload.data the following:
486 *
487 * parentsload: {
488 * data: function(){} // this function overwrites core data.data options
489 * }
490 *
491 * OR
492 *
493 * parentsload: {
494 * data: {
495 * url: function(node){} OR string,
496 * data: function(node){} OR associative array as json{data} jQuery parameter
497 * }
498 * }
499 *
500 * In last case at least on of 'url' or 'data' must be presented.
501 *
502 * At first, the plugin load_node() detects if the node already downloaded. If is - uses the core.data settings, if not - uses parentsload.data settings
503 * to fetch in one query the specified node and all its parent. The data must be in the first mentioned JSON format with set nested children[].
504 * Each node level should consist of all nodes on the level to properly work with the tree in the future. Otherwise, you must manually call load_node
505 * on every parent node to fetch all children nodes on that level.
506 *
507 * @name $.jstree.defaults.parentsload
508 * @plugin parentsload
509 */
510 $.jstree.defaults.parentsload = null;
511 $.jstree.plugins.parentsload = function (options, parent) {
512 this.init = function (el, options) {
513 parent.init.call(this, el, options);
514 this.patch_data()
515 };
516 this.patch_data = function(){
517 var parentsloadSettings = this.settings.parentsload;
518 var jsTreeDataSettings = this.settings.core.data;
519 var self = this;
520
521 var callError = function(number, message) {
522 self._data.core.last_error = { 'error' : 'configuration', 'plugin' : 'parentsload', 'id' : 'parentsload_' + number, 'reason' : message, 'data' : JSON.stringify({config: parentsloadSettings}) };
523 self.settings.core.error.call(self, self._data.core.last_error);
524 }
525
526 if(!parentsloadSettings) {
527 callError('01', 'The configuration must be presented')
528 return
529 }
530 parentsloadSettings = parentsloadSettings.data;
531
532 var patchSettingsProperty = function (propertyName) {
533 var property = parentsloadSettings[propertyName],
534 coreProperty = jsTreeDataSettings[propertyName];
535 if (property) {
536 jsTreeDataSettings[propertyName] = function(node) {
537 if (this.get_node(node).parentsload_required) {
538 if ($.isFunction(property)) {
539 return property.call(this, node)
540 } else {// (typeof property === 'string')
541 return property
542 }
543 } else {
544 if ($.isFunction(coreProperty)) {
545 return coreProperty.call(this, node)
546 } else { // (typeof coreProperty === 'string')
547 return coreProperty
548 }
549 }
550 }
551 } /* else {
552 use jstree the same data[propertyName] settings
553 }*/
554 }
555
556 if($.isFunction(parentsloadSettings)) {
557 this.settings.data = parentsloadSettings
558 } else if (typeof parentsloadSettings === 'object') {
559 if (! (parentsloadSettings.url || parentsloadSettings.data)) {
560 callError('02', 'The "data.url" or "data.data" must be presented in configuration')
561 return
562 }
563 patchSettingsProperty('url')
564 patchSettingsProperty('data')
565
566 } else {
567 callError('03', 'The appropriate "data.url" or "data.data" must be presented in configuration')
568 }
569 }
570
571 this.load_node = function (obj, callback) {
572 if($.isArray(obj)) {
573 // FIXME: _load_nodes will not load nodes not presented in the tree
574 this._load_nodes(obj.slice(), callback);
575 return true;
576 }
577 var foundObj = this.get_node(obj);
578 if (foundObj) {
579 return parent.load_node.apply(this, arguments)
580 } else {
581 // node hasn't been loaded
582 var id = obj.id? obj.id: obj;
583 this._model.data[id] = {
584 id : id,
585 parent : '#',
586 parents : [],
587 children : [],
588 children_d : [],
589 state : { loaded : false },
590 li_attr : {},
591 a_attr : {},
592 parentsload_required : true,
593 };
594 return parent.load_node.call(this, obj, function(obj, status){
595 obj.parentsload_required = !status
596 callback.call(this, obj, status)
597 })
598 }
599 }
600 };
601 }));
602
603 // conditional deselect
604 (function (factory) {
605 "use strict";
606 if (typeof define === 'function' && define.amd) {
607 define('jstree.conditionaldeselect', ['jquery','jstree'], factory);
608 }
609 else if(typeof exports === 'object') {
610 factory(require('jquery'), require('jstree'));
611 }
612 else {
613 factory(jQuery, jQuery.jstree);
614 }
615 }(function ($, jstree, undefined) {
616 "use strict";
617
618 if($.jstree.plugins.conditionaldeselect) { return; }
619 $.jstree.defaults.conditionaldeselect = function () { return true; };
620 $.jstree.plugins.conditionaldeselect = function (options, parent) {
621 // own function
622 this.deselect_node = function (obj, supress_event, e) {
623 if(this.settings.conditionaldeselect.call(this, this.get_node(obj), e)) {
624 return parent.deselect_node.call(this, obj, supress_event, e);
625 }
626 };
627 };
628
629 }));