-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMenuItem.js
More file actions
179 lines (126 loc) · 3.88 KB
/
MenuItem.js
File metadata and controls
179 lines (126 loc) · 3.88 KB
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
/*globals define, app*/
/*
se encarga de la comunición con meta
config: puede sobreescribir application, endpoint, template
*/
define( [
'lodash'
], function(
_
) {
'use strict';
var MenuItem = function(options) {
options = options || {};
_.extend(this, options);
if (this.id !== 0) this.id = this.id || undefined;
if (this.id === undefined) throw new Error('menu id not defined');
this.type = this.type || 'menu';
this.parentId = this.parentId || null;
this.order = this.order || undefined;
this.name = this.name ||undefined;
if (this.name === undefined) throw new Error('menu name not defined');
this.description = this.description || '';
this.url = this.url || '';
this.resource = this.resource || '';
this.help = this.help ||'';
this.show = (this.show === undefined ? true : this.show);
this.level = (this.level === undefined ? 0 : this.level);
this.children = this.children || [];
};
MenuItem.prototype = {
hasChildren: function() {
return (this.children ? this.children.length > 0 : false);
},
isParent: function() {
// return (!this.isSep() && (!this.url || this.url === '#'));
return (!this.isSep() && (!this.url || this.url === ''));
},
isSep: function() {
return ((this.type || '') === 'sep');
},
loadChildren: function(items) {
// get only the childs of this menu
var children = _.filter(items, function(item) {
return item.parentId === this.id;
}, this);
if (children.length === 0) {
this.children = [];
return this;
}
// order by order
children = _.sortBy(children, function(child) {
return child.order;
}, this);
// instantiate a new Menu for every child item
children = _.map(children, function(child) {
child.level = this.level + 1;
return new MenuItem(child);
}, this);
// recursive call to load the children of each child
_.each(children, function(child) {
child.loadChildren(items);
}, this);
this.children = children;
return this;
},
applyPermissions: function() {
this._applyPermissions();
this.removeInvisibleItems();
return this;
},
_applyPermissions: function() {
if (_.isUndefined(app) || !app || !app.user) {
throw new Error('app.user has not been loaded yet');
}
if (this.isSep()) {
this.enabled = true;
} else if (!this.resource) {
this.enabled = true;
} else if (app.user.can(this.resource)) {
this.enabled = true;
} else {
this.enabled = false;
}
this.visible = (this.enabled || this.show);
_.each(this.children, function(child) {
child._applyPermissions();
});
},
removeInvisibleItems: function() {
if (!this.hasChildren()) { return; }
// process each child
_.each(this.children, function(child) {
// delete child.visible; // no longer needed, only visible items remained
child.removeInvisibleItems();
});
var prev = null;
this.children = _.filter(this.children, function(child) {
// first sep is removed
if (prev === null && child.isSep()) { return false; }
// avoid two seps together
if (prev && prev.isSep() && child.isSep()) { return false; }
// get rid of invisible items
if (child.visible === false) { return false; }
// parent left with no childs
if (child.isParent() && !child.hasChildren()) { return false; }
prev = child;
return true;
});
// remove last item if it's a sep
if (this.hasChildren() && _.last(this.children).isSep()) {
this.children = _.first(this.children, this.children.length-1);
}
_.each(this.children, function(child) {
delete child.visible; // no longer needed, only visible items remained
});
}
};
MenuItem.root = function() {
return new MenuItem({
id: 'root',
name: 'root',
level: 0
});
};
return MenuItem;
});