'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * 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. */ /** * * View to swap the Live Widget Visualization through the ODT * Options * { * showTitles: boolean, // Whether to show the titles for the available/recommended visualizations * visualizations: { * recommended: [], // Array of visualizations to place in recommended section * other: [] // Array of visualizations to put in other section * }, * currentVis: String, // The id of the currently selected visualization * widget: Live Widget // The widget that is creating the vischanger view * } * **/ define(['../../../lib/@waca/core-client/js/core-client/ui/core/View', '../../../lib/@waca/core-client/js/core-client/utils/Utils', '../nls/StringResources', 'jquery', 'underscore', 'react-dom', 'react', 'ca-ui-toolkit', 'dashboard-analytics/DynamicFileLoader', 'gemini/app/util/ErrorUtils'], function (BaseView, Utils, resources, $, _, ReactDOM, React, Toolkit, DynamicFileLoader, ErrorUtils) { /* eslint react/prop-types: 0 */ // TODO: Remove once we have prop-types brought in from glass. var Accordion = Toolkit.Accordion; var AccordionItem = Toolkit.AccordionItem; var SVGIcon = Toolkit.SVGIcon; var Label = Toolkit.Label; var SVGIconDecoration = Toolkit.SVGIconDecoration; var ProgressIndicator = Toolkit.ProgressIndicator; // Limit the number of recommended visualizations to show in the recommended section. var MaxRecommendedVisualizationsToShow = 6; var AUTO_ID = 'auto'; /****************************************************** * Recommended Accordion Section * AccordionItem * [PropItems] - SVG (with possible deco) and Label * All visualizations Accordion Section * AccordionItem * [PropItems] - SVG (with possible deco) and Label ********************************************************/ /* * Class to render the react flyout components */ var ReactComponents = function (_React$Component) { _inherits(ReactComponents, _React$Component); /* * The props hold a state that should be used as this classes state. * When the state changes the component gets re-rendered. */ function ReactComponents(props) { _classCallCheck(this, ReactComponents); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.state = _extends({}, props.state); _this.state.checkMarkIcon = props.state.iconsFeature.getIcon('CheckmarkSVG'); return _this; } /** * @param {string} id of the selected visualization * @return {React Component} the decoration if the specified id matched the selected id. */ ReactComponents.prototype.getSelectedDecoration = function getSelectedDecoration(id) { // The decoration is basically a checkmark to show the selected vis. if (this.state.selectedId === id) { return React.createElement(SVGIconDecoration, { iconId: this.state.checkMarkIcon.id, location: 'bottomRight', style: { right: '-4px' } }); } }; /** * @param {object} item describing all the info needed to create a prop item * @param {String} dataType either 'Recommended' or 'Other' * @return {React Object} */ ReactComponents.prototype.renderPropItem = function renderPropItem(item, dataType) { var decoration = this.getSelectedDecoration(item.id); var pressed = this.state.selectedId === item.id ? 'true' : 'false'; return React.createElement( 'div', { role: 'group', className: 'prop-item' }, React.createElement( 'div', { tabIndex: 0, 'data-type': dataType, className: 'prop-icon vis', role: 'button', title: item.name, 'data-id': item.id, 'appcues-data-id': item.id, 'aria-label': item.name, 'aria-pressed': pressed }, React.createElement( SVGIcon, { iconId: this.state.svgsMap[item.id].id, height: 42, width: 42 }, decoration ), React.createElement(Label, { className: 'prop-icon-label', label: item.name }) ) ); }; /** * @param {Array} items all the visualizations to add to the row * @param {Boolean} isRecommended true iff the Vis Row is the recommended row. * @returns {Array} of React components for each Vis in the Vis Row. */ ReactComponents.prototype.loadVisRowItems = function loadVisRowItems(items, isRecommended) { var _this2 = this; var svgs = []; var dataType = isRecommended ? 'Recommended' : 'Other'; items.forEach(function (item) { if (item.id && _this2.state.svgsMap[item.id]) { svgs.push(_this2.renderPropItem(item, dataType)); } }); return svgs; }; /** * Render the progress indicator to show we are loading. */ ReactComponents.prototype._renderLoading = function _renderLoading() { return React.createElement( 'div', { className: 'vis-changer-progressIndicator' }, React.createElement(ProgressIndicator, { size: 'large', variant: 'circle' }) ); }; ReactComponents.prototype._getVisRowItems = function _getVisRowItems(options) { var items = options.items, isRecommended = options.isRecommended; var loadedSvgs = this.loadVisRowItems(items, isRecommended); return React.createElement( 'div', { className: 'visChangerRow' }, loadedSvgs ); }; ReactComponents.prototype._getRecommendedAccordionItem = function _getRecommendedAccordionItem() { var recommendedOptions = { items: this.state.recommended.items, isRecommended: true }; return this.state.recommended.isLoading ? this._renderLoading() : this._getVisRowItems(recommendedOptions); }; ReactComponents.prototype._getAllVisAccordionItem = function _getAllVisAccordionItem() { var allVisOptions = { items: this.state.all.items, isRecommended: false }; return this.state.all.isLoading ? this._renderLoading() : this._getVisRowItems(allVisOptions); }; /** * Render the collapsible sections (accordions) * @return {React Object} representing the Accordion and its sections. */ ReactComponents.prototype._renderAccordions = function _renderAccordions() { var recommendedItem = this._getRecommendedAccordionItem(); var isConsumer = this.state.isConsumer; if (!isConsumer) { return React.createElement( Accordion, null, React.createElement( AccordionItem, { itemName: resources.get('recommended_visualizations'), icon: 'left', open: true }, recommendedItem ), React.createElement( AccordionItem, { itemName: resources.get('all_visualizations'), icon: 'left', open: false, onChange: this.state.onItemChange }, this._getAllVisAccordionItem() ) ); } else { return React.createElement( Accordion, null, React.createElement( AccordionItem, { itemName: resources.get('recommended_visualizations'), icon: 'left', open: true }, recommendedItem ) ); } }; /** * @returns {React Object} progress indicator if we are loading, accordions otherwise. */ ReactComponents.prototype.render = function render() { return this._renderAccordions(); }; return ReactComponents; }(React.Component); /** * The following view is the actual ODT Vis Changer view. It will handle * selection of the react components to change vis type, rendering the different * choices into two collapsible sections as well as showing a loading indicator * until all the SVGs are loaded. */ var View = BaseView.extend({ events: { 'primaryaction .vis': '_selectVis' }, /** * Load the SVG files representing all the visualizations */ _loadSVGFileMap: function _loadSVGFileMap() { var _this3 = this; // If we've already loaded the SVG files, return the map if (this.viewState.svgsMap && _.keys(this.viewState.svgsMap).length > 0) { return Promise.resolve(this.viewState.svgsMap); } else { this.viewState.svgsMap = {}; this._getAllAvailableVisualizations(); if (this.viewState.all && this.viewState.all.items) { var iconsFeature = this.viewState.iconsFeature; this.viewState.all.items.forEach(function (item) { var visIcon = iconsFeature.getIcon(item.visId); _this3.viewState.svgsMap[item.visId] = visIcon; }); if (!(Object.keys(this.viewState.svgsMap).indexOf('auto') !== -1)) { var autoIcon = iconsFeature.getIcon('auto'); this.viewState.svgsMap['auto'] = autoIcon; } return Promise.resolve(this.viewState.svgsMap); } } }, init: function init(options) { // Provide the basic element. this.el = $('