'use strict'; /** * Licensed Materials - Property of IBM * IBM Watson Analytics (C) Copyright IBM Corp. 2018 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * */ define(['../../../lib/@waca/core-client/js/core-client/ui/core/View', '../../../lib/@waca/core-client/js/core-client/utils/ContentFormatter', '../../../lib/@waca/core-client/js/core-client/ui/dialogs/ConfirmationDialog', 'doT', 'underscore', 'jquery', 'text!../../templates/DrillThroughManage.html', '../../../widgets/livewidget/nls/StringResources'], function (View, ContentFormatter, ConfirmationDialog, doT, _, $, Template, StringResources) { //@todo: Endor add support for multiple data sources var _validSourceId = function _validSourceId(sources, id) { if (_.isArray(sources)) { for (var index = 0; index < sources.length; index++) { if (sources[index] === id) { return true; } } } return false; }; /** * Creates a view UI for the manage drill through dialog */ var DrillThroughManageView = View.extend({ templateString: Template, events: { 'primaryaction .edit': '_onEdit', 'primaryaction .delete': '_onDelete', 'primaryaction .visDT-ManageView-add': '_onAddNewTarget' }, init: function init(options) { DrillThroughManageView.inherited('init', this, arguments); options = options || {}; this.handlers = options.handlers; if (!this.handlers) { throw new Error('Handlers must be provided to manage drill through definitions'); } this.content = options.content; this.dashboard = options.dashboard; this.drillThroughModelApi = options.drillThroughModelApi; }, render: function render(bEnableOkButton) { var addAnotherJumpTo = StringResources.get('drillthrough_manageAddAnotherTarget'); var entries = this.drillThroughModelApi.getDrillDefinitionEntries(this.content); var visualizationAPI = this.content.getFeature('Visualization'); var dataSource = visualizationAPI.getDataSource(); var sourceId = dataSource.getId(); var jumpToEntries = []; var iconsFeature = this.dashboard.getFeature('Icons'); var reportIcon = iconsFeature.getIcon('reportIcon'); var dashboardIcon = iconsFeature.getIcon('dashboardIcon'); var storyIcon = iconsFeature.getIcon('storyIcon'); var editIcon = iconsFeature.getIcon('common-edit'); var removeIcon = iconsFeature.getIcon('remove'); var addNewIcon = iconsFeature.getIcon('addNew'); _.each(entries, function (entry) { if (_validSourceId(entry.getModelRefs(), sourceId)) { jumpToEntries.push({ getIcon: function getIcon() { var iconMap = { authoring: reportIcon.id, story: storyIcon.id, dashboard: dashboardIcon.id }; return iconMap[entry.getPerspective()]; }, getId: function getId() { return entry.getId(); }, getName: function getName() { return entry.getName(); }, editJumpTo: StringResources.get('drillthrough_manageJumpPaths_edit', { name: entry.getName() }), removeJumpTo: StringResources.get('drillthrough_manageJumpPaths_delete', { name: entry.getName() }) }); } }); var sHtml = this.dotTemplate({ entries: jumpToEntries, editIcon: editIcon.id, removeIcon: removeIcon.id, addNewIcon: addNewIcon.id, addNewTarget: { getId: function getId() { return 'addNewTarget'; }, getName: function getName() { return addAnotherJumpTo; } } }); this.$el.empty().html(sHtml); this._updateText(); this.handlers.enableOk(!!bEnableOkButton); }, _onEdit: function _onEdit(event) { var id = this._getId(event); return id ? this.handlers.onEdit(this.content, id, this._getEditDrillThroughModelOptions()) : Promise.reject('Invalid drill through target'); }, _onDelete: function _onDelete(event) { var id = this._getId(event); var drillTarget = id ? this.drillThroughModelApi.getDrillDefinitionEntry(id) : null; if (drillTarget) { var confirmDlg = new ConfirmationDialog('confirmDeleteDrillTarget', StringResources.get('drillthrough_manageConfirmeDeleteDrillTargetTitle'), StringResources.get('drillthrough_manageConfirmeDeleteDrillTarget', { name: drillTarget.getName() })); confirmDlg.confirm(function () { this.handlers.onDelete(id, this._getEditDrillThroughModelOptions()); }.bind(this)); } else { throw new Error('Invalid drill through target'); } }, _onAddNewTarget: function _onAddNewTarget() { return this.handlers.onCreate(this.content, this._getEditDrillThroughModelOptions()); }, _getItem: function _getItem(event) { var $target = $(event.currentTarget); return $target.closest('.visDT-ManageView-item-container'); }, _getId: function _getId(event) { var $ancestor = this._getItem(event); return $ancestor.length > 0 ? $ancestor[0].getAttribute('data-id') : null; }, _getEditDrillThroughModelOptions: function _getEditDrillThroughModelOptions() { return { undoRedoOptions: { silent: true }, callback: this.render.bind(this, true) }; }, setFocus: function setFocus() { this.$el.focus(); this._updateText(); }, _updateText: function _updateText() { this.$el.find('.visDT-ManageView-item.visDT-ManageView-item-target').each(function (index, elem) { ContentFormatter.middleShortenString(elem); }); } }); return DrillThroughManageView; }); //# sourceMappingURL=DrillThroughManageView.js.map