BaseCustomizationService.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Cognos Analytics
  5. * Copyright IBM Corp. 2017, 2017
  6. * US Government Users Restricted Rights - Use, duplication or disclosure
  7. * restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['underscore', 'bi/commons/ui/core/Class'], function (_, Class) {
  10. var BaseCustomizationService = Class.extend({
  11. init: function init(options) {
  12. BaseCustomizationService.inherited('init', this, arguments);
  13. _.extend(this, options);
  14. this._ajaxService = options.glassContext.services.ajax;
  15. this._customizations = null;
  16. },
  17. /*
  18. * Abstract methods
  19. */
  20. getCustomizations: function getCustomizations(id) {},
  21. saveCustomizations: function saveCustomizations(id) {},
  22. _getCustomizationsWithDefaults: function _getCustomizationsWithDefaults() {},
  23. _getCustomizations: function _getCustomizations(url) {
  24. return Promise.try(function () {
  25. if (this._customizations === null) {
  26. return this._ajaxGet(url).then(function (result) {
  27. this._customizations = result;
  28. }.bind(this));
  29. }
  30. }.bind(this)).then(this._getCustomizationsWithDefaults.bind(this));
  31. },
  32. _setCustomization: function _setCustomization(id, key, value) {
  33. return Promise.try(function () {
  34. return this.getCustomizations(id).then(function () {
  35. this._customizations[key] = value;
  36. return this.saveCustomizations(id);
  37. }.bind(this)).then(this._getCustomizationsWithDefaults.bind(this));
  38. }.bind(this));
  39. },
  40. _saveCustomizations: function _saveCustomizations(url) {
  41. return this._ajaxPut(url, this._customizations);
  42. },
  43. _replaceDefaultsWCustomValues: function _replaceDefaultsWCustomValues(returnValue, customizations, key) {
  44. if (returnValue.hasOwnProperty(key) && customizations[key]) {
  45. if (key === 'ui_excludedFeatures' && customizations.ui_excludedFeatures.ids) {
  46. returnValue.ui_excludedFeatures.ids = customizations.ui_excludedFeatures.ids;
  47. } else if (key === 'ui_teamFolders' && customizations.ui_teamFolders.pathRef) {
  48. returnValue.ui_teamFolders.pathRef = customizations.ui_teamFolders.pathRef;
  49. } else if (key === 'fileUpload_location' && customizations.fileUpload_location) {
  50. returnValue.fileUpload_location = customizations.fileUpload_location;
  51. } else if (key === 'baseTextDirection') {//do nothing
  52. } else {
  53. returnValue[key] = customizations[key];
  54. }
  55. if (key === 'biDirectionalFeaturesEnabled') {
  56. returnValue.baseTextDirection = customizations.baseTextDirection;
  57. }
  58. }
  59. },
  60. setHomePage: function setHomePage(id, homePage) {
  61. return this._setCustomization(id, 'ui_homePage', homePage);
  62. },
  63. setTheme: function setTheme(id, theme) {
  64. return this._setCustomization(id, 'ui_theme', theme);
  65. },
  66. setExcludedFeatures: function setExcludedFeatures(id, excludedFeatures) {
  67. return this._setCustomization(id, 'ui_excludedFeatures', excludedFeatures);
  68. },
  69. setTeamFolders: function setTeamFolders(id, teamFolders) {
  70. return this._setCustomization(id, 'ui_teamFolders', teamFolders);
  71. },
  72. setCustomSaveLocation: function setCustomSaveLocation(id, customSaveLocation) {
  73. return this._setCustomization(id, 'fileUpload_location', customSaveLocation);
  74. },
  75. setParameters: function setParameters(id, parameters) {
  76. return this._setCustomization(id, 'parameters', parameters);
  77. },
  78. setParameterValues: function setParameterValues(id, parameterValues) {
  79. return this._setCustomization(id, 'parameter_values', parameterValues);
  80. },
  81. resetFeatures: function resetFeatures(id) {
  82. return this._setCustomization(id, 'ui_excludedFeatures', {
  83. 'ids': []
  84. });
  85. },
  86. setRegionalSettings: function setRegionalSettings(id, regionalSettings) {
  87. return this.getCustomizations(id).then(function () {
  88. this._customizations.productLocale = regionalSettings.productLocale;
  89. this._customizations.contentLocale = regionalSettings.contentLocale;
  90. this._customizations.timeZoneID = regionalSettings.timeZoneID;
  91. this._customizations.baseTextDirection = regionalSettings.baseTextDirection;
  92. this._customizations.biDirectionalFeaturesEnabled = regionalSettings.biDirectionalFeaturesEnabled;
  93. return this.saveCustomizations(id);
  94. }.bind(this)).then(this._getCustomizationsWithDefaults.bind(this));
  95. },
  96. _getBaseOptions: function _getBaseOptions() {
  97. return {
  98. contentType: BaseCustomizationService._contentType,
  99. dataType: 'json'
  100. };
  101. },
  102. _ajaxHelper: function _ajaxHelper(url, method, data) {
  103. var options = this._getBaseOptions();
  104. options.method = method;
  105. options.url = url;
  106. if (data !== undefined) {
  107. options.data = JSON.stringify(data);
  108. }
  109. return this._ajaxService.ajax(options);
  110. },
  111. _ajaxGet: function _ajaxGet(url) {
  112. return this._ajaxHelper(url, 'GET');
  113. },
  114. _ajaxDelete: function _ajaxDelete(url) {
  115. return this._ajaxHelper(url, 'DELETE');
  116. },
  117. _ajaxPut: function _ajaxPut(url, data) {
  118. return this._ajaxHelper(url, 'PUT', data);
  119. },
  120. _ajaxPost: function _ajaxPost(url, data) {
  121. return this._ajaxHelper(url, 'POST', data);
  122. },
  123. _fetchGet: function _fetchGet(url) {
  124. var options = this._getBaseOptions();
  125. return this.glassContext.services.fetch.get(url, options);
  126. },
  127. _fetchPut: function _fetchPut(url, data) {
  128. var options = this._getBaseOptions();
  129. options.data = JSON.stringify(data);
  130. return this.glassContext.services.fetch.put(url, options);
  131. }
  132. });
  133. BaseCustomizationService._contentType = 'application/json; charset=utf-8';
  134. return BaseCustomizationService;
  135. });