GroupListView.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: admin
  5. * Copyright IBM Corp. 2015, 2018
  6. * US Government Users Restricted Rights
  7. * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['jquery', 'doT', 'q', 'underscore', 'bi/commons/ui/View', 'bi/admin/common/ui/MagicWand', 'bi/admin/nls/StringResource', 'bacontentnav/utils/WidgetNavigator', 'text!bi/admin/common/templates/GroupListTemplate.html', 'bi/commons/utils/ContentFormatter'], function ($, dot, Q, _, View, MagicWand, StringResource, WidgetNavigator, listTemp, ContentFormatter) {
  10. //NOSONAR: needed for amd
  11. 'use strict'; //NOSONAR: meant to be strict
  12. var GroupListView = View.extend({
  13. items: null,
  14. simpleList: false,
  15. showListHeader: false,
  16. showGroupHeader: false,
  17. showFilter: true,
  18. showAddButton: false,
  19. filterTips: StringResource.get('filterTips'),
  20. listHeader: '',
  21. template: listTemp,
  22. /**
  23. * @constructor
  24. */
  25. init: function init(options) {
  26. GroupListView.inherited('init', this, arguments);
  27. _.extend(this, options);
  28. },
  29. /**
  30. Main entry point - will render the form view
  31. **/
  32. render: function render(items) {
  33. var htmlStr = dot.template(listTemp)({
  34. showFilter: this.showFilter,
  35. showAddButton: this.showAddButton,
  36. addButtonTip: this.addButtonTip
  37. });
  38. this.$el.html(htmlStr);
  39. var deferred = Q.defer();
  40. var self = this;
  41. if (this.showAddButton) {
  42. this.$el.find('#button_add').on('primaryaction', function () {
  43. this.trigger('addItem');
  44. }.bind(this));
  45. }
  46. this._renderListBody(items);
  47. if (this.showFilter) {
  48. MagicWand.searchInput(this.$el).done(function (widgets) {
  49. if (widgets.length === 1) {
  50. this._filterInput = widgets[0];
  51. this._filterInput.options.hint = self.filterTips;
  52. this._filterInput.on('changed', function (e) {
  53. var trimmed = e.text.trim();
  54. if (trimmed.length > 0 || e.text === '') {
  55. var filterStr = e.text;
  56. var filteredItems = _.filter(self.items, function (item) {
  57. var searchTerm = item.title ? item.title : item.defaultName;
  58. return searchTerm.toLowerCase().indexOf(trimmed.toLowerCase()) !== -1;
  59. });
  60. self._renderListBody(filteredItems);
  61. }
  62. });
  63. deferred.resolve();
  64. }
  65. }.bind(this));
  66. } else {
  67. deferred.resolve();
  68. }
  69. return deferred.promise;
  70. },
  71. _getTemplateOptions: function _getTemplateOptions(items) {
  72. var listItems = items || this.items;
  73. var groupedData;
  74. if (items && items.length) {
  75. groupedData = {};
  76. groupedData.simpleList = this.simpleList;
  77. groupedData.showGroupHeader = this.showGroupHeader;
  78. groupedData.showListHeader = this.showListHeader;
  79. groupedData.listHeader = this.listHeader;
  80. var lItems = {
  81. "group": listItems
  82. };
  83. groupedData.items = lItems;
  84. }
  85. return groupedData;
  86. },
  87. _renderListBody: function _renderListBody(items, bClearFilter) {
  88. var htmlStr;
  89. var groupedData = this._getTemplateOptions(items);
  90. if (groupedData) {
  91. htmlStr = dot.template(this.template)(groupedData);
  92. } else {
  93. htmlStr = '<span class="blank-content"><i class="wft_bee"></i><span class="msg">' + StringResource.get('nothingFound') + '</span></span>';
  94. }
  95. this.$el.find('div.bi-admin-listbody').html(htmlStr);
  96. if (bClearFilter) {
  97. this.$el.find('#filterInput').val('');
  98. }
  99. this._bindEvents();
  100. this.widgetKeyController = new WidgetNavigator({
  101. $el: this.$el.find(".groupList-table"),
  102. focusClass: "groupListFocusable",
  103. fCallBack: function fCallBack() {}
  104. });
  105. this._shortenLabelText();
  106. },
  107. _shortenLabelText: function _shortenLabelText() {
  108. $.each(this.$el.find(".admin-grouplist-title"), function (index, value) {
  109. ContentFormatter.middleShortenString(value);
  110. });
  111. this.$el.find(".list-item.bi-admin-hidden").removeClass("bi-admin-hidden");
  112. },
  113. _getItemById: function _getItemById(id) {
  114. var item = _.find(this.items, function (item) {
  115. return id === item.id;
  116. });
  117. return item;
  118. },
  119. _getItemByConnId: function _getItemByConnId(id) {
  120. var item = _.find(this.items, function (item) {
  121. return id === item.connId;
  122. });
  123. return item;
  124. },
  125. _bindEvents: function _bindEvents() {
  126. var $items = this.$el.find('tr.list-item');
  127. var self = this;
  128. $items.on('click', function () {
  129. if ($(this).data('dragging')) {
  130. return;
  131. }
  132. if ($(this).attr('id')) {
  133. var id = $(this).attr('id').trim();
  134. var item = self._getItemById(id);
  135. if (!item) {
  136. item = self._getItemByConnId(id);
  137. }
  138. }
  139. self.trigger('selected', item);
  140. }); // group accordion
  141. var $groupTitles = this.$el.find('a.group-header');
  142. $groupTitles.on('primaryaction', function (e) {
  143. if ($(this).is('.active')) {
  144. $(this).removeClass('active');
  145. $(this).find('.wft').removeClass('wfa_up').addClass('wfa_down');
  146. $(this).next().slideUp(300);
  147. } else {
  148. $(this).addClass('active');
  149. $(this).find('.wft').removeClass('wfa_down').addClass('wfa_up');
  150. $(this).next().slideDown(300);
  151. }
  152. e.preventDefault();
  153. }); // context menu
  154. var $contextMenus = this.$el.find('td .ellipsesButton');
  155. $contextMenus.on('primaryaction', function (e) {
  156. var id = $(this).closest('tr.list-item').attr('id').trim();
  157. var item = self._getItemById(id);
  158. var position = {};
  159. if (e.pageX === undefined || e.gesture === undefined || e.gesture.center === undefined || e.gesture.center.pageX === undefined) {
  160. position = $(e.target).offset();
  161. } else {
  162. position.left = e.pageX || e.gesture.center.pageX;
  163. position.top = e.pageY || e.gesture.center.pageY;
  164. }
  165. self.trigger('showContextMenu', {
  166. data: item,
  167. position: {
  168. "pageX": position.left,
  169. "pageY": position.top
  170. }
  171. });
  172. e.stopPropagation();
  173. return false;
  174. });
  175. },
  176. selectById: function selectById(id) {
  177. var item = this._getItemById(id) || this._getItemByConnId(id);
  178. this.trigger('selected', item);
  179. },
  180. selectByIndex: function selectByIndex(index) {
  181. var item = this.items[index];
  182. this.trigger('selected', item);
  183. },
  184. removeSelectedRowById: function removeSelectedRowById(id) {
  185. var item = _.find(this.$el.find('tr.list-item'), function (item) {
  186. if (item.id === id) {
  187. return item;
  188. }
  189. });
  190. try {
  191. item.removeNode(true);
  192. } catch (err) {
  193. item.remove();
  194. }
  195. if ($('#workPane').is(":visible")) {
  196. $('#workPane').addClass('hide');
  197. }
  198. },
  199. insertCopyRowById: function insertCopyRowById(copyRow, id) {
  200. var copySource = _.find(this.$el.find('tr.list-item'), function (item) {
  201. if (item.id === id) {
  202. return item;
  203. }
  204. });
  205. var targetSource = _.clone(copySource);
  206. targetSource.id = id;
  207. targetSource.insertAfter(copySource);
  208. }
  209. });
  210. return GroupListView;
  211. });