Toolbar.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Dashboard (C) Copyright IBM Corp. 2016, 2020
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['jquery', 'underscore', '../DynamicFileLoader', '../lib/@waca/core-client/js/core-client/ui/core/View' //TODO change to non lib version
  8. ], function ($, _, DynamicFileLoader, View) {
  9. /**
  10. * Creates an application bar to serve as a main menu bar
  11. */
  12. var Toolbar = View.extend({
  13. moduleMap: [],
  14. addItemsReady: null,
  15. init: function init(options) {
  16. Toolbar.inherited('init', this, arguments);
  17. this.options = !this.options ? {} : this.options;
  18. _.extend(this.options, options);
  19. this.items = options.items;
  20. this.itemMap = {};
  21. this._readyArray = [];
  22. this._reactToolbarContainer = null;
  23. this._isReactToolbar = this._canRenderInReactToolbar(options.items);
  24. },
  25. /**
  26. * Adds an item to the toolbar
  27. *
  28. * Returns a deferred object that will be resolve once the rendered item is added to the toolbar
  29. */
  30. addItem: function addItem(itemSpec) {
  31. return this._addItem(itemSpec);
  32. },
  33. _addItem: function _addItem(itemSpec) {
  34. // Need to add the item to the bar before requiring a module
  35. var $item = $('<div>');
  36. this.$el.append($item);
  37. return this._renderItem(itemSpec, $item);
  38. },
  39. _renderItem: function _renderItem(itemSpec, $item) {
  40. if (!itemSpec.type && !itemSpec.module) {
  41. itemSpec.type = 'Menu';
  42. }
  43. var module = itemSpec.type ? 'ui/toolbar_components/' + itemSpec.type : itemSpec.module;
  44. return this._renderItemHelper(module, itemSpec, $item);
  45. },
  46. _renderItemHelper: function _renderItemHelper(module, itemSpec, $item) {
  47. var _this = this;
  48. var promise = DynamicFileLoader.load([module]).then(function (modules) {
  49. var Item = modules[0];
  50. itemSpec.el = $item;
  51. var item = new Item(itemSpec, _this.rootMenu || _this.parentView, _this);
  52. _this.itemMap[item.name] = item;
  53. return Promise.resolve().then(item.render.bind(item));
  54. });
  55. this._readyArray.push(promise);
  56. return promise;
  57. },
  58. /**
  59. * Removes an item from the toolbar
  60. */
  61. removeItem: function removeItem(itemName) {
  62. this.itemMap[itemName].remove();
  63. delete this.itemMap[itemName];
  64. },
  65. /**
  66. * If any item is of type SubView, we want to create the toolbar in the old way.
  67. * React toolbar does not support SubViews
  68. */
  69. _canRenderInReactToolbar: function _canRenderInReactToolbar() {
  70. var itemSpecs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  71. var isSupportedInReactToolbar = function isSupportedInReactToolbar(spec) {
  72. return spec.type !== 'SubView';
  73. };
  74. return this.options.reactToolbar && itemSpecs.every(isSupportedInReactToolbar);
  75. },
  76. /**
  77. * Add an array of items to the toolbar
  78. * @param itemSpecs
  79. *
  80. * Returns a deferred object that will be resolved when all items are rendered and added to the toolbar
  81. */
  82. addItems: function addItems() {
  83. var _this2 = this;
  84. var itemSpecs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  85. this._isReactToolbar = this._canRenderInReactToolbar(itemSpecs);
  86. if (this._isReactToolbar) {
  87. this.addItemsReady = DynamicFileLoader.load(['ui/react_toolbar/ReactToolbarContainer']).then(function (modules) {
  88. var ReactToolbarContainer = modules[0];
  89. _this2._reactToolbarContainer = new ReactToolbarContainer(itemSpecs, _this2.el, _this2);
  90. _this2.itemMap = itemSpecs; // TODO: consumers are expecting an item name:view map and not an array of specs
  91. });
  92. } else {
  93. this.addItemsReady = Promise.all(itemSpecs.map(function (itemSpec) {
  94. return _this2._addItem(itemSpec);
  95. }));
  96. }
  97. return this.addItemsReady;
  98. },
  99. clearItems: function clearItems() {
  100. _.each(this.itemMap, function (item) {
  101. if (item.remove) {
  102. item.remove();
  103. }
  104. });
  105. if (this._isReactToolbar) {
  106. if (this._reactToolbarContainer) {
  107. this._reactToolbarContainer.clearItems();
  108. this._reactToolbarContainer = null;
  109. }
  110. } else {
  111. this.$el.empty();
  112. }
  113. this.itemMap = {};
  114. this._readyArray = [];
  115. },
  116. /**
  117. * Draws the Toolbar
  118. */
  119. render: function render() {
  120. this.$el.empty();
  121. if (this.items) {
  122. return this.addItems(this.items);
  123. }
  124. return Promise.resolve();
  125. },
  126. ready: function ready() {
  127. var _this3 = this;
  128. if (this._isReactToolbar) {
  129. return this.addItemsReady.then(function () {
  130. var orientation = _this3.options.preferredVertical ? 'vertical' : 'horizontal';
  131. orientation = _this3.placement === 'top' ? 'horizontal' : orientation;
  132. return _this3._reactToolbarContainer.ready(orientation);
  133. });
  134. } else {
  135. return Promise.all(this._readyArray);
  136. }
  137. },
  138. setFocus: function setFocus() {
  139. var first = Object.keys(this.itemMap)[0];
  140. if (first) {
  141. var focusElement = this.itemMap[first];
  142. if (focusElement && focusElement.setFocus) {
  143. focusElement.setFocus();
  144. }
  145. }
  146. },
  147. updateButtons: function updateButtons() {
  148. if (this.itemMap) {
  149. _.each(this.itemMap, function (item) {
  150. if (item.update) {
  151. item.render();
  152. }
  153. });
  154. }
  155. },
  156. remove: function remove() {
  157. this.clearItems();
  158. Toolbar.inherited('remove', this, arguments);
  159. }
  160. });
  161. return Toolbar;
  162. });
  163. //# sourceMappingURL=Toolbar.js.map