/
side-menu-content.component.ts
executable file
·291 lines (239 loc) · 10.1 KB
/
side-menu-content.component.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
// Angular
import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core'; // tslint:disable-line
// Ionic
import { Events, Platform } from 'ionic-angular';
// Models
import { SideMenuSettings } from './models/side-menu-settings';
import { MenuOptionModel } from './models/menu-option-model';
import { SideMenuRedirectEvent, SideMenuRedirectEventData } from './models/side-menu-redirect-events';
import { InnerMenuOptionModel } from './models/inner-menu-option-model';
@Component({
selector: 'side-menu-content',
templateUrl: 'side-menu-content.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SideMenuContentComponent {
// Main inputs
public menuSettings: SideMenuSettings;
public menuOptions: MenuOptionModel[];
public collapsableItems: InnerMenuOptionModel[] = [];
// Outputs: return the selected option to the caller
@Output() selectOption = new EventEmitter<any>();
// Private properties
private selectedOption: InnerMenuOptionModel;
@Input('options')
set options(value: MenuOptionModel[]) {
if (value) {
// Keep a reference to the options
// sent to this component
this.menuOptions = value;
this.collapsableItems = new Array<InnerMenuOptionModel>();
// Map the options to our internal models
this.menuOptions.forEach((option) => {
const innerMenuOption = InnerMenuOptionModel.fromMenuOptionModel(option);
this.collapsableItems.push(innerMenuOption);
// Check if there's any option marked as selected
if (option.selected) {
this.selectedOption = innerMenuOption;
} else if (innerMenuOption.subItemsCount) {
innerMenuOption.subOptions.forEach((subItem) => {
if (subItem.selected) {
this.selectedOption = subItem;
}
});
}
});
}
}
@Input('settings')
set settings(value: SideMenuSettings) {
if (value) {
this.menuSettings = value;
this.mergeSettings();
}
}
constructor(private platform: Platform,
private eventsCtrl: Events,
private cdRef: ChangeDetectorRef) {
// Handle the redirect event
this.eventsCtrl.subscribe(SideMenuRedirectEvent, (data: SideMenuRedirectEventData) => {
this.updateSelectedOption(data);
});
}
ngOnDestroy() {
this.eventsCtrl.unsubscribe(SideMenuRedirectEvent);
}
// ---------------------------------------------------
// PUBLIC methods
// ---------------------------------------------------
// Send the selected option to the caller component
public select(option: InnerMenuOptionModel): void {
if (this.menuSettings.showSelectedOption) {
this.setSelectedOption(option);
}
// Return the selected option (not our inner option)
this.selectOption.emit(option.targetOption);
}
// Toggle the sub options of the selected item
public toggleItemOptions(targetOption: InnerMenuOptionModel): void {
if (!targetOption) return;
// If the accordion mode is set to true, we need
// to collapse all the other menu options
if (this.menuSettings.accordionMode) {
this.collapsableItems.forEach((option) => {
if (option.id !== targetOption.id) {
option.expanded = false;
}
});
}
// Toggle the selected option
targetOption.expanded = !targetOption.expanded;
}
// Reset the entire menu
public collapseAllOptions(): void {
this.collapsableItems.forEach((option) => {
if (!option.selected) {
option.expanded = false;
}
if (option.subItemsCount) {
option.subOptions.forEach((subItem) => {
if (subItem.selected) {
// Expand the parent if any of
// its childs is selected
subItem.parent.expanded = true;
}
});
}
});
// Update the view since there wasn't
// any user interaction with it
this.cdRef.detectChanges();
}
// Get the proper indentation of each option
public get subOptionIndentation(): string {
if (this.platform.is('ios')) return this.menuSettings.subOptionIndentation.ios;
if (this.platform.is('windows')) return this.menuSettings.subOptionIndentation.wp;
return this.menuSettings.subOptionIndentation.md;
}
// Get the proper height of each option
public get itemHeight(): number {
if (this.platform.is('ios')) return this.menuSettings.itemHeight.ios;
if (this.platform.is('windows')) return this.menuSettings.itemHeight.wp;
return this.menuSettings.itemHeight.md;
}
// ---------------------------------------------------
// PRIVATE methods
// ---------------------------------------------------
// Method that set the selected option and its parent
private setSelectedOption(option: InnerMenuOptionModel) {
if (!option.targetOption.component) return;
// Clean the current selected option if any
if (this.selectedOption) {
this.selectedOption.selected = false;
this.selectedOption.targetOption.selected = false;
if (this.selectedOption.parent) {
this.selectedOption.parent.selected = false;
this.selectedOption.parent.expanded = false;
}
this.selectedOption = null;
}
// Set this option to be the selected
option.selected = true;
option.targetOption.selected = true;
if (option.parent) {
option.parent.selected = true;
option.parent.expanded = true;
}
// Keep a reference to the selected option
this.selectedOption = option;
// Update the view if needed since we may have
// expanded or collapsed some options
this.cdRef.detectChanges();
}
// Update the selected option
private updateSelectedOption(data: SideMenuRedirectEventData): void {
if (!data.displayName) {
return;
}
let targetOption;
this.collapsableItems.forEach((option) => {
if (option.displayName.toLowerCase() === data.displayName.toLowerCase()) {
targetOption = option;
} else if (option.subItemsCount) {
option.subOptions.forEach((subOption) => {
if (subOption.displayName.toLowerCase() === data.displayName.toLowerCase()) {
targetOption = subOption;
}
});
}
});
if (targetOption) {
this.setSelectedOption(targetOption);
}
}
// Merge the settings received with the default settings
private mergeSettings(): void {
const defaultSettings: SideMenuSettings = {
accordionMode: false,
itemHeight: {
ios: 50,
md: 50,
wp: 50
},
arrowIcon: 'ios-arrow-down',
showSelectedOption: false,
selectedOptionClass: 'selected-option',
indentSubOptionsWithoutIcons: false,
subOptionIndentation: {
ios: '16px',
md: '16px',
wp: '16px'
}
};
if (!this.menuSettings) {
// Use the default values
this.menuSettings = defaultSettings;
return;
}
const ms = this.menuSettings;
if (!ms.itemHeight) {
ms.itemHeight = defaultSettings.itemHeight;
} else {
const defIos = this.isDefinedAndPositive(ms.itemHeight.ios);
const defMd = this.isDefinedAndPositive(ms.itemHeight.md);
const defWp = this.isDefinedAndPositive(ms.itemHeight.wp);
const heightIos = defIos ? ms.itemHeight.ios : defaultSettings.itemHeight.ios;
const heightMd = defMd ? ms.itemHeight.md : defaultSettings.itemHeight.md;
const heightWp = defWp ? ms.itemHeight.wp : defaultSettings.itemHeight.wp;
ms.itemHeight.ios = heightIos;
ms.itemHeight.md = heightMd;
ms.itemHeight.wp = heightWp;
}
const shSeOpt = 'showSelectedOption';
ms[shSeOpt] = this.isDefined(ms[shSeOpt]) ? ms[shSeOpt] : defaultSettings[shSeOpt];
const accModOpt = 'accordionMode';
ms[accModOpt] = this.isDefined(ms[accModOpt]) ? ms[accModOpt] : defaultSettings[accModOpt];
const arrIco = 'arrowIcon';
ms[arrIco] = this.isDefined(ms[arrIco]) ? ms[arrIco] : defaultSettings[arrIco];
const selOptClass = 'selectedOptionClass';
ms[selOptClass] = this.isDefined(ms[selOptClass]) ? ms[selOptClass] : defaultSettings[selOptClass];
const suOptInd = 'subOptionIndentation';
ms[suOptInd] = this.isDefined(ms[suOptInd]) ? ms[suOptInd] : defaultSettings[suOptInd];
const indSuOptWIco = 'indentSubOptionsWithoutIcons';
ms[indSuOptWIco] = this.isDefined(ms[indSuOptWIco]) ? ms[indSuOptWIco] : defaultSettings[indSuOptWIco];
if (!ms[suOptInd]) {
ms[suOptInd] = defaultSettings[suOptInd];
} else {
const suOpInd = 'subOptionIndentation';
ms[suOpInd].ios = this.isDefined(ms[suOpInd].ios) ? ms[suOpInd].ios : defaultSettings[suOpInd].ios;
ms[suOpInd].md = this.isDefined(ms[suOpInd].md) ? ms[suOpInd].md : defaultSettings[suOpInd].md;
ms[suOpInd].wp = this.isDefined(ms[suOpInd].wp) ? ms[suOpInd].wp : defaultSettings[suOpInd].wp;
}
}
private isDefined(property: any): boolean {
return property !== null && property !== undefined;
}
private isDefinedAndPositive(property: any): boolean {
return this.isDefined(property) && !isNaN(property) && property > 0;
}
}