zhaojs
2023-06-27 0a0d605f68e0f02d518b9106b7433067a18b07c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
 * ### Types plugin
 *
 * 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.
 */
/*globals jQuery, define, exports, require */
(function (factory) {
    "use strict";
    if (typeof define === 'function' && define.amd) {
        define('jstree.types', ['jquery','./jstree.js'], factory);
    }
    else if(typeof exports === 'object') {
        factory(require('jquery'), require('./jstree.js'));
    }
    else {
        factory(jQuery, jQuery.jstree);
    }
}(function ($, jstree, undefined) {
    "use strict";
 
    if($.jstree.plugins.types) { return; }
 
    /**
     * 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).
     *
     * * `max_children` the maximum number of immediate children this node type can have. Do not specify or set to `-1` for unlimited.
     * * `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.
     * * `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.
     * * `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.
     * * `li_attr` an object of values which will be used to add HTML attributes on the resulting LI DOM node (merged with the node's own data)
     * * `a_attr` an object of values which will be used to add HTML attributes on the resulting A DOM node (merged with the node's own data)
     *
     * There are two predefined types:
     *
     * * `#` represents the root of the tree, for example `max_children` would control the maximum number of root nodes.
     * * `default` represents the default node - any settings here will be applied to all nodes that do not have a type specified.
     *
     * @name $.jstree.defaults.types
     * @plugin types
     */
    $.jstree.defaults.types = {
        'default' : {}
    };
    $.jstree.defaults.types[$.jstree.root] = {};
 
    $.jstree.plugins.types = function (options, parent) {
        this.init = function (el, options) {
            var i, j;
            if(options && options.types && options.types['default']) {
                for(i in options.types) {
                    if(i !== "default" && i !== $.jstree.root && options.types.hasOwnProperty(i)) {
                        for(j in options.types['default']) {
                            if(options.types['default'].hasOwnProperty(j) && options.types[i][j] === undefined) {
                                options.types[i][j] = options.types['default'][j];
                            }
                        }
                    }
                }
            }
            parent.init.call(this, el, options);
            this._model.data[$.jstree.root].type = $.jstree.root;
        };
        this.refresh = function (skip_loading, forget_state) {
            parent.refresh.call(this, skip_loading, forget_state);
            this._model.data[$.jstree.root].type = $.jstree.root;
        };
        this.bind = function () {
            this.element
                .on('model.jstree', function (e, data) {
                        var m = this._model.data,
                            dpc = data.nodes,
                            t = this.settings.types,
                            i, j, c = 'default', k;
                        for(i = 0, j = dpc.length; i < j; i++) {
                            c = 'default';
                            if(m[dpc[i]].original && m[dpc[i]].original.type && t[m[dpc[i]].original.type]) {
                                c = m[dpc[i]].original.type;
                            }
                            if(m[dpc[i]].data && m[dpc[i]].data.jstree && m[dpc[i]].data.jstree.type && t[m[dpc[i]].data.jstree.type]) {
                                c = m[dpc[i]].data.jstree.type;
                            }
                            m[dpc[i]].type = c;
                            if(m[dpc[i]].icon === true && t[c].icon !== undefined) {
                                m[dpc[i]].icon = t[c].icon;
                            }
                            if(t[c].li_attr !== undefined && typeof t[c].li_attr === 'object') {
                                for (k in t[c].li_attr) {
                                    if (t[c].li_attr.hasOwnProperty(k)) {
                                        if (k === 'id') {
                                            continue;
                                        }
                                        else if (m[dpc[i]].li_attr[k] === undefined) {
                                            m[dpc[i]].li_attr[k] = t[c].li_attr[k];
                                        }
                                        else if (k === 'class') {
                                            m[dpc[i]].li_attr['class'] = t[c].li_attr['class'] + ' ' + m[dpc[i]].li_attr['class'];
                                        }
                                    }
                                }
                            }
                            if(t[c].a_attr !== undefined && typeof t[c].a_attr === 'object') {
                                for (k in t[c].a_attr) {
                                    if (t[c].a_attr.hasOwnProperty(k)) {
                                        if (k === 'id') {
                                            continue;
                                        }
                                        else if (m[dpc[i]].a_attr[k] === undefined) {
                                            m[dpc[i]].a_attr[k] = t[c].a_attr[k];
                                        }
                                        else if (k === 'href' && m[dpc[i]].a_attr[k] === '#') {
                                            m[dpc[i]].a_attr['href'] = t[c].a_attr['href'];
                                        }
                                        else if (k === 'class') {
                                            m[dpc[i]].a_attr['class'] = t[c].a_attr['class'] + ' ' + m[dpc[i]].a_attr['class'];
                                        }
                                    }
                                }
                            }
                        }
                        m[$.jstree.root].type = $.jstree.root;
                    }.bind(this));
            parent.bind.call(this);
        };
        this.get_json = function (obj, options, flat) {
            var i, j,
                m = this._model.data,
                opt = options ? $.extend(true, {}, options, {no_id:false}) : {},
                tmp = parent.get_json.call(this, obj, opt, flat);
            if(tmp === false) { return false; }
            if($.vakata.is_array(tmp)) {
                for(i = 0, j = tmp.length; i < j; i++) {
                    tmp[i].type = tmp[i].id && m[tmp[i].id] && m[tmp[i].id].type ? m[tmp[i].id].type : "default";
                    if(options && options.no_id) {
                        delete tmp[i].id;
                        if(tmp[i].li_attr && tmp[i].li_attr.id) {
                            delete tmp[i].li_attr.id;
                        }
                        if(tmp[i].a_attr && tmp[i].a_attr.id) {
                            delete tmp[i].a_attr.id;
                        }
                    }
                }
            }
            else {
                tmp.type = tmp.id && m[tmp.id] && m[tmp.id].type ? m[tmp.id].type : "default";
                if(options && options.no_id) {
                    tmp = this._delete_ids(tmp);
                }
            }
            return tmp;
        };
        this._delete_ids = function (tmp) {
            if($.vakata.is_array(tmp)) {
                for(var i = 0, j = tmp.length; i < j; i++) {
                    tmp[i] = this._delete_ids(tmp[i]);
                }
                return tmp;
            }
            delete tmp.id;
            if(tmp.li_attr && tmp.li_attr.id) {
                delete tmp.li_attr.id;
            }
            if(tmp.a_attr && tmp.a_attr.id) {
                delete tmp.a_attr.id;
            }
            if(tmp.children && $.vakata.is_array(tmp.children)) {
                tmp.children = this._delete_ids(tmp.children);
            }
            return tmp;
        };
        this.check = function (chk, obj, par, pos, more) {
            if(parent.check.call(this, chk, obj, par, pos, more) === false) { return false; }
            obj = obj && obj.id ? obj : this.get_node(obj);
            par = par && par.id ? par : this.get_node(par);
            var m = obj && obj.id ? (more && more.origin ? more.origin : $.jstree.reference(obj.id)) : null, tmp, d, i, j;
            m = m && m._model && m._model.data ? m._model.data : null;
            switch(chk) {
                case "create_node":
                case "move_node":
                case "copy_node":
                    if(chk !== 'move_node' || $.inArray(obj.id, par.children) === -1) {
                        tmp = this.get_rules(par);
                        if(tmp.max_children !== undefined && tmp.max_children !== -1 && tmp.max_children === par.children.length) {
                            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 }) };
                            return false;
                        }
                        if(tmp.valid_children !== undefined && tmp.valid_children !== -1 && $.inArray((obj.type || 'default'), tmp.valid_children) === -1) {
                            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 }) };
                            return false;
                        }
                        if(m && obj.children_d && obj.parents) {
                            d = 0;
                            for(i = 0, j = obj.children_d.length; i < j; i++) {
                                d = Math.max(d, m[obj.children_d[i]].parents.length);
                            }
                            d = d - obj.parents.length + 1;
                        }
                        if(d <= 0 || d === undefined) { d = 1; }
                        do {
                            if(tmp.max_depth !== undefined && tmp.max_depth !== -1 && tmp.max_depth < d) {
                                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 }) };
                                return false;
                            }
                            par = this.get_node(par.parent);
                            tmp = this.get_rules(par);
                            d++;
                        } while(par);
                    }
                    break;
            }
            return true;
        };
        /**
         * used to retrieve the type settings object for a node
         * @name get_rules(obj)
         * @param {mixed} obj the node to find the rules for
         * @return {Object}
         * @plugin types
         */
        this.get_rules = function (obj) {
            obj = this.get_node(obj);
            if(!obj) { return false; }
            var tmp = this.get_type(obj, true);
            if(tmp.max_depth === undefined) { tmp.max_depth = -1; }
            if(tmp.max_children === undefined) { tmp.max_children = -1; }
            if(tmp.valid_children === undefined) { tmp.valid_children = -1; }
            return tmp;
        };
        /**
         * used to retrieve the type string or settings object for a node
         * @name get_type(obj [, rules])
         * @param {mixed} obj the node to find the rules for
         * @param {Boolean} rules if set to `true` instead of a string the settings object will be returned
         * @return {String|Object}
         * @plugin types
         */
        this.get_type = function (obj, rules) {
            obj = this.get_node(obj);
            return (!obj) ? false : ( rules ? $.extend({ 'type' : obj.type }, this.settings.types[obj.type]) : obj.type);
        };
        /**
         * used to change a node's type
         * @name set_type(obj, type)
         * @param {mixed} obj the node to change
         * @param {String} type the new type
         * @plugin types
         */
        this.set_type = function (obj, type) {
            var m = this._model.data, t, t1, t2, old_type, old_icon, k, d, a;
            if($.vakata.is_array(obj)) {
                obj = obj.slice();
                for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
                    this.set_type(obj[t1], type);
                }
                return true;
            }
            t = this.settings.types;
            obj = this.get_node(obj);
            if(!t[type] || !obj) { return false; }
            d = this.get_node(obj, true);
            if (d && d.length) {
                a = d.children('.jstree-anchor');
            }
            old_type = obj.type;
            old_icon = this.get_icon(obj);
            obj.type = type;
            if(old_icon === true || !t[old_type] || (t[old_type].icon !== undefined && old_icon === t[old_type].icon)) {
                this.set_icon(obj, t[type].icon !== undefined ? t[type].icon : true);
            }
 
            // remove old type props
            if(t[old_type] && t[old_type].li_attr !== undefined && typeof t[old_type].li_attr === 'object') {
                for (k in t[old_type].li_attr) {
                    if (t[old_type].li_attr.hasOwnProperty(k)) {
                        if (k === 'id') {
                            continue;
                        }
                        else if (k === 'class') {
                            m[obj.id].li_attr['class'] = (m[obj.id].li_attr['class'] || '').replace(t[old_type].li_attr[k], '');
                            if (d) { d.removeClass(t[old_type].li_attr[k]); }
                        }
                        else if (m[obj.id].li_attr[k] === t[old_type].li_attr[k]) {
                            m[obj.id].li_attr[k] = null;
                            if (d) { d.removeAttr(k); }
                        }
                    }
                }
            }
            if(t[old_type] && t[old_type].a_attr !== undefined && typeof t[old_type].a_attr === 'object') {
                for (k in t[old_type].a_attr) {
                    if (t[old_type].a_attr.hasOwnProperty(k)) {
                        if (k === 'id') {
                            continue;
                        }
                        else if (k === 'class') {
                            m[obj.id].a_attr['class'] = (m[obj.id].a_attr['class'] || '').replace(t[old_type].a_attr[k], '');
                            if (a) { a.removeClass(t[old_type].a_attr[k]); }
                        }
                        else if (m[obj.id].a_attr[k] === t[old_type].a_attr[k]) {
                            if (k === 'href') {
                                m[obj.id].a_attr[k] = '#';
                                if (a) { a.attr('href', '#'); }
                            }
                            else {
                                delete m[obj.id].a_attr[k];
                                if (a) { a.removeAttr(k); }
                            }
                        }
                    }
                }
            }
 
            // add new props
            if(t[type].li_attr !== undefined && typeof t[type].li_attr === 'object') {
                for (k in t[type].li_attr) {
                    if (t[type].li_attr.hasOwnProperty(k)) {
                        if (k === 'id') {
                            continue;
                        }
                        else if (m[obj.id].li_attr[k] === undefined) {
                            m[obj.id].li_attr[k] = t[type].li_attr[k];
                            if (d) {
                                if (k === 'class') {
                                    d.addClass(t[type].li_attr[k]);
                                }
                                else {
                                    d.attr(k, t[type].li_attr[k]);
                                }
                            }
                        }
                        else if (k === 'class') {
                            m[obj.id].li_attr['class'] = t[type].li_attr[k] + ' ' + m[obj.id].li_attr['class'];
                            if (d) { d.addClass(t[type].li_attr[k]); }
                        }
                    }
                }
            }
            if(t[type].a_attr !== undefined && typeof t[type].a_attr === 'object') {
                for (k in t[type].a_attr) {
                    if (t[type].a_attr.hasOwnProperty(k)) {
                        if (k === 'id') {
                            continue;
                        }
                        else if (m[obj.id].a_attr[k] === undefined) {
                            m[obj.id].a_attr[k] = t[type].a_attr[k];
                            if (a) {
                                if (k === 'class') {
                                    a.addClass(t[type].a_attr[k]);
                                }
                                else {
                                    a.attr(k, t[type].a_attr[k]);
                                }
                            }
                        }
                        else if (k === 'href' && m[obj.id].a_attr[k] === '#') {
                            m[obj.id].a_attr['href'] = t[type].a_attr['href'];
                            if (a) { a.attr('href', t[type].a_attr['href']); }
                        }
                        else if (k === 'class') {
                            m[obj.id].a_attr['class'] = t[type].a_attr['class'] + ' ' + m[obj.id].a_attr['class'];
                            if (a) { a.addClass(t[type].a_attr[k]); }
                        }
                    }
                }
            }
 
            return true;
        };
    };
    // include the types plugin by default
    // $.jstree.defaults.plugins.push("types");
}));