table_view.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 filters = require('views/common/filter_view');
  20. App.TableView = Em.View.extend(App.UserPref, {
  21. init: function() {
  22. this.set('filterConditions', []);
  23. this._super();
  24. },
  25. /**
  26. * Defines to show pagination or show all records
  27. * @type {Boolean}
  28. */
  29. pagination: true,
  30. /**
  31. * Shows if all data is loaded and filtered
  32. * @type {Boolean}
  33. */
  34. filteringComplete: false,
  35. /**
  36. * intermediary for filteringComplete
  37. * @type {Boolean}
  38. */
  39. tableFilteringComplete: false,
  40. /**
  41. * The number of rows to show on every page
  42. * The value should be a number converted into string type in order to support select element API
  43. * Example: "10", "25"
  44. * @type {String}
  45. */
  46. displayLength: null,
  47. /**
  48. * default value of display length
  49. * The value should be a number converted into string type in order to support select element API
  50. * Example: "10", "25"
  51. */
  52. defaultDisplayLength: "10",
  53. /**
  54. * number of items in table after applying filters
  55. */
  56. filteredCount: Em.computed.alias('filteredContent.length'),
  57. /**
  58. * total number of items in table before applying filters
  59. */
  60. totalCount: Em.computed.alias('content.length'),
  61. /**
  62. * Do filtering, using saved in the local storage filter conditions
  63. */
  64. willInsertElement: function () {
  65. var self = this;
  66. var name = this.get('controller.name');
  67. if (!this.get('displayLength')) {
  68. var displayLength = App.db.getDisplayLength(name);
  69. if (displayLength) {
  70. if (this.get('state') !== "inBuffer") {
  71. self.set('displayLength', displayLength);
  72. self.initFilters();
  73. } else {
  74. Em.run.next(function () {
  75. self.set('displayLength', displayLength);
  76. self.initFilters();
  77. });
  78. }
  79. } else {
  80. if (!$.mocho) {
  81. this.getUserPref(this.displayLengthKey()).complete(function () {
  82. self.initFilters();
  83. });
  84. }
  85. }
  86. }
  87. },
  88. /**
  89. * initialize filters
  90. * restore values from local DB
  91. * or clear filters in case there is no filters to restore
  92. */
  93. initFilters: function () {
  94. var name = this.get('controller.name');
  95. var self = this;
  96. var filterConditions = App.db.getFilterConditions(name);
  97. if (!Em.isEmpty(filterConditions)) {
  98. this.set('filterConditions', filterConditions);
  99. var childViews = this.get('childViews');
  100. filterConditions.forEach(function (condition, index, filteredConditions) {
  101. var view = !Em.isNone(condition.iColumn) && childViews.findProperty('column', condition.iColumn);
  102. if (view) {
  103. if (view.get('emptyValue')) {
  104. view.clearFilter();
  105. self.saveFilterConditions(condition.iColumn, view.get('appliedEmptyValue'), condition.type, false);
  106. } else {
  107. view.setValue(condition.value);
  108. }
  109. Em.run.next(function () {
  110. view.showClearFilter();
  111. // check if it is the last iteration
  112. if (filteredConditions.length - index - 1 === 0) {
  113. self.set('tableFilteringComplete', true);
  114. }
  115. });
  116. } else {
  117. self.set('tableFilteringComplete', true);
  118. }
  119. });
  120. } else {
  121. this.set('tableFilteringComplete', true);
  122. }
  123. },
  124. /**
  125. * Persist-key of current table displayLength property
  126. * @param {String} loginName current user login name
  127. * @returns {String}
  128. */
  129. displayLengthKey: function (loginName) {
  130. if (App.get('testMode')) {
  131. return 'pagination_displayLength';
  132. }
  133. loginName = loginName ? loginName : App.router.get('loginName');
  134. return this.get('controller.name') + '-pagination-displayLength-' + loginName;
  135. },
  136. /**
  137. * Set received from server value to <code>displayLengthOnLoad</code>
  138. * @param {Number} response
  139. * @param {Object} request
  140. * @param {Object} data
  141. * @returns {*}
  142. */
  143. getUserPrefSuccessCallback: function (response, request, data) {
  144. this.set('displayLength', response);
  145. return response;
  146. },
  147. /**
  148. * Set default value to <code>displayLength</code> (and send it on server) if value wasn't found on server
  149. * @returns {Number}
  150. */
  151. getUserPrefErrorCallback: function () {
  152. // this user is first time login
  153. var displayLengthDefault = this.get('defaultDisplayLength');
  154. this.set('displayLength', displayLengthDefault);
  155. if (App.isAuthorized('SERVICE.VIEW_METRICS')) {
  156. this.saveDisplayLength();
  157. }
  158. this.filter();
  159. return displayLengthDefault;
  160. },
  161. /**
  162. * Return pagination information displayed on the page
  163. * @type {String}
  164. */
  165. paginationInfo: Em.computed.i18nFormat('tableView.filters.paginationInfo', 'startIndex', 'endIndex', 'filteredCount'),
  166. paginationLeft: Ember.View.extend({
  167. tagName: 'a',
  168. template: Ember.Handlebars.compile('<i class="icon-arrow-left"></i>'),
  169. classNameBindings: ['class'],
  170. class: function () {
  171. if (this.get("parentView.startIndex") > 1) {
  172. return "paginate_previous";
  173. }
  174. return "paginate_disabled_previous";
  175. }.property("parentView.startIndex", 'parentView.filteredCount'),
  176. click: function () {
  177. if (this.get('class') === "paginate_previous") {
  178. this.get('parentView').previousPage();
  179. }
  180. }
  181. }),
  182. paginationRight: Ember.View.extend({
  183. tagName: 'a',
  184. template: Ember.Handlebars.compile('<i class="icon-arrow-right"></i>'),
  185. classNameBindings: ['class'],
  186. class: function () {
  187. if ((this.get("parentView.endIndex")) < this.get("parentView.filteredCount")) {
  188. return "paginate_next";
  189. }
  190. return "paginate_disabled_next";
  191. }.property("parentView.endIndex", 'parentView.filteredCount'),
  192. click: function () {
  193. if (this.get('class') === "paginate_next") {
  194. this.get('parentView').nextPage();
  195. }
  196. }
  197. }),
  198. paginationFirst: Ember.View.extend({
  199. tagName: 'a',
  200. template: Ember.Handlebars.compile('<i class="icon-step-backward"></i>'),
  201. classNameBindings: ['class'],
  202. class: function () {
  203. if ((this.get("parentView.endIndex")) > parseInt(this.get("parentView.displayLength"))) {
  204. return "paginate_previous";
  205. }
  206. return "paginate_disabled_previous";
  207. }.property("parentView.endIndex", 'parentView.filteredCount'),
  208. click: function () {
  209. if (this.get('class') === "paginate_previous") {
  210. this.get('parentView').firstPage();
  211. }
  212. }
  213. }),
  214. paginationLast: Ember.View.extend({
  215. tagName: 'a',
  216. template: Ember.Handlebars.compile('<i class="icon-step-forward"></i>'),
  217. classNameBindings: ['class'],
  218. class: function () {
  219. if (this.get("parentView.endIndex") !== this.get("parentView.filteredCount")) {
  220. return "paginate_next";
  221. }
  222. return "paginate_disabled_next";
  223. }.property("parentView.endIndex", 'parentView.filteredCount'),
  224. click: function () {
  225. if (this.get('class') === "paginate_next") {
  226. this.get('parentView').lastPage();
  227. }
  228. }
  229. }),
  230. /**
  231. * Select View with list of "rows-per-page" options
  232. * @type {Ember.View}
  233. */
  234. rowsPerPageSelectView: Em.Select.extend({
  235. content: ['10', '25', '50', '100'],
  236. change: function () {
  237. this.get('parentView').saveDisplayLength();
  238. }
  239. }),
  240. /**
  241. * Start index for displayed content on the page
  242. */
  243. startIndex: 1,
  244. /**
  245. * Calculate end index for displayed content on the page
  246. */
  247. endIndex: function () {
  248. if (this.get('pagination') && this.get('displayLength')) {
  249. return Math.min(this.get('filteredCount'), this.get('startIndex') + parseInt(this.get('displayLength')) - 1);
  250. } else {
  251. return this.get('filteredCount') || 0;
  252. }
  253. }.property('startIndex', 'displayLength', 'filteredCount'),
  254. /**
  255. * Onclick handler for previous page button on the page
  256. */
  257. previousPage: function () {
  258. var result = this.get('startIndex') - parseInt(this.get('displayLength'));
  259. this.set('startIndex', (result < 2) ? 1 : result);
  260. },
  261. /**
  262. * Onclick handler for next page button on the page
  263. */
  264. nextPage: function () {
  265. var result = this.get('startIndex') + parseInt(this.get('displayLength'));
  266. if (result - 1 < this.get('filteredCount')) {
  267. this.set('startIndex', result);
  268. }
  269. },
  270. /**
  271. * Onclick handler for first page button on the page
  272. */
  273. firstPage: function () {
  274. this.set('startIndex', 1);
  275. },
  276. /**
  277. * Onclick handler for last page button on the page
  278. */
  279. lastPage: function () {
  280. var pagesCount = this.get('filteredCount') / parseInt(this.get('displayLength'));
  281. var startIndex = (this.get('filteredCount') % parseInt(this.get('displayLength')) === 0) ?
  282. (pagesCount - 1) * parseInt(this.get('displayLength')) :
  283. Math.floor(pagesCount) * parseInt(this.get('displayLength'));
  284. this.set('startIndex', ++startIndex);
  285. },
  286. /**
  287. * Calculates default value for startIndex property after applying filter or changing displayLength
  288. */
  289. updatePaging: function (controller, property) {
  290. var displayLength = this.get('displayLength');
  291. var filteredContentLength = this.get('filteredCount');
  292. if (property == 'displayLength') {
  293. this.set('startIndex', Math.min(1, filteredContentLength));
  294. } else if (!filteredContentLength) {
  295. this.set('startIndex', 0);
  296. } else if (this.get('startIndex') > filteredContentLength) {
  297. this.set('startIndex', Math.floor((filteredContentLength - 1) / displayLength) * displayLength + 1);
  298. } else if (!this.get('startIndex')) {
  299. this.set('startIndex', 1);
  300. }
  301. }.observes('displayLength', 'filteredCount'),
  302. /**
  303. * Apply each filter to each row
  304. *
  305. * @param {Number} iColumn number of column by which filter
  306. * @param {Object} value
  307. * @param {String} type
  308. */
  309. updateFilter: function (iColumn, value, type) {
  310. this.saveFilterConditions(iColumn, value, type, false);
  311. this.filtersUsedCalc();
  312. this.filter();
  313. },
  314. /**
  315. * save filter conditions to local storage
  316. * @param iColumn {Number}
  317. * @param value {String|Array}
  318. * @param type {String}
  319. * @param skipFilter {Boolean}
  320. */
  321. saveFilterConditions: function(iColumn, value, type, skipFilter) {
  322. var filterCondition = this.get('filterConditions').findProperty('iColumn', iColumn);
  323. if (filterCondition) {
  324. filterCondition.value = value;
  325. filterCondition.skipFilter = skipFilter;
  326. } else {
  327. filterCondition = {
  328. skipFilter: skipFilter,
  329. iColumn: iColumn,
  330. value: value,
  331. type: type
  332. };
  333. this.get('filterConditions').push(filterCondition);
  334. }
  335. this.saveAllFilterConditions();
  336. },
  337. /**
  338. * Save not empty <code>filterConditions</code> to the localStorage
  339. *
  340. * @method saveAllFilterConditions
  341. */
  342. saveAllFilterConditions: function () {
  343. var filterConditions = this.get('filterConditions');
  344. // remove empty entries
  345. filterConditions = filterConditions.filter(function(item) {
  346. return !Em.isEmpty(item.value);
  347. });
  348. this.set('filterConditions', filterConditions);
  349. App.db.setFilterConditions(this.get('controller.name'), filterConditions);
  350. },
  351. saveDisplayLength: function() {
  352. var self = this;
  353. Em.run.next(function() {
  354. App.db.setDisplayLength(self.get('controller.name'), self.get('displayLength'));
  355. if (!App.get('testMode')) {
  356. if (App.isAuthorized('SERVICE.VIEW_METRICS')) {
  357. self.postUserPref(self.displayLengthKey(), self.get('displayLength'));
  358. }
  359. }
  360. });
  361. },
  362. clearFilterConditionsFromLocalStorage: function() {
  363. var result = false;
  364. var currentFCs = App.db.getFilterConditions(this.get('controller.name'));
  365. if (currentFCs != null) {
  366. App.db.setFilterConditions(this.get('controller.name'), null);
  367. result = true;
  368. }
  369. var query = App.db.getComboSearchQuery(this.get('controller.name'));
  370. if (query != null) {
  371. App.db.setComboSearchQuery(this.get('controller.name'), null);
  372. result = true;
  373. }
  374. return result;
  375. },
  376. /**
  377. * Contain filter conditions for each column
  378. * @type {Array}
  379. */
  380. filterConditions: [],
  381. /**
  382. * Contains content after implementing filters
  383. * @type {Array}
  384. */
  385. filteredContent: [],
  386. /**
  387. * Determine if <code>filteredContent</code> is empty or not
  388. * @type {Boolean}
  389. */
  390. hasFilteredItems: Em.computed.bool('filteredCount'),
  391. /**
  392. * Contains content to show on the current page of data page view
  393. * @type {Array}
  394. */
  395. pageContent: function () {
  396. return this.get('filteredContent').slice(this.get('startIndex') - 1, this.get('endIndex'));
  397. }.property('filteredCount', 'startIndex', 'endIndex'),
  398. /**
  399. * flag to toggle displaying filtered hosts counter
  400. */
  401. showFilteredContent: function () {
  402. var result = false;
  403. if (this.get('filterConditions.length') > 0) {
  404. this.get('filterConditions').forEach(function(f) {
  405. if (f.value) {
  406. if (Em.typeOf(f.value) == "array") {
  407. if (f.value[0] || f.value[1]) {
  408. result = true;
  409. }
  410. } else {
  411. result = true;
  412. }
  413. }
  414. });
  415. }
  416. return result;
  417. }.property('filteredContent.length'),
  418. /**
  419. * Filter table by filterConditions
  420. */
  421. filter: function () {
  422. var content = this.get('content');
  423. var filterConditions = this.get('filterConditions').filterProperty('value');
  424. var result;
  425. var assoc = this.get('colPropAssoc');
  426. if (filterConditions.length) {
  427. result = content.filter(function (item) {
  428. var match = true;
  429. filterConditions.forEach(function (condition) {
  430. var filterFunc = filters.getFilterByType(condition.type, false);
  431. if (match) {
  432. match = filterFunc(item.get(assoc[condition.iColumn]), condition.value);
  433. }
  434. });
  435. return match;
  436. });
  437. this.set('filteredContent', result);
  438. } else {
  439. this.set('filteredContent', content ? content.toArray() : []);
  440. }
  441. }.observes('content.length'),
  442. /**
  443. * Does any filter is used on the page
  444. * @type {Boolean}
  445. */
  446. filtersUsed: false,
  447. /**
  448. * Determine if some filters are used on the page
  449. * Set <code>filtersUsed</code> value
  450. */
  451. filtersUsedCalc: function() {
  452. var filterConditions = this.get('filterConditions');
  453. if (!filterConditions.length) {
  454. this.set('filtersUsed', false);
  455. return;
  456. }
  457. var filtersUsed = false;
  458. filterConditions.forEach(function(filterCondition) {
  459. if (filterCondition.value.toString() !== '') {
  460. filtersUsed = true;
  461. }
  462. });
  463. this.set('filtersUsed', filtersUsed);
  464. },
  465. /**
  466. * Run <code>clearFilter</code> in the each child filterView
  467. */
  468. clearFilters: function() {
  469. this.set('filterConditions', []);
  470. this.get('childViews').forEach(function(childView) {
  471. Em.tryInvoke(childView, 'clearFilter');
  472. });
  473. }
  474. });