"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: Cognos Analytics * Copyright IBM Corp. 2017,2018 * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', 'bi/admin/nls/StringResource', 'bacontentnav/common/ContentListPageView', 'bi/admin/system/services/TopicUploader', 'bi/commons/ui/properties/PropertyUIControl', 'bi/commons/ui/dialogs/ConfirmationDialog', 'bi/admin/system/services/TopicsListController', 'bi/commons/utils/Downloader'], function (_, StringResource, View, Uploader, PropertyUIControl, ConfirmationDialog, TopicsListController, Downloader) { 'use strict'; //NOSONAR: meant to be strict var CustomTopicTab = View.extend({ loggingConfigUrl: 'v1/glug/config/logging', builtinTopicUrl: 'v1/glug/topics/builtin', customTopicUrl: 'v1/glug/topics/custom', getAddOnContextMenuId: function getAddOnContextMenuId() { return "com.ibm.bi.admin.loggingAddOnTopicListViewMenu"; }, currentCustomSelection: { topicName: '', topicType: 'CUSTOM' }, currentTopic: { topicName: '', topicType: '' }, init: function init(options) { CustomTopicTab.inherited('init', this, arguments); this.showTitle = true; _.extend(this, options); this.topicsController = new TopicsListController({ glassContext: this.glassContext }); if (this.currentHomeValue.topicType === 'CUSTOM') { this.currentCustomSelection = this.currentHomeValue; } }, renderContent: function renderContent() { var aControls = []; this.$el.addClass("adminContentListView"); aControls.push({ 'type': 'SingleLineLinks', 'items': [{ 'align': 'right', 'items': [{ 'type': 'icon', 'svgIcon': 'common-upload', 'clickCallback': this.uploadTopic.bind(this), 'iconTooltip': StringResource.get('uploadTopic') }, { 'type': 'text', 'value': StringResource.get('uploadTopic') }] }] }, { 'module': 'bi/content_apps/ui/RenderCallback', 'renderCallback': this.renderListControl.bind(this), 'el': this.$el }); this._oPropertyUIControl = this._newPropertyUIControl(aControls); return this._oPropertyUIControl.render(); }, _newPropertyUIControl: function _newPropertyUIControl(items) { return new PropertyUIControl({ 'glassContext': this.glassContext, 'el': this.$el, 'items': items }); }, _getSelectedTopic: function _getSelectedTopic() { var selectedDefaultItem = this._listControl.getSelectedObjects()[0]; if (!_.isUndefined(selectedDefaultItem)) { this.currentCustomSelection.topicName = selectedDefaultItem.id; this.currentValue = this.currentCustomSelection; } }, getSelectedObjects: function getSelectedObjects() { var i; var rowObjects = []; var ancestors = this._listControl.contentView.getAncestors(); for (i = 0; i < this.aSelectedRows.length; i += 1) { var obj = this._dTable.fnGetData(this.aSelectedRows[i]); if (!obj[ContentStoreObject.ANCESTORS] && ancestors && ancestors.length > 0) { obj[ContentStoreObject.ANCESTORS] = ancestors; } rowObjects.push(obj); } return rowObjects; }, renderListControl: function renderListControl(container) { if (!_.isUndefined(container)) { this.$container = $(container); } if (_.isUndefined(this.$container)) { this.$container = this.$el; } this.$wrapper = $('
'); this.$container.append(this.$wrapper); this.$wrapper.append(this.$contentBars); return this._renderContentList(); }, _renderContentList: function _renderContentList() { return this.renderContentList({ 'minHeight': 350, '$container': this.$el, 'columns': this._getColumnSpecs(), 'ajaxProp': '', 'getJSONDataCallback': this.topicsController._getAddOnTopicList.bind(this), 'defaultSort': [1, 'asc'], 'multiSelect': false, 'selectedRow': ['name', StringResource.get(this.currentCustomSelection.topicName)], 'disableColumnHeaders': true, 'singleSelectCallback': this.onSelected.bind(this), 'getSelectedObjectWPermissions': function getSelectedObjectWPermissions(selectedObjects) { return Promise.resolve(selectedObjects); }, 'getContextMenuId': this.getAddOnContextMenuId, 'dataTableOptions': { 'iDisplayLength': 100 }, '_showLoadBuffer': 100, 'emptyFolderString': StringResource.get('noCustomTopic') }); }, onSelected: function onSelected() { this._getSelectedTopic(); return this.onSelectCallback(this.currentValue); }, _getContentBarAccesibleLabel: function _getContentBarAccesibleLabel() { return StringResource.get('topics'); }, _getColumnSpecs: function _getColumnSpecs() { return [{ 'type': 'RadioButtons' }, { 'type': 'Text', 'propertyName': 'name', '_bNavigable': true, 'label': StringResource.get('name'), 'scope': 'row' }, { 'type': 'ContextMenu', 'width': '10%', 'module': 'bacontentnav/common/ui/list_columns/ContextMenu' }]; }, _deleteTopic: function _deleteTopic() { var oDialog = new ConfirmationDialog('confirmDelete', StringResource.get('confirmDelete'), StringResource.get('confirmDeleteMessage')); oDialog.confirm(function () { this._getSelectedTopic(); var selectedTopicName = this.currentValue.topicName; this.topicsController.deleteAddOnTopic(selectedTopicName).then(function () { var sText = StringResource.get('topicDeleteMsg', { 'topicName': selectedTopicName }); this.glassContext.appController.showToast(sText, { 'type': 'success' }); this._listControl.removeSelectedRows(); // when the topic is deleted, need to update the selected item to null this.currentValue = null; this.onSelectCallback(this.currentValue); }.bind(this), function (ajaxObj, err) { this.glassContext.appController.showErrorMessage(AJAXUtils.buildErrorMessage(err.responseJSON.errors), StringResource.get('error')); }.bind(this)); }.bind(this)); oDialog.renderContent($('
')); }, uploadTopic: function uploadTopic() { var ajaxOptions = { 'isUpload': true, 'type': 'topic' }; var uploader = new Uploader({ '$el': this.$el, 'glassContext': this.glassContext, 'ajax': this.updateOrUpload.bind(this), 'ajaxOptions': ajaxOptions, 'fileType': 'json' }); return uploader.doUpload().then(); }, updateOrUpload: function updateOrUpload(options) { return this.topicsController._updateOrUpload(options).then(this.refreshList.bind(this)); }, _downloadTopic: function _downloadTopic() { var selectedItem = this._listControl.getSelectedObjects()[0]; if (_.isUndefined(selectedItem)) { //in this case nothing selected so nothing to download return; } else { var name = this._listControl.getSelectedObjects()[0].name; var promise = this.topicsController._getAddOnTopicDescription(name); return promise.then(function (data) { var downloader = new Downloader({ 'url': 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data, 0, 4)), 'name': name + '.json' }); downloader.doDownload(); }); } }, cancel: function cancel() {}, onContextMenu: function onContextMenu(id) { switch (id) { case 'deleteTopic': this._deleteTopic(); break; case 'downloadTopic': this._downloadTopic(); break; default: this.logger.error("Unexpected context menu id for logging list pane: " + id); } }, refreshList: function refreshList() { this._listControl.remove(); this.renderListControl(); } }); return CustomTopicTab; });