| 4 |
lars |
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 |
* 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)`.
|
|
|
37 |
* @name $.jstree.defaults.unique.duplicate
|
|
|
38 |
* @plugin unique
|
|
|
39 |
*/
|
|
|
40 |
duplicate : function (name, counter) {
|
|
|
41 |
return name + ' (' + counter + ')';
|
|
|
42 |
}
|
|
|
43 |
};
|
|
|
44 |
|
|
|
45 |
$.jstree.plugins.unique = function (options, parent) {
|
|
|
46 |
this.check = function (chk, obj, par, pos, more) {
|
|
|
47 |
if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
|
|
|
48 |
obj = obj && obj.id ? obj : this.get_node(obj);
|
|
|
49 |
par = par && par.id ? par : this.get_node(par);
|
|
|
50 |
if(!par || !par.children) { return true; }
|
|
|
51 |
var n = chk === "rename_node" ? pos : obj.text,
|
|
|
52 |
c = [],
|
|
|
53 |
s = this.settings.unique.case_sensitive,
|
|
|
54 |
m = this._model.data, i, j;
|
|
|
55 |
for(i = 0, j = par.children.length; i < j; i++) {
|
|
|
56 |
c.push(s ? m[par.children[i]].text : m[par.children[i]].text.toLowerCase());
|
|
|
57 |
}
|
|
|
58 |
if(!s) { n = n.toLowerCase(); }
|
|
|
59 |
switch(chk) {
|
|
|
60 |
case "delete_node":
|
|
|
61 |
return true;
|
|
|
62 |
case "rename_node":
|
|
|
63 |
i = ($.inArray(n, c) === -1 || (obj.text && obj.text[ s ? 'toString' : 'toLowerCase']() === n));
|
|
|
64 |
if(!i) {
|
|
|
65 |
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 }) };
|
|
|
66 |
}
|
|
|
67 |
return i;
|
|
|
68 |
case "create_node":
|
|
|
69 |
i = ($.inArray(n, c) === -1);
|
|
|
70 |
if(!i) {
|
|
|
71 |
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 }) };
|
|
|
72 |
}
|
|
|
73 |
return i;
|
|
|
74 |
case "copy_node":
|
|
|
75 |
i = ($.inArray(n, c) === -1);
|
|
|
76 |
if(!i) {
|
|
|
77 |
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 }) };
|
|
|
78 |
}
|
|
|
79 |
return i;
|
|
|
80 |
case "move_node":
|
|
|
81 |
i = ( (obj.parent === par.id && (!more || !more.is_multi)) || $.inArray(n, c) === -1);
|
|
|
82 |
if(!i) {
|
|
|
83 |
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 }) };
|
|
|
84 |
}
|
|
|
85 |
return i;
|
|
|
86 |
}
|
|
|
87 |
return true;
|
|
|
88 |
};
|
|
|
89 |
this.create_node = function (par, node, pos, callback, is_loaded) {
|
|
|
90 |
if(!node || node.text === undefined) {
|
|
|
91 |
if(par === null) {
|
|
|
92 |
par = $.jstree.root;
|
|
|
93 |
}
|
|
|
94 |
par = this.get_node(par);
|
|
|
95 |
if(!par) {
|
|
|
96 |
return parent.create_node.call(this, par, node, pos, callback, is_loaded);
|
|
|
97 |
}
|
|
|
98 |
pos = pos === undefined ? "last" : pos;
|
|
|
99 |
if(!pos.toString().match(/^(before|after)$/) && !is_loaded && !this.is_loaded(par)) {
|
|
|
100 |
return parent.create_node.call(this, par, node, pos, callback, is_loaded);
|
|
|
101 |
}
|
|
|
102 |
if(!node) { node = {}; }
|
|
|
103 |
var tmp, n, dpc, i, j, m = this._model.data, s = this.settings.unique.case_sensitive, cb = this.settings.unique.duplicate;
|
|
|
104 |
n = tmp = this.get_string('New node');
|
|
|
105 |
dpc = [];
|
|
|
106 |
for(i = 0, j = par.children.length; i < j; i++) {
|
|
|
107 |
dpc.push(s ? m[par.children[i]].text : m[par.children[i]].text.toLowerCase());
|
|
|
108 |
}
|
|
|
109 |
i = 1;
|
|
|
110 |
while($.inArray(s ? n : n.toLowerCase(), dpc) !== -1) {
|
|
|
111 |
n = cb.call(this, tmp, (++i)).toString();
|
|
|
112 |
}
|
|
|
113 |
node.text = n;
|
|
|
114 |
}
|
|
|
115 |
return parent.create_node.call(this, par, node, pos, callback, is_loaded);
|
|
|
116 |
};
|
|
|
117 |
};
|
|
|
118 |
|
|
|
119 |
// include the unique plugin by default
|
|
|
120 |
// $.jstree.defaults.plugins.push("unique");
|
|
|
121 |
}));
|