-
Notifications
You must be signed in to change notification settings - Fork 0
/
jstree.columns.js
138 lines (120 loc) · 4.3 KB
/
jstree.columns.js
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
/*
* http://github.com/ramseydsilva/jstree-columns
*
* This plugin handles adds additional columns to your jstree
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Author Ramsey D'silva (ramseydsilva.com)
*
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery', 'jstree'], factory);
} else {
factory(jQuery);
}
}(function ($) {
var _triggerSelect = true;
$.jstree.defaults.columns = {};
$.jstree.plugins.columns = function(options,parent) {
var getNodeName = function(toReplace, mainInstance, newInstance) {
if (typeof toReplace == "string") {
return toReplace.replace('j'+mainInstance._id+'_', 'j'+newInstance._id+'_');
} else if (Object.prototype.toString.call( toReplace ) === '[object Array]') {
for(var i=0; i<toReplace.length; i++) {
toReplace[i] = getNodeName(toReplace[i], mainInstance, newInstance);
}
return toReplace;
} else {
return toReplace;
}
}
var replaceKeyValues = function(object, key, mainInstance, newInstance, textKey) {
var newKey = getNodeName(key, mainInstance, newInstance); // replace key
if (newKey != key) {
object[newKey] = object[key]; // copy data into new key
delete object[key];
} else {
newKey = key;
}
if (newKey == "text") {
object[newKey] = object.data ? object.data[textKey] : "";
}
object[newKey] = getNodeName(object[newKey], mainInstance, newInstance); // replace value of the key
return newKey;
}
var generateDataModel = function(object, mainInstance, newInstance, textKey) {
for (var key in object) {
if (object.hasOwnProperty(key)) {
var newKey = replaceKeyValues(object, key, mainInstance, newInstance, textKey);
if (newKey.indexOf('data') != -1 || newKey.indexOf("#") != -1 || newKey.indexOf("j"+newInstance._id+"_") != -1
|| newKey.indexOf("children") != -1 || newKey.indexOf("parent") != -1)
generateDataModel(object[newKey], mainInstance, newInstance, textKey);
}
}
}
var redraw = function(mainInstance, newInstance, textKey) {
var _model = $.extend(true, {}, mainInstance._model);
generateDataModel(_model, mainInstance, newInstance, textKey);
newInstance._model = _model;
newInstance.redraw(true);
}
var redrawParent = function(parentNode, mainInstance, newInstance, textKey) {
var _model = $.extend(true, {}, mainInstance._model);
generateDataModel(_model, mainInstance, newInstance, textKey);
newInstance._model = _model;
newInstance.redraw_node(parentNode);
}
var getNode = function(node, mainInstance, newInstance) {
return newInstance.get_node(getNodeName(node.id, mainInstance, newInstance));
}
var bind = function(column) {
var mainInstance = this;
var instance = column.instance;
var textKey = column.key;
mainInstance.element
.on("open_node.jstree", function(e, data) {
var node = getNode(data.node, mainInstance, instance);
redrawParent(node, mainInstance, instance, textKey);
})
.on("close_node.jstree", function(e, data) {
var node = getNode(data.node, mainInstance, instance);
instance.close_node(node);
})
.on("delete_node.jstree", function(e, data) {
redraw(mainInstance, instance, textKey);
})
.on("move_node.jstree", function(e, data) {
redraw(mainInstance, instance, textKey);
})
.on("model.jstree", function(e, args) {
redraw(mainInstance, instance, textKey);
});
instance.element
.on("rename_node.jstree", function(e, args) {
var node = getNode(args.node, instance, mainInstance);
node.data[textKey] = instance.get_text(args.node);
}).on("click", function(e) {
e.preventDefault();
return false;
});
};
var initialize = function(el, options) {
for(var i=0; i < this.settings.columns.length; i++) {
var tree = this.settings.columns[i].tree;
if (typeof tree.on != "function") {
tree = tree.element;
}
this.settings.columns[i].instance = $.jstree.reference(tree);
var column = this.settings.columns[i];
bind.call(this, column);
};
};
this.init = function (el,options) {
parent.init.call(this, el, options);
initialize.call(this, el, options);
};
};
}));