FeaturesBody.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Cognos Analytics
  5. * Copyright IBM Corp. 2015, 2017
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['q', 'underscore', 'bi/commons/ui/View', 'bi/admin/nls/StringResource', 'bi/commons/ui/properties/PropertyUIControl', 'react-dom', 'react', 'ba-react-admin/ba-react-admin.min'], function (Q, _, View, StringResource, PropertyUIControl, ReactDOM, React, AdminReact) {
  9. 'use strict'; //NOSONAR
  10. var FeaturesBody = View.extend({
  11. g_MappedIcons: {
  12. 'com.ibm.bi.glass.appbar': 'common-appbar',
  13. 'com.ibm.bi.glass.navbar': 'common-navbar',
  14. 'com.ibm.bi.glass.common.cognosLogo': 'common-cognoslogo',
  15. 'com.ibm.bi.sample.cognosLogoHomePage': 'common-cognoslogo',
  16. 'com.ibm.bi.glass.contextmenus': 'common-contextmenus',
  17. 'com.ibm.bi.glass.collections': 'common-collections'
  18. },
  19. init: function init(options) {
  20. FeaturesBody.inherited('init', this, arguments);
  21. _.extend(this, options);
  22. },
  23. reset: function reset() {
  24. if (this._propertyUIControl) {
  25. _.each(this._propertyUIControl.getProperties(), function (treeView) {
  26. treeView.selectAll();
  27. }.bind(this));
  28. }
  29. },
  30. filter: function filter(value) {
  31. AdminReact.FeaturesTreeStore.setFilterString(value);
  32. },
  33. render: function render() {
  34. var deferred = Q.defer();
  35. var getPerspectivesPromise = this.getPerspectives();
  36. var getExcludedFeaturesPromise = this.getExcludeList();
  37. Q.all([getPerspectivesPromise, getExcludedFeaturesPromise]).then(function (results) {
  38. var lensiblePerspectives = results[0
  39. /*LENSIBLE_PERSPECTIVES_RESULT_INDEX*/
  40. ];
  41. var excludedFeatures = results[1
  42. /*EXCLUDED_FEATURES_RESULT_INDEX*/
  43. ];
  44. var spec = this._buildCollapsibleViewDataSpecification(excludedFeatures.ids, lensiblePerspectives);
  45. this.featureTree = React.createElement(AdminReact.FeaturesTree, {
  46. StringResource: StringResource,
  47. data: spec,
  48. glassContext: this.glassContext,
  49. excludedFeatures: excludedFeatures,
  50. onChange: this.onChange
  51. });
  52. var reactEl = ReactDOM.render(this.featureTree, this.$el[0]);
  53. deferred.resolve(this.$el);
  54. }.bind(this), function (err) {
  55. this.glassContext.appController.showToast(StringResource.get('failedToRetrievePerspectivesList'), {
  56. type: 'error'
  57. });
  58. }.bind(this));
  59. return deferred.promise;
  60. },
  61. remove: function remove() {
  62. if (this._propertyUIControler) {
  63. this._propertyUIControl.remove();
  64. }
  65. },
  66. _buildCollapsibleViewDataSpecification: function _buildCollapsibleViewDataSpecification(excludedIdList, lensiblePerspectives) {
  67. var items = []; // build a data specification for the CollapsibleSection
  68. _.each(_.sortBy(lensiblePerspectives, function (perspective) {
  69. return perspective.name;
  70. }), function (perspective) {
  71. var item = this._createTreeItem(perspective, false, excludedIdList); // use the label specified in the definition if it exists
  72. if (perspective.definition && perspective.definition.label) {
  73. item.label = perspective.definition.label;
  74. }
  75. item.onChange = this._onChange.bind(this);
  76. item.items = this._extractPerspectiveFeatures(perspective, excludedIdList);
  77. items.push(item);
  78. }.bind(this), this);
  79. return items;
  80. },
  81. _extractPerspectiveFeatures: function _extractPerspectiveFeatures(perspectiveSpecification, excludedIdList) {
  82. var items = [];
  83. if (perspectiveSpecification.definition.toolBars) {
  84. _.each(perspectiveSpecification.definition.toolBars, function (toolbar) {
  85. var item = this._createTreeItem(toolbar, false, excludedIdList);
  86. _.each(toolbar.toolItemGroups, function (toolItemGroup) {
  87. this._addPerspectiveFeature(item, toolItemGroup.toolItems, excludedIdList);
  88. }.bind(this));
  89. item.hasChildren = item.items.length > 0;
  90. items.push(item);
  91. }.bind(this));
  92. }
  93. if (perspectiveSpecification.definition.contextMenus) {
  94. var contextMenusTreeItem = this._createTreeItem({
  95. 'label': 'Context Menus',
  96. 'id': 'com.ibm.bi.glass.contextmenus'
  97. }, false, excludedIdList);
  98. _.each(perspectiveSpecification.definition.contextMenus, function (contextMenuDefinition) {
  99. var contextMenuTreeItem = this._createTreeItem(contextMenuDefinition, true, excludedIdList);
  100. contextMenusTreeItem.items.push(contextMenuTreeItem);
  101. this._addPerspectiveFeature(contextMenuTreeItem, contextMenuDefinition.items, excludedIdList);
  102. }.bind(this));
  103. items.push(contextMenusTreeItem);
  104. }
  105. if (perspectiveSpecification.definition.collectionContainers) {
  106. var collectionsTreeItem = this._createTreeItem({
  107. 'label': 'Collections',
  108. 'id': 'com.ibm.bi.glass.collections'
  109. }, false, excludedIdList);
  110. _.each(perspectiveSpecification.definition.collectionContainers, function (collectionDefinition) {
  111. var collectionTreeItem = this._createTreeItem(collectionDefinition, true, excludedIdList);
  112. collectionsTreeItem.items.push(collectionTreeItem);
  113. this._addPerspectiveFeature(collectionTreeItem, collectionDefinition.items, excludedIdList);
  114. }.bind(this));
  115. items.push(collectionsTreeItem);
  116. }
  117. return items;
  118. },
  119. _addPerspectiveFeature: function _addPerspectiveFeature(parent, itemsToAdd, excludedIdList) {
  120. _.each(itemsToAdd, function (itemToAdd) {
  121. var item = this._createTreeItem(itemToAdd, true, excludedIdList);
  122. parent.items.push(item);
  123. this._addPerspectiveFeature(item, itemToAdd.items, excludedIdList);
  124. }.bind(this));
  125. },
  126. _createTreeItem: function _createTreeItem(item, checkBox, excludedIdList) {
  127. var itemId = this._getIdFromItem(item);
  128. return {
  129. 'glassContext': this.glassContext,
  130. 'slideout': this.slideout,
  131. 'name': itemId,
  132. 'label': this._getLabelFromItem(item),
  133. 'ariaLabel': item['aria-label'] ? item['aria-label'] : null,
  134. 'checkBox': checkBox,
  135. 'root': item.definition ? true : false,
  136. 'checked': checkBox ? _.indexOf(excludedIdList, itemId) === -1 ? true : false : null,
  137. 'indent': 1,
  138. 'icon': this._getIconFromItem(item),
  139. 'readOnly': this._isReadOnly(item),
  140. 'items': []
  141. };
  142. },
  143. _getIconFromItem: function _getIconFromItem(item) {
  144. var iconType;
  145. var iconValue;
  146. var mappedValue = this.g_MappedIcons[this._getIdFromItem(item)];
  147. if (mappedValue) {
  148. iconType = 'sprite';
  149. iconValue = mappedValue;
  150. } else if (item.icon) {
  151. if (item.icon.indexOf('.svg') === -1) {
  152. iconType = 'sprite';
  153. } else {
  154. iconType = 'path';
  155. }
  156. iconValue = item.icon;
  157. } else if (item.fonticon) {
  158. iconType = 'font';
  159. iconValue = item.fonticon;
  160. } else {
  161. iconValue = this.g_MappedIcons[this._getIdFromItem(item)];
  162. iconType = iconValue ? 'sprite' : 'blank';
  163. }
  164. return {
  165. 'type': iconType,
  166. 'value': iconValue
  167. };
  168. },
  169. _isReadOnly: function _isReadOnly(item) {
  170. return item.id === "com.ibm.bi.admin.admin" && this.objectInfo.id === "xOjpTeXN0ZW0gQWRtaW5pc3RyYXRvcnM_";
  171. },
  172. _getIdFromItem: function _getIdFromItem(item) {
  173. return item.id ? item.id : item.name ? item.name : item.label;
  174. },
  175. _getLabelFromItem: function _getLabelFromItem(item) {
  176. if (item.label) {
  177. return item.label;
  178. } else if (item['aria-label']) {
  179. return item['aria-label'];
  180. } else if (item.title) {
  181. return item.title;
  182. } else if (item.name) {
  183. return item.name;
  184. } else {
  185. return this._getIdFromItem(item);
  186. }
  187. },
  188. _onChange: function _onChange(name, checked) {
  189. _.each(this._propertyUIControl.getProperties(), function (property) {
  190. property.synchCommon(name, checked);
  191. });
  192. if (this.onChange) {
  193. this.onChange(name, checked);
  194. }
  195. }
  196. });
  197. return FeaturesBody;
  198. });