GenericListView.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: admin
  5. * Copyright IBM Corp. 2017
  6. * US Government Users Restricted Rights
  7. * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['bi/glass/app/util/View', 'underscore'], function (View, _) {
  10. 'use strict'; //NOSONAR: meant to be strict
  11. var GenericListView = View.extend({
  12. selectedRows: [],
  13. sortAsc: true,
  14. render: function render(refresh) {
  15. return this._renderListView(refresh);
  16. },
  17. _setHeights: function _setHeights() {
  18. var list = this.$el.find(".bi-admin-list");
  19. var windowHeight = $(window).height();
  20. var headerElementsHeight = this.$el.offset().top;
  21. var updatedListHeight = windowHeight - headerElementsHeight - 20;
  22. list.height(updatedListHeight);
  23. },
  24. _getItemById: function _getItemById(id) {
  25. return _.find(this.items, function (item) {
  26. return _.unescape(id) === _.unescape(item.id);
  27. });
  28. },
  29. _getSelectedItems: function _getSelectedItems() {
  30. return this.selectedRows.map(function (row) {
  31. return this._getItemById(row.id);
  32. }.bind(this));
  33. },
  34. _bindEvents: function _bindEvents() {
  35. var $items = this.$el.find('.bi-admin-list-item');
  36. $items.off();
  37. this.off('showContextMenu');
  38. $items.on('primaryaction', function (e) {
  39. this._handleSelection(e, false);
  40. }.bind(this));
  41. $items.on('contextmenu', function (e) {
  42. e.preventDefault();
  43. }.bind(this));
  44. this.on('showContextMenu', function (e) {
  45. var handler = this._getNewActionHandler();
  46. var args = {
  47. position: e.position,
  48. menuId: this.menuId,
  49. activeObject: {
  50. handler: handler,
  51. data: e.data,
  52. parentView: this
  53. }
  54. };
  55. this.glassContext.appController.showContextMenu(args);
  56. }.bind(this)); // context menu
  57. var $contextMenus = this.$el.find('.ellipsesButton');
  58. $contextMenus.on('primaryaction', function (e) {
  59. this._handleSelection(e, true);
  60. this._handleContextMenuEvent(e);
  61. }.bind(this));
  62. $(window).on('resize', this._setHeights.bind(this));
  63. },
  64. _handleContextMenuEvent: function _handleContextMenuEvent(e) {
  65. var position = {};
  66. if (e.pageX === undefined || e.gesture === undefined || e.gesture.center === undefined || e.gesture.center.pageX === undefined) {
  67. position = $(e.target).offset();
  68. } else {
  69. position.left = e.pageX || e.gesture.center.pageX;
  70. position.top = e.pageY || e.gesture.center.pageY;
  71. }
  72. this.trigger('showContextMenu', {
  73. data: this._getSelectedItems(),
  74. position: {
  75. "pageX": position.left,
  76. "pageY": position.top
  77. }
  78. });
  79. e.stopPropagation();
  80. return false;
  81. },
  82. _handleNormalSelect: function _handleNormalSelect(id, $item, selectedIndex, dirtySelections, contextMenuAction) {
  83. var selections = dirtySelections; // normal click or keypress
  84. var contextSelectUnselectedNode = contextMenuAction && !selectedIndex;
  85. var singleSelection = !contextMenuAction && (!selectedIndex || this.selectedRows.length > 1);
  86. if (contextSelectUnselectedNode || singleSelection) {
  87. selections = this._clearAllSelections();
  88. this._selectItem(id, $item, selections);
  89. }
  90. this.lastSelectionId = id;
  91. this.lastShiftSelectionIndex = -1;
  92. return selections;
  93. },
  94. _handleMetaSelect: function _handleMetaSelect(id, $item, selectedIndex, dirtySelections) {
  95. var selections = dirtySelections; // multi-select via ctrl key (or macOS equivalent)
  96. if (!selectedIndex) {
  97. this._selectItem(id, $item, selections);
  98. } else {
  99. this._removeItem(id, $item, selections);
  100. }
  101. this.lastSelectionId = id;
  102. this.lastShiftSelectionIndex = -1;
  103. return selections;
  104. },
  105. _handleShiftSelect: function _handleShiftSelect(selectedItem, dirtySelections) {
  106. var selections = dirtySelections; // multi-select items between last selected and target
  107. var lastSelectedIndex = _.indexOf(this.items, this._getItemById(this.lastSelectionId));
  108. var targetIndex = _.indexOf(this.items, selectedItem); // account for the indexing of jquery slice method (include last node)
  109. if (targetIndex > lastSelectedIndex) {
  110. targetIndex++;
  111. }
  112. if (targetIndex < lastSelectedIndex) {
  113. lastSelectedIndex++;
  114. }
  115. var shiftBetweenLower = targetIndex < lastSelectedIndex && targetIndex > this.lastShiftSelectionIndex;
  116. var shiftBetweenHigher = targetIndex > lastSelectedIndex && targetIndex < this.lastShiftSelectionIndex;
  117. if (this.lastShiftSelectionIndex !== -1 && (shiftBetweenHigher || shiftBetweenLower)) {
  118. // selection is between the last selected and the last shift select target
  119. // must remove the difference
  120. this._removeShiftSelection(targetIndex, this.lastShiftSelectionIndex, selections);
  121. } else {
  122. if (this.lastShiftSelectionIndex >= 0) {
  123. this._removeShiftSelection(targetIndex, this.lastShiftSelectionIndex, selections);
  124. }
  125. this._addShiftSelection(lastSelectedIndex, targetIndex, selections);
  126. }
  127. this.lastShiftSelectionIndex = targetIndex;
  128. return selections;
  129. },
  130. _selectItem: function _selectItem(id, $item, dirtySelections) {
  131. var schemaToSelect = {
  132. id: id,
  133. item: $item
  134. };
  135. var index;
  136. if (this._isItemSelectable(this._getItemById(id))) {
  137. for (var i = 0; i < this.selectedRows.length; i++) {
  138. if (this.selectedRows[i].id === id) {
  139. index = i;
  140. break;
  141. }
  142. }
  143. if (index > -1) {
  144. this.selectedRows[index] = schemaToSelect;
  145. } else {
  146. this.selectedRows.push(schemaToSelect);
  147. }
  148. dirtySelections.push({
  149. item: $item,
  150. selected: true
  151. });
  152. }
  153. },
  154. _removeItem: function _removeItem(id, $item, dirtySelections) {
  155. var pos;
  156. for (var i = 0; i < this.selectedRows.length; i++) {
  157. if (this.selectedRows[i].id === id) {
  158. pos = i;
  159. break;
  160. }
  161. }
  162. this.selectedRows.splice(pos, 1);
  163. dirtySelections.push({
  164. item: $item,
  165. selected: false
  166. });
  167. },
  168. _clearAllSelections: function _clearAllSelections() {
  169. var dirtySelections = this.selectedRows.map(function (selectedRow) {
  170. return {
  171. item: selectedRow.item,
  172. selected: false
  173. };
  174. });
  175. this.selectedRows = [];
  176. return dirtySelections;
  177. },
  178. _addShiftSelection: function _addShiftSelection(index0, index1, dirtySelections) {
  179. // new selection is last selected -> new target
  180. var $items = this._getSelectionsBetweenIndicies(index0, index1);
  181. _.forEach($items, function (shiftItem) {
  182. var $shiftItem = $(shiftItem);
  183. this._selectItem($shiftItem.attr('id').trim(), $shiftItem, dirtySelections);
  184. }.bind(this));
  185. },
  186. _removeShiftSelection: function _removeShiftSelection(index0, index1, dirtySelections) {
  187. // remove previously shift selected items
  188. var $items = this._getSelectionsBetweenIndicies(index0, index1);
  189. _.forEach($items, function (shiftItem) {
  190. var $shiftItem = $(shiftItem);
  191. this._removeItem($shiftItem.attr('id').trim(), $shiftItem, dirtySelections);
  192. }.bind(this));
  193. },
  194. _getSelectionsBetweenIndicies: function _getSelectionsBetweenIndicies(id0, id1) {
  195. var $items;
  196. if (id0 < id1) {
  197. $items = this.$el.find('.bi-admin-list-item').slice(id0, id1);
  198. } else {
  199. $items = this.$el.find('.bi-admin-list-item').slice(id1, id0);
  200. }
  201. return $items;
  202. },
  203. _applySelectionStyling: function _applySelectionStyling(dirtySelections) {
  204. _.forEach(dirtySelections, function (dirtySelection) {
  205. if (dirtySelection.selected) {
  206. dirtySelection.item.addClass('bi-admin-list-table-selected-schema');
  207. } else {
  208. dirtySelection.item.removeClass('bi-admin-list-table-selected-schema');
  209. }
  210. });
  211. dirtySelections = [];
  212. },
  213. selectById: function selectById(id) {
  214. var item = this._getItemById(id) || this._getItemByConnId(id);
  215. this.trigger('selected', item);
  216. },
  217. selectByIndex: function selectByIndex(index) {
  218. var item = this.items[index];
  219. this.trigger('selected', item);
  220. },
  221. removeSelectedRowById: function removeSelectedRowById(id) {
  222. _.find(this.$el.find('tr.list-item'), function (item) {
  223. if (item.id === id) {
  224. return item;
  225. }
  226. }).remove();
  227. if ($('#workPane').is(":visible")) {
  228. $('#workPane').addClass('hide');
  229. }
  230. },
  231. insertCopyRowById: function insertCopyRowById(copyRow, id) {
  232. var copySource = _.find(this.$el.find('tr.list-item'), function (item) {
  233. if (item.id === id) {
  234. return item;
  235. }
  236. });
  237. var targetSource = _.clone(copySource);
  238. targetSource.id = id;
  239. targetSource.insertAfter(copySource);
  240. },
  241. _sortItems: function _sortItems(field) {
  242. var tempSortField;
  243. for (var key in this.sortMap) {
  244. if (field.indexOf(key) > -1) {
  245. tempSortField = key;
  246. break;
  247. }
  248. } // no sort key found, do not sort
  249. if (tempSortField) {
  250. if (tempSortField !== this.sortField) {
  251. // only sort on first press
  252. this.sortField = tempSortField;
  253. this.items = _.sortBy(this.items, function (item) {
  254. return item[this.sortMap[this.sortField]].toUpperCase();
  255. }.bind(this));
  256. this.sortAsc = true;
  257. } else {
  258. // reverse on subsequent clicks
  259. this.items = this.items.reverse();
  260. this.sortAsc = !this.sortAsc;
  261. }
  262. }
  263. },
  264. remove: function remove() {
  265. $(window).off("resize", this._setHeights);
  266. },
  267. _isItemSelectable: function _isItemSelectable(item) {
  268. return true;
  269. }
  270. });
  271. return GenericListView;
  272. });