GridView.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2013, 2020
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. *
  7. * GridWidget
  8. */
  9. define(['jquery', 'underscore', './GridEventTarget', '../VisView', '../VisEventHandler', '../../../widgets/livewidget/nls/StringResources'], function ($, _, GridEventTarget, VisView, VisEventHandler, StringResources) {
  10. 'use strict';
  11. var GridView = VisView.extend({
  12. templateString: '<div class="dataview grid-view" data-attach-point="contentNode"></div>',
  13. className: 'dataview grid-view',
  14. alwaysRenderOnResize: true,
  15. // Maps related to changing text properties
  16. // Map properties to css style strings
  17. PROPERTY_TO_STYLE: {
  18. 'font-weight': 'bold',
  19. 'font-style': 'italic',
  20. 'text-decoration': 'underline'
  21. },
  22. CELL_VALUE_PROPERTY_MAP: {
  23. cellValueColor: 'color',
  24. cellValueFontSize: 'font-size',
  25. cellValueFontFace: 'font-family',
  26. cellValueFontBold: 'font-weight',
  27. cellValueFontItalic: 'font-style',
  28. cellValueFontUnderline: 'text-decoration',
  29. cellValueFontAlign: 'text-align'
  30. },
  31. COL_HEADING_PROPERTY_MAP: {
  32. columnHeadingColor: 'color',
  33. columnHeadingFontSize: 'font-size',
  34. columnHeadingFontFace: 'font-family',
  35. columnHeadingFontBold: 'font-weight',
  36. columnHeadingFontItalic: 'font-style',
  37. columnHeadingFontUnderline: 'text-decoration',
  38. columnHeadingFontAlign: 'text-align'
  39. },
  40. /**
  41. * Cache focused group cell IDs
  42. */
  43. _focusedCellIDs: {
  44. 'key': null,
  45. 'cellIDs': []
  46. },
  47. init: function init() {
  48. GridView.inherited('init', this, arguments);
  49. this.isMobilePannable = true;
  50. if (this._isNewConditionalFormatFeature()) {
  51. this._conditionalFormatting = this.content.getFeature('ConditionalFormatting');
  52. } else {
  53. // deregister here since the new component do not have access to the dashboardAPI to check whether the feature flag is turned on or off
  54. // Instead when the new feature gets loaded it always register itself with the 'Properties' and 'CustomColor' feature
  55. // Remove this code when the new feature get turn on by default
  56. var newCondFeature = this.content.getFeature('ConditionalFormatting');
  57. if (newCondFeature) {
  58. var properties = this.content.getFeature('Properties');
  59. var customColor = this.dashboardApi.getFeature('CustomColor');
  60. properties.deregisterProvider(newCondFeature);
  61. customColor.deregisterProvider(newCondFeature);
  62. }
  63. }
  64. },
  65. _isNewConditionalFormatFeature: function _isNewConditionalFormatFeature() {
  66. return !this.dashboardApi.getGlassCoreSvc('.FeatureChecker').checkValue('dashboard', 'xtabcondFormat', 'disabled');
  67. },
  68. _initializeConditionalFormatting: function _initializeConditionalFormatting() {
  69. // This is temporary until features have a proper way of initializing themselves
  70. return this._conditionalFormatting ? this._conditionalFormatting.initializeFeature() : Promise.resolve();
  71. },
  72. /**
  73. * Loads and creates the control. Returns a promise which is resolved when the control
  74. * is created and ready to render
  75. */
  76. whenVisControlReady: function whenVisControlReady() {
  77. var _this = this;
  78. var result = void 0;
  79. if (this.visControl) {
  80. result = Promise.resolve(this.visControl);
  81. } else {
  82. //Load the control class defined in the item definition (eg JQGrid).
  83. result = new Promise(function (resolve, reject) {
  84. try {
  85. require([_this.visModel.getDefinition().control], function (VisControl) {
  86. _this.visControl = new VisControl({
  87. domNode: _this.$el.find('.grid-view')[0],
  88. gridView: _this,
  89. tabNavigation: _this.ownerWidget.interactivitySettings.tabNavigation,
  90. dashboardApi: _this.dashboardApi,
  91. summaryFeature: _this.content.getFeature('SummaryFeature'),
  92. conditionalFormatting: _this._conditionalFormatting,
  93. cellRenderer: _this.content.getFeature('CellRenderer.react')
  94. });
  95. _this.visControl.reset(_this.visModel);
  96. resolve(_this.visControl);
  97. }, reject);
  98. } catch (error) {
  99. reject(error);
  100. }
  101. }).then(function () {
  102. _this.eventHandler = new VisEventHandler({
  103. target: new GridEventTarget({
  104. $el: _this.$el,
  105. visControl: _this.visControl,
  106. visAPI: _this.visModel
  107. }),
  108. transaction: _this.transactionApi,
  109. ownerWidget: _this.ownerWidget,
  110. visAPI: _this.visModel,
  111. edgeSelection: true
  112. });
  113. return _this.visControl;
  114. });
  115. }
  116. return result;
  117. },
  118. getCurrentViewSelector: function getCurrentViewSelector() {
  119. return this.eventHandler;
  120. },
  121. animate: function animate() {
  122. //Override the animate to avoid the fade in/out when we resize or apply filters
  123. if (this.resizing) {
  124. this.visModel.renderCompleteBeforeAnimation(); // Rendering is complete, no animation
  125. } else {
  126. GridView.inherited('animate', this, arguments);
  127. }
  128. },
  129. whenSetDataReady: function whenSetDataReady(renderInfo) {
  130. if (renderInfo.data && renderInfo.data.getResult) {
  131. this.visControl.reset(this.visModel);
  132. this.visControl.setData(renderInfo.data.getResult());
  133. }
  134. return Promise.resolve(true);
  135. },
  136. /**
  137. * @param {Object} renderInfo - renderInfo passed to all render methods from the render sequence.
  138. * @returns a promise which is resolved when render is complete
  139. */
  140. render: function render(renderInfo) {
  141. var _this2 = this,
  142. _arguments = arguments;
  143. return this._initializeConditionalFormatting().then(function () {
  144. if (!_this2.isMappingComplete() || _this2.hasMissingFilters() || _this2.hasUnavailableMetadataColumns()) {
  145. _this2.removeGridView();
  146. _this2.resizeToWidget(renderInfo);
  147. _this2.renderIconView();
  148. _this2.updateSubViews();
  149. return Promise.resolve(_this2);
  150. }
  151. if (!_this2.visControl) {
  152. //This should never happen because the render sequence ensures the control is loaded prior to calling render.
  153. return Promise.reject(new Error());
  154. }
  155. if (!_this2.visControl.hasData()) {
  156. return Promise.resolve(_this2);
  157. }
  158. _this2.removeIconView();
  159. _this2.resizeToWidget(renderInfo);
  160. var bResize = _this2.resizing;
  161. var renderingNewData = _this2.getRenderingNewData();
  162. var styleObj = {};
  163. styleObj.headerStyles = _this2._constructTextStyles(_this2.COL_HEADING_PROPERTY_MAP, _this2.visModel);
  164. styleObj.cellStyles = _this2._constructTextStyles(_this2.CELL_VALUE_PROPERTY_MAP, _this2.visModel);
  165. _this2.visControl.setStyles(styleObj);
  166. _this2.setSelections();
  167. return _this2.visControl.render(bResize, renderingNewData).then(function (isRenderValid) {
  168. if (!isRenderValid) {
  169. return { reRenderNeeded: true };
  170. } else {
  171. //Note: renderComplete is called in the base render method.
  172. return GridView.inherited('render', _this2, _arguments);
  173. }
  174. });
  175. });
  176. },
  177. _constructTextStyles: function _constructTextStyles(properties, visModel) {
  178. var stylesMap = {};
  179. var propertyToStyle = this.PROPERTY_TO_STYLE;
  180. _.each(properties, function (styleName, propertyName) {
  181. var propertyValue = visModel.getPropertyValue(propertyName);
  182. if (propertyValue) {
  183. if (propertyToStyle[styleName]) {
  184. propertyValue = propertyToStyle[styleName];
  185. }
  186. stylesMap[styleName] = propertyValue;
  187. } else {
  188. stylesMap[styleName] = '';
  189. }
  190. });
  191. return stylesMap;
  192. },
  193. getDescription: function getDescription() {
  194. // Append the F12 key instruction to the description
  195. var description = GridView.inherited('getDescription', this, arguments);
  196. return StringResources.get('WidgetLabelWithDescripion', {
  197. label: description,
  198. description: StringResources.get('f12KeyDescription')
  199. });
  200. },
  201. removeGridView: function removeGridView() {
  202. this.visControl && this.visControl.removeGridView();
  203. },
  204. // @override
  205. remove: function remove() {
  206. if (this.eventHandler) {
  207. this.eventHandler.remove();
  208. this.eventHandler = null;
  209. }
  210. if (this.visControl) {
  211. this.visControl.remove();
  212. this.visControl = null;
  213. }
  214. GridView.inherited('remove', this, arguments);
  215. },
  216. _extendTuples: function _extendTuples(aSourceArray, aTargetArray, name, value) {
  217. _.each(aSourceArray, function (prevTuple) {
  218. var obj = {};
  219. obj[name] = value;
  220. aTargetArray.push(_.extend(obj, prevTuple));
  221. });
  222. },
  223. /**
  224. * @returns an array that represents current filter as tuple
  225. */
  226. _getSelectionValues: function _getSelectionValues() {
  227. var aTuple = [];
  228. var selections = this.getController();
  229. var colIndex = this.visControl.getRankDataItemCount();
  230. _.each(this.visualization.getSlots().getMappedSlotList(), function (slot) {
  231. if (slot.getId() !== 'heat') {
  232. _.each(slot.getDataItemList(), function (dataItem) {
  233. var values = selections.getSelectedTuples([dataItem.getColumnId()]);
  234. if (values && values.length) {
  235. var aTemp = [];
  236. _.each(values, function (value) {
  237. if (aTuple.length === 0) {
  238. var tuple = {};
  239. tuple[colIndex] = value;
  240. aTemp.push(tuple);
  241. } else {
  242. this._extendTuples(aTuple, aTemp, colIndex, value);
  243. }
  244. }.bind(this));
  245. aTuple = aTemp.slice();
  246. }
  247. colIndex++;
  248. }.bind(this));
  249. }
  250. }.bind(this));
  251. return aTuple;
  252. },
  253. /**
  254. * Update the selection tuple of the grid
  255. */
  256. setSelections: function setSelections() {
  257. this.visControl.setSelections(this._getSelectionValues());
  258. },
  259. /**
  260. * Update the selection tuple of the grid and update the view
  261. * this will be called from visView once grid view is ready
  262. * should never be called before view is ready
  263. */
  264. renderSelected: function renderSelected() {
  265. this.setSelections();
  266. if (this.visControl) {
  267. // force a re-render so selections are handled properly
  268. this.visControl.onResize();
  269. }
  270. },
  271. getFocusedCellIDs: function getFocusedCellIDs() {
  272. return this._focusedCellIDs;
  273. },
  274. setFocusedCellIDs: function setFocusedCellIDs(cellID, cells) {
  275. this._focusedCellIDs.key = cellID;
  276. this._focusedCellIDs.cellIDs = cells;
  277. },
  278. /**
  279. * Handle the on container entered event
  280. * Sets up keyboard navigation inside the list
  281. */
  282. onEnterContainer: function onEnterContainer() {
  283. this.visControl.onEnterContainer();
  284. },
  285. filterNodeKeydownHandler: function filterNodeKeydownHandler(event) {
  286. event.stopPropagation();
  287. }
  288. });
  289. return GridView;
  290. });
  291. //# sourceMappingURL=GridView.js.map