/* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Content Explorer *| (C) Copyright IBM Corp. 2017, 2018 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define([ 'bacontentnav/lib/@waca/core-client/js/core-client/ui/properties/PropertyPageView', 'bi/content_apps/nls/StringResource', 'bacontentnav/utils/ContentStoreObject', 'bacontentnav/utils/UIHelper', 'underscore' ], function(PropertyPageView, StringResource, ContentStoreObject, UIHelper, _) { 'use strict'; var TenantSlideout = PropertyPageView.extend({ init: function(options) { TenantSlideout.inherited('init', this, arguments); _.extend(this, options); }, render: function() { return this.renderPropertyUIControl({ 'glassContext': this.glassContext, 'el': this.$el, 'items': [{ 'name': 'tenantsBanner', 'value': StringResource.get('tenantSettings'), 'type': 'Banner', 'centerLabel': true, 'backButton': true, 'editable': false, 'readOnly': true, 'onClose': this._onClose.bind(this) }, { 'name': 'tenantsDropDown', 'type': 'DropDown', 'label': StringResource.get('tenant'), 'options': this._buildTenantsList(), 'style': 'max-width:200px;' }, { 'type': 'SingleLineLinks', 'name': 'tenantWarning', 'indent': 2, 'items': [{ 'align': 'left', 'items': [{ 'type': 'icon', 'svgIcon': 'common-warning', 'class': 'tenantWarningIcon' }, { 'type': 'text', 'value': StringResource.get('changeTenantWarning'), 'class': 'tenantWarningText' }] }] }, { 'type': 'Footer', 'items': [{ 'type': 'Button', 'label': StringResource.get('applyLabel'), 'onSelect': this._onApplyClick.bind(this), 'primary': true }, { 'type': 'Button', 'label': StringResource.get('clear'), 'onSelect': this._onClearClick.bind(this), 'primary': false }] }] }); }, // override base functionality of PropertyPageView which closes the slideout _canClose: function() { return true; }, _onClose: function() { this.slideout.hide(); }, _buildTenantsList: function() { var tenants = this.glassContext.getCoreSvc('.UserProfile').tenants; var tenantsList = [{ label: StringResource.get('none'), value: null, selected: this.currentTenantID === '' }]; for (var item in tenants) { if (tenants.hasOwnProperty(item)) { tenantsList.push({ 'label': tenants[item].defaultName, 'value': tenants[item].tenantID, 'selected': tenants[item].tenantID === this.currentTenantID }); } } // Sort tenants by label. tenantsList.sort(function(a, b) { return UIHelper.getCollator().compare(a.label.toLowerCase(), b.label.toLowerCase()); }); return tenantsList; }, _getTenantNameByID: function(tenantID) { var tenants = this.glassContext.getCoreSvc('.UserProfile').tenants; for (var item in tenants) { if (tenants[item].tenantID === tenantID) { return tenants[item].defaultName; } } return tenantID; }, _onApplyClick: function() { var props = this.getModifiedProperties(); if (props.tenantsDropDown === '') { this._onClearClick(); } else if (props.tenantsDropDown !== undefined) { this._saveTenant(props.tenantsDropDown); } }, _onClearClick: function() { this.glassContext.appController.showMessage(StringResource.get('confirmClearTenantText'), StringResource.get('confirmClearTenant'), 'info', ['ok', 'cancel'], '400px', function(evt) { if (evt.btn === 'ok') { this._clearTenant(); } }.bind(this), true); }, _saveTenant: function(tenantID) { var data = { 'tenantID': tenantID, 'type': this.objectInfo.type }; return this._sendUpdateRequest(data).then(function() { this.slideout.hide(); if (_.isFunction(this.onChangeCallback)) { this.onChangeCallback(tenantID); } this.glassContext.appController.showToast(StringResource.get('tenantSet')); }.bind(this), function() { this.glassContext.appController.showToast(StringResource.get('tenantSetError'), { type: 'error' }); }.bind(this)); }, _clearTenant: function() { var data = { 'tenantID': '', 'type': this.objectInfo.type }; return this._sendUpdateRequest(data).then(function() { this.slideout.hide(); if (_.isFunction(this.onChangeCallback)) { this.onChangeCallback(''); } this.glassContext.appController.showToast(StringResource.get('tenantClear')); }.bind(this), function() { this.glassContext.appController.showToast(StringResource.get('tenantClearError'), { type: 'error' }); }.bind(this)); }, _sendUpdateRequest: function(data) { var jsonData = JSON.stringify(data); var url = ContentStoreObject.getSelfLink(this.objectInfo); url += '?updateTenantIDRecursive=true'; var options = { 'headers': { 'Accept': 'application/json', 'Content-Type': 'application/json' }, 'type': 'PUT', 'url': url, 'data': jsonData }; return this.glassContext.getCoreSvc('.Ajax').ajax(options); }, getModifiedProperties: function() { return this.getPropertyUIControl().getModifiedProperties(); } }); return TenantSlideout; });