This repository has been archived by the owner on Jun 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Menu.js
122 lines (104 loc) · 2.71 KB
/
Menu.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
import Entity from '../Entity/Entity'
function alwaysFalse() {
return false;
}
var uuid = 0;
var autoClose = true;
class Menu {
constructor() {
this._link = null;
this._activeFunc = alwaysFalse;
this._title = null;
this._icon = false;
this._children = [];
this._template = false;
this._autoClose = true;
this.uuid = uuid++;
}
title() {
if (arguments.length) {
this._title = arguments[0];
return this;
}
return this._title;
}
isLink() {
return !!this._link;
}
link() {
if (arguments.length) {
this._link = arguments[0];
if (this._activeFunc == alwaysFalse) {
this._activeFunc = url => url.indexOf(this._link) === 0;
}
return this;
}
return this._link;
}
autoClose() {
if (arguments.length) {
autoClose = arguments[0];
return this;
}
return autoClose;
}
active(activeFunc) {
if (arguments.length) {
this._activeFunc = arguments[0];
return this;
}
return this._activeFunc;
}
isActive(url) {
return this._activeFunc(url);
}
isChildActive(url) {
return this.isActive(url) || (this.children().filter(menu => menu.isChildActive(url)).length > 0);
}
addChild(child) {
if (!(child instanceof Menu)) {
throw new Error('Only Menu instances are accepted as children of a Menu');
}
this._children.push(child);
return this;
}
hasChild() {
return this._children.length > 0;
}
getChildByTitle(title) {
return this.children().filter(child => child.title() == title).pop();
}
children() {
if (arguments.length) {
this._children = arguments[0];
return this;
}
return this._children;
}
icon() {
if (arguments.length) {
this._icon = arguments[0];
return this;
}
return this._icon;
}
template() {
if (arguments.length) {
this._template = arguments[0];
return this;
}
return this._template;
}
populateFromEntity(entity) {
if (!(entity instanceof Entity)) {
throw new Error('populateFromEntity() only accepts an Entity parameter');
}
this.title(entity.label());
this.active(path => path.indexOf(`/${entity.name()}/`) === 0 );
this.link(`/${entity.name()}/list`);
// deprecated
this.icon(entity.menuView().icon());
return this;
}
}
export default Menu;