forked from ManageIQ/ui-components
/
toolbarComponent.ts
280 lines (258 loc) · 10.2 KB
/
toolbarComponent.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
import {IToolbarItem} from '../../interfaces/toolbar';
import {ToolbarType} from '../../interfaces/toolbarType';
import * as _ from 'lodash';
const CUSTOM_ID = 'custom_';
/**
* @memberof miqStaticAssets
* @ngdoc controller
* @name ToolbarController
* @param $window {ng.IWindowService} window service for redirecting to non angular pages.
* @param $location {ng.ILocationService} location service to handle redirect to different angular pages.
* @param $sce {ng.ISCEService} service for enabling html strings to be html objects injected to page as html and not as
* string.
*/
export class ToolbarController {
public toolbarItems: any;
public toolbarViews: any;
public onViewClick: (args: {item: any, $event: any}) => void;
/*@ngInject*/
constructor(private $window: ng.IWindowService,
private $location: ng.ILocationService,
private $sce: ng.ISCEService) {
}
/**
* Method for finding custom toolbar item's per each toolbar group.
* @memberof ToolbarController
* @function toolbarHasCustom
* @param toolbarItem toolbar group item.
* @returns {any[]} array of custom items in toolbar.
*/
public toolbarHasCustom(toolbarItem): any {
return _.find(toolbarItem, {name: 'custom'});
}
/**
* Handle clicking on item in toolbar.
* Check what function has item for handling click action it's one of [actionUrl, redirectUrl, actionFunction,
* eventFunction].
* * actionUrl - will perform location path change.
* * redirectUrl - will perform window redirect.
* * actionFunction - will perform call without any arguments.
* * eventFunction - will perform call to this function with $event specified.
* @memberof ToolbarController
* @function onItemClick
* @param {Object} item what was clicked in toolbar (member of toolbar items).
* @param {Object} $event for passing it to eventFunction of item (good for checking target).
*/
public onItemClick(item: any, $event: any) {
if (item.hasOwnProperty('actionUrl')) {
this.$location.path(item.actionUrl);
} else if (item.hasOwnProperty('redirectUrl')) {
this.$window.location.replace(item.redirectUrl);
} else if (item.hasOwnProperty('actionFunction')) {
item.actionFunction();
} else if (item.hasOwnProperty('eventFunction')) {
item.eventFunction($event);
}
}
/**
* Filter out items which does not have buttons, select or custom html in them and check if array is not empty. If
* this array would be empty there is no content which could be shown in toolbar group.
* * see {@link miqStaticAssets.ToolbarController#isButtonOrSelect} on how it is checked button or select item.
* * see {@link miqStaticAssets.ToolbarController#isCustom} on how it is checked custom html item.
* @memberof ToolbarController
* @function hasContent
* @param {Array<IToolbarItem>} toolbarItem array of items which are checked for content.
* @returns {boolean} true|false isEmpty or not.
*/
public hasContent(toolbarItem: Array<IToolbarItem>): boolean {
return toolbarItem && toolbarItem.filter((item) => {
return item && (ToolbarController.isButtonOrSelect(item) || ToolbarController.isCustom(item));
}).length !== 0;
}
/**
* Escape html custom data and make them available for html insertion to toolbar.
* @memberof ToolbarController
* @function hasContent
* @param escapedString html string without escaped items.
* @returns {any} html object, this object can be bound to see
* {@link https://docs.angularjs.org/api/ng/directive/ngBindHtml}
*/
public trustAsHtml(escapedString: string): any {
escapedString = ToolbarController.htmlDecode(escapedString);
return this.$sce.trustAsHtml(escapedString);
}
/**
* Helper method for getting string value of {@link ToolbarType.BUTTON_SELECT}
* @memberof ToolbarController
* @function getToolbarListType
* @returns {string}
*/
public getToolbarListType(): string {
return ToolbarType.BUTTON_SELECT;
}
/**
* Helper method for getting string value of {@link ToolbarType.BUTTON}
* @memberof ToolbarController
* @function getToolbarListType
* @returns {string}
*/
public getButtonType(): string {
return ToolbarType.BUTTON;
}
public getToolbarKebabType(): string {
return ToolbarType.KEBAB;
}
/**
* Helper method for getting string value of {@link ToolbarType.CUSTOM}
* @memberof ToolbarController
* @function getToolbarListType
* @returns {string}
*/
public getCustomType(): string {
return ToolbarType.CUSTOM;
}
public getButtonTwoState() {
return ToolbarType.BUTTON_TWO_STATE;
}
public collapseButtons() {
let buttonsIndex;
if (this.toolbarItems) {
buttonsIndex = _.findLastIndex(
this.toolbarItems,
(itemGroup: any) => itemGroup.filter(item => item.id.includes(CUSTOM_ID) !== false).length !== 0
);
if(buttonsIndex !== -1) {
this.toolbarItems[buttonsIndex] = ToolbarController.createKebabFromItems(this.toolbarItems[buttonsIndex]);
}
}
}
private $onChanges(changesObj) {
if (changesObj.toolbarItems) {
this.collapseButtons();
}
}
/**
* Private static function for decoding html.
* @memberof ToolbarController
* @function htmlDecode
* @param input html string containing custom html.
* @returns {string} unescaped html string.
*/
public static htmlDecode(input: string): string {
let e = document.createElement('div');
e.innerHTML = input;
return e.childNodes.length === 0 ? '' : e.childNodes[0].nodeValue;
}
/**
* Private static function for checking if toolbar item has name and if this name is `"custom"`.
* @memberof ToolbarController
* @function hasContent
* @param {IToolbarItem} item which is checked for name property.
* @returns {boolean} true|false if it's item with custom html.
*/
private static isCustom(item: IToolbarItem): boolean {
return item.name && item.name === ToolbarType.CUSTOM;
}
/**
* Private static function for checking if toolbar item type and if this type is button or select.
* * see {@link miqStaticAssets.ToolbarController#isButtonSelect} on how it's checked for select type.
* * see {@link miqStaticAssets.ToolbarController#isButton} on how it's checked for button type.
* @memberof ToolbarController
* @function isButtonOrSelect
* @param {IToolbarItem} item which is checked for type property.
* @returns {boolean} true|false if it's item with button or button select type.
*/
private static isButtonOrSelect(item: IToolbarItem): boolean {
return item.type && (
(ToolbarController.isButtonSelect(item) && item.items && item.items.length !== 0)
|| ToolbarController.isButton(item)
|| ToolbarController.isButtonTwoState(item)
|| ToolbarController.isKebabMenu(item)
);
}
private static isButtonTwoState(item: IToolbarItem): boolean {
return item.type === ToolbarType.BUTTON_TWO_STATE;
}
/**
* Private static function for checking if toolbar item type is buttonSelect.
* @memberof ToolbarController
* @function isButtonSelect
* @param {IToolbarItem} item item which is checked for type property.
* @returns {boolean} true|false if it's item with type equals to `"buttonSelect"`.
*/
private static isButtonSelect(item: IToolbarItem): boolean {
return item.type === ToolbarType.BUTTON_SELECT;
}
private static isKebabMenu(item: IToolbarItem): boolean {
return item.type === ToolbarType.KEBAB;
}
/**
* Private static function for checking if toolbar item type is button.
* @memberof ToolbarController
* @function isButton
* @param {IToolbarItem} item item which is checked for type property.
* @returns {boolean} true|false if it's item with type equals to `"button"`.
*/
private static isButton(item): boolean {
return item.type === ToolbarType.BUTTON;
}
private static createKebabFromItems(itemsGroup: any[]) {
if (itemsGroup.length > 3) {
return itemsGroup.reduce((acc, curr) => {
curr.id.includes(CUSTOM_ID) ? acc[0].items.push(curr) : acc.push(curr);
return acc;
}, [{type: ToolbarType.KEBAB, items: []}]);
}
return itemsGroup;
}
}
/**
* @memberof miqStaticAssets
* @ngdoc component
* @name miqToolbarMenu
* @description
* This component is for whole toolbar implementation. It's advantages are, that it takes custom components as well
* so they can be used instead of just plain JS objects (pass html inside toolbarItems attribute).
* See {@link miqStaticAssets.ToolbarController} for implementation of all methods and behavior of this component.
* Attribute toolbarItems for custom html needs to have set `args.html`:
* ```JSON
* [[
* {"name" : "custom",
* "args" : {
* ...
* "html" : "<div>html string, this string will be generated as part of form group</div>"
* ...
* }}
* ]]
* ```
* How each button is treated see {@link miqStaticAssets.ToolbarController#hasContent} and observe each static
* function which
* is responsible for deciding what type of button will be used.
*
* @attr {Expression} onViewClick
* Method which will be executed when clicked on view. See {@link miqStaticAssets.ToolbarController#onViewClick}
* which arguments are
* needed.
* @attr {Array} toolbarViews
* List of all views which are used in toolbar. Since we use typescript this attribute has specific type of:
* `Array<IToolbarItem>` See {@link IToolbarItem} for entities of toolbarViews.
* @attr {Array} toolbarItems
* List of all items which are used in toolbar. Since we use typescript this attribute has specific type of:
* `Array<Array<IToolbarItem>>` See {@link IToolbarItem} for entities of toolbarItems.
* @example
* <miq-toolbar-menu toolbar-views="ctrl.toolbarViews"
* toolbar-items="ctrl.toolbarItems"
* on-view-click="ctrl.onClick(item)">
* </miq-toolbar-menu>
*/
export default class Toolbar {
public replace: boolean = true;
public template = require('./toolbar-menu.html');
public controller: any = ToolbarController;
public controllerAs: string = 'vm';
public bindings: any = {
toolbarViews: '<',
toolbarItems: '<',
onViewClick: '&'
};
}