-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathmenu.ts
313 lines (290 loc) · 10.9 KB
/
menu.ts
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
301
302
303
304
305
306
307
308
309
310
311
312
313
/* eslint-disable @typescript-eslint/triple-slash-reference */
/// <reference path='../common/menu-base-model.d.ts'/>
import { attributes, NotifyPropertyChanges, INotifyPropertyChanged, Property } from '@syncfusion/ej2-base';
import { Browser, Complex, getUniqueID, SanitizeHtmlHelper } from '@syncfusion/ej2-base';
import { MenuBase, FieldSettings } from '../common/menu-base';
import { MenuItemModel, FieldSettingsModel } from '../common/menu-base-model';
import { MenuModel } from './menu-model';
const VMENU: string = 'e-vertical';
const SCROLLABLE: string = 'e-scrollable';
const HAMBURGER: string = 'e-hamburger';
/**
* Defines the different types of orientation option available in the Menu.
* ```props
* Horizontal - It renders the menu in a horizontal orientation mode.
* Vertical - It renders the menu in a vertical orientation mode.
* ```
*/
export type Orientation = 'Horizontal' | 'Vertical';
type objColl = { [key: string]: Object }[];
type obj = { [key: string]: Object };
/**
* The Menu is a graphical user interface that serve as navigation headers for your application or site.
* ```html
* <ul id = 'menu'></ul>
* ```
* ```typescript
* <script>
* var menuObj = new Menu({ items: [{ text: 'Home' }, { text: 'Contact Us' },{ text: 'Login' }]});
* menuObj.appendTo("#menu");
* </script>
* ```
*/
@NotifyPropertyChanges
export class Menu extends MenuBase implements INotifyPropertyChanged {
private tempItems: objColl = [];
/**
* Specified the orientation of Menu whether it can be horizontal or vertical.
*
* @default 'Horizontal'
*/
@Property('Horizontal')
public orientation: Orientation;
/**
* Specifies target element to open/close Menu while click in Hamburger mode.
*
* @default ''
*/
@Property('')
public target: string;
/**
* Specifies the template for Menu item.
*
* @default null
* @aspType string
*/
@Property(null)
public template: string | Function;
/**
* Specifies whether to enable / disable the scrollable option in Menu.
*
* @default false
*/
@Property(false)
public enableScrolling: boolean;
/**
* Specifies whether to enable / disable the hamburger mode in Menu.
*
* @default false
*/
@Property(false)
public hamburgerMode: boolean;
/**
* Specifies the title text for hamburger mode in Menu.
*
* @default 'Menu'
*/
@Property('Menu')
public title: string;
/**
* Specifies whether to enable the rendering of untrusted HTML values in the Menu component.
* If 'enableHtmlSanitizer' set to true, the component will sanitize any suspected untrusted strings and scripts before rendering them.
*
* @default true
*/
@Property(true)
public enableHtmlSanitizer: boolean;
/**
* Specifies mapping fields from the dataSource.
*
* @default { itemId: "id", text: "text", parentId: "parentId", iconCss: "iconCss", url: "url", separator: "separator",
* children: "items" }
*/
// eslint:disable-next-line
@Complex<FieldSettingsModel>({ itemId: 'id', text: 'text', parentId: 'parentId', iconCss: 'iconCss', url: 'url', separator: 'separator', children: 'items' }, FieldSettings)
public fields: FieldSettingsModel;
/**
* Constructor for creating the component.
*
* @private
* @param {MenuModel} options - Specifies the menu model
* @param {string} element - Specifies the element
*/
constructor(options?: MenuModel, element?: string | HTMLUListElement) {
super(options, <HTMLUListElement | string>element);
}
/**
* Get module name.
*
* @private
* @returns {string} - Module Name
*/
protected getModuleName(): string {
return 'menu';
}
/**
* For internal use only - prerender processing.
*
* @private
* @returns {void}
*/
protected preRender(): void {
this.isMenu = true;
this.element.id = this.element.id || getUniqueID('ej2-menu');
if (this.template) {
try {
if (typeof this.template !== 'function' && document.querySelectorAll(this.template).length) {
this.template = document.querySelector(this.template).innerHTML.trim();
this.clearChanges();
}
} catch (e) {
/* action on catch */
}
this.updateMenuItems(this.items);
} else {
this.updateMenuItems(this.items);
}
super.preRender();
}
protected initialize(): void {
super.initialize();
attributes(this.element, <{ [key: string]: string }>{ 'role': 'menubar', 'tabindex': '0' });
if (this.orientation === 'Vertical') {
this.element.classList.add(VMENU);
if (this.hamburgerMode && !this.target) {
this.element.previousElementSibling.classList.add(VMENU);
}
this.element.setAttribute('aria-orientation', 'vertical');
} else {
if (Browser.isDevice && !this.enableScrolling) {
this.element.parentElement.classList.add(SCROLLABLE);
}
}
if (this.hamburgerMode) {
this.element.parentElement.classList.add(HAMBURGER);
if (this.orientation === 'Horizontal') {
this.element.classList.add('e-hide-menu');
}
}
}
private updateMenuItems(items: MenuItemModel[]): void {
this.tempItems = items as objColl;
this.items = [];
this.tempItems.map(this.createMenuItems, this);
this.setProperties({ items: this.items }, true);
this.tempItems = [];
}
/**
* Called internally if any of the property value changed.
*
* @private
* @param {MenuModel} newProp - Specifies the new properties.
* @param {MenuModel} oldProp - Specifies the old properties.
* @returns {void}
*/
public onPropertyChanged(newProp: MenuModel, oldProp: MenuModel): void {
for (const prop of Object.keys(newProp)) {
switch (prop) {
case 'orientation':
if (newProp.orientation === 'Vertical') {
this.element.classList.add(VMENU);
if (this.hamburgerMode) {
if (!this.target) { this.element.previousElementSibling.classList.add(VMENU); }
this.element.classList.remove('e-hide-menu');
}
this.element.setAttribute('aria-orientation', 'vertical');
} else {
this.element.classList.remove(VMENU);
if (this.hamburgerMode) {
if (!this.target) { this.element.previousElementSibling.classList.remove(VMENU); }
this.element.classList.add('e-hide-menu');
}
this.element.removeAttribute('aria-orientation');
}
break;
case 'items':
if (!Object.keys(oldProp.items).length) { this.updateMenuItems(newProp.items); }
break;
case 'hamburgerMode':
if (!this.element.previousElementSibling) {
super.createHeaderContainer();
}
if (newProp.hamburgerMode) {
this.element.parentElement.classList.add(HAMBURGER);
[].slice.call(this.element.getElementsByClassName('e-blankicon')).forEach((li: HTMLElement): void => {
li.style[this.enableRtl ? 'paddingRight' : 'paddingLeft'] = '';
});
} else {
this.element.parentElement.classList.remove(HAMBURGER);
if (this.orientation === 'Vertical') { this.setBlankIconStyle(this.element); }
}
if (this.orientation === 'Vertical') {
if (!this.target) { this.element.previousElementSibling.classList.add(VMENU); }
this.element.classList.remove('e-hide-menu');
} else {
if (this.target) {
this.element.previousElementSibling.classList.add(VMENU);
} else {
this.element.previousElementSibling.classList.remove(VMENU);
}
this.element.classList[newProp.hamburgerMode ? 'add' : 'remove']('e-hide-menu');
}
break;
case 'title':
if (this.hamburgerMode && this.element.previousElementSibling) {
newProp.title = (this.enableHtmlSanitizer) ? SanitizeHtmlHelper.sanitize(newProp.title) : newProp.title;
this.element.previousElementSibling.querySelector('.e-menu-title').innerHTML = newProp.title;
}
break;
case 'target':
if (this.hamburgerMode) {
this.unWireEvents(oldProp.target);
this.wireEvents();
if (this.orientation === 'Horizontal') {
if (!newProp.target) {
if (!this.element.previousElementSibling) {
super.createHeaderContainer();
}
this.element.previousElementSibling.classList.remove(VMENU);
} else {
this.element.previousElementSibling.classList.add(VMENU);
}
this.element.classList.add('e-hide-menu');
}
}
break;
case 'template':
this.template = newProp.template;
this.refresh();
break;
}
}
super.onPropertyChanged(newProp, oldProp);
}
private createMenuItems(item: obj): void {
let idx: number[];
let i: number;
let items: MenuItemModel[] = this.items as objColl;
const pIdField: string = this.getField('parentId');
if (item[`${pIdField}`]) {
idx = this.getIndex(item[`${pIdField}`].toString(), true);
for (i = 0; i < idx.length; i++) {
if (!items[idx[i as number]].items) {
items[idx[i as number]].items = [];
}
items = items[idx[i as number]].items;
}
items.push(item);
} else {
(<MenuItemModel[]>this.items).push(item);
}
}
/**
* This method is used to open the Menu in hamburger mode.
*
* @function open
* @returns {void}
*/
public open(): void {
super.openHamburgerMenu();
}
/**
* Closes the Menu if it is opened in hamburger mode.
*
* @function close
* @returns {void}
*/
public close(): void {
super.closeHamburgerMenu();
}
}