-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathcontext-menu.ts
155 lines (143 loc) · 4.65 KB
/
context-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
/* eslint-disable @typescript-eslint/triple-slash-reference */
/// <reference path='../common/menu-base-model.d.ts'/>
import { attributes, getUniqueID, Collection, NotifyPropertyChanges, INotifyPropertyChanged, Property } from '@syncfusion/ej2-base';
import { getZindexPartial } from '@syncfusion/ej2-popups';
import { ContextMenuModel } from './context-menu-model';
import { MenuBase, MenuItem } from '../common/menu-base';
import { MenuItemModel } from './../common/menu-base-model';
/**
* The ContextMenu is a graphical user interface that appears on the user right click/touch hold operation.
* ```html
* <div id = 'target'></div>
* <ul id = 'contextmenu'></ul>
* ```
* ```typescript
* <script>
* var contextMenuObj = new ContextMenu({items: [{ text: 'Cut' }, { text: 'Copy' },{ text: 'Paste' }], target: '#target'});
* </script>
* ```
*/
@NotifyPropertyChanges
export class ContextMenu extends MenuBase implements INotifyPropertyChanged {
/**
* Constructor for creating the widget.
*
* @private
* @param {ContextMenuModel} options - Specifies the context menu model
* @param {string} element - Specifies the element
*/
constructor(options?: ContextMenuModel, element?: string | HTMLUListElement) {
super(options, <HTMLUListElement | string>element);
}
/**
* Specifies target element selector in which the ContextMenu should be opened.
*
* @default ''
*/
@Property('')
public target: string;
/**
* Specifies the filter selector for elements inside the target in that the context menu will be opened.
*
* @default ''
*/
@Property('')
public filter: string;
/**
* Specifies menu items with its properties which will be rendered as ContextMenu.
*
* @default []
* @aspType object
* @blazorType object
*/
@Collection<MenuItemModel>([], MenuItem)
public items: MenuItemModel[];
/**
* This property allows you to define custom templates for items in the ContextMenu.
*
* @default null
* @aspType string
*/
@Property(null)
public itemTemplate: string | Function;
/**
* Specifies whether to enable / disable the scrollable option in ContextMenu.
*
* @default false
*/
@Property(false)
public enableScrolling: boolean;
/**
* For internal use only - prerender processing.
*
* @private
* @returns {void}
*/
protected preRender(): void {
this.isMenu = false;
this.element.id = this.element.id || getUniqueID('ej2-contextmenu');
super.preRender();
}
protected initialize(): void {
this.template = this.itemTemplate ? this.itemTemplate : null;
super.initialize();
attributes(this.element, <{ [key: string]: string }>{ 'role': 'menubar', 'tabindex': '0' });
this.element.style.zIndex = getZindexPartial(this.element).toString();
}
/**
* This method is used to open the ContextMenu in specified position.
*
* @param {number} top - To specify ContextMenu vertical positioning.
* @param {number} left - To specify ContextMenu horizontal positioning.
* @param {HTMLElement} target - To calculate z-index for ContextMenu based upon the specified target.
* @function open
* @returns {void}
*/
public open(top: number, left: number, target?: HTMLElement): void {
super.openMenu(null, null, top, left, null, target);
}
/**
* Closes the ContextMenu if it is opened.
*
* @function close
* @returns {void}
*/
public close(): void {
super.closeMenu();
}
/**
* Called internally if any of the property value changed.
*
* @private
* @param {ContextMenuModel} newProp - Specifies new properties
* @param {ContextMenuModel} oldProp - Specifies old properties
* @returns {void}
*/
public onPropertyChanged(newProp: ContextMenuModel, oldProp: ContextMenuModel): void {
super.onPropertyChanged(newProp, oldProp);
for (const prop of Object.keys(newProp)) {
switch (prop) {
case 'filter':
this.close();
this.filter = newProp.filter;
break;
case 'target':
this.unWireEvents(oldProp.target);
this.wireEvents();
break;
case 'itemTemplate':
this.itemTemplate = newProp.itemTemplate;
this.refresh();
}
}
}
/**
* Get module name.
*
* @returns {string} - Module Name
* @private
*/
protected getModuleName(): string {
return 'contextmenu';
}
}