VisualItem.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. 'use strict';
  2. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  6. /**
  7. * Licensed Materials - Property of IBM
  8. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019, 2021
  9. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  10. */
  11. define(['react', 'prop-types', 'ca-ui-toolkit', 'jquery', './contextMenu/ContextMenuWrapper', '../../widgets/livewidget/nls/StringResources', '../../visualizations/vipr/VIPRBundleHelper', '../../lib/@waca/dashboard-common/dist/lib/@ba-ui-toolkit/ba-graphics/dist/icons-js/overflow-menu--horizontal_16'], function (React, PropTypes, UI_Toolkit, $, ContextMenuWrapper, StringResources, VIPRBundleHelper, menuOverflow_24) {
  12. var Component = React.Component;
  13. var FlexLayout = UI_Toolkit.FlexLayout,
  14. FlexItem = UI_Toolkit.FlexItem,
  15. VSpacer = UI_Toolkit.VSpacer,
  16. HSpacer = UI_Toolkit.HSpacer,
  17. SVGIcon = UI_Toolkit.SVGIcon,
  18. Label = UI_Toolkit.Label,
  19. Dialog = UI_Toolkit.Dialog;
  20. var VisualItem = function (_Component) {
  21. _inherits(VisualItem, _Component);
  22. function VisualItem(props) {
  23. _classCallCheck(this, VisualItem);
  24. var _this = _possibleConstructorReturn(this, _Component.call(this, props));
  25. _this.target = undefined;
  26. _this._iconRef = null;
  27. _this._rootRef = React.createRef();
  28. _this.state = {
  29. item: _this.props.item,
  30. isContextMenuOpen: false,
  31. openDeleteDialog: false
  32. };
  33. var item = _this.state.item;
  34. _this._renderContext = {
  35. icon: item.icon,
  36. description: item.label,
  37. isDisabled: false
  38. };
  39. return _this;
  40. }
  41. VisualItem.prototype.componentDidMount = function componentDidMount() {
  42. if (!this._renderContext.isDisabled) {
  43. this._attachCallbacksAndImages();
  44. }
  45. };
  46. VisualItem.prototype._attachCallbacksAndImages = function _attachCallbacksAndImages() {
  47. var $node = this._findChildVisCustomVisNode();
  48. if ($node) {
  49. var handlers = this.props.handlers;
  50. handlers.componentDidMountCallback('vis-custom-container', 'prop-item:not(.is-disabled)', $node);
  51. }
  52. this._appendIconNode();
  53. };
  54. VisualItem.prototype.componentWillUnmount = function componentWillUnmount() {
  55. if (!this._renderContext.isDisabled) {
  56. this._detachCallbacksAndImages();
  57. }
  58. };
  59. VisualItem.prototype._detachCallbacksAndImages = function _detachCallbacksAndImages() {
  60. var $node = this._findChildVisCustomVisNode();
  61. if ($node) {
  62. var handlers = this.props.handlers;
  63. handlers.componentWillUnmountCallback('vis-custom-container', 'prop-item:not(.is-disabled)', $node);
  64. }
  65. this._iconRef = null;
  66. };
  67. VisualItem.prototype.render = function render() {
  68. var item = this.state.item;
  69. var className = 'vis-custom-vis' + (this._renderContext.isDisabled ? ' no-sdk' : '');
  70. return React.createElement(
  71. FlexItem,
  72. {
  73. className: this._getVisualItemCssClasses(),
  74. role: 'group'
  75. },
  76. React.createElement(
  77. 'div',
  78. { ref: this._rootRef },
  79. React.createElement(
  80. FlexLayout,
  81. {
  82. fullHeight: true,
  83. direction: 'column',
  84. alignItems: 'flext-start'
  85. },
  86. React.createElement(
  87. FlexItem,
  88. {
  89. className: className,
  90. 'data-name': item.name
  91. },
  92. this._renderItem()
  93. ),
  94. React.createElement(
  95. FlexItem,
  96. null,
  97. React.createElement(VSpacer, { size: 1 })
  98. ),
  99. React.createElement(
  100. FlexItem,
  101. null,
  102. this._renderVisLabel()
  103. ),
  104. React.createElement(
  105. FlexItem,
  106. null,
  107. React.createElement(VSpacer, { size: 3 })
  108. )
  109. ),
  110. this.renderDeleteDialog()
  111. )
  112. );
  113. };
  114. VisualItem.prototype._getVisualItemCssClasses = function _getVisualItemCssClasses() {
  115. return 'vis-custom-visual-item';
  116. };
  117. VisualItem.prototype._renderItem = function _renderItem() {
  118. var item = this.state.item;
  119. var renderOptionsContent = void 0;
  120. if (this._canRenderMenuOption()) {
  121. if (this._isSchematic(item) && item.numSVGs > 1) {
  122. renderOptionsContent = this._renderSchematicCountWithOptions();
  123. } else {
  124. renderOptionsContent = this._renderMenuOption();
  125. }
  126. } else {
  127. renderOptionsContent = React.createElement(VSpacer, { size: 1.5 });
  128. }
  129. return React.createElement(
  130. FlexLayout,
  131. {
  132. fullHeight: true,
  133. direction: 'column',
  134. alignItems: 'flext-start'
  135. },
  136. React.createElement(
  137. FlexItem,
  138. null,
  139. React.createElement(VSpacer, { size: 0.5 })
  140. ),
  141. React.createElement(
  142. FlexItem,
  143. { className: 'vis-options' },
  144. renderOptionsContent
  145. ),
  146. React.createElement(
  147. FlexItem,
  148. null,
  149. React.createElement(VSpacer, { size: 0.5 })
  150. ),
  151. React.createElement(
  152. FlexItem,
  153. null,
  154. this._renderContent()
  155. )
  156. );
  157. };
  158. VisualItem.prototype._renderContent = function _renderContent() {
  159. var item = this.state.item;
  160. var className = 'prop-item' + (this._renderContext.isDisabled ? ' is-disabled' : '');
  161. return React.createElement(
  162. FlexLayout,
  163. {
  164. fullHeight: true,
  165. direction: 'column',
  166. alignItems: 'flext-start'
  167. },
  168. React.createElement(
  169. FlexItem,
  170. {
  171. role: 'group', className: className,
  172. 'data-name': item.name
  173. },
  174. this._renderVisIcon()
  175. )
  176. );
  177. };
  178. VisualItem.prototype._canRenderMenuOption = function _canRenderMenuOption() {
  179. var customVisAction = this.props.customVisAction;
  180. return customVisAction.canManage();
  181. };
  182. VisualItem.prototype._isSchematic = function _isSchematic(item) {
  183. var handlers = this.props.handlers;
  184. return handlers.isSchematicType(item) && !handlers.isSchematicTypePreview(item);
  185. };
  186. VisualItem.prototype._renderMenuOption = function _renderMenuOption() {
  187. var menuIcon = this.props.menuIcon;
  188. var item = this.state.item;
  189. return React.createElement(
  190. FlexLayout,
  191. {
  192. fullHeight: true,
  193. direction: 'row-reverse',
  194. className: 'vis-custom-menu-overflow'
  195. },
  196. React.createElement(
  197. FlexItem,
  198. null,
  199. React.createElement(HSpacer, { size: 1 })
  200. ),
  201. React.createElement(
  202. FlexItem,
  203. {
  204. 'data-name': item.name,
  205. className: 'vis-custom-menu-overflow-anchor',
  206. title: StringResources.get('extVisMore')
  207. },
  208. React.createElement(SVGIcon, {
  209. size: 'small',
  210. className: 'svgIcon',
  211. verticalAlign: 'initial',
  212. tabIndex: 0,
  213. iconId: menuIcon.default.id,
  214. onKeyUp: this._renderContextMenu.bind(this),
  215. onClick: this._handleOnClick.bind(this)
  216. }),
  217. this._renderContextMenu()
  218. )
  219. );
  220. };
  221. VisualItem.prototype._renderSchematicCountWithOptions = function _renderSchematicCountWithOptions() {
  222. var item = this.state.item;
  223. var renderMenuOption = this._canRenderMenuOption();
  224. var width = renderMenuOption ? '50%' : '100%';
  225. return React.createElement(
  226. FlexLayout,
  227. {
  228. fullHeight: true,
  229. direction: 'row',
  230. justifyContent: 'space-between'
  231. },
  232. React.createElement(
  233. FlexItem,
  234. { width: width, grow: true, shrink: true },
  235. React.createElement(
  236. FlexLayout,
  237. {
  238. fullHeight: true,
  239. direction: 'row',
  240. justifyContent: 'flex-start'
  241. },
  242. React.createElement(
  243. FlexItem,
  244. null,
  245. React.createElement(HSpacer, { size: 0.5 })
  246. ),
  247. React.createElement(
  248. FlexItem,
  249. null,
  250. React.createElement(
  251. 'div',
  252. {
  253. className: 'vis-svg-count-container',
  254. title: item.numSVGs,
  255. 'aria-label': item.numSVGs
  256. },
  257. React.createElement(Label, {
  258. className: 'vis-svg-count-content',
  259. label: item.numSVGs })
  260. )
  261. )
  262. )
  263. ),
  264. renderMenuOption && React.createElement(
  265. FlexItem,
  266. { grow: true, shrink: true },
  267. this._renderMenuOption()
  268. )
  269. );
  270. };
  271. VisualItem.prototype._renderVisIcon = function _renderVisIcon() {
  272. var item = this.state.item;
  273. return React.createElement(
  274. FlexLayout,
  275. {
  276. fullHeight: true,
  277. direction: 'column',
  278. alignItems: 'center',
  279. tabIndex: 0,
  280. className: 'prop-icon',
  281. role: 'button',
  282. title: item.label,
  283. 'aria-label': item.label
  284. },
  285. React.createElement(
  286. FlexItem,
  287. null,
  288. VIPRBundleHelper.isValidIconFileExt(item.icon) ? this._renderIconRef() : this._renderIcon()
  289. )
  290. );
  291. };
  292. VisualItem.prototype._renderIcon = function _renderIcon() {
  293. var className = 'svgIcon' + (this._renderContext.isDisabled ? ' is-disabled' : '');
  294. return React.createElement(SVGIcon, {
  295. iconId: this._renderContext.icon,
  296. height: 30,
  297. width: 30,
  298. className: className });
  299. };
  300. VisualItem.prototype._renderIconRef = function _renderIconRef() {
  301. this._iconRef = React.createRef();
  302. return React.createElement('div', { ref: this._iconRef });
  303. };
  304. VisualItem.prototype._appendIconNode = function _appendIconNode() {
  305. if (this._iconRef && this._iconRef.current) {
  306. var item = this.state.item;
  307. var placeholderIcon = item.placeholderIcon;
  308. var imgStr = '<img src="' + placeholderIcon + '"></img>';
  309. var $child = $(imgStr);
  310. $child.addClass('is-small');
  311. $child.addClass('ba-common-svgIcon');
  312. $child.css({
  313. height: '30px',
  314. width: '30px',
  315. 'vertical-align': 'bottom'
  316. });
  317. this._iconRef.current.appendChild($child[0]);
  318. }
  319. };
  320. VisualItem.prototype._renderVisLabel = function _renderVisLabel() {
  321. var className = 'vis-custom-vis-label' + (this._renderContext.isDisabled ? ' no-sdk' : '');
  322. return React.createElement(
  323. FlexLayout,
  324. {
  325. fullHeight: true,
  326. direction: 'column',
  327. alignItems: 'center',
  328. title: this._renderContext.description,
  329. 'aria-label': this._renderContext.description
  330. },
  331. React.createElement(
  332. FlexItem,
  333. null,
  334. React.createElement(Label, {
  335. className: className,
  336. label: this._renderContext.description })
  337. )
  338. );
  339. };
  340. VisualItem.prototype._renderContextMenu = function _renderContextMenu() {
  341. var item = this.state.item;
  342. if (!this.state.isContextMenuOpen || !this.target || this.targetName && this.targetName !== item.name) {
  343. return;
  344. }
  345. return this._getContextMenuContent();
  346. };
  347. VisualItem.prototype._getContextMenuContent = function _getContextMenuContent() {
  348. var placement = 'bottomRight';
  349. var align = 'left';
  350. return React.createElement(ContextMenuWrapper, {
  351. theme: true,
  352. placement: placement,
  353. align: align,
  354. triggerNode: this.target,
  355. domNodeToAttachTo: document.body,
  356. onClose: this.onClose.bind(this),
  357. onChange: this.onChange.bind(this),
  358. contextMenuItems: this._getContextMenuItems() });
  359. };
  360. VisualItem.prototype.onClose = function onClose() {
  361. if (this.target) {
  362. $(this.target).removeClass('open-context-menu');
  363. }
  364. this.target = undefined;
  365. this.targetName = undefined;
  366. this.setState({
  367. isContextMenuOpen: false
  368. });
  369. };
  370. VisualItem.prototype.onChange = function onChange(name, value) {
  371. if (value === 'delete') {
  372. this.setState({ openDeleteDialog: true });
  373. } else if (value === 'update') {
  374. var customVisAction = this.props.customVisAction;
  375. var item = this.state.item;
  376. if (customVisAction.openSelectBundleFileDialog) {
  377. customVisAction.openSelectBundleFileDialog(item);
  378. }
  379. }
  380. this.onClose();
  381. };
  382. VisualItem.prototype.renderDeleteDialog = function renderDeleteDialog() {
  383. var _this2 = this;
  384. var dialogProps = {
  385. size: 'small',
  386. width: '20px',
  387. minWidth: '450px',
  388. maxWidth: '500px',
  389. clickaway: false,
  390. theme: 'ba-theme-default',
  391. startingFocusIndex: 1
  392. };
  393. return React.createElement(
  394. 'div',
  395. null,
  396. this.state.openDeleteDialog && React.createElement(
  397. Dialog,
  398. _extends({}, dialogProps, { onClose: function onClose() {
  399. _this2.setState({ openDeleteDialog: false });
  400. } }),
  401. React.createElement(
  402. Dialog.Header,
  403. null,
  404. StringResources.get('extVisDeleteDialogTitle')
  405. ),
  406. React.createElement(
  407. Dialog.Body,
  408. null,
  409. StringResources.get('extVisDeleteDialogBody')
  410. ),
  411. React.createElement(
  412. Dialog.Footer,
  413. null,
  414. React.createElement(Dialog.Button, { primary: true, label: StringResources.get('extVisCustomDelete'), onClick: function onClick() {
  415. _this2.deleteCustomVisualization();
  416. } }),
  417. React.createElement(Dialog.Button, { label: StringResources.get('dlg_cancel'), onClick: function onClick() {
  418. _this2.setState({ openDeleteDialog: false });
  419. } })
  420. )
  421. )
  422. );
  423. };
  424. VisualItem.prototype.deleteCustomVisualization = function deleteCustomVisualization() {
  425. this.setState({ openDeleteDialog: false });
  426. var onContextMenuChange = this.props.onContextMenuChange;
  427. var item = this.state.item;
  428. onContextMenuChange(item, 'delete');
  429. };
  430. VisualItem.prototype._getContextMenuItems = function _getContextMenuItems() {
  431. var updateItem = [{
  432. id: 'update',
  433. value: 'update',
  434. label: StringResources.get('extVisCustomUpdate')
  435. }];
  436. var deleteItem = [{
  437. id: 'delete',
  438. value: 'delete',
  439. label: StringResources.get('extVisCustomDelete')
  440. }];
  441. var sepator = {};
  442. return [{ items: updateItem }, sepator, { items: deleteItem }];
  443. };
  444. VisualItem.prototype._handleOnClick = function _handleOnClick(event) {
  445. if (this.state.isContextMenuOpen) {
  446. if (this.target) {
  447. $(this.target).removeClass('open-context-menu');
  448. }
  449. this.target = undefined;
  450. this.targetName = undefined;
  451. this.setState({
  452. isContextMenuOpen: false
  453. });
  454. } else {
  455. var $node = $(event.target);
  456. $node = $node.closest('.vis-custom-menu-overflow-anchor');
  457. $node.addClass('open-context-menu');
  458. this.target = $node[0];
  459. this.targetName = $node.data('name');
  460. this.setState({
  461. isContextMenuOpen: true
  462. });
  463. }
  464. };
  465. VisualItem.prototype._findChildVisCustomVisNode = function _findChildVisCustomVisNode() {
  466. return this._rootRef.current ? $(this._rootRef.current).find('.prop-item') : null;
  467. };
  468. return VisualItem;
  469. }(Component);
  470. VisualItem.propTypes = {
  471. item: PropTypes.object.isRequired,
  472. iconsFeature: PropTypes.object.isRequired,
  473. handlers: PropTypes.shape({
  474. componentDidMountCallback: PropTypes.func.isRequired,
  475. componentWillUnmountCallback: PropTypes.func.isRequired,
  476. isSchematicType: PropTypes.func.isRequired,
  477. isSchematicTypePreview: PropTypes.func.isRequired
  478. }).isRequired,
  479. openSelectBundleFileDialog: PropTypes.func.isRequired,
  480. onContextMenuChange: PropTypes.func,
  481. customVisAction: PropTypes.object.isRequired,
  482. menuIcon: PropTypes.object
  483. };
  484. VisualItem.defaultProps = {
  485. onContextMenuChange: function onContextMenuChange() {},
  486. openSelectBundleFileDialog: function openSelectBundleFileDialog() {},
  487. menuIcon: menuOverflow_24
  488. };
  489. return VisualItem;
  490. });
  491. //# sourceMappingURL=VisualItem.js.map