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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336 | 1
40
40
40
40
40
40
5
4
40
3
2
40
2873
2873
2873
40
3
3
21
40
968
968
933
968
40
968
968
933
968
40
2
40
3
3
3
3
40
7
7
7
7
7
7
5
1
1
1
1
1
4
4
4
5
5
7
7
1
7
40
1
1
1
40
40
1103
1103
1103
1039
64
44
51
1103
40
5812
1
41
36
5
3
5
5
5
3
5
41
40
40
3
40
40
39
40
40
1
39
39
40
237
2
237
3
3
3
3
3
40
40
40
1103
40
| angular.module('patternfly.views').component('pfListView', {
bindings: {
config: '=?',
pageConfig: '=?',
items: '=',
actionButtons: '=?',
enableButtonForItemFn: '=?',
menuActions: '=?',
hideMenuForItemFn: '=?',
menuClassForItemFn: '=?',
updateMenuActionForItemFn: '=?',
actions: '=?',
updateActionForItemFn: '=?',
customScope: '=?',
emptyStateConfig: '=?',
emptyStateActionButtons: '=?'
},
transclude: {
expandedContent: '?listExpandedContent'
},
templateUrl: 'views/listview/list-view.html',
controller: function ($window, $element, $timeout) {
'use strict';
var ctrl = this;
var prevPageConfig, prevItems;
var setDropMenuLocation = function (parentDiv) {
var dropButton = parentDiv.querySelector('.dropdown-toggle');
var dropMenu = parentDiv.querySelector('.dropdown-menu');
var parentRect = $element[0].querySelector('.list-view-pf.list-view-pf-view').getBoundingClientRect();
var buttonRect = dropButton.getBoundingClientRect();
var menuRect = dropMenu.getBoundingClientRect();
var buttonTop = buttonRect.top - parentRect.top;
var buttonBottom = buttonTop + buttonRect.height;
var menuTop = buttonTop - menuRect.height;
var menuBottom = buttonBottom + menuRect.height;
if ((menuBottom <= parentRect.height) || (menuTop < 0)) {
ctrl.dropdownClass = 'dropdown';
} else {
ctrl.dropdownClass = 'dropup';
}
// OK to display the menu now
ctrl.kebabMenuReady = true;
};
ctrl.defaultConfig = {
selectItems: false,
multiSelect: false,
dblClick: false,
dragEnabled: false,
dragEnd: null,
dragMoved: null,
dragStart: null,
selectionMatchProp: 'uuid',
selectedItems: [],
checkDisabled: false,
useExpandingRows: false,
showSelectBox: true,
onSelect: null,
onSelectionChange: null,
onCheckBoxChange: null,
onClick: null,
onDblClick: null,
itemsAvailable: true
};
ctrl.dropdownClass = 'dropdown';
ctrl.handleButtonAction = function (action, item) {
if (!ctrl.checkDisabled(item) && action && action.actionFn && ctrl.enableButtonForItem(action, item)) {
action.actionFn(action, item);
}
};
ctrl.handleMenuAction = function (action, item) {
if (!ctrl.checkDisabled(item) && action && action.actionFn && (action.isDisabled !== true)) {
action.actionFn(action, item);
}
};
ctrl.enableButtonForItem = function (action, item) {
var enable = true;
Eif (typeof ctrl.enableButtonForItemFn === 'function') {
return ctrl.enableButtonForItemFn(action, item);
}
return enable;
};
ctrl.updateActions = function (item) {
Eif (typeof ctrl.updateMenuActionForItemFn === 'function') {
ctrl.menuActions.forEach(function (action) {
ctrl.updateMenuActionForItemFn(action, item);
});
}
};
ctrl.getMenuClassForItem = function (item) {
var menuClass = '';
if (angular.isFunction(ctrl.menuClassForItemFn)) {
menuClass = ctrl.menuClassForItemFn(item);
}
return menuClass;
};
ctrl.hideMenuForItem = function (item) {
var hideMenu = false;
if (angular.isFunction(ctrl.hideMenuForItemFn)) {
hideMenu = ctrl.hideMenuForItemFn(item);
}
return hideMenu;
};
ctrl.toggleItemExpansion = function (item) {
item.isExpanded = !item.isExpanded;
};
ctrl.setupActions = function (item, event) {
// Ignore disabled items completely
Iif (ctrl.checkDisabled(item)) {
return;
}
// update the actions based on the current item
ctrl.updateActions(item);
// Hide the kebab until we determine dropup or dropdown to avoid flicker
ctrl.kebabMenuReady = false;
$timeout(function() {
var parentDiv = undefined;
var nextElement;
nextElement = event.target;
while (nextElement && !parentDiv) {
if (nextElement.className.indexOf('dropdown-kebab-pf') !== -1) {
parentDiv = nextElement;
if (nextElement.className.indexOf('open') !== -1) {
setDropMenuLocation(parentDiv);
}
}
nextElement = nextElement.parentElement;
}
});
};
ctrl.itemClick = function (e, item) {
var alreadySelected;
var selectionChanged = false;
var continueEvent = true;
var enableRowExpansion = ctrl.config && ctrl.config.useExpandingRows && !ctrl.config.compoundExpansionOnly && item && !item.disableRowExpansion;
// Ignore disabled item clicks completely
Iif (ctrl.checkDisabled(item)) {
return continueEvent;
}
if (ctrl.config && ctrl.config.selectItems && item) {
if (ctrl.config.multiSelect && !ctrl.config.dblClick) {
alreadySelected = _.find(ctrl.config.selectedItems, function (itemObj) {
return itemObj === item;
});
Iif (alreadySelected) {
// already selected so deselect
ctrl.config.selectedItems = _.without(ctrl.config.selectedItems, item);
} else {
// add the item to the selected items
ctrl.config.selectedItems.push(item);
selectionChanged = true;
}
} else {
Iif (ctrl.config.selectedItems[0] === item) {
if (!ctrl.config.dblClick) {
ctrl.config.selectedItems = [];
selectionChanged = true;
}
continueEvent = false;
} else {
ctrl.config.selectedItems = [item];
selectionChanged = true;
}
}
Iif (selectionChanged && ctrl.config.onSelect) {
ctrl.config.onSelect(item, e);
}
Iif (selectionChanged && ctrl.config.onSelectionChange) {
ctrl.config.onSelectionChange(ctrl.config.selectedItems, e);
}
}
Iif (ctrl.config.onClick) {
if (ctrl.config.onClick(item, e) !== false && enableRowExpansion) {
ctrl.toggleItemExpansion(item);
}
} else if (enableRowExpansion) {
ctrl.toggleItemExpansion(item);
}
return continueEvent;
};
ctrl.dblClick = function (e, item) {
// Ignore disabled item clicks completely
Iif (ctrl.checkDisabled(item)) {
return continueEvent;
}
Eif (ctrl.config.onDblClick) {
ctrl.config.onDblClick(item, e);
}
};
ctrl.checkBoxChange = function (item) {
if (ctrl.config.onCheckBoxChange) {
ctrl.config.onCheckBoxChange(item);
}
};
ctrl.isSelected = function (item) {
var matchProp = ctrl.config.selectionMatchProp;
var selected = false;
if (ctrl.config.showSelectBox) {
selected = item.selected;
} else if (ctrl.config.selectItems && ctrl.config.selectedItems.length) {
selected = _.find(ctrl.config.selectedItems, function (itemObj) {
return itemObj[matchProp] === item[matchProp];
});
}
return selected;
};
ctrl.checkDisabled = function (item) {
return ctrl.config.checkDisabled && ctrl.config.checkDisabled(item);
};
function setPagination () {
if (angular.isUndefined(ctrl.pageConfig)) {
ctrl.pageConfig = {
pageNumber: 1,
pageSize: ctrl.items.length,
numTotalItems: ctrl.items.length,
showPaginationControls: false
};
} else {
if (angular.isUndefined(ctrl.pageConfig.showPaginationControls)) {
ctrl.pageConfig.showPaginationControls = true;
}
Iif (!angular.isNumber(ctrl.pageConfig.pageNumber)) {
ctrl.pageConfig.pageNumber = 1;
}
Iif (!angular.isNumber(ctrl.pageConfig.pageSize)) {
ctrl.pageConfig.pageSize = 10;
}
if (!angular.isNumber(ctrl.pageConfig.numTotalItems)) {
ctrl.pageConfig.numTotalItems = ctrl.items.length;
}
// if not showing pagination, keep pageSize equal to numTotalItems
Iif (!ctrl.pageConfig.showPaginationControls) {
ctrl.pageConfig.pageSize = ctrl.pageConfig.numTotalItems;
}
}
prevPageConfig = angular.copy(ctrl.pageConfig);
}
ctrl.$onInit = function () {
if (angular.isUndefined(ctrl.config)) {
ctrl.config = {};
}
_.defaults(ctrl.config, ctrl.defaultConfig);
if (!ctrl.config.selectItems) {
ctrl.config.selectedItems = [];
}
Iif (!ctrl.config.multiSelect && ctrl.config.selectedItems && ctrl.config.selectedItems.length > 0) {
ctrl.config.selectedItems = [ctrl.config.selectedItems[0]];
}
if (ctrl.config.selectItems && ctrl.config.showSelectBox) {
throw new Error('pfListView - ' +
'Illegal use of pListView component! ' +
'Cannot allow both select box and click selection in the same list view.');
}
prevItems = angular.copy(ctrl.items);
setPagination();
};
ctrl.$doCheck = function () {
if (!angular.equals(ctrl.pageConfig, prevPageConfig)) {
setPagination();
}
if (!angular.equals(ctrl.items, prevItems)) {
Eif (ctrl.items) {
ctrl.config.itemsAvailable = ctrl.items.length > 0;
}
Eif (angular.isDefined(ctrl.pageConfig)) {
ctrl.pageConfig.numTotalItems = ctrl.items.length;
}
prevItems = angular.copy(ctrl.items);
}
};
ctrl.dragEnd = function () {
if (angular.isFunction(ctrl.config.dragEnd)) {
ctrl.config.dragEnd();
}
};
ctrl.dragMoved = function () {
if (angular.isFunction(ctrl.config.dragMoved)) {
ctrl.config.dragMoved();
}
};
ctrl.isDragOriginal = function (item) {
return (item === ctrl.dragItem);
};
ctrl.dragStart = function (item) {
ctrl.dragItem = item;
if (angular.isFunction(ctrl.config.dragStart)) {
ctrl.config.dragStart(item);
}
};
}
});
|