/* * Licensed Materials - Property of IBM * * IBM Cognos Products: SHARE * * (C) Copyright IBM Corp. 2015, 2019 * * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with * IBM Corp. */ define([ 'bi/commons/ui/View', 'bi/sharecommon/utils/translator', 'bi/schedule/app/appControler', 'bi/notifications/app/n10nController' ], function(view, t, controler, n10nController) { var SubscriptionButtonView = view.extend({ type : '', reportId : '', context: {}, isNotificationStateAvailable: false, notificationState: false, onRender: function(context) { this.context = context; if (context.glassContext && context.glassContext.appController && context.glassContext.appController.currentAppView && context.glassContext.appController.currentAppView.defaultContent) { this.type = this._setType(context.glassContext.appController.currentAppView.defaultContent); this.reportId = this._setReportId(context.glassContext.appController.currentAppView.defaultContent); } if (this._isNotifyMeOptionRequired()) { this._getNotificationState(); } }, onSelectItem: function(context) { var id = context.target.itemId; var idTokens = id.split('.'); var key = idTokens[idTokens.length - 1]; if(key === 'subscribe') { // first check if we have access to the selectedContext. If not, show dialog. var contentView = context.glassContext.appController.currentAppView && context.glassContext.appController.currentAppView.currentContentView; var content = contentView && contentView.getContent(); if(content && content.application) { var selectedContext = content.application; // Check application contains the right info var hasStoreID = ( !selectedContext.storeID || selectedContext.storeID == '' )?false:true; if ( hasStoreID && !selectedContext.isModified ) { var descriptor = { name: selectedContext.reportName, reportId: selectedContext.storeID, type: selectedContext.type }; // Check if there are any parameters try { var promptParameters = contentView.getParameterValues && contentView.getParameterValues(); if (!promptParameters) { var parameters = content.promptParameters; if (parameters) { promptParameters = JSON.parse(parameters); } } if ( Array.isArray(promptParameters) && promptParameters.length > 0 ) { descriptor.parameters = promptParameters; } } catch(e) { // eslint-disable-next-line no-mixed-spaces-and-tabs context.glassContext.appController .showErrorMessage(t.translate('error_wrong_subscription_context'), t.translate('error_label_share')); } controler.showSubscriptionPane(context.glassContext, descriptor); } else { context.glassContext.appController .showErrorMessage(t.translate('error_wrong_subscription_context'), t.translate('error_label_share')); } } else { context.glassContext.appController .showErrorMessage(t.translate('error_wrong_subscription_context'), t.translate('error_label_share')); } } else if (key === 'start_notify_me') { var startdescriptor = { reportId: this.reportId, state: true }; this._toggleNotificationState(t.translate('start_notify_me_toast'), startdescriptor); } else if (key === 'stop_notify_me') { var stopdescriptor = { reportId: this.reportId, state: false }; this._toggleNotificationState(t.translate('stop_notify_me_toast'), stopdescriptor); } }, isItemVisible: function(context) { var id = context.target.itemId; var idTokens = id.split('.'); var key = idTokens[idTokens.length-1]; if (key === 'stop_notify_me') { if (this._isNotifyMeOptionRequired() && this.isNotificationStateAvailable && this.notificationState) { // Notification is on, so present option to turn notification off return true; } else { return false; } } else if (key === 'start_notify_me') { if (this._isNotifyMeOptionRequired() && this.isNotificationStateAvailable && !(this.notificationState)) { // Notification is off, so present option to turn notification on return true; } else { return false; } } else if (key === 'subscribe') { if (this._isNotifyMeOptionRequired()) { // Don't show 'Subscribe' option when 'Notify Me' option is presented return false; } else { return true; } } else { // Unknown key return false; } }, _setType: function(defaultContent) { return defaultContent.type ? defaultContent.type : ''; }, _setReportId: function(defaultContent) { if (this.type === 'output' && defaultContent.reportProperties && defaultContent.reportProperties.id) { return defaultContent.reportProperties.id; } else if (defaultContent.id) { return defaultContent.id; } else { return ''; } }, _isNotifyMeOptionRequired: function() { return (this.type === 'output'); }, _getNotificationState: function() { if(this.reportId && this.context) { n10nController.getNotificationState(this.reportId, this.context).then( function(data) { this._setThisNotificationState(data); }.bind(this)).catch( function() { // Unable to retrieve notification state as expected this.isNotificationStateAvailable = false; }.bind(this)); } }, _setThisNotificationState: function(data, message) { if (data && data.state !== null) { this.isNotificationStateAvailable = true; this.notificationState = data.state; if (message) { this.context.glassContext.appController.showToast(message, { 'type': 'info', 'preventDuplicates': false }); } } else { // Unable to resolve notification state as expected this.isNotificationStateAvailable = false; } }, _toggleNotificationState: function(message, descriptor) { if(this.reportId && this.context) { n10nController.updateNotificationState(descriptor, this.context).then( function(data) { this._setThisNotificationState(data, message); }.bind(this)).catch( function() { // Unable to update notification state as expected this.isNotificationStateAvailable = false; }.bind(this)); } } }); return SubscriptionButtonView; });