| 875 |
lars |
1 |
/**
|
|
|
2 |
* ### Types plugin
|
|
|
3 |
*
|
|
|
4 |
* Makes it possible to add predefined types for groups of nodes, which make it possible to easily control nesting rules and icon for each group.
|
|
|
5 |
*/
|
|
|
6 |
/*globals jQuery, define, exports, require */
|
|
|
7 |
(function (factory) {
|
|
|
8 |
"use strict";
|
|
|
9 |
if (typeof define === 'function' && define.amd) {
|
|
|
10 |
define('jstree.types', ['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.types) { return; }
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* An object storing all types as key value pairs, where the key is the type name and the value is an object that could contain following keys (all optional).
|
|
|
25 |
*
|
|
|
26 |
* * `max_children` the maximum number of immediate children this node type can have. Do not specify or set to `-1` for unlimited.
|
|
|
27 |
* * `max_depth` the maximum number of nesting this node type can have. A value of `1` would mean that the node can have children, but no grandchildren. Do not specify or set to `-1` for unlimited.
|
|
|
28 |
* * `valid_children` an array of node type strings, that nodes of this type can have as children. Do not specify or set to `-1` for no limits.
|
|
|
29 |
* * `icon` a string - can be a path to an icon or a className, if using an image that is in the current directory use a `./` prefix, otherwise it will be detected as a class. Omit to use the default icon from your theme.
|
|
|
30 |
*
|
|
|
31 |
* There are two predefined types:
|
|
|
32 |
*
|
|
|
33 |
* * `#` represents the root of the tree, for example `max_children` would control the maximum number of root nodes.
|
|
|
34 |
* * `default` represents the default node - any settings here will be applied to all nodes that do not have a type specified.
|
|
|
35 |
*
|
|
|
36 |
* @name $.jstree.defaults.types
|
|
|
37 |
* @plugin types
|
|
|
38 |
*/
|
|
|
39 |
$.jstree.defaults.types = {
|
|
|
40 |
'default' : {}
|
|
|
41 |
};
|
|
|
42 |
$.jstree.defaults.types[$.jstree.root] = {};
|
|
|
43 |
|
|
|
44 |
$.jstree.plugins.types = function (options, parent) {
|
|
|
45 |
this.init = function (el, options) {
|
|
|
46 |
var i, j;
|
|
|
47 |
if(options && options.types && options.types['default']) {
|
|
|
48 |
for(i in options.types) {
|
|
|
49 |
if(i !== "default" && i !== $.jstree.root && options.types.hasOwnProperty(i)) {
|
|
|
50 |
for(j in options.types['default']) {
|
|
|
51 |
if(options.types['default'].hasOwnProperty(j) && options.types[i][j] === undefined) {
|
|
|
52 |
options.types[i][j] = options.types['default'][j];
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
parent.init.call(this, el, options);
|
|
|
59 |
this._model.data[$.jstree.root].type = $.jstree.root;
|
|
|
60 |
};
|
|
|
61 |
this.refresh = function (skip_loading, forget_state) {
|
|
|
62 |
parent.refresh.call(this, skip_loading, forget_state);
|
|
|
63 |
this._model.data[$.jstree.root].type = $.jstree.root;
|
|
|
64 |
};
|
|
|
65 |
this.bind = function () {
|
|
|
66 |
this.element
|
|
|
67 |
.on('model.jstree', $.proxy(function (e, data) {
|
|
|
68 |
var m = this._model.data,
|
|
|
69 |
dpc = data.nodes,
|
|
|
70 |
t = this.settings.types,
|
|
|
71 |
i, j, c = 'default';
|
|
|
72 |
for(i = 0, j = dpc.length; i < j; i++) {
|
|
|
73 |
c = 'default';
|
|
|
74 |
if(m[dpc[i]].original && m[dpc[i]].original.type && t[m[dpc[i]].original.type]) {
|
|
|
75 |
c = m[dpc[i]].original.type;
|
|
|
76 |
}
|
|
|
77 |
if(m[dpc[i]].data && m[dpc[i]].data.jstree && m[dpc[i]].data.jstree.type && t[m[dpc[i]].data.jstree.type]) {
|
|
|
78 |
c = m[dpc[i]].data.jstree.type;
|
|
|
79 |
}
|
|
|
80 |
m[dpc[i]].type = c;
|
|
|
81 |
if(m[dpc[i]].icon === true && t[c].icon !== undefined) {
|
|
|
82 |
m[dpc[i]].icon = t[c].icon;
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
m[$.jstree.root].type = $.jstree.root;
|
|
|
86 |
}, this));
|
|
|
87 |
parent.bind.call(this);
|
|
|
88 |
};
|
|
|
89 |
this.get_json = function (obj, options, flat) {
|
|
|
90 |
var i, j,
|
|
|
91 |
m = this._model.data,
|
|
|
92 |
opt = options ? $.extend(true, {}, options, {no_id:false}) : {},
|
|
|
93 |
tmp = parent.get_json.call(this, obj, opt, flat);
|
|
|
94 |
if(tmp === false) { return false; }
|
|
|
95 |
if($.isArray(tmp)) {
|
|
|
96 |
for(i = 0, j = tmp.length; i < j; i++) {
|
|
|
97 |
tmp[i].type = tmp[i].id && m[tmp[i].id] && m[tmp[i].id].type ? m[tmp[i].id].type : "default";
|
|
|
98 |
if(options && options.no_id) {
|
|
|
99 |
delete tmp[i].id;
|
|
|
100 |
if(tmp[i].li_attr && tmp[i].li_attr.id) {
|
|
|
101 |
delete tmp[i].li_attr.id;
|
|
|
102 |
}
|
|
|
103 |
if(tmp[i].a_attr && tmp[i].a_attr.id) {
|
|
|
104 |
delete tmp[i].a_attr.id;
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
else {
|
|
|
110 |
tmp.type = tmp.id && m[tmp.id] && m[tmp.id].type ? m[tmp.id].type : "default";
|
|
|
111 |
if(options && options.no_id) {
|
|
|
112 |
tmp = this._delete_ids(tmp);
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
return tmp;
|
|
|
116 |
};
|
|
|
117 |
this._delete_ids = function (tmp) {
|
|
|
118 |
if($.isArray(tmp)) {
|
|
|
119 |
for(var i = 0, j = tmp.length; i < j; i++) {
|
|
|
120 |
tmp[i] = this._delete_ids(tmp[i]);
|
|
|
121 |
}
|
|
|
122 |
return tmp;
|
|
|
123 |
}
|
|
|
124 |
delete tmp.id;
|
|
|
125 |
if(tmp.li_attr && tmp.li_attr.id) {
|
|
|
126 |
delete tmp.li_attr.id;
|
|
|
127 |
}
|
|
|
128 |
if(tmp.a_attr && tmp.a_attr.id) {
|
|
|
129 |
delete tmp.a_attr.id;
|
|
|
130 |
}
|
|
|
131 |
if(tmp.children && $.isArray(tmp.children)) {
|
|
|
132 |
tmp.children = this._delete_ids(tmp.children);
|
|
|
133 |
}
|
|
|
134 |
return tmp;
|
|
|
135 |
};
|
|
|
136 |
this.check = function (chk, obj, par, pos, more) {
|
|
|
137 |
if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
|
|
|
138 |
obj = obj && obj.id ? obj : this.get_node(obj);
|
|
|
139 |
par = par && par.id ? par : this.get_node(par);
|
|
|
140 |
var m = obj && obj.id ? (more && more.origin ? more.origin : $.jstree.reference(obj.id)) : null, tmp, d, i, j;
|
|
|
141 |
m = m && m._model && m._model.data ? m._model.data : null;
|
|
|
142 |
switch(chk) {
|
|
|
143 |
case "create_node":
|
|
|
144 |
case "move_node":
|
|
|
145 |
case "copy_node":
|
|
|
146 |
if(chk !== 'move_node' || $.inArray(obj.id, par.children) === -1) {
|
|
|
147 |
tmp = this.get_rules(par);
|
|
|
148 |
if(tmp.max_children !== undefined && tmp.max_children !== -1 && tmp.max_children === par.children.length) {
|
|
|
149 |
this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_01', 'reason' : 'max_children prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
|
|
|
150 |
return false;
|
|
|
151 |
}
|
|
|
152 |
if(tmp.valid_children !== undefined && tmp.valid_children !== -1 && $.inArray((obj.type || 'default'), tmp.valid_children) === -1) {
|
|
|
153 |
this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_02', 'reason' : 'valid_children prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
|
|
|
154 |
return false;
|
|
|
155 |
}
|
|
|
156 |
if(m && obj.children_d && obj.parents) {
|
|
|
157 |
d = 0;
|
|
|
158 |
for(i = 0, j = obj.children_d.length; i < j; i++) {
|
|
|
159 |
d = Math.max(d, m[obj.children_d[i]].parents.length);
|
|
|
160 |
}
|
|
|
161 |
d = d - obj.parents.length + 1;
|
|
|
162 |
}
|
|
|
163 |
if(d <= 0 || d === undefined) { d = 1; }
|
|
|
164 |
do {
|
|
|
165 |
if(tmp.max_depth !== undefined && tmp.max_depth !== -1 && tmp.max_depth < d) {
|
|
|
166 |
this._data.core.last_error = { 'error' : 'check', 'plugin' : 'types', 'id' : 'types_03', 'reason' : 'max_depth prevents function: ' + chk, 'data' : JSON.stringify({ 'chk' : chk, 'pos' : pos, 'obj' : obj && obj.id ? obj.id : false, 'par' : par && par.id ? par.id : false }) };
|
|
|
167 |
return false;
|
|
|
168 |
}
|
|
|
169 |
par = this.get_node(par.parent);
|
|
|
170 |
tmp = this.get_rules(par);
|
|
|
171 |
d++;
|
|
|
172 |
} while(par);
|
|
|
173 |
}
|
|
|
174 |
break;
|
|
|
175 |
}
|
|
|
176 |
return true;
|
|
|
177 |
};
|
|
|
178 |
/**
|
|
|
179 |
* used to retrieve the type settings object for a node
|
|
|
180 |
* @name get_rules(obj)
|
|
|
181 |
* @param {mixed} obj the node to find the rules for
|
|
|
182 |
* @return {Object}
|
|
|
183 |
* @plugin types
|
|
|
184 |
*/
|
|
|
185 |
this.get_rules = function (obj) {
|
|
|
186 |
obj = this.get_node(obj);
|
|
|
187 |
if(!obj) { return false; }
|
|
|
188 |
var tmp = this.get_type(obj, true);
|
|
|
189 |
if(tmp.max_depth === undefined) { tmp.max_depth = -1; }
|
|
|
190 |
if(tmp.max_children === undefined) { tmp.max_children = -1; }
|
|
|
191 |
if(tmp.valid_children === undefined) { tmp.valid_children = -1; }
|
|
|
192 |
return tmp;
|
|
|
193 |
};
|
|
|
194 |
/**
|
|
|
195 |
* used to retrieve the type string or settings object for a node
|
|
|
196 |
* @name get_type(obj [, rules])
|
|
|
197 |
* @param {mixed} obj the node to find the rules for
|
|
|
198 |
* @param {Boolean} rules if set to `true` instead of a string the settings object will be returned
|
|
|
199 |
* @return {String|Object}
|
|
|
200 |
* @plugin types
|
|
|
201 |
*/
|
|
|
202 |
this.get_type = function (obj, rules) {
|
|
|
203 |
obj = this.get_node(obj);
|
|
|
204 |
return (!obj) ? false : ( rules ? $.extend({ 'type' : obj.type }, this.settings.types[obj.type]) : obj.type);
|
|
|
205 |
};
|
|
|
206 |
/**
|
|
|
207 |
* used to change a node's type
|
|
|
208 |
* @name set_type(obj, type)
|
|
|
209 |
* @param {mixed} obj the node to change
|
|
|
210 |
* @param {String} type the new type
|
|
|
211 |
* @plugin types
|
|
|
212 |
*/
|
|
|
213 |
this.set_type = function (obj, type) {
|
|
|
214 |
var t, t1, t2, old_type, old_icon;
|
|
|
215 |
if($.isArray(obj)) {
|
|
|
216 |
obj = obj.slice();
|
|
|
217 |
for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
|
|
|
218 |
this.set_type(obj[t1], type);
|
|
|
219 |
}
|
|
|
220 |
return true;
|
|
|
221 |
}
|
|
|
222 |
t = this.settings.types;
|
|
|
223 |
obj = this.get_node(obj);
|
|
|
224 |
if(!t[type] || !obj) { return false; }
|
|
|
225 |
old_type = obj.type;
|
|
|
226 |
old_icon = this.get_icon(obj);
|
|
|
227 |
obj.type = type;
|
|
|
228 |
if(old_icon === true || (t[old_type] && t[old_type].icon !== undefined && old_icon === t[old_type].icon)) {
|
|
|
229 |
this.set_icon(obj, t[type].icon !== undefined ? t[type].icon : true);
|
|
|
230 |
}
|
|
|
231 |
return true;
|
|
|
232 |
};
|
|
|
233 |
};
|
|
|
234 |
// include the types plugin by default
|
|
|
235 |
// $.jstree.defaults.plugins.push("types");
|
|
|
236 |
}));
|