5
|
1 /**
|
|
2 * ### Sort plugin
|
|
3 *
|
|
4 * Automatically sorts all siblings in the tree according to a sorting function.
|
|
5 */
|
|
6 /*globals jQuery, define, exports, require */
|
|
7 (function (factory) {
|
|
8 "use strict";
|
|
9 if (typeof define === 'function' && define.amd) {
|
|
10 define('jstree.sort', ['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.sort) { return; }
|
|
22
|
|
23 /**
|
|
24 * the settings function used to sort the nodes.
|
|
25 * It is executed in the tree's context, accepts two nodes as arguments and should return `1` or `-1`.
|
|
26 * @name $.jstree.defaults.sort
|
|
27 * @plugin sort
|
|
28 */
|
|
29 $.jstree.defaults.sort = function (a, b) {
|
|
30 //return this.get_type(a) === this.get_type(b) ? (this.get_text(a) > this.get_text(b) ? 1 : -1) : this.get_type(a) >= this.get_type(b);
|
|
31 return this.get_text(a) > this.get_text(b) ? 1 : -1;
|
|
32 };
|
|
33 $.jstree.plugins.sort = function (options, parent) {
|
|
34 this.bind = function () {
|
|
35 parent.bind.call(this);
|
|
36 this.element
|
|
37 .on("model.jstree", $.proxy(function (e, data) {
|
|
38 this.sort(data.parent, true);
|
|
39 }, this))
|
|
40 .on("rename_node.jstree create_node.jstree", $.proxy(function (e, data) {
|
|
41 this.sort(data.parent || data.node.parent, false);
|
|
42 this.redraw_node(data.parent || data.node.parent, true);
|
|
43 }, this))
|
|
44 .on("move_node.jstree copy_node.jstree", $.proxy(function (e, data) {
|
|
45 this.sort(data.parent, false);
|
|
46 this.redraw_node(data.parent, true);
|
|
47 }, this));
|
|
48 };
|
|
49 /**
|
|
50 * used to sort a node's children
|
|
51 * @private
|
|
52 * @name sort(obj [, deep])
|
|
53 * @param {mixed} obj the node
|
|
54 * @param {Boolean} deep if set to `true` nodes are sorted recursively.
|
|
55 * @plugin sort
|
|
56 * @trigger search.jstree
|
|
57 */
|
|
58 this.sort = function (obj, deep) {
|
|
59 var i, j;
|
|
60 obj = this.get_node(obj);
|
|
61 if(obj && obj.children && obj.children.length) {
|
|
62 obj.children.sort($.proxy(this.settings.sort, this));
|
|
63 if(deep) {
|
|
64 for(i = 0, j = obj.children_d.length; i < j; i++) {
|
|
65 this.sort(obj.children_d[i], false);
|
|
66 }
|
|
67 }
|
|
68 }
|
|
69 };
|
|
70 };
|
|
71
|
|
72 // include the sort plugin by default
|
|
73 // $.jstree.defaults.plugins.push("sort");
|
|
74 })); |