ViewsTab.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Cognos Analytics
  5. * Copyright IBM Corp. 2016, 2018
  6. * US Government Users Restricted Rights - Use, duplication or disclosure
  7. * restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['underscore', 'q', 'jquery', 'bi/glass/app/ContentView', 'bi/glass/app/services/ConfigService', 'bi/glass/api/Url', 'bi/admin/nls/StringResource', 'bi/commons/ui/properties/PropertyUIControl', 'bi/admin/multitenancy/services/TenantsCustomizationService'], function (_, Q, $, View, Config, Url, StringResource, PropertyUIControl, TenantsCustomizationService) {
  10. 'use strict'; //NOSONAR: meant to be strict
  11. var ViewsTab = View.extend({
  12. init: function init(options) {
  13. ViewsTab.inherited('init', this, arguments);
  14. $.extend(this, options);
  15. this.glassContext = options.glassContext;
  16. this.tenantsCustomizationService = this._getNewTenantsCustomizationService({
  17. glassContext: this.glassContext
  18. });
  19. this._url = this._getNewUrl();
  20. },
  21. _getNewUrl: function _getNewUrl(options) {
  22. return new Url(options);
  23. },
  24. _getNewTenantsCustomizationService: function _getNewTenantsCustomizationService(options) {
  25. return new TenantsCustomizationService(options);
  26. },
  27. _getNewPropertyUIControl: function _getNewPropertyUIControl(spec) {
  28. return new PropertyUIControl(spec);
  29. },
  30. render: function render() {
  31. var loginProm = this._getDefaultLogin();
  32. var welcomeProm;
  33. if (this.isPortalAdmin && this.tenantID) {
  34. welcomeProm = this.tenantsCustomizationService.getCustomizations(this.tenantID).then(function (resolve) {
  35. var deferred = Q.defer();
  36. deferred.resolve(resolve.ui_homePage);
  37. return deferred.promise;
  38. });
  39. } else {
  40. welcomeProm = this._getDefaultHome();
  41. }
  42. return Promise.all([welcomeProm, loginProm]).then(function (defaults) {
  43. var defaultHome = defaults[0];
  44. var defaultLogin = defaults[1];
  45. var aControls = [];
  46. this.currentHome = defaultHome;
  47. this.currentLogin = defaultLogin;
  48. return this.getHomeViewValue(defaultHome).then(function (homeViewValue) {
  49. this.currentHomeViewValue = homeViewValue;
  50. aControls.push({
  51. 'type': 'SingleLineValue',
  52. 'name': 'welcomePage',
  53. 'label': StringResource.get('homePage'),
  54. 'value': homeViewValue,
  55. 'editCallback': function () {
  56. this.homePageSlideout = this.glassContext.appController.showSlideOut({
  57. parent: this.slideout,
  58. width: '400px',
  59. label: StringResource.get('homePage'),
  60. content: {
  61. module: 'bi/admin/system/HomePerspectiveListView',
  62. glassContext: this.glassContext,
  63. title: StringResource.get('homePage'),
  64. currentValue: this.currentHome,
  65. homeViewValue: this.currentHomeViewValue,
  66. onApplyCallback: this.isPortalAdmin && this.tenantID ? this.applyHomeForTenant.bind(this) : this.applyHome.bind(this),
  67. global: true
  68. }
  69. });
  70. }.bind(this)
  71. });
  72. if (this.isSysAdmin) {
  73. aControls.push({
  74. 'type': 'SingleLineValue',
  75. 'name': 'loginPage',
  76. 'label': StringResource.get('signInPage'),
  77. 'value': defaultLogin,
  78. 'editCallback': function () {
  79. this.loginPageSlideout = this.glassContext.appController.showSlideOut({
  80. parent: this.slideout,
  81. width: '400px',
  82. label: StringResource.get('signInPage'),
  83. content: {
  84. module: 'bi/admin/system/LoginPerspectiveListView',
  85. glassContext: this.glassContext,
  86. title: StringResource.get('signInPage'),
  87. currentValue: this.currentLogin,
  88. onApplyCallback: this.applyLoginPerspective.bind(this)
  89. }
  90. });
  91. }.bind(this)
  92. });
  93. }
  94. this._oPropertyUIControl = this._getNewPropertyUIControl({
  95. 'el': this.$el,
  96. 'glassContext': this.glassContext,
  97. 'items': aControls
  98. });
  99. return this._oPropertyUIControl.render();
  100. }.bind(this));
  101. }.bind(this));
  102. },
  103. getHomeViewValue: function getHomeViewValue(defaultHome) {
  104. return Promise.try(function () {
  105. if (_.isUndefined(defaultHome.content)) {
  106. return defaultHome.perspective;
  107. } else {
  108. return this._getGlobalHomeViewValue(defaultHome);
  109. }
  110. }.bind(this));
  111. },
  112. _getTenantedHomeViewValue: function _getTenantedHomeViewValue(resolve) {
  113. this.tenantsCustomizationService.getCustomizations(this.tenantID).then(function (data) {
  114. resolve(data.ui_homePage);
  115. });
  116. },
  117. _getGlobalHomeViewValue: function _getGlobalHomeViewValue(defaultHome) {
  118. return this._url.getObjInfoFromContent(this.glassContext, defaultHome.content, ['defaultName']).then(function (object) {
  119. return object.defaultName;
  120. }).catch(function (err) {
  121. this.logger.error(err);
  122. return StringResource.get('unavailable');
  123. }.bind(this));
  124. },
  125. applyHomeForTenant: function applyHomeForTenant(selection) {
  126. return this.tenantsCustomizationService.setHomePage(this.tenantID, selection.value).then(function (result) {
  127. this._tenantCustomizations = result;
  128. this.currentHome = selection.value;
  129. this.currentHomeViewValue = selection.name;
  130. this._oPropertyUIControl.getProperty('welcomePage').setValue(selection.name);
  131. this.homePageSlideout.hide();
  132. this.glassContext.appController.showToast(StringResource.get('defaultHomeViewSet'));
  133. }.bind(this), function () {
  134. this.glassContext.appController.showToast(StringResource.get('defaultHomeViewError'), {
  135. type: 'error'
  136. });
  137. }.bind(this));
  138. },
  139. applyHome: function applyHome(selection) {
  140. var defaultHomeObj = {
  141. 'ui_homePage': selection.value
  142. };
  143. return this.glassContext.services.ajax.ajax({
  144. 'contentType': 'application/json',
  145. 'type': 'PUT',
  146. 'url': 'v1/system_profile_settings',
  147. 'data': JSON.stringify(defaultHomeObj)
  148. }).then(function () {
  149. this._oPropertyUIControl.getProperty('welcomePage').setValue(selection.name);
  150. this.currentHome = selection.value;
  151. this.currentHomeViewValue = selection.name;
  152. this.homePageSlideout.hide();
  153. this.glassContext.appController.showToast(StringResource.get('defaultHomeViewSet'));
  154. }.bind(this), function () {
  155. this.glassContext.appController.showToast(StringResource.get('defaultHomeViewError'), {
  156. type: 'error'
  157. });
  158. }.bind(this));
  159. },
  160. applyLoginPerspective: function applyLoginPerspective(selection) {
  161. var defaultLoginObj = {
  162. 'ui_loginPage': selection.name
  163. };
  164. return this.glassContext.services.ajax.ajax({
  165. 'contentType': 'application/json',
  166. 'type': 'PUT',
  167. 'url': 'v1/system_profile_settings',
  168. 'data': JSON.stringify(defaultLoginObj)
  169. }).then(function () {
  170. this._oPropertyUIControl.getProperty('loginPage').setValue(selection.name);
  171. this.currentLogin = selection.name;
  172. this.loginPageSlideout.hide();
  173. this.glassContext.appController.showToast(StringResource.get('defaultLoginViewSet'));
  174. }.bind(this), function () {
  175. this.glassContext.appController.showToast(StringResource.get('defaultLoginViewError'), {
  176. type: 'error'
  177. });
  178. }.bind(this));
  179. },
  180. _getDefaultLogin: function _getDefaultLogin() {
  181. return this.glassContext.services.ajax.ajax({
  182. 'type': 'GET',
  183. 'url': 'v1/system_profile_settings'
  184. }).then(function (response) {
  185. if (!response.ui_loginPage) {
  186. return this.glassContext.services.config.getDefaultLogin();
  187. }
  188. return response.ui_loginPage;
  189. }.bind(this));
  190. },
  191. _getDefaultHome: function _getDefaultHome() {
  192. return this.glassContext.services.ajax.ajax({
  193. 'type': 'GET',
  194. 'url': 'v1/system_profile_settings'
  195. }).then(function (response) {
  196. if (!response.ui_homePage) {
  197. return this.glassContext.services.config.getDefaultHome();
  198. }
  199. return response.ui_homePage;
  200. }.bind(this));
  201. },
  202. remove: function remove() {
  203. ViewsTab.inherited('remove', this, arguments);
  204. this._oPropertyUIControl.remove();
  205. }
  206. });
  207. return ViewsTab;
  208. });