grid.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var validator = require('utils/validator');
  20. App.GridFilterObject = Em.Object.extend({
  21. checked:false
  22. });
  23. App.GridFilter = Em.View.extend({
  24. tagName:"ul",
  25. classNames:['filter'],
  26. templateName:require('templates/common/grid/filter'),
  27. attributeBindings:['style'],
  28. getHeader:function () {
  29. return this.get('header')
  30. },
  31. filters:function () {
  32. return this.get('header._filters');
  33. }.property('header._filters')
  34. });
  35. App.GridHeader = Em.View.extend({
  36. templateName:require('templates/common/grid/header'),
  37. tagName:'th',
  38. filterable:true,
  39. showFilter:false,
  40. getGrid:function () {
  41. return this.get('grid');
  42. },
  43. _filters:[],
  44. doFilter:function () {
  45. console.log(this.get('grid'));
  46. },
  47. toggleFilter:function () {
  48. this.set('showFilter', 1 - this.get('showFilter'));
  49. },
  50. applyFilter:function () {
  51. console.warn('APPLYING FILTERS');
  52. var filters = this.get('_filters');
  53. var filterValues = [];
  54. $.each(filters, function(){
  55. if(this.get('checked')) {
  56. filterValues.push(this.get('value'));
  57. }
  58. });
  59. var grid = this.get('grid');
  60. grid.addFilters(this.get('name'), filterValues);
  61. this.set('showFilter', false);
  62. },
  63. init:function () {
  64. this._super();
  65. if (!this.get('_filters').length) {
  66. this.filterValues();
  67. var thisHeader = this;
  68. this.set('filter', App.GridFilter.extend({ header:thisHeader }));
  69. }
  70. },
  71. filterValues:function () {
  72. var gridFilters = this.get('grid._filters');
  73. if (gridFilters && gridFilters[this.get('name')]) {
  74. var filters = this.get('grid._filters')[this.get('name')];
  75. // there should be something like filter preparing
  76. var newFilters = [];
  77. $.each(filters, function (i, v) {
  78. newFilters.push(App.GridFilterObject.create({label:v, value:v}));
  79. });
  80. this.set('_filters', newFilters);
  81. }
  82. }.observes('grid._filters')
  83. });
  84. App.GridRow = Em.View.extend({
  85. tagName:'tr',
  86. init:function (options) {
  87. var object = this.get('object');
  88. var grid = this.get('grid');
  89. var fieldNames = grid.get('fieldNames');
  90. var template = '';
  91. if (fieldNames) {
  92. $.each(grid.get('fieldNames'), function (i, field) {
  93. template += "<td>" + object.get(field) + "</td>";
  94. });
  95. this.set('template', Em.Handlebars.compile(template));
  96. }
  97. return this._super();
  98. }
  99. });
  100. App.GridPage = Em.Object.extend({
  101. activeClass:function () {
  102. return this.get('active') ? "active" : "";
  103. }.property('active'),
  104. active:function () {
  105. return parseInt(this.get('number')) == parseInt(this.get('pager.grid.currentPage'));
  106. }.property('pager.grid.currentPage')
  107. });
  108. App.GridPager = Em.View.extend({
  109. pages:[],
  110. templateName:require('templates/common/grid/pager'),
  111. classNames:['pagination'],
  112. activatePrevPage:function () {
  113. var current = this.get('grid.currentPage');
  114. if (current > 1) this.set('grid.currentPage', current - 1);
  115. },
  116. activateNextPage:function () {
  117. var current = this.get('grid.currentPage');
  118. if (current < this.get('pages').length) this.set('grid.currentPage', current + 1);
  119. },
  120. prevPageDisabled:function () {
  121. return this.get('grid.currentPage') > 1 ? false : "disabled";
  122. }.property('grid.currentPage'),
  123. nextPageDisabled:function () {
  124. return this.get('grid.currentPage') < this.get('pages').length ? false : "disabled";
  125. }.property('grid.currentPage'),
  126. init:function () {
  127. this._super();
  128. this.clearPages();
  129. this.pushPages();
  130. },
  131. activatePage:function (event) {
  132. var page = event.context;
  133. this.get('grid').set('currentPage', parseInt(event.context.get('number')));
  134. },
  135. clearPages:function () {
  136. this.set('pages', []);
  137. },
  138. pushPages:function () {
  139. var thisPager = this;
  140. var pages = this.get('grid._pager.pages');
  141. $.each(pages, function () {
  142. var thisNumber = this;
  143. thisPager.get('pages').push(App.GridPage.create({
  144. number:thisNumber,
  145. pager:thisPager
  146. }));
  147. })
  148. }.observes('grid._pager')
  149. });
  150. App.Grid = Em.View.extend({
  151. _columns:{}, // not used
  152. _filters:{}, // prepared filters from data values
  153. _pager:{pages:[1, 2, 3, 4, 5]}, // observed by pager to config it
  154. _collection:{className:false, staticOptions:{}}, // collection config
  155. currentPage:1,
  156. fieldNames:[],
  157. appliedFilters:{},
  158. filteredArray:[],
  159. columns:[],
  160. collection:[],
  161. initComleted:false,
  162. rows:[],
  163. templateName:require('templates/main/admin/audit'),
  164. init:function () {
  165. console.warn(" Grid INIT ");
  166. this._super();
  167. this.prepareColumns(); // should be the 1
  168. this.prepareCollection();
  169. this.preparePager();
  170. },
  171. preparePager:function () {
  172. // this.set('pager', App.GridPager.extend({ grid:this })); ask to hide
  173. },
  174. addFilters: function(field, values){
  175. var filters = this.get('appliedFilters');
  176. filters[field] = values;
  177. var collection = this.get('_collection.className');
  178. collection = collection.find();
  179. var arrayCollection = collection.filter(function(data) {
  180. var oneFilterFail = false;
  181. $.each(filters, function(fieldname, values){
  182. if(values.length && values.indexOf(data.get(fieldname)) == -1) {
  183. return oneFilterFail = true;
  184. }
  185. });
  186. return !oneFilterFail;
  187. });
  188. this.set('filteredArray', arrayCollection);
  189. },
  190. prepareCollection:function () {
  191. if (validator.empty(this.get('_collection.className'))) {
  192. throw "_collection.className field is not defined";
  193. }
  194. var collection = this.get('_collection.className');
  195. this.set('collection', collection.find(this.get('_collection.staticOptions')));
  196. },
  197. addColumn:function (options) {
  198. options.grid = this;
  199. if (validator.empty(options.name)) {
  200. throw "define column name";
  201. }
  202. if (this.get('_columns.' + options.name)) {
  203. throw "column with this '" + options.name + "' already exists";
  204. }
  205. var field = App.GridHeader.extend(options);
  206. this.columns.push(field);
  207. if (field.filterable || 1) { // .filterable - field not working :(
  208. this.fieldNames.push(options.name);
  209. }
  210. },
  211. clearColumns:function () {
  212. this.set('_columns', {});
  213. this.set('columns', []);
  214. this.set('fieldNames', []);
  215. },
  216. prepareColumns:function () {
  217. this.clearColumns();
  218. },
  219. prepareFilters:function () {
  220. var thisGrid = this;
  221. var collection = this.get('collection');
  222. var fieldNames = this.get('fieldNames');
  223. var options = {};
  224. if (collection && collection.content) {
  225. collection.forEach(function (object, i) {
  226. $.each(fieldNames, function (j, field) {
  227. if (!options[field]) {
  228. options[field] = [];
  229. }
  230. var filter = object.get(field);
  231. if (options[field].indexOf(filter) == -1) {
  232. options[field].push(filter);
  233. }
  234. });
  235. });
  236. thisGrid.set('_filters', options);
  237. }
  238. }.observes('collection.length'),
  239. clearRows:function () {
  240. this.set('rows', [])
  241. },
  242. prepareRows:function () {
  243. var collection = this.get('collection');
  244. var thisGrid = this;
  245. this.clearRows();
  246. console.warn("PREPARE ROWS LEN:", collection.get('length'));
  247. var i=1;
  248. if (collection && collection.content) {
  249. collection.forEach(function (object, i) {
  250. var row = App.GridRow.extend({grid:thisGrid, object:object});
  251. thisGrid.rows.push(row);
  252. });
  253. }
  254. }.observes('collection.length'),
  255. filteredRows:function () {
  256. var collection = this.get('filteredArray');
  257. var thisGrid = this;
  258. this.clearRows();
  259. collection.forEach(function (object) {
  260. var row = App.GridRow.extend({grid:thisGrid, object:object});
  261. thisGrid.rows.push(row);
  262. });
  263. }.observes('filteredArray')
  264. });