123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- 'use strict';
- /**
- * Licensed Materials - Property of IBM
- * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2020
- * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- */
- define(['underscore', './StaticWidget', 'text!./Multipurpose.html', './IconPlaceholderView', './ShortcutPlaceholderView', './TextWidget'], function (_, StaticWidget, MultiPurposeTemplate, IconPlaceholderView, ShortcutPlaceholderView, TextWidget) {
- var MultipurposeWidget = StaticWidget.extend({
- init: function init(options) {
- MultipurposeWidget.inherited('init', this, arguments);
- if (options && options.initialConfigJSON && options.initialConfigJSON.placeholder) {
- this.placeholder = options.initialConfigJSON.placeholder;
- }
- },
- onContainerReady: function onContainerReady() {
- MultipurposeWidget.inherited('onContainerReady', this, arguments);
- if (this.model) {
- if (this.model.fillColor) {
- this.$el.find('div.staticContent').addClass('fill-' + this.model.fillColor);
- }
- if (this.model.borderColor) {
- this.$el.find('div.staticContent').addClass('border-' + this.model.borderColor);
- }
- }
- },
- render: function render() {
- var _this = this;
- this.$el.append(MultiPurposeTemplate);
- var viewOptions = { el: this.$el.find('.dropZone'), dashboardApi: this.dashboardApi };
- if (this.placeholder) {
- _.extend(viewOptions, this.placeholder);
- this.multiView = new IconPlaceholderView(viewOptions);
- } else {
- // shortcut callbacks
- _.extend(viewOptions, {
- onVisualizationSelected: function onVisualizationSelected() {
- _this.replaceWithVis();
- },
- onTextSelected: function onTextSelected() {
- _this.replaceWithText();
- },
- onImageSelected: function onImageSelected() {
- _this.replaceWithImage();
- }
- });
- this.multiView = new ShortcutPlaceholderView(viewOptions);
- }
- this.multiView.render();
- var DndManager = this.dashboardApi.getFeature('DashboardDnd.internal');
- DndManager.addDropTarget(this.$el, {
- accepts: function accepts(dragObject) {
- if (_this.placeholder) {
- return _this._isDraggingData(dragObject);
- } else {
- return _this._isDraggingData(dragObject) || _this._isDraggingWidget(dragObject) || _this._isMovingWidget(dragObject) || _this._isDraggingPin(dragObject) || _this._isDraggingGroup(dragObject);
- }
- },
- onDrop: function onDrop(dragObject) {
- _this.$el.find('.dropZone').removeClass('active');
- return _this._onDrop(dragObject);
- },
- onDragEnter: function onDragEnter() {
- _this.$el.find('.dropZone').addClass('active');
- },
- onDragLeave: function onDragLeave() {
- _this.$el.find('.dropZone').removeClass('active');
- }
- });
- return this.$el;
- },
- _isDraggingData: function _isDraggingData() {
- var dragObject = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var canvasDnD = this.dashboardApi.getFeature('CanvasDnD');
- return canvasDnD.accepts(dragObject, {
- fromMultipurposeWidget: true
- });
- },
- _isDraggingWidget: function _isDraggingWidget(dragObject) {
- return dragObject.type === 'widget' && dragObject.data && dragObject.data.operation === 'new';
- },
- _isMovingWidget: function _isMovingWidget(dragObject) {
- // Only can move one widget to the placeholder at once
- return dragObject.type === 'widget' && dragObject.data && dragObject.data.nodeInfoList && dragObject.data.nodeInfoList.length === 1;
- },
- _isDraggingPin: function _isDraggingPin(dragObject) {
- return dragObject.type === 'pin';
- },
- _isDraggingGroup: function _isDraggingGroup(dragObject) {
- return dragObject.type === 'groupContent';
- },
- destroy: function destroy() {
- if (this.multiView && this.multiView.destroy) {
- this.multiView.destroy();
- }
- if (this.model && this.model.id) {
- this.onChromeDeselected();
- }
- MultipurposeWidget.inherited('destroy', this, arguments);
- },
- registerEventGroup: function registerEventGroup() {
- // Multipurpose widgets are not added to event groups
- },
- _onDrop: function _onDrop(dragObject) {
- if (dragObject.data.operation === 'move') {
- return this._moveDrop(dragObject);
- } else if (this._isDraggingPin(dragObject) && dragObject.data.operation === 'new') {
- return this._onPinDrop(dragObject);
- } else if (dragObject.data.operation === 'new' || this._isDraggingData(dragObject)) {
- return this._newDrop(dragObject);
- }
- },
- _moveDrop: function _moveDrop(dragObject) {
- var transaction = this.dashboardApi.getFeature('Transaction');
- var canvas = this.dashboardApi.getCanvas();
- var transactionToken = transaction.startTransaction();
- var placeholderContent = canvas.getContent(this.id);
- var layout = this._getLayoutProperties(placeholderContent);
- canvas.removeContent(this.id, transactionToken);
- var nodeInfo = dragObject.data.nodeInfoList[0];
- var nodeModel = nodeInfo.node._layout.model;
- var content = canvas.getContent(nodeModel.id);
- this._setLayoutProperties(content, layout, transactionToken);
- // Need to move back to canvas as the widget drops to body by dnd manager
- canvas.moveContent(content.getContainer().getId(), [content.getId()], transactionToken);
- transaction.endTransaction(transactionToken);
- },
- _onPinDrop: function _onPinDrop(dragObject) {
- var pinSpec = dragObject.data.pinSpec;
- return this._replaceSelf(pinSpec.content);
- },
- _newDrop: function _newDrop(dragObject) {
- var _this2 = this;
- var canvasDnD = this.dashboardApi.getFeature('CanvasDnD');
- return canvasDnD.onDrop(dragObject).then(function (newModel) {
- return _this2._replaceSelf(newModel);
- });
- },
- _replaceSelf: function _replaceSelf(model) {
- var _this3 = this;
- var transaction = this.dashboardApi.getFeature('Transaction');
- var canvas = this.dashboardApi.getCanvas();
- var transactionToken = transaction.startTransaction();
- if (this.model) {
- if (this.model.fillColor) {
- model.fillColor = this.model.fillColor;
- }
- if (this.model.borderColor) {
- model.borderColor = this.model.borderColor;
- }
- }
- var placeholderContent = canvas.getContent(this.id);
- var content = {
- spec: model,
- containerId: this.content.getContainer().getId(),
- properties: this._getLayoutProperties(placeholderContent)
- };
- return canvas.addContent(content, transactionToken).then(function (newContent) {
- var state = newContent.getFeature('state');
- return state.whenStatusChanges(state.STATUS.RENDERED).then(function () {
- var selectedContentList = canvas.getSelectedContentList();
- var selectedContentIds = selectedContentList.map(function (item) {
- return item.getId();
- });
- canvas.deselectContent(selectedContentIds);
- canvas.removeContent(_this3.id, transactionToken);
- canvas.selectContent([newContent.getId()]);
- transaction.endTransaction(transactionToken);
- });
- });
- },
- _getLayoutProperties: function _getLayoutProperties(content) {
- return {
- height: content.getPropertyValue('height'),
- width: content.getPropertyValue('width'),
- left: content.getPropertyValue('left'),
- top: content.getPropertyValue('top')
- };
- },
- _setLayoutProperties: function _setLayoutProperties(content, layout, transactionToken) {
- content.setPropertyValue('height', layout.height, transactionToken);
- content.setPropertyValue('width', layout.width, transactionToken);
- content.setPropertyValue('left', layout.left, transactionToken);
- content.setPropertyValue('top', layout.top, transactionToken);
- },
- replaceWithVis: function replaceWithVis() {
- return this._replaceSelf({
- type: 'live',
- visId: 'com.ibm.vis.rave2bundlecolumn',
- expandOnRender: true,
- usePreferredSize: false
- });
- },
- replaceWithText: function replaceWithText() {
- var _this4 = this;
- var options = {
- style: 'body',
- placeholder: {
- showIcon: false
- }
- };
- return TextWidget.getDefaultSpec('', options).then(function (defaultSpec) {
- return _this4._replaceSelf(defaultSpec.model);
- });
- },
- replaceWithImage: function replaceWithImage() {
- return this._replaceSelf({ type: 'image' });
- },
- resize: function resize() {
- if (this.multiView.resize) {
- this.multiView.resize();
- }
- }
- });
- MultipurposeWidget.getDefaultSpec = function () {
- return Promise.resolve({
- model: {
- type: 'multipurpose',
- avatarHtml: MultiPurposeTemplate
- },
- layoutProperties: {
- style: {
- width: '240px',
- height: '180px'
- }
- }
- });
- };
- return MultipurposeWidget;
- });
- //# sourceMappingURL=MultipurposeWidget.js.map
|