SubscriptionButtonView.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Licensed Materials - Property of IBM
  3. *
  4. * IBM Cognos Products: SHARE
  5. *
  6. * (C) Copyright IBM Corp. 2015, 2019
  7. *
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  9. * IBM Corp.
  10. */
  11. define([
  12. 'bi/commons/ui/View', 'bi/sharecommon/utils/translator', 'bi/schedule/app/appControler',
  13. 'bi/notifications/app/n10nController'
  14. ], function(view, t, controler, n10nController) {
  15. var SubscriptionButtonView = view.extend({
  16. type : '',
  17. reportId : '',
  18. context: {},
  19. isNotificationStateAvailable: false,
  20. notificationState: false,
  21. onRender: function(context) {
  22. this.context = context;
  23. if (context.glassContext &&
  24. context.glassContext.appController &&
  25. context.glassContext.appController.currentAppView &&
  26. context.glassContext.appController.currentAppView.defaultContent) {
  27. this.type = this._setType(context.glassContext.appController.currentAppView.defaultContent);
  28. this.reportId = this._setReportId(context.glassContext.appController.currentAppView.defaultContent);
  29. }
  30. if (this._isNotifyMeOptionRequired()) {
  31. this._getNotificationState();
  32. }
  33. },
  34. onSelectItem: function(context) {
  35. var id = context.target.itemId;
  36. var idTokens = id.split('.');
  37. var key = idTokens[idTokens.length - 1];
  38. if(key === 'subscribe') {
  39. // first check if we have access to the selectedContext. If not, show dialog.
  40. var contentView = context.glassContext.appController.currentAppView && context.glassContext.appController.currentAppView.currentContentView;
  41. var content = contentView && contentView.getContent();
  42. if(content && content.application) {
  43. var selectedContext = content.application;
  44. // Check application contains the right info
  45. var hasStoreID = ( !selectedContext.storeID || selectedContext.storeID == '' )?false:true;
  46. if ( hasStoreID && !selectedContext.isModified ) {
  47. var descriptor = {
  48. name: selectedContext.reportName,
  49. reportId: selectedContext.storeID,
  50. type: selectedContext.type
  51. };
  52. // Check if there are any parameters
  53. try {
  54. var promptParameters = contentView.getParameterValues && contentView.getParameterValues();
  55. if (!promptParameters) {
  56. var parameters = content.promptParameters;
  57. if (parameters) {
  58. promptParameters = JSON.parse(parameters);
  59. }
  60. }
  61. if ( Array.isArray(promptParameters) && promptParameters.length > 0 ) {
  62. descriptor.parameters = promptParameters;
  63. }
  64. } catch(e) {
  65. // eslint-disable-next-line no-mixed-spaces-and-tabs
  66. context.glassContext.appController
  67. .showErrorMessage(t.translate('error_wrong_subscription_context'), t.translate('error_label_share'));
  68. }
  69. controler.showSubscriptionPane(context.glassContext, descriptor);
  70. } else {
  71. context.glassContext.appController
  72. .showErrorMessage(t.translate('error_wrong_subscription_context'), t.translate('error_label_share'));
  73. }
  74. } else {
  75. context.glassContext.appController
  76. .showErrorMessage(t.translate('error_wrong_subscription_context'), t.translate('error_label_share'));
  77. }
  78. } else if (key === 'start_notify_me') {
  79. var startdescriptor = {
  80. reportId: this.reportId,
  81. state: true
  82. };
  83. this._toggleNotificationState(t.translate('start_notify_me_toast'), startdescriptor);
  84. } else if (key === 'stop_notify_me') {
  85. var stopdescriptor = {
  86. reportId: this.reportId,
  87. state: false
  88. };
  89. this._toggleNotificationState(t.translate('stop_notify_me_toast'), stopdescriptor);
  90. }
  91. },
  92. isItemVisible: function(context) {
  93. var id = context.target.itemId;
  94. var idTokens = id.split('.');
  95. var key = idTokens[idTokens.length-1];
  96. if (key === 'stop_notify_me') {
  97. if (this._isNotifyMeOptionRequired() && this.isNotificationStateAvailable && this.notificationState) {
  98. // Notification is on, so present option to turn notification off
  99. return true;
  100. } else {
  101. return false;
  102. }
  103. } else if (key === 'start_notify_me') {
  104. if (this._isNotifyMeOptionRequired() && this.isNotificationStateAvailable && !(this.notificationState)) {
  105. // Notification is off, so present option to turn notification on
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. } else if (key === 'subscribe') {
  111. if (this._isNotifyMeOptionRequired()) {
  112. // Don't show 'Subscribe' option when 'Notify Me' option is presented
  113. return false;
  114. } else {
  115. return true;
  116. }
  117. } else {
  118. // Unknown key
  119. return false;
  120. }
  121. },
  122. _setType: function(defaultContent) {
  123. return defaultContent.type ? defaultContent.type : '';
  124. },
  125. _setReportId: function(defaultContent) {
  126. if (this.type === 'output' && defaultContent.reportProperties && defaultContent.reportProperties.id) {
  127. return defaultContent.reportProperties.id;
  128. } else if (defaultContent.id) {
  129. return defaultContent.id;
  130. } else {
  131. return '';
  132. }
  133. },
  134. _isNotifyMeOptionRequired: function() {
  135. return (this.type === 'output');
  136. },
  137. _getNotificationState: function() {
  138. if(this.reportId && this.context) {
  139. n10nController.getNotificationState(this.reportId, this.context).then(
  140. function(data) { this._setThisNotificationState(data); }.bind(this)).catch(
  141. function() {
  142. // Unable to retrieve notification state as expected
  143. this.isNotificationStateAvailable = false;
  144. }.bind(this));
  145. }
  146. },
  147. _setThisNotificationState: function(data, message) {
  148. if (data && data.state !== null) {
  149. this.isNotificationStateAvailable = true;
  150. this.notificationState = data.state;
  151. if (message) {
  152. this.context.glassContext.appController.showToast(message, {
  153. 'type': 'info',
  154. 'preventDuplicates': false
  155. });
  156. }
  157. } else {
  158. // Unable to resolve notification state as expected
  159. this.isNotificationStateAvailable = false;
  160. }
  161. },
  162. _toggleNotificationState: function(message, descriptor) {
  163. if(this.reportId && this.context) {
  164. n10nController.updateNotificationState(descriptor, this.context).then(
  165. function(data) { this._setThisNotificationState(data, message); }.bind(this)).catch(
  166. function() {
  167. // Unable to update notification state as expected
  168. this.isNotificationStateAvailable = false;
  169. }.bind(this));
  170. }
  171. }
  172. });
  173. return SubscriptionButtonView;
  174. });