| 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 |
1
| 'use strict';
;require.register("views/common/grid", 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 validator = require('utils/validator');
App.GridFilterObject = Em.Object.extend({
checked: false
});
App.GridFilter = Em.View.extend({
tagName: "ul",
classNames: ['filter'],
templateName: require('templates/common/grid/filter'),
attributeBindings: ['style'],
getHeader: function getHeader() {
return this.get('header');
},
filters: Em.computed.alias('header._filters')
});
App.GridHeader = Em.View.extend({
templateName: require('templates/common/grid/header'),
tagName: 'th',
filterable: true,
showFilter: false,
getGrid: function getGrid() {
return this.get('grid');
},
_filters: [],
doFilter: function doFilter() {},
toggleFilter: function toggleFilter() {
this.set('showFilter', 1 - this.get('showFilter'));
},
applyFilter: function applyFilter() {
var filters = this.get('_filters');
var filterValues = [];
$.each(filters, function () {
if (this.get('checked')) {
filterValues.push(this.get('value'));
}
});
var grid = this.get('grid');
grid.addFilters(this.get('name'), filterValues);
this.set('showFilter', false);
},
init: function init() {
this._super();
if (!this.get('_filters').length) {
this.filterValues();
var thisHeader = this;
this.set('filter', App.GridFilter.extend({ header: thisHeader }));
}
},
filterValues: function () {
var gridFilters = this.get('grid._filters');
if (gridFilters && gridFilters[this.get('name')]) {
var filters = this.get('grid._filters')[this.get('name')];
// there should be something like filter preparing
var newFilters = [];
$.each(filters, function (i, v) {
newFilters.push(App.GridFilterObject.create({ label: v, value: v }));
});
this.set('_filters', newFilters);
}
}.observes('grid._filters')
});
App.GridRow = Em.View.extend({
tagName: 'tr',
init: function init(options) {
var object = this.get('object');
var grid = this.get('grid');
var fieldNames = grid.get('fieldNames');
var template = '';
if (fieldNames) {
$.each(grid.get('fieldNames'), function (i, field) {
template += "<td>" + object.get(field) + "</td>";
});
this.set('template', Em.Handlebars.compile(template));
}
return this._super();
}
});
App.GridPage = Em.Object.extend({
activeClass: Em.computed.ifThenElse('active', 'active', ''),
active: function () {
return parseInt(this.get('number')) == parseInt(this.get('pager.grid.currentPage'));
}.property('pager.grid.currentPage')
});
App.GridPager = Em.View.extend({
pages: [],
templateName: require('templates/common/grid/pager'),
classNames: ['pagination'],
activatePrevPage: function activatePrevPage() {
var current = this.get('grid.currentPage');
if (current > 1) this.set('grid.currentPage', current - 1);
},
activateNextPage: function activateNextPage() {
var current = this.get('grid.currentPage');
if (current < this.get('pages').length) this.set('grid.currentPage', current + 1);
},
prevPageDisabled: function () {
return this.get('grid.currentPage') > 1 ? false : "disabled";
}.property('grid.currentPage'),
nextPageDisabled: function () {
return this.get('grid.currentPage') < this.get('pages').length ? false : "disabled";
}.property('grid.currentPage'),
init: function init() {
this._super();
this.clearPages();
this.pushPages();
},
activatePage: function activatePage(event) {
var page = event.context;
this.get('grid').set('currentPage', parseInt(event.context.get('number')));
},
clearPages: function clearPages() {
this.set('pages', []);
},
pushPages: function () {
var thisPager = this;
var pages = this.get('grid._pager.pages');
$.each(pages, function () {
var thisNumber = this;
thisPager.get('pages').push(App.GridPage.create({
number: thisNumber,
pager: thisPager
}));
});
}.observes('grid._pager')
});
App.Grid = Em.View.extend({
_columns: {}, // not used
_filters: {}, // prepared filters from data values
_pager: { pages: [1, 2, 3, 4, 5] }, // observed by pager to config it
_collection: { className: false, staticOptions: {} }, // collection config
currentPage: 1,
fieldNames: [],
appliedFilters: {},
filteredArray: [],
columns: [],
collection: [],
initComleted: false,
rows: [],
templateName: require('templates/main/admin/audit'),
init: function init() {
this._super();
this.prepareColumns(); // should be the 1
this.prepareCollection();
this.preparePager();
},
preparePager: function preparePager() {
// this.set('pager', App.GridPager.extend({ grid:this })); ask to hide
},
addFilters: function addFilters(field, values) {
var filters = this.get('appliedFilters');
filters[field] = values;
var collection = this.get('_collection.className');
collection = collection.find();
var arrayCollection = collection.filter(function (data) {
var oneFilterFail = false;
$.each(filters, function (fieldname, values) {
if (values.length && values.indexOf(data.get(fieldname)) == -1) {
return oneFilterFail = true;
}
});
return !oneFilterFail;
});
this.set('filteredArray', arrayCollection);
},
prepareCollection: function prepareCollection() {
if (validator.empty(this.get('_collection.className'))) {
throw "_collection.className field is not defined";
}
var collection = this.get('_collection.className');
this.set('collection', collection.find(this.get('_collection.staticOptions')));
},
addColumn: function addColumn(options) {
options.grid = this;
if (validator.empty(options.name)) {
throw "define column name";
}
if (this.get('_columns.' + options.name)) {
throw "column with this '" + options.name + "' already exists";
}
var field = App.GridHeader.extend(options);
this.columns.push(field);
if (field.filterable || 1) {
// .filterable - field not working :(
this.fieldNames.push(options.name);
}
},
clearColumns: function clearColumns() {
this.set('_columns', {});
this.set('columns', []);
this.set('fieldNames', []);
},
prepareColumns: function prepareColumns() {
this.clearColumns();
},
prepareFilters: function () {
var thisGrid = this;
var collection = this.get('collection');
var fieldNames = this.get('fieldNames');
var options = {};
if (collection && collection.content) {
collection.forEach(function (object, i) {
$.each(fieldNames, function (j, field) {
if (!options[field]) {
options[field] = [];
}
var filter = object.get(field);
if (options[field].indexOf(filter) == -1) {
options[field].push(filter);
}
});
});
thisGrid.set('_filters', options);
}
}.observes('collection.length'),
clearRows: function clearRows() {
this.set('rows', []);
},
prepareRows: function () {
var collection = this.get('collection');
var thisGrid = this;
this.clearRows();
var i = 1;
if (collection && collection.content) {
collection.forEach(function (object, i) {
var row = App.GridRow.extend({ grid: thisGrid, object: object });
thisGrid.rows.push(row);
});
}
}.observes('collection.length'),
filteredRows: function () {
var collection = this.get('filteredArray');
var thisGrid = this;
this.clearRows();
collection.forEach(function (object) {
var row = App.GridRow.extend({ grid: thisGrid, object: object });
thisGrid.rows.push(row);
});
}.observes('filteredArray')
});
}); |