AILearningConfigSlideout.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Content Explorer
  5. *| (C) Copyright IBM Corp. 2017, 2020
  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. 'jquery',
  13. 'underscore',
  14. 'baglass/core-client/js/core-client/ui/properties/PropertyPageView',
  15. 'baglass/core-client/js/core-client/ui/dialogs/ConfirmationDialog',
  16. '../../nls/StringResources'
  17. ], function($, _, PropertyPageView, ConfirmationDialog, StringResources) {
  18. 'use strict';
  19. var AILearningConfigSlideout;
  20. AILearningConfigSlideout = PropertyPageView.extend({
  21. init: function (options) {
  22. AILearningConfigSlideout.inherited('init', this, arguments);
  23. _.extend(this, options);
  24. this.logger = this.glassContext.getCoreSvc('.Logger');
  25. this.ajaxSvc = this.glassContext.getCoreSvc('.Ajax');
  26. this.userProfile = this.glassContext.getCoreSvc('.UserProfile');
  27. },
  28. render: function () {
  29. this.$el.addClass('AILearningConfigPane');
  30. return this._hasLearningUsageData()
  31. .then( function(hasLearningUsageData) {
  32. return this.renderPropertyUIControl({
  33. 'ariaLabel': StringResources.get('AILearningConfigurationSlideoutAllyLabel'),
  34. 'glassContext': this.glassContext,
  35. 'el': this.$el,
  36. 'items': [{
  37. 'value': StringResources.get('AILearning_config_title'),
  38. 'type': 'Banner',
  39. 'backButton': true,
  40. 'centerLabel': true,
  41. 'onClose': this._onClose.bind(this),
  42. 'doClose': this._closeSlideout.bind(this)
  43. }, {
  44. 'type': 'HintText',
  45. 'name': 'TypeInHintText',
  46. 'visibility': 'visible',
  47. 'label': StringResources.get('AILearning_config_description')
  48. }, {
  49. 'name': 'implicit_learning',
  50. 'checked': this._isLearningEnabled(),
  51. 'label': StringResources.get('AILearning_active'),
  52. 'type': 'ToggleButton'
  53. }, {
  54. 'type': 'Separator'
  55. },{
  56. 'name': 'deleteProductUsage',
  57. 'type': 'SingleLineLinks',
  58. 'disabled': !hasLearningUsageData,
  59. 'items': [{
  60. 'align': 'left',
  61. 'items': [{
  62. 'type': 'text',
  63. 'name': 'deleteUsageDataText',
  64. 'value': StringResources.get('deleteUsageData')
  65. }]
  66. }, {
  67. 'align': 'right',
  68. 'items': [{
  69. 'type': 'icon',
  70. 'name': 'deleteUsageDataTextIcon',
  71. 'svgIcon': 'common-remove-trash',
  72. 'iconTooltip': StringResources.get('deleteUsageData'),
  73. 'role': 'button',
  74. 'clickCallback': this._showConfirmationDialog.bind(this)
  75. }]
  76. }]
  77. }]
  78. });
  79. }.bind(this));
  80. },
  81. _isLearningEnabled: function () {
  82. return this.userProfile.userProfileSettings.implicit_learning;
  83. },
  84. _showConfirmationDialog: function () {
  85. var dialog = new ConfirmationDialog(
  86. 'implicit_learning',
  87. StringResources.get('confirmDeletion'),
  88. StringResources.get( 'usageDataDeletionConfirmation'));
  89. dialog.confirm( this._deleteUsageData.bind(this) );
  90. },
  91. _deleteUsageData: function(){
  92. var options = {
  93. type: 'DELETE',
  94. 'headers': {
  95. 'Accept': 'application/json',
  96. 'Content-Type': 'application/json'
  97. },
  98. url: AILearningConfigSlideout.LEARNING
  99. };
  100. return this.ajaxSvc.ajax(options).then(function() {
  101. //Only disable the option if request is successful
  102. this.getPropertyUIControl().getProperty('deleteProductUsage').disable();
  103. }.bind(this));
  104. },
  105. _hasLearningUsageData: function(){
  106. var options = {
  107. type: 'GET',
  108. 'headers': {
  109. 'Accept': 'application/json',
  110. 'Content-Type': 'application/json'
  111. },
  112. url: AILearningConfigSlideout.LEARNING + '/status'
  113. };
  114. return this.ajaxSvc.ajax(options)
  115. .then(function(response){
  116. if( response && response.data ){
  117. return (response.data.recModelAvailable );
  118. }
  119. return false;
  120. }).catch( function(){
  121. //If request failed, just set it to false
  122. return Promise.resolve(false);
  123. });
  124. },
  125. _doSave: function () {
  126. if(!this.getPropertyUIControl().hasModifiedProperties()) {
  127. return Promise.resolve();
  128. }
  129. var modifiedProps = this.getPropertyUIControl().getModifiedProperties();
  130. var options = {
  131. type: 'PUT',
  132. 'headers': {
  133. 'Accept': 'application/json',
  134. 'Content-Type': 'application/json'
  135. },
  136. url: AILearningConfigSlideout.USER_PROFILE_SETTINGS,
  137. data: JSON.stringify( { implicit_learning : modifiedProps.implicit_learning })
  138. };
  139. return this.ajaxSvc.ajax(options)
  140. .then(function(){
  141. //Update glassContext if save is successful
  142. return this.userProfile.updateContext({ userProfileSettings: { implicit_learning: modifiedProps.implicit_learning } });
  143. }.bind(this));
  144. },
  145. _onClose: function () {
  146. this._doSave();
  147. },
  148. _closeSlideout: function () {
  149. this.slideout.hide();
  150. }
  151. });
  152. AILearningConfigSlideout.LEARNING = 'v1/smarts/user-actions/visualization/online';
  153. AILearningConfigSlideout.USER_PROFILE_SETTINGS = 'v1/users/~/user_profile_settings';
  154. return AILearningConfigSlideout;
  155. });