-
Notifications
You must be signed in to change notification settings - Fork 3
/
active-menu-node.js
300 lines (258 loc) · 6.39 KB
/
active-menu-node.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
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
/**
* Active Menu Node Class
*
* @param menuInstance
* @param parentNode
* @constructor
*/
var ActiveMenuNode = module.exports = function(menuInstance, parentNode) {
/**
* Parent Node
* @type {ActiveMenu | ActiveMenuNode}
*/
this.parent = parentNode;
/**
* Menu Instance
* @type {ActiveMenu}
*/
this.menuInstance = menuInstance;
/**
* Display Text
* @type {String}
*/
this.text = null;
/**
* Route
* @type {String}
*/
this.route = null;
/**
* Element Type
* @type {string}
*/
this.elementType = 'ul';
/**
* Children Nodes
* @type {ActiveMenuNode[]}
*/
this.childNodes = [];
/**
* HTML Attributes
* @type {Array}
*/
this.htmlAttributes = [];
/**
* Menu Level / Depth
* @type {number}
*/
this.depth = parentNode.depth + 1;
/**
* Whether This Node is Active
* @type {boolean}
*/
this.isActive = false;
/**
* Whether This Node is First In The Current Level
* @type {boolean}
*/
this.isFirst = false;
/**
* Whether This Node is Last In The Current Level
* @type {boolean}
*/
this.isLast = false;
};
/**
* Set HTML Attributes
*
* @param attributes
* @returns {ActiveMenuNode}
*/
ActiveMenuNode.prototype.setAttributes = function(attributes) {
this.htmlAttributes = attributes;
return this;
};
/**
* Check If This Node Is a List
*
* @returns {boolean}
*/
ActiveMenuNode.prototype.isList = function() {
return this.elementType == 'ul';
};
/**
* Check If This Node is a Parent
*/
ActiveMenuNode.prototype.isParent = function() {
return this.childNodes.length > 0;
};
/**
* Add a Child Node to This Node
*
* @param text
* @param route
* @returns {ActiveMenuNode}
*/
ActiveMenuNode.prototype.addMenuNode = function(text, route) {
var newNode = new ActiveMenuNode(this.menuInstance, this);
newNode.text = text;
newNode.route = route;
if (this.isList()) {
newNode.elementType = 'li';
}
this.childNodes.push(newNode);
return newNode;
};
/**
* Activate This Branch of the Menu Tree.
*
* Iterates up the parents of this node, activating them
* by appending a class of 'active' to their list element.
*/
ActiveMenuNode.prototype.activateBranch = function() {
this.isActive = true;
// Stop At The Top of The Branch
if (this.parent.hasOwnProperty('activateBranch')) {
this.parent.activateBranch();
} else {
this.parent.isActive = true;
}
};
/**
* Get The Render-able HTML Attributes
*
* @returns {Array}
*/
ActiveMenuNode.prototype.getRenderHtmlAttributes = function() {
// Attributes
var htmlAttributes = [];
for (var key in this.htmlAttributes) {
htmlAttributes[key] = this.htmlAttributes[key];
}
// Get Class Array
var htmlClassArray = [];
// If Classes Are Already Defined, We Need to Append Rather Than Replace
if (this.htmlAttributes.hasOwnProperty('class')) {
htmlClassArray = this.htmlAttributes.class.split(' ');
}
// Handle Active State
if (this.isActive) {
htmlClassArray.push('active');
}
// Handle Depth
htmlClassArray.push('level-' + this.depth);
// Handle Case of Being Only Child, Then First, Then Last
if (this.isFirst && this.isLast) {
htmlClassArray.push('only');
} else if (this.isFirst) {
htmlClassArray.push('first');
} else if (this.isLast) {
htmlClassArray.push('last');
}
// Handle Case of Being A Parent
if (this.isParent()) {
htmlClassArray.push('parent');
}
// Join Class List
htmlAttributes.class = htmlClassArray.join(' ');
// Return
return htmlAttributes;
};
/**
* Get HTML Attributes for a List Element (<ul>)
* @returns {Array}
*/
ActiveMenuNode.prototype.getListHtmlAttributes = function()
{
// Html Attributes
var htmlAttributes = [];
// HTML Classes
var htmlClasses = [];
// Handle Active State
if (this.isActive) {
htmlClasses.push('active');
}
// Handle Depth
htmlClasses.push('level-' + this.depth);
// Add Classe
htmlAttributes.class = htmlClasses.join(' ');
// Return
return htmlAttributes;
};
/**
* Generate The Inner HTML For This Node
* @returns {String}
*/
ActiveMenuNode.prototype.getInnerHtml = function() {
// Inner Element Type
var elementType;
if (this.route) {
elementType = "a";
} else {
elementType = "span";
}
// Html Attributes
var htmlAttributes = [];
htmlAttributes.href = this.route;
// Check Current Route
if (this.route == this.menuInstance.getCurrentRequestRoute()) {
// Add Active Link Class to Link
htmlAttributes.class = 'active';
// Activate Parents
this.activateBranch();
}
// Inner Html
var innerHtml = this.menuInstance.generator[elementType](
htmlAttributes,
this.text
);
// If A List, Inner HTML Must Be Wrapped in a List Item
if (this.isList()) {
innerHtml = this.menuInstance.generator.li(
this.getRenderHtmlAttributes(),
innerHtml
);
}
// Compile and Return
return innerHtml.compile();
};
/**
* Generate the HTML for this Node
* @returns {String}
*/
ActiveMenuNode.prototype.toHtml = function() {
// New HTML Tree
var elementInnerHtml = [];
// Set To Inactive (Prevents Caching of Active State on Nodes That Aren't Cached)
this.isActive = false;
// Inner Html
elementInnerHtml.push(this.getInnerHtml());
// Node List for Reference Below
var nodeList = this.childNodes;
// Render Children Source
this.childNodes.forEach(function(childNode, key) {
// Handle First and Last
if (key == 0) {
childNode.isFirst = true;
}
if (key == (nodeList.length - 1)) {
childNode.isLast = true;
}
// Append
elementInnerHtml.push(childNode.toHtml());
});
// Handle List Attributes
var htmlAttributes;
if (this.isList()) {
htmlAttributes = this.getListHtmlAttributes();
} else {
htmlAttributes = this.getRenderHtmlAttributes();
}
// Render
var elementHtml = this.menuInstance.generator[this.elementType](
htmlAttributes,
elementInnerHtml
);
// Compile and Return
return elementHtml.compile();
};