MultipurposeWidget.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2020
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['underscore', './StaticWidget', 'text!./Multipurpose.html', './IconPlaceholderView', './ShortcutPlaceholderView', './TextWidget'], function (_, StaticWidget, MultiPurposeTemplate, IconPlaceholderView, ShortcutPlaceholderView, TextWidget) {
  8. var MultipurposeWidget = StaticWidget.extend({
  9. init: function init(options) {
  10. MultipurposeWidget.inherited('init', this, arguments);
  11. if (options && options.initialConfigJSON && options.initialConfigJSON.placeholder) {
  12. this.placeholder = options.initialConfigJSON.placeholder;
  13. }
  14. },
  15. onContainerReady: function onContainerReady() {
  16. MultipurposeWidget.inherited('onContainerReady', this, arguments);
  17. if (this.model) {
  18. if (this.model.fillColor) {
  19. this.$el.find('div.staticContent').addClass('fill-' + this.model.fillColor);
  20. }
  21. if (this.model.borderColor) {
  22. this.$el.find('div.staticContent').addClass('border-' + this.model.borderColor);
  23. }
  24. }
  25. },
  26. render: function render() {
  27. var _this = this;
  28. this.$el.append(MultiPurposeTemplate);
  29. var viewOptions = { el: this.$el.find('.dropZone'), dashboardApi: this.dashboardApi };
  30. if (this.placeholder) {
  31. _.extend(viewOptions, this.placeholder);
  32. this.multiView = new IconPlaceholderView(viewOptions);
  33. } else {
  34. // shortcut callbacks
  35. _.extend(viewOptions, {
  36. onVisualizationSelected: function onVisualizationSelected() {
  37. _this.replaceWithVis();
  38. },
  39. onTextSelected: function onTextSelected() {
  40. _this.replaceWithText();
  41. },
  42. onImageSelected: function onImageSelected() {
  43. _this.replaceWithImage();
  44. }
  45. });
  46. this.multiView = new ShortcutPlaceholderView(viewOptions);
  47. }
  48. this.multiView.render();
  49. var DndManager = this.dashboardApi.getFeature('DashboardDnd.internal');
  50. DndManager.addDropTarget(this.$el, {
  51. accepts: function accepts(dragObject) {
  52. if (_this.placeholder) {
  53. return _this._isDraggingData(dragObject);
  54. } else {
  55. return _this._isDraggingData(dragObject) || _this._isDraggingWidget(dragObject) || _this._isMovingWidget(dragObject) || _this._isDraggingPin(dragObject) || _this._isDraggingGroup(dragObject);
  56. }
  57. },
  58. onDrop: function onDrop(dragObject) {
  59. _this.$el.find('.dropZone').removeClass('active');
  60. return _this._onDrop(dragObject);
  61. },
  62. onDragEnter: function onDragEnter() {
  63. _this.$el.find('.dropZone').addClass('active');
  64. },
  65. onDragLeave: function onDragLeave() {
  66. _this.$el.find('.dropZone').removeClass('active');
  67. }
  68. });
  69. return this.$el;
  70. },
  71. _isDraggingData: function _isDraggingData() {
  72. var dragObject = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  73. var canvasDnD = this.dashboardApi.getFeature('CanvasDnD');
  74. return canvasDnD.accepts(dragObject, {
  75. fromMultipurposeWidget: true
  76. });
  77. },
  78. _isDraggingWidget: function _isDraggingWidget(dragObject) {
  79. return dragObject.type === 'widget' && dragObject.data && dragObject.data.operation === 'new';
  80. },
  81. _isMovingWidget: function _isMovingWidget(dragObject) {
  82. // Only can move one widget to the placeholder at once
  83. return dragObject.type === 'widget' && dragObject.data && dragObject.data.nodeInfoList && dragObject.data.nodeInfoList.length === 1;
  84. },
  85. _isDraggingPin: function _isDraggingPin(dragObject) {
  86. return dragObject.type === 'pin';
  87. },
  88. _isDraggingGroup: function _isDraggingGroup(dragObject) {
  89. return dragObject.type === 'groupContent';
  90. },
  91. destroy: function destroy() {
  92. if (this.multiView && this.multiView.destroy) {
  93. this.multiView.destroy();
  94. }
  95. if (this.model && this.model.id) {
  96. this.onChromeDeselected();
  97. }
  98. MultipurposeWidget.inherited('destroy', this, arguments);
  99. },
  100. registerEventGroup: function registerEventGroup() {
  101. // Multipurpose widgets are not added to event groups
  102. },
  103. _onDrop: function _onDrop(dragObject) {
  104. if (dragObject.data.operation === 'move') {
  105. return this._moveDrop(dragObject);
  106. } else if (this._isDraggingPin(dragObject) && dragObject.data.operation === 'new') {
  107. return this._onPinDrop(dragObject);
  108. } else if (dragObject.data.operation === 'new' || this._isDraggingData(dragObject)) {
  109. return this._newDrop(dragObject);
  110. }
  111. },
  112. _moveDrop: function _moveDrop(dragObject) {
  113. var transaction = this.dashboardApi.getFeature('Transaction');
  114. var canvas = this.dashboardApi.getCanvas();
  115. var transactionToken = transaction.startTransaction();
  116. var placeholderContent = canvas.getContent(this.id);
  117. var layout = this._getLayoutProperties(placeholderContent);
  118. canvas.removeContent(this.id, transactionToken);
  119. var nodeInfo = dragObject.data.nodeInfoList[0];
  120. var nodeModel = nodeInfo.node._layout.model;
  121. var content = canvas.getContent(nodeModel.id);
  122. this._setLayoutProperties(content, layout, transactionToken);
  123. // Need to move back to canvas as the widget drops to body by dnd manager
  124. canvas.moveContent(content.getContainer().getId(), [content.getId()], transactionToken);
  125. transaction.endTransaction(transactionToken);
  126. },
  127. _onPinDrop: function _onPinDrop(dragObject) {
  128. var pinSpec = dragObject.data.pinSpec;
  129. return this._replaceSelf(pinSpec.content);
  130. },
  131. _newDrop: function _newDrop(dragObject) {
  132. var _this2 = this;
  133. var canvasDnD = this.dashboardApi.getFeature('CanvasDnD');
  134. return canvasDnD.onDrop(dragObject).then(function (newModel) {
  135. return _this2._replaceSelf(newModel);
  136. });
  137. },
  138. _replaceSelf: function _replaceSelf(model) {
  139. var _this3 = this;
  140. var transaction = this.dashboardApi.getFeature('Transaction');
  141. var canvas = this.dashboardApi.getCanvas();
  142. var transactionToken = transaction.startTransaction();
  143. if (this.model) {
  144. if (this.model.fillColor) {
  145. model.fillColor = this.model.fillColor;
  146. }
  147. if (this.model.borderColor) {
  148. model.borderColor = this.model.borderColor;
  149. }
  150. }
  151. var placeholderContent = canvas.getContent(this.id);
  152. var content = {
  153. spec: model,
  154. containerId: this.content.getContainer().getId(),
  155. properties: this._getLayoutProperties(placeholderContent)
  156. };
  157. return canvas.addContent(content, transactionToken).then(function (newContent) {
  158. var state = newContent.getFeature('state');
  159. return state.whenStatusChanges(state.STATUS.RENDERED).then(function () {
  160. var selectedContentList = canvas.getSelectedContentList();
  161. var selectedContentIds = selectedContentList.map(function (item) {
  162. return item.getId();
  163. });
  164. canvas.deselectContent(selectedContentIds);
  165. canvas.removeContent(_this3.id, transactionToken);
  166. canvas.selectContent([newContent.getId()]);
  167. transaction.endTransaction(transactionToken);
  168. });
  169. });
  170. },
  171. _getLayoutProperties: function _getLayoutProperties(content) {
  172. return {
  173. height: content.getPropertyValue('height'),
  174. width: content.getPropertyValue('width'),
  175. left: content.getPropertyValue('left'),
  176. top: content.getPropertyValue('top')
  177. };
  178. },
  179. _setLayoutProperties: function _setLayoutProperties(content, layout, transactionToken) {
  180. content.setPropertyValue('height', layout.height, transactionToken);
  181. content.setPropertyValue('width', layout.width, transactionToken);
  182. content.setPropertyValue('left', layout.left, transactionToken);
  183. content.setPropertyValue('top', layout.top, transactionToken);
  184. },
  185. replaceWithVis: function replaceWithVis() {
  186. return this._replaceSelf({
  187. type: 'live',
  188. visId: 'com.ibm.vis.rave2bundlecolumn',
  189. expandOnRender: true,
  190. usePreferredSize: false
  191. });
  192. },
  193. replaceWithText: function replaceWithText() {
  194. var _this4 = this;
  195. var options = {
  196. style: 'body',
  197. placeholder: {
  198. showIcon: false
  199. }
  200. };
  201. return TextWidget.getDefaultSpec('', options).then(function (defaultSpec) {
  202. return _this4._replaceSelf(defaultSpec.model);
  203. });
  204. },
  205. replaceWithImage: function replaceWithImage() {
  206. return this._replaceSelf({ type: 'image' });
  207. },
  208. resize: function resize() {
  209. if (this.multiView.resize) {
  210. this.multiView.resize();
  211. }
  212. }
  213. });
  214. MultipurposeWidget.getDefaultSpec = function () {
  215. return Promise.resolve({
  216. model: {
  217. type: 'multipurpose',
  218. avatarHtml: MultiPurposeTemplate
  219. },
  220. layoutProperties: {
  221. style: {
  222. width: '240px',
  223. height: '180px'
  224. }
  225. }
  226. });
  227. };
  228. return MultipurposeWidget;
  229. });
  230. //# sourceMappingURL=MultipurposeWidget.js.map