"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: admin * Copyright IBM Corp. 2015, 2021 * US Government Users Restricted Rights - * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['q', 'bi/admin/common/slideout/BasePane', 'bi/admin/system/services/ApiSvc', 'text!bi/admin/license/templates/LicensePane.html', 'jquery', 'doT', 'bi/commons/ui/properties/PropertyUIControl', 'bi/admin/common/utils/WaitingMask', 'bi/commons/i18n/Formatter', 'bi/admin/nls/StringResource'], function (Q, BasePane, Api, template, $, doT, PropertyUIControl, WaitingMask, Formatter, StringResource) { //NOSONAR 'use strict'; //NOSONAR: meant to be strict var LicensePane = BasePane.extend({ init: function init(options) { LicensePane.inherited('init', this, arguments); $.extend(this, options); Api.glassContext = this.glassContext; }, renderBody: function renderBody() { var deferred = Q.defer(); var envTypePromise = this.glassContext.services.ajax.ajax({ dataType: 'json', type: 'GET', url: 'v1/license' }); var licSummaryPromise = this.glassContext.services.ajax.ajax({ dataType: 'json', type: 'GET', url: 'v1/licenses' }); $.when(envTypePromise, licSummaryPromise).then(function (envTypeResult, licSummaryResult) { this.envType = envTypeResult[0]; this.licenseSummary = this._sortLicenses(licSummaryResult[0]); this._renderPaneBody().then(function () { this.$el.find(".bi-admin-lic-counts-section input.bi-admin-lic-ownedValue").attr("type", "number"); deferred.resolve(this); }.bind(this)); }.bind(this)); return deferred.promise; }, fixButtonLabelling: function fixButtonLabelling() { var buttons = this.$el.find(".arrow.blueArrow.clickable.rightArrow"); buttons.removeAttr("aria-labelledby"); buttons.attr("aria-label", StringResource.get('licenseUsageByUser')); }, _renderPaneBody: function _renderPaneBody() { var deferred = Q.defer(); this.$body.html(doT.template(template)(this._getTemplateData())); this._oPropertyUIControl = new PropertyUIControl({ 'el': this.$body.find('.bi-admin-licenses'), 'glassContext': this.glassContext, 'slideout': this.slideout, 'items': this._getPaneItems() }); this._oPropertyUIControl.render().then(function () { this._disableApplyButton(); this._showLinkIcons(); this.fixButtonLabelling(); deferred.resolve(this); }.bind(this)); return deferred.promise; }, _getApplyButton: function _getApplyButton() { var footerSection = this._oPropertyUIControl.getProperty('footerSection'); return footerSection._oPropertyUIControl.getProperty('applyButton'); }, _disableApplyButton: function _disableApplyButton() {}, _enableApplyButton: function _enableApplyButton() {}, _showLinkIcons: function _showLinkIcons() { this.$body.find('.bi-admin-lic-link .svgIcon').css('visibility', 'visible'); }, _sortLicenses: function _sortLicenses(licensesSumary) { // Sort licenses in descending order of their levels licensesSumary.licenses.sort(function (licenseA, licenseB) { if (licenseA.level > licenseB.level) { return -1; } else if (licenseA.level < licenseB.level) { return 1; } else { return 0; } }); return licensesSumary; }, _getPaneItems: function _getPaneItems() { var items = []; this._addEnvTypeItems(items, this.envType); this._addLicensesUsageHeader(items); this._addLicenseCounts(items); this._addExportRefresh(); this._addPaneFooter(items); return items; }, _addSeparator: function _addSeparator(items, container) { items.push({ 'type': 'Separator', 'el': container }); }, _addEnvTypeItems: function _addEnvTypeItems(items, envType) { var container = this.$body.find('.bi-admin-lic-envtype-section'); items.push({ 'type': 'SectionLabel', 'el': container, 'name': 'envType', 'label': StringResource.get('envType') }); items.push({ 'type': 'RadioButtonGroup', 'el': container, 'name': 'envTypeRadio', 'ariaLabel': StringResource.get('envType'), 'controlOnLeft': true, 'indent': 1, 'value': envType.status, 'items': [{ 'value': 'production', 'label': StringResource.get('production') }, { 'value': 'non-production', 'label': StringResource.get('nonProduction') }], 'onChange': function () { this._enableApplyButton(); }.bind(this) }); }, _addLicensesUsageHeader: function _addLicensesUsageHeader(items) { var container = this.$body.find('.bi-admin-lic-usageheader-section'); items.push({ 'type': 'SectionLabel', 'el': container, 'name': 'globalLicUsage', 'label': StringResource.get('globalLicUsage') }); var descSecEl$ = $(this.$body.find('.bi-admin-lic-description-section')); descSecEl$.attr("aria-label", StringResource.get('globalLicenseDescription')); descSecEl$.text(StringResource.get('licUsageDescription')); }, _getTemplateData: function _getTemplateData() { var totalUsedLicenses = 0; $.each(this.licenseSummary.licenses, function (index, licInfo) { if (licInfo.level !== -1) { totalUsedLicenses += licInfo.used; } }); return { 'strings': { 'licenses': StringResource.get('licenses'), 'used': StringResource.get('usedLicenses'), 'owned': StringResource.get('ownedLicenses'), 'totalLicenses': StringResource.get('totalLicenses'), 'exportText': StringResource.get('export'), 'exportTitle': StringResource.get('exportAsCSV'), 'refreshText': StringResource.get('refresh'), 'refreshTitle': StringResource.get('refreshUsedLic'), 'lastRefreshLabel': StringResource.get('lastRefresh'), 'lastRefreshDateTime': Formatter.format(new Date(this.licenseSummary.asOfTime), { type: 'datetime', formatLength: 'short', locale: this.glassContext.services.userProfile.preferences.productLocale, timezone: this.glassContext.services.userProfile.preferences.timeZoneID }) }, 'licenses': this.licenseSummary.licenses, 'totalUsedLicenses': totalUsedLicenses }; }, _addLicenseCounts: function _addLicenseCounts(items) { var $tdElems = this.$el.find('.bi-admin-lic-separator'); $.each($tdElems, function (index, tdElem) { this._addSeparator(items, tdElem); }.bind(this)); this._addOwnedValues(items); this._addLicenseByUserPaneLinks(items); this._addTotalOwnedValues(items); }, _addLicenseByUserPaneLinks: function _addLicenseByUserPaneLinks(items) { var $tdElems = this.$el.find('.bi-admin-lic-pane-links'); $.each($tdElems, function (index, tdElem) { var licenseInfo = this.licenseSummary.licenses[index]; items.push({ 'type': 'SingleLineValue', 'el': tdElem, 'licenseInfo': licenseInfo, 'label': '', 'ellipses': true, 'licensePane': this, 'editCallback': this._showLicenseByUserPane }); }.bind(this)); }, _addOwnedValues: function _addOwnedValues(items) { var $tdElems = this.$el.find('.bi-admin-lic-ownedCount'); $.each($tdElems, function (index, tdElem) { var licenseInfo = this.licenseSummary.licenses[index]; items.push({ 'type': 'SingleLineLinks', 'el': tdElem, 'name': 'ownedValueLink', 'items': [{ 'align': 'right', 'items': [{ 'type': 'Input', 'name': 'ownedValue_' + licenseInfo.level, 'ellipses': false, 'inputType': 'number', 'inputClass': 'bi-admin-lic-ownedValueInput bi-admin-lic-ownedValue', 'highlightTextOnFocus': false, 'value': licenseInfo.owned, 'onChange': function (propertyId) { if (this._oPropertyUIControl.getProperty(propertyId).value < 0) { this.glassContext.appController.showToast(StringResource.get('licenseInvalidInput'), { 'type': 'warning' }); this._oPropertyUIControl.getProperty(propertyId).setValue(0); } this._onOwnedValueChange(); }.bind(this), 'handleReturnKey': true }, { 'type': 'icon', 'name': 'ownedValueIcon_' + licenseInfo.level, 'svgIcon': 'common-edit', 'class': 'bi-admin-lic-ownedValueIcon' }] }] }); }.bind(this)); $tdElems.hover(this._onHoverIn.bind(this), this._onHoverOut.bind(this)); }, _addTotalOwnedValues: function _addTotalOwnedValues(items) { // Add total owned value as a SingleLineLinks to make it align with // the rest of the owned values in the column var totalOwnedLicenses = 0; $.each(this.licenseSummary.licenses, function (index, licInfo) { if (licInfo.level !== -1) { totalOwnedLicenses += licInfo.owned; } }); var $tdElem = this.$el.find('#bi-admin-lic-totalOwned'); this._addOwnedColumnValue(items, $tdElem, 'totalOwnedLicLink', 'totalOwnedValue', totalOwnedLicenses); }, _addOwnedColumnValue: function _addOwnedColumnValue(items, elem, controlName, rowName, value) { items.push({ 'type': 'SingleLineLinks', 'el': elem, 'name': controlName, 'items': [{ 'align': 'right', 'items': [{ 'type': 'Input', 'name': rowName, 'inputType': 'number', 'ariaLabel': StringResource.get('totalOwnedLicenses'), 'ellipses': false, 'inputClass': 'bi-admin-lic-ownedValue bi-admin-lic-ownedValueInput ba-admin-lic-readOnly', 'value': value, 'readOnly': true }, { 'type': 'icon', 'svgIcon': 'common-edit', 'class': 'bi-admin-lic-ownedValueIcon' }] }] }); }, _editOwnedValue: function _editOwnedValue($el, evt) { var $input = $($el.parent()).find("input.bi-admin-lic-ownedValueInput"); $input.focus(); var $iconEl = this.$el.find('.bi-admin-lic-ownedValueIcon'); $iconEl.css('visibility', 'hidden'); $iconEl.off("primaryaction"); }, _onHoverIn: function _onHoverIn(evt) { var $el = $($(evt.target).find('.bi-admin-lic-ownedValueIcon')); $el.css('visibility', 'visible'); $el.on("primaryaction", this._editOwnedValue.bind(this, $el)); }, _onHoverOut: function _onHoverOut(evt) { var $el = $($(evt.target).find('.bi-admin-lic-ownedValueIcon')); $el.css('visibility', 'hidden'); $el.off("primaryaction"); }, _showLicenseByUserPane: function _showLicenseByUserPane() { var paneTitle = this.licenseInfo.name + ' - ' + StringResource.get('licenseUsageByUser'); this.licensePane.glassContext.appController.showSlideOut({ parent: this.licensePane.slideout, width: '650px', label: paneTitle, content: { module: 'bi/admin/license/slideout/LicenseByUserPane', title: paneTitle, licenseInfo: this.licenseInfo } }); }, _onOwnedValueChange: function _onOwnedValueChange() { this._enableApplyButton(); var totalOwnedLicenses = 0; for (var i = 0; i < this.licenseSummary.licenses.length; i++) { if (this.licenseSummary.licenses[i].level === -1) { continue; } totalOwnedLicenses += this._oPropertyUIControl.getProperty('ownedValue_' + i).value; } var totalOwnedLink = this._oPropertyUIControl.getProperty('totalOwnedLicLink'); var totalOwnedValue = totalOwnedLink._oPropertyUIControl.getProperty('totalOwnedValue'); totalOwnedValue.setValue(totalOwnedLicenses); }, _addExportRefresh: function _addExportRefresh() { this.$el.find('.bi-admin-lic-refresh-section').on('primaryaction', function () { WaitingMask.show(this.$body.find('.bi-admin-licenses'), StringResource.get('refreshingLicUsed')); this._refreshLicenseSummary(); }.bind(this)); }, _addPaneFooter: function _addPaneFooter(items) { var container = this.$body.find('.bi-admin-lic-footer-section'); items.push({ 'type': 'Footer', 'el': container, 'name': 'footerSection', 'items': [{ 'type': 'Button', 'name': 'applyButton', 'label': StringResource.get('apply'), 'primary': true, 'onSelect': this._onApplyButton.bind(this) }, { 'type': 'Button', 'name': 'cancelButton', 'label': StringResource.get('cancel'), 'primary': false, 'onSelect': function () { this.slideout.hide(); }.bind(this) }] }); }, _onApplyButton: function _onApplyButton() { var envTypeSavePromise = this._saveEnvType(); var ownedLicSavePromise = this._saveOwnedLicenses(); return Q.when(envTypeSavePromise, ownedLicSavePromise).then(function () { this._disableApplyButton(); }.bind(this)); }, _saveEnvType: function _saveEnvType() { var deferred = Q.defer(); var modifiedProperties = this._oPropertyUIControl.getModifiedProperties(); if (modifiedProperties.envTypeRadio) { deferred = Api.updateProductionSetting({ 'status': modifiedProperties.envTypeRadio }); } else { deferred.resolve(); } return deferred.promise; }, _saveOwnedLicenses: function _saveOwnedLicenses() { var deferred = Q.defer(); var modifiedProperties = this._oPropertyUIControl.getModifiedProperties(); var ownedValuesModified = !_.isUndefined(modifiedProperties.ownedValue_0) || !_.isUndefined(modifiedProperties.ownedValue_1) || !_.isUndefined(modifiedProperties.ownedValue_2) || !_.isUndefined(modifiedProperties.ownedValue_3); if (ownedValuesModified) { WaitingMask.show(this.$body.find('.bi-admin-licenses'), StringResource.get('refreshingLicUsed')); var ownedValues = []; for (var i = 0; i < this.licenseSummary.licenses.length; i++) { if (this.licenseSummary.licenses[i].level === -1) { continue; } ownedValues.push({ 'level': i, 'owned': this._oPropertyUIControl.getProperty('ownedValue_' + i).value }); } this.glassContext.services.ajax.ajax({ dataType: 'json', contentType: 'application/json; charset=utf-8', type: 'PUT', data: JSON.stringify({ 'licenses': ownedValues }), url: 'v1/licenses/owned' }).then(this._refreshLicenseSummary.bind(this)).then(function () { deferred.resolve(); }); } else { deferred.resolve(); } return deferred.promise; }, _refreshLicenseSummary: function _refreshLicenseSummary() { var deferred = Q.defer(); this.glassContext.services.ajax.ajax({ dataType: 'json', contentType: 'application/json; charset=utf-8', type: 'POST', url: 'v1/licenses' }).then(function (licSummary) { // Make sure the progress shows long enough to be able to see the message setTimeout(function () { WaitingMask.clear(this.$body.find('.bi-admin-licenses')); this.$body.find('.bi-admin-licenses').empty(); if (licSummary.licenses) { this.licenseSummary = this._sortLicenses(licSummary); } this._renderPaneBody().then(function () { deferred.resolve(this); }.bind(this)); }.bind(this), 1000); }.bind(this)); return deferred.promise; } }); return LicensePane; });