"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(['doT', 'underscore', 'bi/commons/ui/View', 'bi/admin/multitenancy/services/TenantsCustomizationService', 'bi/commons/ui/content/SingleSelectListControl', 'bi/admin/nls/StringResource', 'text!bi/admin/multitenancy/templates/TenantNotifications.html', 'text!bi/commons/ui/template/RadioButton.html', 'bi/commons/ui/properties/Input', 'bi/commons/ui/ButtonBar'], function (dot, _, View, TenantsCustomizationService, SingleSelectListControl, StringResource, TenantNotificationTemplate, RadioButton, Input, ButtonBar) { 'use strict'; //NOSONAR var NotificationsTab = View.extend({ init: function init(options) { NotificationsTab.inherited('init', this, arguments); _.extend(this, options); this.listControl = this._getNewSingleSelectListControl(this.listSpec); this.selectorID = _.uniqueId(); this.tenantsController = this._getNewTenantsCustomizationService({ glassContext: this.glassContext }); this.tenant = this.objectInfo; this._selectedObjectData = null; this.systemDefaultField = null; }, _getNewSingleSelectListControl: function _getNewSingleSelectListControl(options) { return new SingleSelectListControl(options); }, _getNewTenantsCustomizationService: function _getNewTenantsCustomizationService(options) { return new TenantsCustomizationService(options); }, render: function render() { var templateOptions = { options: [{ name: 'defaultSender', label: StringResource.get('defaultSenderEmail') }, { name: 'tenantSender', label: StringResource.get('tenantSenderEmail') }] }; var $Html = $(dot.template(TenantNotificationTemplate)(templateOptions)); for (var i = 0; i < templateOptions.options.length; i++) { $Html.find('.homeSelect.' + templateOptions.options[i].name).append(dot.template(RadioButton)({ label: templateOptions.options[i].label, index: i, id: this.selectorID, controlOnLeft: true })); } this.$el.append($Html); this.$el.find('.homeSelect .roundButton').on('primaryaction', this._handleClickSelect.bind(this)); this.systemDefaultField = this._getNewInput({ $el: this.$el.find('.injectView.defaultSender'), id: 'systemDefault', name: 'systemDefault', ariaLabel: StringResource.get('defaultSenderEmail'), type: 'Input', readOnly: true, inputClass: 'bi-admin-input bi-notification-input', ellipses: false, indent: 4, editable: false }); this.tenantSenderField = this._getNewInput({ $el: this.$el.find('.injectView.tenantSender'), id: 'tenantSender', name: 'tenantSender', ariaLabel: StringResource.get('tenantSenderEmail'), placeHolderText: StringResource.get('tenantSenderPlaceholderText'), type: 'Input', inputClass: 'bi-admin-input bi-notification-input', ellipses: false, indent: 4, editable: false }); this.buttonBar = this._getNewButtonBar({ buttons: [{ label: StringResource.get("apply"), onSelect: this._saveNotificationHandler.bind(this) }] }); var tenantValue; var dfdSysNotificationSettings = this.tenantsController.getSystemNotificationSettings(); var dfdTenantNotificationSettings = this.tenantsController.getTenantNotificationSettings(this.tenant.tenantID); return Promise.all([dfdSysNotificationSettings, dfdTenantNotificationSettings]).then(function (responses) { this.systemDefaultField.value = responses[0].data["NC.defaultSender"]; tenantValue = responses[1].data && responses[1].data.defaultSender ? responses[1].data.defaultSender : ""; this.tenantSenderField.value = tenantValue; return Promise.all([this.systemDefaultField.render(), this.tenantSenderField.render(), this.buttonBar.render()]); }.bind(this)).then(function (results) { this._initSelection(tenantValue); $('input.bi-notification-input').on('focus', this._handleClickSelect.bind(this)); this.$el.find('.standardFooter').html(results[2]); }.bind(this)); }, _getNewInput: function _getNewInput(options) { return new Input(options); }, _getNewButtonBar: function _getNewButtonBar(options) { return new ButtonBar(options); }, _validateInput: function _validateInput(email) { return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email); }, _saveNotificationHandler: function _saveNotificationHandler() { var checked = this.$el.find('.homeSelect.defaultSender .roundButton').hasClass('bi-admin-checked'); var email = checked ? this.systemDefaultField.getValue() : this.tenantSenderField.getValue(); if (this._validateInput(email)) { var senderFieldValue = checked ? '' : this.tenantSenderField.getValue(); var successStrKey = checked ? 'systemNotificationToast' : 'tenantNotificationToast'; var errorStrKey = checked ? 'systemNotificationErrorToast' : 'tenantNotificationErrorToast'; return this.tenantsController.setTenantNotificationSettings(this.tenant.tenantID, senderFieldValue).then(function (response) { this.glassContext.appController.showToast(StringResource.get(successStrKey, { 'email': email })); }.bind(this)).catch(function (ajaxOptions, error) { this.glassContext.appController.showErrorMessage(StringResource.get(errorStrKey)); }.bind(this)); } else { this.glassContext.appController.showToast(StringResource.get('emailInvalidErrorMsg'), { type: 'warning' }); } }, _handleClickSelect: function _handleClickSelect(event) { this._select(event.currentTarget); event.stopPropagation(); return false; }, _select: function _select(rowNode) { var $rowNode = $(rowNode); this._uncheckCurrentSelection(); $rowNode.attr('aria-checked', 'true'); $rowNode.addClass('bi-admin-checked'); if ($rowNode.parent().hasClass('defaultSender')) { this.$el.find('.homeSelect.defaultSender .roundButton').addClass('checked'); this.$el.find('.homeSelect.defaultSender .roundButton circle.inner').css('opacity', '1'); } else if ($rowNode.parent().hasClass('tenantSender') || rowNode.id.indexOf('tenantSender') > -1) { this.$el.find('.homeSelect.tenantSender .roundButton').addClass('checked'); this.$el.find('.homeSelect.tenantSender .roundButton circle.inner').css('opacity', '1'); } if (this._selectionChanged) { this._selectionChanged(); } }, _uncheckCurrentSelection: function _uncheckCurrentSelection() { var $currentFocusedRow = this.$el.find('.homeSelect .roundButton.bi-admin-checked'); $currentFocusedRow.removeClass('bi-admin-checked'); this.$el.find('.homeSelect .roundButton circle.inner').css('opacity', '0'); $currentFocusedRow.attr('aria-checked', 'false'); }, _initSelection: function _initSelection(tenantSender) { var $selectedRadio; if (!tenantSender) { $selectedRadio = this.$el.find('.homeSelect.defaultSender .roundButton'); } else { $selectedRadio = this.$el.find('.homeSelect.tenantSender .roundButton'); } this._select($selectedRadio); }, getCurrentSelection: function getCurrentSelection() { var currentSelection = { 'name': this._selectedObjectData.defaultName, 'value': this.currentValue }; return currentSelection; } }); NotificationsTab.NOT_SUPPORTED_TYPE = 'The object type is not supported: '; return NotificationsTab; });