SetHomePageView.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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(['doT', 'underscore', 'bi/commons/ui/View', 'bacontentnav/ui/widgets/FileSelector', 'bi/commons/ui/content/SingleSelectListControl', 'baglass/utils/Utils', 'baglass/api/Url', 'bi/admin/nls/StringResource', 'text!bi/admin/system/templates/SetHomePageTemplate.html', 'text!bi/commons/ui/template/RadioButton.html', 'bacontentnav/utils/ContentStoreObject'], function (dot, _, View, FileSelector, SingleSelectListControl, Utils, Url, StringResource, SetHomePageTemplate, RadioButton, ContentStoreObject) {
  10. 'use strict'; //NOSONAR
  11. var SetHomePageView = View.extend({
  12. init: function init(options) {
  13. SetHomePageView.inherited('init', this, arguments);
  14. _.extend(this, options);
  15. this.listControl = this._getNewSingleSelectListControl(this.listSpec);
  16. this.selectorID = _.uniqueId();
  17. this._url = this._getNewURL();
  18. this._selectedObjectData = null;
  19. },
  20. _getNewSingleSelectListControl: function _getNewSingleSelectListControl(spec) {
  21. return new SingleSelectListControl(spec);
  22. },
  23. _getNewURL: function _getNewURL() {
  24. return new Url();
  25. },
  26. _getNewFileSelector: function _getNewFileSelector(options) {
  27. return new FileSelector(options);
  28. },
  29. render: function render() {
  30. var getDataDfd = this._getHomePageTypes();
  31. var templateOptions = {
  32. options: [{
  33. name: 'cmObject',
  34. label: StringResource.get('selectDashboardOrReport')
  35. }, {
  36. name: 'perspective',
  37. label: StringResource.get('selectView')
  38. }]
  39. };
  40. var $Html = $(dot.template(SetHomePageTemplate)(templateOptions));
  41. for (var i = 0; i < templateOptions.options.length; i++) {
  42. $Html.find('.homeSelect.' + templateOptions.options[i].name).append(dot.template(RadioButton)({
  43. label: templateOptions.options[i].label,
  44. index: i,
  45. id: this.selectorID,
  46. controlOnLeft: true
  47. }));
  48. }
  49. this.$el.append($Html);
  50. this.$el.find('.homeSelect .roundButton').on('primaryaction', this._handleClickSelect.bind(this));
  51. this.$el.find('.cmObject .roundButton').attr("aria-label", StringResource.get('selectDashboardOrReport'));
  52. this.$el.find('.perspective .roundButton').attr("aria-label", StringResource.get('selectView'));
  53. this.listControl.$el = this.$el.find('.injectView.perspective');
  54. return getDataDfd.then(function (aHomePageTypes) {
  55. this.fileSelector = this._getNewFileSelector({
  56. $el: this.$el.find('.injectView.cmObject'),
  57. placeholder: StringResource.get('name'),
  58. title: StringResource.get('selectDashboardOrReport'),
  59. openDialogOptions: {
  60. 'glassContext': this.glassContext,
  61. 'typesToOpen': aHomePageTypes,
  62. 'multiSelect': false,
  63. 'onOpenCallback': this._setContentPath.bind(this),
  64. 'dialogTitle': StringResource.get('selectDashboardOrReport'),
  65. 'primaryBtnText': StringResource.get('select'),
  66. 'teamContentOnly': true
  67. }
  68. });
  69. return this.fileSelector.render();
  70. }.bind(this)).then(this.listControl.render.bind(this.listControl)).then(function () {
  71. if (this.currentValue && this.currentValue.content) {
  72. return this._url.getObjInfoFromContent(this.glassContext, this.currentValue.content, ['defaultName', 'ancestors']);
  73. }
  74. }.bind(this)).catch(this.logger.error.bind(this.logger)).then(this._initSelection.bind(this));
  75. },
  76. _handleClickSelect: function _handleClickSelect(event) {
  77. this._select(event.currentTarget);
  78. event.stopPropagation();
  79. return false;
  80. },
  81. _select: function _select(rowNode) {
  82. var $rowNode = $(rowNode);
  83. this._uncheckCurrentSelection();
  84. $rowNode.attr('aria-checked', 'true');
  85. $rowNode.addClass('checked');
  86. if ($rowNode.parent().hasClass('cmObject')) {
  87. this._enableFileSelector(true);
  88. this._enablePerspectiveList(false);
  89. } else if ($rowNode.parent().hasClass('perspective')) {
  90. this._enableFileSelector(false);
  91. this._enablePerspectiveList(true);
  92. }
  93. if (this._selectionChanged) {
  94. this._selectionChanged();
  95. }
  96. },
  97. _uncheckCurrentSelection: function _uncheckCurrentSelection() {
  98. var $currentFocusedRow = this.$el.find('.homeSelect .roundButton.checked');
  99. $currentFocusedRow.removeClass('checked');
  100. $currentFocusedRow.attr('aria-checked', 'false');
  101. },
  102. _enableFileSelector: function _enableFileSelector(enable) {
  103. if (enable) {
  104. this.fileSelector.enable();
  105. } else {
  106. this.fileSelector.disable();
  107. }
  108. },
  109. _enablePerspectiveList: function _enablePerspectiveList(enable) {
  110. this.listControl.$el.removeClass('disable');
  111. if (!enable) {
  112. this.listControl.$el.addClass('disable');
  113. }
  114. },
  115. _getHomePageTypes: function _getHomePageTypes() {
  116. return Promise.resolve(['exploration', 'report', 'reportView', 'package']);
  117. },
  118. _setContentPath: function _setContentPath(selectedObject) {
  119. this._selectedObjectData = selectedObject[0];
  120. this._setFileSelectorLabel(selectedObject[0]);
  121. return this._getUrlMap(selectedObject).then(function (urlMap) {
  122. this.currentValue = {};
  123. this.currentValue.perspective = urlMap.perspective;
  124. this.currentValue.content = urlMap;
  125. if (this._selectionChanged) {
  126. this._selectionChanged();
  127. }
  128. }.bind(this), this.logger.error.bind(this.logger));
  129. },
  130. _initSelection: function _initSelection(objectInfo) {
  131. var $selectedRadio;
  132. if (_.isUndefined(this.currentValue.content)) {
  133. $selectedRadio = this.$el.find('.homeSelect.perspective .roundButton');
  134. } else {
  135. $selectedRadio = this.$el.find('.homeSelect.cmObject .roundButton');
  136. this._setFileSelectorLabel(objectInfo);
  137. }
  138. this._select($selectedRadio);
  139. },
  140. _setFileSelectorLabel: function _setFileSelectorLabel(objectInfo) {
  141. var path = objectInfo ? ContentStoreObject.getLocation(objectInfo, true) : StringResource.get('unavailable');
  142. this.fileSelector.setValue(path);
  143. },
  144. getCurrentSelection: function getCurrentSelection() {
  145. var currentSelection = {
  146. 'name': '',
  147. 'value': null
  148. };
  149. if (this.isPerspective()) {
  150. var selectedPerspective = this.listControl.getCurrentSelection();
  151. if (selectedPerspective) {
  152. currentSelection = {
  153. 'name': selectedPerspective.name,
  154. 'value': {
  155. perspective: selectedPerspective.name
  156. }
  157. };
  158. }
  159. } else if (this._selectedObjectData) {
  160. currentSelection = {
  161. 'name': this._selectedObjectData.defaultName,
  162. 'value': this.currentValue
  163. };
  164. }
  165. return currentSelection;
  166. },
  167. isPerspective: function isPerspective() {
  168. var $currentFocusedRow = this.$el.find('.homeSelect .roundButton.checked');
  169. return $currentFocusedRow.parent().hasClass('perspective');
  170. },
  171. _getUrlMap: function _getUrlMap(selectedObject) {
  172. return Utils.getSharedResourceActionController(this.glassContext, selectedObject[0].type).then(function (module) {
  173. var context = this.glassContext.addToOptions({
  174. target: {
  175. activeObject: {
  176. aSelectedContext: selectedObject
  177. }
  178. },
  179. options: selectedObject
  180. });
  181. var publicContext = {
  182. urlMap: {
  183. objRef: selectedObject[0].id,
  184. type: selectedObject[0].type
  185. },
  186. mode: Url.DYNAMIC
  187. };
  188. if (selectedObject[0].defaultScreenTip === 'story') {
  189. publicContext.urlMap.mode = 'story';
  190. }
  191. _.extend(publicContext, context);
  192. return this._url.getUrlMap(module, this.glassContext, publicContext);
  193. }.bind(this));
  194. }
  195. });
  196. SetHomePageView.NOT_SUPPORTED_TYPE = 'The object type is not supported: ';
  197. return SetHomePageView;
  198. });