TenantSlideoutView.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Content Explorer
  5. *| (C) Copyright IBM Corp. 2017, 2018
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or disclosure
  8. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *+------------------------------------------------------------------------+
  10. */
  11. define([
  12. 'bacontentnav/lib/@waca/core-client/js/core-client/ui/properties/PropertyPageView',
  13. 'bi/content_apps/nls/StringResource',
  14. 'bacontentnav/utils/ContentStoreObject',
  15. 'bacontentnav/utils/UIHelper',
  16. 'underscore'
  17. ], function(PropertyPageView, StringResource, ContentStoreObject, UIHelper, _) {
  18. 'use strict';
  19. var TenantSlideout = PropertyPageView.extend({
  20. init: function(options) {
  21. TenantSlideout.inherited('init', this, arguments);
  22. _.extend(this, options);
  23. },
  24. render: function() {
  25. return this.renderPropertyUIControl({
  26. 'glassContext': this.glassContext,
  27. 'el': this.$el,
  28. 'items': [{
  29. 'name': 'tenantsBanner',
  30. 'value': StringResource.get('tenantSettings'),
  31. 'type': 'Banner',
  32. 'centerLabel': true,
  33. 'backButton': true,
  34. 'editable': false,
  35. 'readOnly': true,
  36. 'onClose': this._onClose.bind(this)
  37. }, {
  38. 'name': 'tenantsDropDown',
  39. 'type': 'DropDown',
  40. 'label': StringResource.get('tenant'),
  41. 'options': this._buildTenantsList(),
  42. 'style': 'max-width:200px;'
  43. }, {
  44. 'type': 'SingleLineLinks',
  45. 'name': 'tenantWarning',
  46. 'indent': 2,
  47. 'items': [{
  48. 'align': 'left',
  49. 'items': [{
  50. 'type': 'icon',
  51. 'svgIcon': 'common-warning',
  52. 'class': 'tenantWarningIcon'
  53. }, {
  54. 'type': 'text',
  55. 'value': StringResource.get('changeTenantWarning'),
  56. 'class': 'tenantWarningText'
  57. }]
  58. }]
  59. }, {
  60. 'type': 'Footer',
  61. 'items': [{
  62. 'type': 'Button',
  63. 'label': StringResource.get('applyLabel'),
  64. 'onSelect': this._onApplyClick.bind(this),
  65. 'primary': true
  66. }, {
  67. 'type': 'Button',
  68. 'label': StringResource.get('clear'),
  69. 'onSelect': this._onClearClick.bind(this),
  70. 'primary': false
  71. }]
  72. }]
  73. });
  74. },
  75. // override base functionality of PropertyPageView which closes the slideout
  76. _canClose: function() {
  77. return true;
  78. },
  79. _onClose: function() {
  80. this.slideout.hide();
  81. },
  82. _buildTenantsList: function() {
  83. var tenants = this.glassContext.getCoreSvc('.UserProfile').tenants;
  84. var tenantsList = [{
  85. label: StringResource.get('none'),
  86. value: null,
  87. selected: this.currentTenantID === ''
  88. }];
  89. for (var item in tenants) {
  90. if (tenants.hasOwnProperty(item)) {
  91. tenantsList.push({
  92. 'label': tenants[item].defaultName,
  93. 'value': tenants[item].tenantID,
  94. 'selected': tenants[item].tenantID === this.currentTenantID
  95. });
  96. }
  97. }
  98. // Sort tenants by label.
  99. tenantsList.sort(function(a, b) {
  100. return UIHelper.getCollator().compare(a.label.toLowerCase(), b.label.toLowerCase());
  101. });
  102. return tenantsList;
  103. },
  104. _getTenantNameByID: function(tenantID) {
  105. var tenants = this.glassContext.getCoreSvc('.UserProfile').tenants;
  106. for (var item in tenants) {
  107. if (tenants[item].tenantID === tenantID) {
  108. return tenants[item].defaultName;
  109. }
  110. }
  111. return tenantID;
  112. },
  113. _onApplyClick: function() {
  114. var props = this.getModifiedProperties();
  115. if (props.tenantsDropDown === '') {
  116. this._onClearClick();
  117. } else if (props.tenantsDropDown !== undefined) {
  118. this._saveTenant(props.tenantsDropDown);
  119. }
  120. },
  121. _onClearClick: function() {
  122. this.glassContext.appController.showMessage(StringResource.get('confirmClearTenantText'), StringResource.get('confirmClearTenant'), 'info', ['ok', 'cancel'], '400px', function(evt) {
  123. if (evt.btn === 'ok') {
  124. this._clearTenant();
  125. }
  126. }.bind(this), true);
  127. },
  128. _saveTenant: function(tenantID) {
  129. var data = {
  130. 'tenantID': tenantID,
  131. 'type': this.objectInfo.type
  132. };
  133. return this._sendUpdateRequest(data).then(function() {
  134. this.slideout.hide();
  135. if (_.isFunction(this.onChangeCallback)) {
  136. this.onChangeCallback(tenantID);
  137. }
  138. this.glassContext.appController.showToast(StringResource.get('tenantSet'));
  139. }.bind(this), function() {
  140. this.glassContext.appController.showToast(StringResource.get('tenantSetError'), {
  141. type: 'error'
  142. });
  143. }.bind(this));
  144. },
  145. _clearTenant: function() {
  146. var data = {
  147. 'tenantID': '',
  148. 'type': this.objectInfo.type
  149. };
  150. return this._sendUpdateRequest(data).then(function() {
  151. this.slideout.hide();
  152. if (_.isFunction(this.onChangeCallback)) {
  153. this.onChangeCallback('');
  154. }
  155. this.glassContext.appController.showToast(StringResource.get('tenantClear'));
  156. }.bind(this), function() {
  157. this.glassContext.appController.showToast(StringResource.get('tenantClearError'), {
  158. type: 'error'
  159. });
  160. }.bind(this));
  161. },
  162. _sendUpdateRequest: function(data) {
  163. var jsonData = JSON.stringify(data);
  164. var url = ContentStoreObject.getSelfLink(this.objectInfo);
  165. url += '?updateTenantIDRecursive=true';
  166. var options = {
  167. 'headers': {
  168. 'Accept': 'application/json',
  169. 'Content-Type': 'application/json'
  170. },
  171. 'type': 'PUT',
  172. 'url': url,
  173. 'data': jsonData
  174. };
  175. return this.glassContext.getCoreSvc('.Ajax').ajax(options);
  176. },
  177. getModifiedProperties: function() {
  178. return this.getPropertyUIControl().getModifiedProperties();
  179. }
  180. });
  181. return TenantSlideout;
  182. });