comparison vakata-jstree-3.3.5/src/jstree.unique.js @ 0:25602263cff0 draft default tip

planemo upload commit 841d8b22bf9f1aaed6bfe8344b60617f45b275b2-dirty
author mingchen0919
date Sun, 30 Dec 2018 13:11:48 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:25602263cff0
1 /**
2 * ### Unique plugin
3 *
4 * Enforces that no nodes with the same name can coexist as siblings.
5 */
6 /*globals jQuery, define, exports, require */
7 (function (factory) {
8 "use strict";
9 if (typeof define === 'function' && define.amd) {
10 define('jstree.unique', ['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.unique) { return; }
22
23 /**
24 * stores all defaults for the unique plugin
25 * @name $.jstree.defaults.unique
26 * @plugin unique
27 */
28 $.jstree.defaults.unique = {
29 /**
30 * Indicates if the comparison should be case sensitive. Default is `false`.
31 * @name $.jstree.defaults.unique.case_sensitive
32 * @plugin unique
33 */
34 case_sensitive : false,
35 /**
36 * Indicates if white space should be trimmed before the comparison. Default is `false`.
37 * @name $.jstree.defaults.unique.trim_whitespace
38 * @plugin unique
39 */
40 trim_whitespace : false,
41 /**
42 * A callback executed in the instance's scope when a new node is created and the name is already taken, the two arguments are the conflicting name and the counter. The default will produce results like `New node (2)`.
43 * @name $.jstree.defaults.unique.duplicate
44 * @plugin unique
45 */
46 duplicate : function (name, counter) {
47 return name + ' (' + counter + ')';
48 }
49 };
50
51 $.jstree.plugins.unique = function (options, parent) {
52 this.check = function (chk, obj, par, pos, more) {
53 if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
54 obj = obj && obj.id ? obj : this.get_node(obj);
55 par = par && par.id ? par : this.get_node(par);
56 if(!par || !par.children) { return true; }
57 var n = chk === "rename_node" ? pos : obj.text,
58 c = [],
59 s = this.settings.unique.case_sensitive,
60 w = this.settings.unique.trim_whitespace,
61 m = this._model.data, i, j, t;
62 for(i = 0, j = par.children.length; i < j; i++) {
63 t = m[par.children[i]].text;
64 if (!s) {
65 t = t.toLowerCase();
66 }
67 if (w) {
68 t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
69 }
70 c.push(t);
71 }
72 if(!s) { n = n.toLowerCase(); }
73 if (w) { n = n.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }
74 switch(chk) {
75 case "delete_node":
76 return true;
77 case "rename_node":
78 t = obj.text || '';
79 if (!s) {
80 t = t.toLowerCase();
81 }
82 if (w) {
83 t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
84 }
85 i = ($.inArray(n, c) === -1 || (obj.text && t === n));
86 if(!i) {
87 this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_01', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
88 }
89 return i;
90 case "create_node":
91 i = ($.inArray(n, c) === -1);
92 if(!i) {
93 this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_04', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
94 }
95 return i;
96 case "copy_node":
97 i = ($.inArray(n, c) === -1);
98 if(!i) {
99 this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_02', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
100 }
101 return i;
102 case "move_node":
103 i = ( (obj.parent === par.id && (!more || !more.is_multi)) || $.inArray(n, c) === -1);
104 if(!i) {
105 this._data.core.last_error = { 'error' : 'check', 'plugin' : 'unique', 'id' : 'unique_03', 'reason' : 'Child with name ' + n + ' already exists. Preventing: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
106 }
107 return i;
108 }
109 return true;
110 };
111 this.create_node = function (par, node, pos, callback, is_loaded) {
112 if(!node || node.text === undefined) {
113 if(par === null) {
114 par = $.jstree.root;
115 }
116 par = this.get_node(par);
117 if(!par) {
118 return parent.create_node.call(this, par, node, pos, callback, is_loaded);
119 }
120 pos = pos === undefined ? "last" : pos;
121 if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
122 return parent.create_node.call(this, par, node, pos, callback, is_loaded);
123 }
124 if(!node) { node = {}; }
125 var tmp, n, dpc, i, j, m = this._model.data, s = this.settings.unique.case_sensitive, w = this.settings.unique.trim_whitespace, cb = this.settings.unique.duplicate, t;
126 n = tmp = this.get_string('New node');
127 dpc = [];
128 for(i = 0, j = par.children.length; i < j; i++) {
129 t = m[par.children[i]].text;
130 if (!s) {
131 t = t.toLowerCase();
132 }
133 if (w) {
134 t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
135 }
136 dpc.push(t);
137 }
138 i = 1;
139 t = n;
140 if (!s) {
141 t = t.toLowerCase();
142 }
143 if (w) {
144 t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
145 }
146 while($.inArray(t, dpc) !== -1) {
147 n = cb.call(this, tmp, (++i)).toString();
148 t = n;
149 if (!s) {
150 t = t.toLowerCase();
151 }
152 if (w) {
153 t = t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
154 }
155 }
156 node.text = n;
157 }
158 return parent.create_node.call(this, par, node, pos, callback, is_loaded);
159 };
160 };
161
162 // include the unique plugin by default
163 // $.jstree.defaults.plugins.push("unique");
164 }));