forked from syncfusion/ej2-javascript-ui-controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
212 lines (197 loc) · 6.51 KB
/
common.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
import { ChildProperty, extend, deleteObject, Property, BaseEventArgs, addClass, isNullOrUndefined } from '@syncfusion/ej2-base';
import { ItemModel } from './common-model';
/**
* Defines the icon position of Split Button.
*/
export type SplitButtonIconPosition = 'Left' | 'Top';
/**
* @param {Object} props - Specifies the properties
* @param {string[]} model - Specifies the model
* @returns {Object} Component Model
*/
export function getModel(props: Object, model: string[]): Object {
const obj: Object = extend({}, props);
for (const prop of Object.keys(obj)) {
if ((model).indexOf(prop) < 0) {
deleteObject(obj, prop);
}
}
return obj as Object;
}
/** @hidden
* @param {HTMLElement} ul - Specifies the UL element
* @param {number} keyCode - Specifies the keycode
* @returns {void}
*/
export function upDownKeyHandler(ul: HTMLElement, keyCode: number): void {
const defaultIdx: number = keyCode === 40 ? 0 : ul.childElementCount - 1;
let liIdx: number = defaultIdx;
let li: Element;
const selectedLi: Element = ul.querySelector('.e-selected');
if (selectedLi) { selectedLi.classList.remove('e-selected'); }
for (let i: number = 0, len: number = ul.children.length; i < len; i++) {
if (ul.children[i as number].classList.contains('e-focused')) {
li = ul.children[i as number];
liIdx = i;
li.classList.remove('e-focused');
if (keyCode === 40) {
liIdx++;
} else {
liIdx--;
}
if (liIdx === (keyCode === 40 ? ul.childElementCount : -1)) {
liIdx = defaultIdx;
}
}
}
li = ul.children[liIdx as number];
liIdx = isValidLI(ul, li, liIdx, keyCode);
if (liIdx !== -1) {
addClass([ul.children[liIdx as number]], 'e-focused');
(ul.children[liIdx as number] as HTMLElement).focus();
}
}
/**
* Get Valid LI element
*
* @param {HTMLElement} ul - Specifies the UL element
* @param {Element} li - Specifies the LI element
* @param {number} index - Specifies the index
* @param {number} keyCode - Specifies the keycode
* @param {number} count - Specifies the count
* @returns {number} - Index
*/
function isValidLI(ul: HTMLElement, li: Element, index: number, keyCode: number, count: number = 0): number {
if (li.classList.contains('e-separator') || li.classList.contains('e-disabled')) {
if (index === (keyCode === 40 ? ul.childElementCount - 1 : 0)) {
index = keyCode === 40 ? 0 : ul.childElementCount - 1;
} else {
if (keyCode === 40) {
index++;
} else {
index--;
}
}
}
li = ul.children[index as number];
if (li.classList.contains('e-separator') || li.classList.contains('e-disabled')) {
count++;
if (count === ul.childElementCount) {
return index = -1;
}
index = isValidLI(ul, li, index, keyCode, count);
}
return index;
}
/** @hidden
* @param {HTMLElement} popup - Specifies the popup element.
* @returns {void}
*/
export function setBlankIconStyle(popup: HTMLElement, blankIcon?: boolean): void {
const blankIconList: HTMLElement[] = [].slice.call(popup.getElementsByClassName('e-blank-icon'));
if (blankIcon) {
const menuItem: HTMLElement[] = [].slice.call(popup.getElementsByClassName('e-item'));
menuItem.forEach((li: HTMLElement): void => {
if (li.style.paddingLeft || li.style.paddingRight) {
li.removeAttribute('style');
}
});
}
if (!blankIconList.length) { return; }
let iconLi: HTMLElement = popup.querySelector('.e-item:not(.e-blank-icon):not(.e-separator)') as HTMLElement;
if (isNullOrUndefined(iconLi)) {return; }
if (iconLi.classList.contains('e-url')) { iconLi = iconLi.querySelector('.e-menu-url'); }
const icon: HTMLElement = iconLi.querySelector('.e-menu-icon') as HTMLElement;
let cssProp: { padding: string, margin: string };
const enableRtl: boolean = popup.classList.contains('e-rtl');
if (enableRtl) {
cssProp = { padding: 'paddingRight', margin: 'marginLeft' };
} else {
cssProp = { padding: 'paddingLeft', margin: 'marginRight' };
}
/* eslint-disable */
let size: string = `${parseInt(getComputedStyle(icon).fontSize, 10) + parseInt(
(enableRtl ? (getComputedStyle(icon) as any)[cssProp.margin] : (getComputedStyle(icon) as any)[cssProp.margin]), 10)
+ parseInt(getComputedStyle(iconLi).paddingLeft, 10)}px`;
blankIconList.forEach((li: HTMLElement): void => {
if (li.classList.contains('e-url')) {
((li.querySelector('.e-menu-url') as HTMLElement).style as any)[cssProp.padding] = size;
} else {
(li.style as any)[cssProp.padding] = size;
}
});
/* eslint-enable */
}
/**
* Defines the items of Split Button/DropDownButton.
*/
export class Item extends ChildProperty<Item> {
/**
* Defines class/multiple classes separated by a space for the item that is used to include an icon.
* Action item can include font icon and sprite image.
*
* @default ''
*/
@Property('')
public iconCss: string;
/**
* Specifies the id for item.
*
* @default ''
*/
@Property('')
public id: string;
/**
* Specifies separator between the items. Separator are horizontal lines used to group action items.
*
* @default false
*/
@Property(false)
public separator: boolean;
/**
* Specifies text for item.
*
* @default ''
*/
@Property('')
public text: string;
/**
* Specifies url for item that creates the anchor link to navigate to the url provided.
*
* @default ''
*/
@Property('')
public url: string;
/**
* Used to enable or disable the item.
*
* @default false
*/
@Property(false)
public disabled: boolean;
}
/**
* Interface for before item render / select event.
*/
export interface MenuEventArgs extends BaseEventArgs {
element: HTMLElement;
item: ItemModel;
event?: Event;
}
/**
* Interface for before open / close event.
*/
export interface BeforeOpenCloseMenuEventArgs extends BaseEventArgs {
element: HTMLElement;
items: ItemModel[];
event: Event;
cancel?: boolean;
}
/**
* Interface for open/close event.
*/
export interface OpenCloseMenuEventArgs extends BaseEventArgs {
element: HTMLElement;
items: ItemModel[];
parentItem?: ItemModel;
}