BasePerspectiveListView.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Cognos Analytics
  5. * Copyright IBM Corp. 2016, 2017
  6. * US Government Users Restricted Rights - Use, duplication or disclosure
  7. * restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['q', 'jquery', 'underscore', 'bi/admin/nls/StringResource', 'bi/commons/ui/core/Class', 'bi/commons/ui/ButtonBar', 'bi/commons/ui/SlideoutHeaderView', 'bi/commons/ui/StandardLayoutView', 'bi/glass/utils/ClassFactory'], function (Q, $, _, StringResource, Class, ButtonBar, SlideoutHeaderView, StandardLayoutView, classFactory) {
  10. var BasePerspectiveListView = Class.extend({
  11. previewDialogModule: 'bi/glass/common/ui/PreviewDialog',
  12. APPLY_BUTTON_ID: 'applySelection',
  13. PREVIEW_BUTTON_ID: 'previewPerspective',
  14. init: function init(options) {
  15. BasePerspectiveListView.inherited('init', this, arguments);
  16. _.extend(this, options);
  17. this._contentView = this._getNewStandardLayoutView();
  18. this._header = this._getNewSlideoutHeaderView({
  19. title: this.title,
  20. slideout: this.slideout
  21. });
  22. this._contentView.headerView = this._header;
  23. this._footer = this._getNewButtonBar({
  24. buttons: [{
  25. 'id': this.PREVIEW_BUTTON_ID,
  26. 'label': StringResource.get('preview'),
  27. 'onSelect': this._showPreview.bind(this)
  28. }, {
  29. 'id': this.APPLY_BUTTON_ID,
  30. 'label': StringResource.get('apply'),
  31. 'onSelect': this._applyPerspective.bind(this)
  32. }]
  33. });
  34. this._contentView.footerView = this._footer;
  35. this._selectionChanged = this._selectionChanged.bind(this);
  36. },
  37. _getNewStandardLayoutView: function _getNewStandardLayoutView(options) {
  38. return new StandardLayoutView(options);
  39. },
  40. _getNewSlideoutHeaderView: function _getNewSlideoutHeaderView(options) {
  41. return new SlideoutHeaderView(options);
  42. },
  43. _getNewButtonBar: function _getNewButtonBar(options) {
  44. return new ButtonBar(options);
  45. },
  46. _applyPerspective: function _applyPerspective() {
  47. var currentSelection = this._getCurrentSelection();
  48. if (currentSelection && _.isFunction(this.onApplyCallback)) {
  49. if (currentSelection.name === BasePerspectiveListView.DEFAULT) {
  50. currentSelection.value = null;
  51. }
  52. this.onApplyCallback(currentSelection);
  53. }
  54. ;
  55. },
  56. _showPreview: function _showPreview() {
  57. var currentSelection = this._getCurrentSelection();
  58. if (currentSelection) {
  59. return Q(this._getPreviewMap(currentSelection)).then(function (map) {
  60. var dialogOptions = {
  61. glassContext: this.glassContext,
  62. urlMap: map
  63. };
  64. return classFactory.instantiate(this.previewDialogModule, dialogOptions).then(function (dialog) {
  65. dialog.open();
  66. }.bind(this));
  67. }.bind(this)).fail(function () {
  68. this.glassContext.appController.showToast(StringResource.get('previewDialogFailed'), {
  69. type: 'error'
  70. });
  71. return Q.reject();
  72. }.bind(this));
  73. } else {
  74. this.glassContext.appController.showToast(StringResource.get('invalidPerspective'), {
  75. type: 'error'
  76. });
  77. return Q.reject();
  78. }
  79. },
  80. _getPreviewMap: function _getPreviewMap(currentSelection) {
  81. var map;
  82. if (currentSelection.value !== undefined) {
  83. if (currentSelection.name === BasePerspectiveListView.DEFAULT) {
  84. // getDefaultHome returns a Promise which will be resolved to the map
  85. map = this._getDefaultHome();
  86. } else {
  87. map = currentSelection.value.content || {};
  88. map.perspective = currentSelection.value.perspective;
  89. }
  90. } else {
  91. map = {
  92. 'perspective': currentSelection.name
  93. };
  94. }
  95. return map;
  96. },
  97. _getListSpec: function _getListSpec() {
  98. return {
  99. 'defaultSort': [1, 'asc'],
  100. 'getData': this._getPerspectives.bind(this),
  101. 'columns': this._getListColumnSpecification(),
  102. 'a11yLabel': this.title,
  103. 'glassContext': this.glassContext,
  104. 'selectCallback': this._selectionChanged.bind(this)
  105. };
  106. },
  107. _getListColumnSpecification: function _getListColumnSpecification() {
  108. return [{
  109. 'module': 'bi/content_apps/common/ui/list_columns/RadioButtons'
  110. }, {
  111. 'module': 'bacontentnav/common/ui/list_columns/Text',
  112. 'propertyName': 'name',
  113. '_bNavigable': true,
  114. 'label': StringResource.get('name'),
  115. 'scope': 'row'
  116. }];
  117. },
  118. _getCurrentSelection: function _getCurrentSelection() {
  119. var selection;
  120. if (this._body && _.isFunction(this._body.getCurrentSelection)) {
  121. selection = this._body.getCurrentSelection();
  122. }
  123. return selection;
  124. },
  125. _selectionChanged: function _selectionChanged() {
  126. var currentSelection = this._getCurrentSelection();
  127. if (currentSelection.name) {
  128. this._footer.enableAllButtons();
  129. } else {
  130. this._footer.disableAllButtons();
  131. }
  132. },
  133. _getDefaultHome: function _getDefaultHome() {
  134. return this.glassContext.services.ajax.ajax({
  135. 'type': 'GET',
  136. 'url': 'v1/system_profile_settings'
  137. }).then(function (response) {
  138. return response.defaultHome;
  139. });
  140. },
  141. remove: function remove() {
  142. this._contentView.remove();
  143. },
  144. render: function render() {
  145. return this._contentView.render();
  146. }
  147. });
  148. BasePerspectiveListView.DEFAULT = StringResource.get('default');
  149. return BasePerspectiveListView;
  150. });