| 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471 |
1
1
1
1
576
576
27
27
27
27
27
3
3
3
24
44
13
31
2
2
3
3
1
242
242
242
15
227
81
146
2
144
23
9
9
9
9
9
9
9
9
9
9
9
9
9
9
6
2
2
4
47
3
3
2
2
2
1
1
1
3
31
31
31
31
31
4
10
10
10
10
10
10
4
27
3
3
1
1
2
2
4
1
2
10
10
| 'use strict';
;require.register("views/common/table_view", function (exports, require, module) {
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var App = require('app');
var filters = require('views/common/filter_view');
App.TableView = Em.View.extend(App.Persist, {
init: function init() {
this.set('filterConditions', []);
this._super();
},
/**
* Defines to show pagination or show all records
* @type {Boolean}
*/
pagination: true,
/**
* Shows if all data is loaded and filtered
* @type {Boolean}
*/
filteringComplete: false,
/**
* intermediary for filteringComplete
* @type {Boolean}
*/
tableFilteringComplete: false,
/**
* The number of rows to show on every page
* The value should be a number converted into string type in order to support select element API
* Example: "10", "25"
* @type {String}
*/
displayLength: null,
/**
* default value of display length
* The value should be a number converted into string type in order to support select element API
* Example: "10", "25"
*/
defaultDisplayLength: "10",
/**
* number of items in table after applying filters
*/
filteredCount: Em.computed.alias('filteredContent.length'),
/**
* total number of items in table before applying filters
*/
totalCount: Em.computed.alias('content.length'),
/**
* Determines whether current page is the first one
*/
isCurrentPageFirst: Em.computed.lte('startIndex', 1),
/**
* Determines whether current page is the last one
*/
isCurrentPageLast: Em.computed.equalProperties('endIndex', 'filteredCount'),
/**
* Do filtering, using saved in the local storage filter conditions
*/
willInsertElement: function willInsertElement() {
var self = this;
var name = this.get('controller.name');
Eif (!this.get('displayLength')) {
var displayLength = App.db.getDisplayLength(name);
if (displayLength) {
Eif (this.get('state') !== "inBuffer") {
self.set('displayLength', displayLength);
self.initFilters();
} else {
Em.run.next(function () {
self.set('displayLength', displayLength);
self.initFilters();
});
}
} else {
Iif (!$.mocho) {
this.getUserPref(this.displayLengthKey()).complete(function () {
self.initFilters();
});
}
}
}
},
/**
* initialize filters
* restore values from local DB
* or clear filters in case there is no filters to restore
*/
initFilters: function initFilters() {
var name = this.get('controller.name');
var self = this;
var filterConditions = App.db.getFilterConditions(name);
if (!Em.isEmpty(filterConditions)) {
this.set('filterConditions', filterConditions);
var childViews = this.get('childViews');
filterConditions.forEach(function (condition, index, filteredConditions) {
var view = !Em.isNone(condition.iColumn) && childViews.findProperty('column', condition.iColumn);
if (view) {
if (view.get('emptyValue')) {
view.clearFilter();
self.saveFilterConditions(condition.iColumn, view.get('appliedEmptyValue'), condition.type, false);
} else {
view.setValue(condition.value);
}
Em.run.next(function () {
view.showClearFilter();
// check if it is the last iteration
if (filteredConditions.length - index - 1 === 0) {
self.set('tableFilteringComplete', true);
}
});
} else {
self.set('tableFilteringComplete', true);
}
});
} else {
this.set('tableFilteringComplete', true);
}
},
/**
* Persist-key of current table displayLength property
* @param {String} loginName current user login name
* @returns {String}
*/
displayLengthKey: function displayLengthKey(loginName) {
if (App.get('testMode')) {
return 'pagination_displayLength';
}
loginName = loginName ? loginName : App.router.get('loginName');
return this.get('controller.name') + '-pagination-displayLength-' + loginName;
},
/**
* Set received from server value to <code>displayLengthOnLoad</code>
* @param {Number} response
* @param {Object} request
* @param {Object} data
* @returns {*}
*/
getUserPrefSuccessCallback: function getUserPrefSuccessCallback(response, request, data) {
this.set('displayLength', response);
return response;
},
/**
* Set default value to <code>displayLength</code> (and send it on server) if value wasn't found on server
* @returns {Number}
*/
getUserPrefErrorCallback: function getUserPrefErrorCallback() {
// this user is first time login
var displayLengthDefault = this.get('defaultDisplayLength');
this.set('displayLength', displayLengthDefault);
if (App.isAuthorized('SERVICE.VIEW_METRICS')) {
this.saveDisplayLength();
}
this.filter();
return displayLengthDefault;
},
/**
* Return pagination information displayed on the page
* @type {String}
*/
paginationInfo: Em.computed.i18nFormat('tableView.filters.paginationInfo', 'startIndex', 'endIndex', 'filteredCount'),
/**
* Start index for displayed content on the page
*/
startIndex: 1,
/**
* Calculate end index for displayed content on the page
*/
endIndex: function () {
if (this.get('pagination') && this.get('displayLength')) {
return Math.min(this.get('filteredCount'), this.get('startIndex') + parseInt(this.get('displayLength')) - 1);
} else {
return this.get('filteredCount') || 0;
}
}.property('startIndex', 'displayLength', 'filteredCount'),
/**
* Onclick handler for previous page button on the page
*/
previousPage: function previousPage() {
var result = this.get('startIndex') - parseInt(this.get('displayLength'));
this.set('startIndex', result < 2 ? 1 : result);
},
/**
* Onclick handler for next page button on the page
*/
nextPage: function nextPage() {
var result = this.get('startIndex') + parseInt(this.get('displayLength'));
if (result - 1 < this.get('filteredCount')) {
this.set('startIndex', result);
}
},
/**
* Onclick handler for first page button on the page
*/
firstPage: function firstPage() {
this.set('startIndex', 1);
},
/**
* Onclick handler for last page button on the page
*/
lastPage: function lastPage() {
var pagesCount = this.get('filteredCount') / parseInt(this.get('displayLength'));
var startIndex = this.get('filteredCount') % parseInt(this.get('displayLength')) === 0 ? (pagesCount - 1) * parseInt(this.get('displayLength')) : Math.floor(pagesCount) * parseInt(this.get('displayLength'));
this.set('startIndex', ++startIndex);
},
/**
* Calculates default value for startIndex property after applying filter or changing displayLength
*/
updatePaging: function (controller, property) {
var displayLength = this.get('displayLength');
var filteredContentLength = this.get('filteredCount');
if (property == 'displayLength') {
this.set('startIndex', Math.min(1, filteredContentLength));
} else if (!filteredContentLength) {
this.set('startIndex', 0);
} else if (this.get('startIndex') > filteredContentLength) {
this.set('startIndex', Math.floor((filteredContentLength - 1) / displayLength) * displayLength + 1);
} else if (!this.get('startIndex')) {
this.set('startIndex', 1);
}
}.observes('displayLength', 'filteredCount'),
/**
* Apply each filter to each row
*
* @param {Number} iColumn number of column by which filter
* @param {Object} value
* @param {String} type
*/
updateFilter: function updateFilter(iColumn, value, type) {
this.saveFilterConditions(iColumn, value, type, false);
this.filtersUsedCalc();
this.filter();
},
/**
*
* @param {Array} filterConditions
*/
updateComboFilter: function updateComboFilter(filterConditions) {
this.set('controller.resetStartIndex', true);
this.set('filterConditions', filterConditions);
this.saveAllFilterConditions();
this.filter();
},
/**
* save filter conditions to local storage
* @param iColumn {Number}
* @param value {String|Array}
* @param type {String}
* @param skipFilter {Boolean}
*/
saveFilterConditions: function saveFilterConditions(iColumn, value, type, skipFilter) {
var filterCondition = this.get('filterConditions').findProperty('iColumn', iColumn);
Iif (filterCondition) {
filterCondition.value = value;
filterCondition.skipFilter = skipFilter;
} else {
filterCondition = {
skipFilter: skipFilter,
iColumn: iColumn,
value: value,
type: type
};
this.get('filterConditions').push(filterCondition);
this.propertyDidChange('showFilteredContent');
}
this.saveAllFilterConditions();
},
/**
* Save not empty <code>filterConditions</code> to the localStorage
*
* @method saveAllFilterConditions
*/
saveAllFilterConditions: function saveAllFilterConditions() {
var filterConditions = this.get('filterConditions');
// remove empty entries
filterConditions = filterConditions.filter(function (item) {
return !Em.isEmpty(item.value);
});
this.set('filterConditions', filterConditions);
App.db.setFilterConditions(this.get('controller.name'), filterConditions);
},
saveDisplayLength: function saveDisplayLength() {
var self = this;
Em.run.next(function () {
App.db.setDisplayLength(self.get('controller.name'), self.get('displayLength'));
if (!App.get('testMode')) {
if (App.isAuthorized('SERVICE.VIEW_METRICS')) {
self.postUserPref(self.displayLengthKey(), self.get('displayLength'));
}
}
});
},
clearFilterConditionsFromLocalStorage: function clearFilterConditionsFromLocalStorage() {
var result = false;
var currentFCs = App.db.getFilterConditions(this.get('controller.name'));
if (currentFCs != null) {
App.db.setFilterConditions(this.get('controller.name'), null);
result = true;
}
var query = App.db.getComboSearchQuery(this.get('controller.name'));
if (query != null) {
App.db.setComboSearchQuery(this.get('controller.name'), null);
result = true;
}
return result;
},
clearStartIndex: function clearStartIndex() {
if (this.get('controller.startIndex') !== 1) {
this.set('controller.resetStartIndex', true);
return true;
}
return false;
},
/**
* Contain filter conditions for each column
* @type {Array}
*/
filterConditions: [],
/**
* Contains content after implementing filters
* @type {Array}
*/
filteredContent: [],
/**
* Determine if <code>filteredContent</code> is empty or not
* @type {Boolean}
*/
hasFilteredItems: Em.computed.bool('filteredCount'),
/**
* Contains content to show on the current page of data page view
* @type {Array}
*/
pageContent: function () {
return this.get('filteredContent').slice(this.get('startIndex') - 1, this.get('endIndex'));
}.property('filteredCount', 'startIndex', 'endIndex'),
/**
* flag to toggle displaying filtered hosts counter
*/
showFilteredContent: function () {
var result = false;
if (this.get('filterConditions.length') > 0) {
this.get('filterConditions').forEach(function (f) {
Eif (f.value) {
if (Em.typeOf(f.value) == "array") {
Eif (f.value[0] || f.value[1]) {
result = true;
}
} else {
result = true;
}
}
});
}
return result;
}.property('filteredContent.length'),
/**
* Filter table by filterConditions
*/
filter: function () {
var content = this.get('content');
var filterConditions = this.get('filterConditions').filterProperty('value');
var result;
var assoc = this.get('colPropAssoc');
if (filterConditions.length) {
result = content.filter(function (item) {
var match = true;
filterConditions.forEach(function (condition) {
var filterFunc = filters.getFilterByType(condition.type, false);
Eif (match) {
match = filterFunc(item.get(assoc[condition.iColumn]), condition.value);
}
});
return match;
});
this.set('filteredContent', result);
} else {
this.set('filteredContent', content ? content.toArray() : []);
}
}.observes('content.length'),
/**
* Does any filter is used on the page
* @type {Boolean}
*/
filtersUsed: false,
/**
* Determine if some filters are used on the page
* Set <code>filtersUsed</code> value
*/
filtersUsedCalc: function filtersUsedCalc() {
var filterConditions = this.get('filterConditions');
if (!filterConditions.length) {
this.set('filtersUsed', false);
return;
}
var filtersUsed = false;
filterConditions.forEach(function (filterCondition) {
if (filterCondition.value.toString() !== '') {
filtersUsed = true;
}
});
this.set('filtersUsed', filtersUsed);
},
/**
* Run <code>clearFilter</code> in the each child filterView
*/
clearFilters: function clearFilters() {
this.set('filterConditions', []);
this.get('childViews').forEach(function (childView) {
Em.tryInvoke(childView, 'clearFilter');
});
}
});
}); |