TableView.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2015, 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['jquery', 'underscore', '../../lib/@waca/dashboard-common/dist/ui/SearchableListView', '../../lib/@waca/core-client/js/core-client/i18n/Formatter'], function ($, _, BaseListView, Formatter) {
  8. var aORDER = ['no', 'asc', 'desc'];
  9. var View = null;
  10. /**
  11. * Extends base list view to provide search functionality within the list view.
  12. * If you extend this class you should implement the following methods
  13. * @see _getSearchableItems
  14. * @see _getSearchableFieldValue
  15. */
  16. View = BaseListView.extend({
  17. onSortClick: function onSortClick(event) {
  18. var $parent = $(event.target.parentElement);
  19. this.sortStateKey = $parent.data('role');
  20. var order = $parent.data('options').sort;
  21. this.sortStateOrder = this._getSortOrder(order);
  22. this._resetSortIcons();
  23. this._updateSortIcon($parent.find('.sortingIcon'), this.sortStateOrder);
  24. var sSearchInput = this._getSearchInputText();
  25. if (sSearchInput) {
  26. this.searchData(sSearchInput);
  27. } else {
  28. this._renderResultListItems(this._getSearchableItems());
  29. }
  30. },
  31. /**
  32. * Overridden method to provide a list of items that are searchable for this view
  33. */
  34. _getSearchableItems: function _getSearchableItems() {
  35. return this._listitems.slice();
  36. },
  37. /**
  38. * Overridden method to sort
  39. */
  40. _sortListItems: function _sortListItems(sortedItems) {
  41. var key = this.sortStateKey;
  42. var order = this.sortStateOrder;
  43. if (order === 'no') {
  44. return sortedItems;
  45. }
  46. sortedItems.sort(function (a, b) {
  47. var aValue = a[key].toLowerCase();
  48. var bValue = b[key].toLowerCase();
  49. return aValue < bValue ? -1 : aValue > bValue ? 1 : 0;
  50. });
  51. if (order === 'desc') {
  52. sortedItems.reverse();
  53. }
  54. return sortedItems;
  55. },
  56. _getSortOrder: function _getSortOrder(order) {
  57. var orderIndex = aORDER.indexOf(order);
  58. var sortOrderIndex = orderIndex + 1;
  59. if (sortOrderIndex === aORDER.length) {
  60. sortOrderIndex = 0;
  61. }
  62. return aORDER[sortOrderIndex];
  63. },
  64. prepareListItem: function prepareListItem(item) {
  65. var sDateTime = item['last-modified'] || item.lastModifiedDate;
  66. if (sDateTime) {
  67. var lastModifiedDateString = Formatter.formatDateTime(sDateTime, {
  68. type: 'date',
  69. formatLength: 'medium'
  70. });
  71. item.lastModified = lastModifiedDateString;
  72. }
  73. return item;
  74. },
  75. _resetSortIcons: function _resetSortIcons() {
  76. var sortIconHeaders = this.$el.find('.sortingIcon').removeClass().addClass('sortingIcon wfg_sorted_no');
  77. _.each(sortIconHeaders, function (header) {
  78. $(header).parent().data('options').sort = 'no';
  79. }.bind(this));
  80. },
  81. _updateSortIcon: function _updateSortIcon(target, sortOrder) {
  82. var $target = $(target);
  83. $target.removeClass('wfg_sorted_no').addClass('wfg_sorted_' + sortOrder);
  84. $target.parent().data('options').sort = sortOrder;
  85. }
  86. });
  87. return View;
  88. });
  89. //# sourceMappingURL=TableView.js.map