NavigationPickerView.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. *
  5. * IBM Cognos Products: Dashbaord
  6. *
  7. * (C) Copyright IBM Corp. 2017, 2019
  8. *
  9. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  10. */
  11. define(['../../lib/@waca/core-client/js/core-client/ui/core/View', '../../app/nls/StringResources', 'jquery', 'underscore', '../../lib/@waca/core-client/js/core-client/utils/Utils', './LayoutPickerView', '../../lib/@waca/core-client/js/core-client/ui/KeyCodes', 'text!./templates/NavigationPickerView.html'], function (View, stringResources, $, _, Utils, LayoutPickerView, KeyCodes, template) {
  12. var NavigationPickerView = View.extend({
  13. CUSTOM_TEMPLATE_PAGE_SIZE: 15,
  14. templateString: template,
  15. events: {
  16. 'primaryaction div.templateItem': 'onNavModelTemplateSelect',
  17. 'keydown div.navigationPicker': 'onKeyDown',
  18. 'primaryaction .loadMoreButton': 'onLoadMoreTemplates'
  19. },
  20. $navPicker: null,
  21. init: function init(options) {
  22. NavigationPickerView.inherited('init', this, arguments);
  23. this._navTemplates = options.navTemplates;
  24. this._selectedNavModel = options.defaultTemplate;
  25. this._layoutPickerOptions = options.layoutPickerOptions;
  26. this._dashboardTemplatesApi = options.dashboardTemplatesApi;
  27. this._showContextMenu = options.showContextMenu;
  28. this._customTemplates = options.customTemplates;
  29. if (this._customTemplates && this._customTemplates.length) {
  30. var numInitialCustomTemplates = this._customTemplates.length - this.CUSTOM_TEMPLATE_PAGE_SIZE <= 0 ? this._customTemplates.length : this.CUSTOM_TEMPLATE_PAGE_SIZE;
  31. this._initialCustomTemplates = this._customTemplates.slice(0, numInitialCustomTemplates);
  32. this._hiddenCustomTemplates = this._customTemplates.slice(numInitialCustomTemplates, this._customTemplates.length);
  33. }
  34. this._action = options.action;
  35. },
  36. render: function render() {
  37. this.$el.empty();
  38. var html = this.dotTemplate({
  39. selectTemplateLabel: stringResources.get('selectTemplateLabel'),
  40. loadMoreLabel: stringResources.get('loadMoreLabel'),
  41. templates: this._navTemplates
  42. });
  43. this.$el.html(html);
  44. this.$navPicker = this.$el.find('.navigationPicker');
  45. if (this.layoutPickerView) {
  46. this.layoutPickerView.remove();
  47. }
  48. this.layoutPickerView = new LayoutPickerView({
  49. action: this.onSelectNavModel.bind(this),
  50. templateListing: this._layoutPickerOptions.templateListing,
  51. disableHeaderSection: this._layoutPickerOptions.disableHeaderSection,
  52. disableFooterSection: this._layoutPickerOptions.disableFooterSection,
  53. dashboardTemplatesApi: this._dashboardTemplatesApi,
  54. excludeTemplates: this._layoutPickerOptions.excludeTemplates,
  55. customTemplates: this._initialCustomTemplates,
  56. showContextMenu: this.onShowContextMenu.bind(this)
  57. });
  58. this.layoutPickerView.render();
  59. this.$('.layoutPicker').append(this.layoutPickerView.el);
  60. this._selectTemplate();
  61. // set icon - webfont or svg
  62. this.$el.find('.navIcon').each(function (index, item) {
  63. Utils.setIcon($(item), this._navTemplates[0].navModels[index].icon);
  64. }.bind(this));
  65. },
  66. remove: function remove() {
  67. if (this.layoutPickerView) {
  68. this.layoutPickerView.remove();
  69. this.layoutPickerView = null;
  70. }
  71. NavigationPickerView.inherited('remove', this, arguments);
  72. },
  73. setFocusOnFirstItem: function setFocusOnFirstItem() {
  74. var layoutTemplateItems = this.$el.find('.layoutTemplateItem');
  75. layoutTemplateItems[0].focus();
  76. },
  77. onShowContextMenu: function onShowContextMenu(template, pageX, pageY) {
  78. this._showContextMenu(template, pageX, pageY);
  79. },
  80. getSelectedNavModel: function getSelectedNavModel() {
  81. return this._selectedNavModel;
  82. },
  83. getSelectedLayoutSpec: function getSelectedLayoutSpec() {
  84. return this.layoutPickerView.getSelectedLayoutSpec();
  85. },
  86. onNavModelTemplateSelect: function onNavModelTemplateSelect(event) {
  87. this._selectedNavModel = event.currentTarget.dataset.id;
  88. this._selectTemplate();
  89. },
  90. onDeleteActionDone: function onDeleteActionDone(id) {
  91. this.layoutPickerView.onDeleteActionDone(id);
  92. if (!this._customTemplates || !this._customTemplates.length) {
  93. return;
  94. }
  95. this._customTemplates = this._customTemplates.filter(function (customTemplate) {
  96. return customTemplate.id !== id;
  97. });
  98. this._customTemplates.length === 0 && this._removeCustomTemplatesTab();
  99. },
  100. _removeCustomTemplatesTab: function _removeCustomTemplatesTab() {
  101. var navModels = this._navTemplates && this._navTemplates.length && this._navTemplates[0].navModels;
  102. if (!navModels || !navModels.length) {
  103. return;
  104. }
  105. this._navTemplates[0].navModels = this._navTemplates[0].navModels.filter(function (navModel) {
  106. return navModel.dataId !== 'custom';
  107. });
  108. this._selectedNavModel = this._navTemplates[0].navModels[0].dataId;
  109. this._selectTemplate();
  110. this.render();
  111. },
  112. onKeyDown: function onKeyDown(event) {
  113. var items = this.$navPicker.find('.templateItem');
  114. var focusedItem = this.$navPicker.find('.templateItem[tabindex=0]');
  115. var focusIndex = items.index(focusedItem);
  116. switch (event.keyCode) {
  117. case KeyCodes.DOWN_ARROW:
  118. case KeyCodes.RIGHT_ARROW:
  119. focusIndex++;
  120. if (focusIndex >= items.length) {
  121. focusIndex = items.length - 1;
  122. }
  123. break;
  124. case KeyCodes.UP_ARROW:
  125. case KeyCodes.LEFT_ARROW:
  126. focusIndex--;
  127. if (focusIndex < 0) {
  128. focusIndex = 0;
  129. }
  130. break;
  131. case KeyCodes.HOME:
  132. focusIndex = 0;
  133. break;
  134. case KeyCodes.END:
  135. focusIndex = items.length - 1;
  136. break;
  137. default:
  138. return; //For non-matched keys, stop processing immediately.
  139. }
  140. //Update tabindex and focus
  141. event.preventDefault();
  142. focusedItem.attr('tabindex', -1);
  143. items.eq(focusIndex).attr('tabindex', 0).focus();
  144. },
  145. onSelectNavModel: function onSelectNavModel() {
  146. var _this = this;
  147. return this.getSelectedLayoutSpec().then(function (_ref) {
  148. var layout = _ref.layout,
  149. shim = _ref.shim;
  150. _this._action(layout, shim, _this._selectedNavModel);
  151. });
  152. },
  153. _selectTemplate: function _selectTemplate() {
  154. this.$navPicker.find('.templateItem').attr('aria-checked', false).attr('tabindex', -1).removeClass('selected');
  155. this.$navPicker.find('[data-id=' + this._selectedNavModel + ']').addClass('selected').attr('aria-checked', true).attr('tabindex', 0);
  156. var description = '';
  157. for (var i = 0; i < this._navTemplates[0].navModels.length; i++) {
  158. var template = this._navTemplates[0].navModels[i];
  159. if (template.dataId === this._selectedNavModel && template.description) {
  160. description = template.description;
  161. break;
  162. }
  163. }
  164. this.layoutPickerView.setNavigationTemplate(this._selectedNavModel, description);
  165. this._toggleLoadButton();
  166. },
  167. _toggleLoadButton: function _toggleLoadButton() {
  168. if (this._selectedNavModel === 'custom' && this._hiddenCustomTemplates.length != 0) {
  169. this.$('.loadMoreButton').show();
  170. } else {
  171. this.$('.loadMoreButton').hide();
  172. }
  173. },
  174. onLoadMoreTemplates: function onLoadMoreTemplates() {
  175. var numCustomTemplatesToShow = this._hiddenCustomTemplates.length - this.CUSTOM_TEMPLATE_PAGE_SIZE <= 0 ? this._hiddenCustomTemplates.length : this.CUSTOM_TEMPLATE_PAGE_SIZE;
  176. var customTemplatesToShow = this._hiddenCustomTemplates.slice(0, numCustomTemplatesToShow);
  177. this._hiddenCustomTemplates = this._hiddenCustomTemplates.slice(numCustomTemplatesToShow, this._hiddenCustomTemplates.length);
  178. this._toggleLoadButton();
  179. this.layoutPickerView.addMoreCustomTemplates(customTemplatesToShow);
  180. },
  181. getSelectedTemplate: function getSelectedTemplate() {
  182. return this.$navPicker.find('.templateItem.selected').first();
  183. }
  184. });
  185. return NavigationPickerView;
  186. });
  187. //# sourceMappingURL=NavigationPickerView.js.map