forked from chniter/bstreeview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bstreeview.js
201 lines (188 loc) · 7.18 KB
/
bstreeview.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
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
/*! @preserve
* bstreeview.js
* Version: 1.1.0
* Authors: Sami CHNITER <sami.chniter@gmail.com>
* Copyright 2020
* License: Apache License 2.0
*
* Project: https://github.com/chniter/bstreeview
*/
; (function ($, window, document, undefined) {
"use strict";
/**
* Default bstreeview options.
*/
var pluginName = "bstreeview",
defaults = {
expandIcon: 'fa fa-angle-down',
collapseIcon: 'fa fa-angle-right',
// expandIcon: 'show',
// collapseIcon: 'hide',
indent: 1.25
};
/**
* bstreeview HTML templates.
*/
var templates = {
treeview: '<div class="bstreeview"></div>',
treeviewItem: '<div href="#itemid" class="list-group-item" data-toggle="collapse"></div>',
treeviewGroupItem: '<div class="list-group collapse" id="itemid"></div>',
treeviewItemStateIcon: '<i class="state-icon"></i>',
treeviewItemIcon: '<i class="item-icon"></i>'
};
/**
* BsTreeview Plugin constructor.
* @param {*} element
* @param {*} options
*/
function bstreeView(element, options) {
this.element = element;
this.itemIdPrefix = element.id + "-item-";
this.settings = $.extend({}, defaults, options);
this.init();
}
/**
* Avoid plugin conflict.
*/
$.extend(bstreeView.prototype, {
/**
* bstreeview intialize.
*/
init: function () {
this.tree = [];
this.nodes = [];
// Retrieve bstreeview Json Data.
if (this.settings.data) {
this.settings.data = $.parseJSON(this.settings.data);
this.tree = $.extend(true, [], this.settings.data);
delete this.settings.data;
}
// Set main bstreeview class to element.
$(this.element).addClass('bstreeview');
this.initData({ nodes: this.tree });
var _this = this;
this.build($(this.element), this.tree, 0);
// Update angle icon on collapse
$('.bstreeview').on('click', '.list-group-item', function () {
$('.state-icon', this)
.toggleClass(_this.settings.expandIcon)
.toggleClass(_this.settings.collapseIcon);
});
},
/**
* Initialize treeview Data.
* @param {*} node
*/
initData: function (node) {
if (!node.nodes) return;
var parent = node;
var _this = this;
$.each(node.nodes, function checkStates(index, node) {
node.nodeId = _this.nodes.length;
node.parentId = parent.nodeId;
_this.nodes.push(node);
if (node.nodes) {
_this.initData(node);
}
});
},
/**
* Build treeview.
* @param {*} parentElement
* @param {*} nodes
* @param {*} depth
*/
build: function (parentElement, nodes, depth) {
var _this = this;
// Calculate item padding.
var leftPadding = "1.25rem;";
if (depth > 0) {
leftPadding = (_this.settings.indent + depth * _this.settings.indent).toString() + "rem;";
}
depth += 1;
// Add each node and sub-nodes.
$.each(nodes, function addNodes(id, node) {
// Main node element.
var treeItem = $(templates.treeviewItem)
.attr('href', "#" + _this.itemIdPrefix + node.nodeId)
.attr('style', 'padding-left:' + leftPadding);
// Set Expand and Collapse icones.
if (node.nodes) {
var treeItemStateIcon = $(templates.treeviewItemStateIcon)
.addClass(_this.settings.collapseIcon);
treeItem.append(treeItemStateIcon);
}
// set node icon if exist.
if (node.icon) {
var treeItemIcon = $(templates.treeviewItemIcon)
.addClass(node.icon);
treeItem.append(treeItemIcon);
}
// Set node Text.
treeItem.append(node.text);
// Reset node href if present
if (node.href) {
treeItem.attr('href', node.href);
}
// Add class to node if present
if (node.class) {
treeItem.addClass(node.class);
}
// Add custom id to node if present
if (node.id) {
treeItem.attr('id', node.id);
}
// Methods
jQuery.fn.extend({
// expand/collapse a list depending on 'mode'
// returns jQuery object with the menu item the method was called on (the item clicked to expand/collapse a list)
bstreeCollapse: function(mode) {
var list = this.next('.list-group'),
collapsed;
if (mode === 'show' && !list.hasClass('show')) {
collapsed = list.collapse('show');
} else if (mode === 'hide' && list.hasClass('show')) {
collapsed = list.collapse('hide');
}
if (collapsed) {
$('.state-icon', this)
.toggleClass(_this.settings.expandIcon)
.toggleClass(_this.settings.collapseIcon);
}
return this;
},
// disable/enable a menu item
// returns jQuery object with the menu item the method was called on (the menu item disabled or enabled)
bstreeItemDisable: function(disable) {
if (disable) {
this.addClass('disabled');
} else {
this.removeClass('disabled');
}
return this;
}
});
// Attach node to parent.
parentElement.append(treeItem);
// Build child nodes.
if (node.nodes) {
// Node group item.
var treeGroup = $(templates.treeviewGroupItem)
.attr('id', _this.itemIdPrefix + node.nodeId);
parentElement.append(treeGroup);
_this.build(treeGroup, node.nodes, depth);
}
});
}
});
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" +
pluginName, new bstreeView(this, options));
}
});
};
})(jQuery, window, document);