"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: Cognos Analytics * Copyright IBM Corp. 2017, 2017 * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', 'bi/commons/ui/core/Class'], function (_, Class) { var BaseCustomizationService = Class.extend({ init: function init(options) { BaseCustomizationService.inherited('init', this, arguments); _.extend(this, options); this._ajaxService = options.glassContext.services.ajax; this._customizations = null; }, /* * Abstract methods */ getCustomizations: function getCustomizations(id) {}, saveCustomizations: function saveCustomizations(id) {}, _getCustomizationsWithDefaults: function _getCustomizationsWithDefaults() {}, _getCustomizations: function _getCustomizations(url) { return Promise.try(function () { if (this._customizations === null) { return this._ajaxGet(url).then(function (result) { this._customizations = result; }.bind(this)); } }.bind(this)).then(this._getCustomizationsWithDefaults.bind(this)); }, _setCustomization: function _setCustomization(id, key, value) { return Promise.try(function () { return this.getCustomizations(id).then(function () { this._customizations[key] = value; return this.saveCustomizations(id); }.bind(this)).then(this._getCustomizationsWithDefaults.bind(this)); }.bind(this)); }, _saveCustomizations: function _saveCustomizations(url) { return this._ajaxPut(url, this._customizations); }, _replaceDefaultsWCustomValues: function _replaceDefaultsWCustomValues(returnValue, customizations, key) { if (returnValue.hasOwnProperty(key) && customizations[key]) { if (key === 'ui_excludedFeatures' && customizations.ui_excludedFeatures.ids) { returnValue.ui_excludedFeatures.ids = customizations.ui_excludedFeatures.ids; } else if (key === 'ui_teamFolders' && customizations.ui_teamFolders.pathRef) { returnValue.ui_teamFolders.pathRef = customizations.ui_teamFolders.pathRef; } else if (key === 'fileUpload_location' && customizations.fileUpload_location) { returnValue.fileUpload_location = customizations.fileUpload_location; } else if (key === 'baseTextDirection') {//do nothing } else { returnValue[key] = customizations[key]; } if (key === 'biDirectionalFeaturesEnabled') { returnValue.baseTextDirection = customizations.baseTextDirection; } } }, setHomePage: function setHomePage(id, homePage) { return this._setCustomization(id, 'ui_homePage', homePage); }, setTheme: function setTheme(id, theme) { return this._setCustomization(id, 'ui_theme', theme); }, setExcludedFeatures: function setExcludedFeatures(id, excludedFeatures) { return this._setCustomization(id, 'ui_excludedFeatures', excludedFeatures); }, setTeamFolders: function setTeamFolders(id, teamFolders) { return this._setCustomization(id, 'ui_teamFolders', teamFolders); }, setCustomSaveLocation: function setCustomSaveLocation(id, customSaveLocation) { return this._setCustomization(id, 'fileUpload_location', customSaveLocation); }, setParameters: function setParameters(id, parameters) { return this._setCustomization(id, 'parameters', parameters); }, setParameterValues: function setParameterValues(id, parameterValues) { return this._setCustomization(id, 'parameter_values', parameterValues); }, resetFeatures: function resetFeatures(id) { return this._setCustomization(id, 'ui_excludedFeatures', { 'ids': [] }); }, setRegionalSettings: function setRegionalSettings(id, regionalSettings) { return this.getCustomizations(id).then(function () { this._customizations.productLocale = regionalSettings.productLocale; this._customizations.contentLocale = regionalSettings.contentLocale; this._customizations.timeZoneID = regionalSettings.timeZoneID; this._customizations.baseTextDirection = regionalSettings.baseTextDirection; this._customizations.biDirectionalFeaturesEnabled = regionalSettings.biDirectionalFeaturesEnabled; return this.saveCustomizations(id); }.bind(this)).then(this._getCustomizationsWithDefaults.bind(this)); }, _getBaseOptions: function _getBaseOptions() { return { contentType: BaseCustomizationService._contentType, dataType: 'json' }; }, _ajaxHelper: function _ajaxHelper(url, method, data) { var options = this._getBaseOptions(); options.method = method; options.url = url; if (data !== undefined) { options.data = JSON.stringify(data); } return this._ajaxService.ajax(options); }, _ajaxGet: function _ajaxGet(url) { return this._ajaxHelper(url, 'GET'); }, _ajaxDelete: function _ajaxDelete(url) { return this._ajaxHelper(url, 'DELETE'); }, _ajaxPut: function _ajaxPut(url, data) { return this._ajaxHelper(url, 'PUT', data); }, _ajaxPost: function _ajaxPost(url, data) { return this._ajaxHelper(url, 'POST', data); }, _fetchGet: function _fetchGet(url) { var options = this._getBaseOptions(); return this.glassContext.services.fetch.get(url, options); }, _fetchPut: function _fetchPut(url, data) { var options = this._getBaseOptions(); options.data = JSON.stringify(data); return this.glassContext.services.fetch.put(url, options); } }); BaseCustomizationService._contentType = 'application/json; charset=utf-8'; return BaseCustomizationService; });