2
2
3
3
//Menu service used for managing menus
4
4
angular . module ( 'core' ) . service ( 'Menus' , [
5
- < << << << HEAD
6
5
function ( ) {
7
6
// Define a set of default roles
8
7
this . defaultRoles = [ 'user' ] ;
@@ -12,14 +11,14 @@ angular.module('core').service('Menus', [
12
11
13
12
// A private function for rendering decision
14
13
var shouldRender = function ( user ) {
15
- if ( user ) {
14
+ if ( user ) {
16
15
for ( var userRoleIndex in user . roles ) {
17
16
for ( var roleIndex in this . roles ) {
18
- if ( this . roles [ roleIndex ] === user . roles [ userRoleIndex ] ) {
17
+ if ( this . roles [ roleIndex ] === user . roles [ userRoleIndex ] ) {
19
18
return true ;
20
19
}
21
20
}
22
- }
21
+ }
23
22
} else {
24
23
return this . isPublic ;
25
24
}
@@ -87,37 +86,37 @@ angular.module('core').service('Menus', [
87
86
uiRoute : menuItemUIRoute || ( '/' + menuItemURL ) ,
88
87
isPublic : isPublic || this . menus [ menuId ] . isPublic ,
89
88
roles : roles || this . defaultRoles ,
90
- subitems : [ ] ,
89
+ subitems : [ ] ,
91
90
shouldRender : shouldRender
92
91
} ) ;
93
92
94
93
// Return the menu object
95
94
return this . menus [ menuId ] ;
96
95
} ;
97
-
96
+
98
97
// Add submenu item object
99
- this . addSubMenuItem = function ( menuId , rootMenuItemURL , menuItemTitle , menuItemURL , menuItemUIRoute , isPublic , roles ) {
100
- // Validate that the menu exists
101
- this . validateMenuExistance ( menuId ) ;
102
-
103
- // Search for menu item
104
- for ( var itemIndex in this . menus [ menuId ] . items ) {
105
- if ( this . menus [ menuId ] . items [ itemIndex ] . link === rootMenuItemURL ) {
106
- // Push new submenu item
107
- this . menus [ menuId ] . items [ itemIndex ] . subitems . push ( {
108
- title : menuItemTitle ,
109
- link : menuItemURL ,
110
- uiRoute : menuItemUIRoute || ( '/' + menuItemURL ) ,
111
- isPublic : isPublic || this . menus [ menuId ] . isPublic ,
112
- roles : roles || this . defaultRoles ,
113
- shouldRender : shouldRender
114
- } ) ;
115
- }
116
- }
117
-
118
- // Return the menu object
119
- return this . menus [ menuId ] ;
120
- } ;
98
+ this . addSubMenuItem = function ( menuId , rootMenuItemURL , menuItemTitle , menuItemURL , menuItemUIRoute , isPublic , roles ) {
99
+ // Validate that the menu exists
100
+ this . validateMenuExistance ( menuId ) ;
101
+
102
+ // Search for menu item
103
+ for ( var itemIndex in this . menus [ menuId ] . items ) {
104
+ if ( this . menus [ menuId ] . items [ itemIndex ] . link === rootMenuItemURL ) {
105
+ // Push new submenu item
106
+ this . menus [ menuId ] . items [ itemIndex ] . subitems . push ( {
107
+ title : menuItemTitle ,
108
+ link : menuItemURL ,
109
+ uiRoute : menuItemUIRoute || ( '/' + menuItemURL ) ,
110
+ isPublic : isPublic || this . menus [ menuId ] . isPublic ,
111
+ roles : roles || this . defaultRoles ,
112
+ shouldRender : shouldRender
113
+ } ) ;
114
+ }
115
+ }
116
+
117
+ // Return the menu object
118
+ return this . menus [ menuId ] ;
119
+ } ;
121
120
122
121
// Remove existing menu object by menu id
123
122
this . removeMenuItem = function ( menuId , menuItemURL ) {
@@ -134,138 +133,26 @@ angular.module('core').service('Menus', [
134
133
// Return the menu object
135
134
return this . menus [ menuId ] ;
136
135
} ;
137
-
136
+
138
137
// Remove existing menu object by menu id
139
- this . removeSubMenuItem = function ( menuId , submenuItemURL ) {
140
- // Validate that the menu exists
141
- this . validateMenuExistance ( menuId ) ;
142
-
143
- // Search for menu item to remove
144
- for ( var itemIndex in this . menus [ menuId ] . items ) {
145
- for ( var subitemIndex in this . menus [ menuId ] . items [ itemIndex ] . subitems ) {
146
- if ( this . menus [ menuId ] . items [ itemIndex ] . subitems [ subitemIndex ] . link === submenuItemURL ) {
147
- this . menus [ menuId ] . items [ itemIndex ] . subitems . splice ( subitemIndex , 1 ) ;
148
- }
149
- }
150
- }
151
-
152
- // Return the menu object
153
- return this . menus [ menuId ] ;
154
- } ;
138
+ this . removeSubMenuItem = function ( menuId , submenuItemURL ) {
139
+ // Validate that the menu exists
140
+ this . validateMenuExistance ( menuId ) ;
141
+
142
+ // Search for menu item to remove
143
+ for ( var itemIndex in this . menus [ menuId ] . items ) {
144
+ for ( var subitemIndex in this . menus [ menuId ] . items [ itemIndex ] . subitems ) {
145
+ if ( this . menus [ menuId ] . items [ itemIndex ] . subitems [ subitemIndex ] . link === submenuItemURL ) {
146
+ this . menus [ menuId ] . items [ itemIndex ] . subitems . splice ( subitemIndex , 1 ) ;
147
+ }
148
+ }
149
+ }
150
+
151
+ // Return the menu object
152
+ return this . menus [ menuId ] ;
153
+ } ;
155
154
156
155
//Adding the topbar menu
157
156
this . addMenu ( 'topbar' ) ;
158
157
}
159
- === = ===
160
-
161
- function ( ) {
162
- // Define a set of default roles
163
- this . defaultRoles = [ 'user' ] ;
164
-
165
- // Define the menus object
166
- this . menus = { } ;
167
-
168
- // A private function for rendering decision
169
- var shouldRender = function ( user ) {
170
- if ( user ) {
171
- for ( var userRoleIndex in user . roles ) {
172
- for ( var roleIndex in this . roles ) {
173
- if ( this . roles [ roleIndex ] === user . roles [ userRoleIndex ] ) {
174
- return true ;
175
- }
176
- }
177
- }
178
- } else {
179
- return this . isPublic ;
180
- }
181
-
182
- return false ;
183
- } ;
184
-
185
- // Validate menu existance
186
- this . validateMenuExistance = function ( menuId ) {
187
- if ( menuId && menuId . length ) {
188
- if ( this . menus [ menuId ] ) {
189
- return true ;
190
- } else {
191
- throw new Error ( 'Menu does not exists' ) ;
192
- }
193
- } else {
194
- throw new Error ( 'MenuId was not provided' ) ;
195
- }
196
-
197
- return false ;
198
- } ;
199
-
200
- // Get the menu object by menu id
201
- this . getMenu = function ( menuId ) {
202
- // Validate that the menu exists
203
- this . validateMenuExistance ( menuId ) ;
204
-
205
- // Return the menu object
206
- return this . menus [ menuId ] ;
207
- } ;
208
-
209
- // Add new menu object by menu id
210
- this . addMenu = function ( menuId , isPublic , roles ) {
211
- // Create the new menu
212
- this . menus [ menuId ] = {
213
- isPublic : isPublic || false ,
214
- roles : roles || this . defaultRoles ,
215
- items : [ ] ,
216
- shouldRender : shouldRender
217
- } ;
218
-
219
- // Return the menu object
220
- return this . menus [ menuId ] ;
221
- } ;
222
-
223
- // Remove existing menu object by menu id
224
- this . removeMenu = function ( menuId ) {
225
- // Validate that the menu exists
226
- this . validateMenuExistance ( menuId ) ;
227
-
228
- // Return the menu object
229
- delete this . menus [ menuId ] ;
230
- } ;
231
-
232
- // Add menu item object
233
- this . addMenuItem = function ( menuId , menuItemTitle , menuItemURL , menuItemUIRoute , isPublic , roles ) {
234
- // Validate that the menu exists
235
- this . validateMenuExistance ( menuId ) ;
236
-
237
- // Push new menu item
238
- this . menus [ menuId ] . items . push ( {
239
- title : menuItemTitle ,
240
- link : menuItemURL ,
241
- uiRoute : menuItemUIRoute || ( '/' + menuItemURL ) ,
242
- isPublic : isPublic || this . menus [ menuId ] . isPublic ,
243
- roles : roles || this . defaultRoles ,
244
- shouldRender : shouldRender
245
- } ) ;
246
-
247
- // Return the menu object
248
- return this . menus [ menuId ] ;
249
- } ;
250
-
251
- // Remove existing menu object by menu id
252
- this . removeMenuItem = function ( menuId , menuItemURL ) {
253
- // Validate that the menu exists
254
- this . validateMenuExistance ( menuId ) ;
255
-
256
- // Search for menu item to remove
257
- for ( var itemIndex in this . menus [ menuId ] . items ) {
258
- if ( this . menus [ menuId ] . items [ itemIndex ] . link === menuItemURL ) {
259
- this . menus [ menuId ] . items . splice ( itemIndex , 1 ) ;
260
- }
261
- }
262
-
263
- // Return the menu object
264
- return this . menus [ menuId ] ;
265
- } ;
266
-
267
- //Adding the topbar menu
268
- this . addMenu ( 'topbar' ) ;
269
- }
270
- >>> > >>> pr / 63
271
- ] ) ;
158
+ ] ) ;
0 commit comments