VisTabs.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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; }
  4. 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; }
  5. /**
  6. * Licensed Materials - Property of IBM
  7. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2020
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. define(['jquery', 'underscore', '../../lib/@waca/core-client/js/core-client/ui/core/View', 'react-dom', 'react', 'ca-ui-toolkit'], function ($, _, View, ReactDOM, React, Toolkit) {
  11. 'use strict';
  12. var TabPanel = Toolkit.TabPanel;
  13. var Tabs = Toolkit.Tabs;
  14. var VisTabControl = function (_React$Component) {
  15. _inherits(VisTabControl, _React$Component);
  16. /* eslint react/prop-types: 0 */
  17. function VisTabControl(props) {
  18. _classCallCheck(this, VisTabControl);
  19. var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
  20. _this.state = _.clone(props.state);
  21. return _this;
  22. }
  23. /**
  24. * Render the tab control
  25. */
  26. VisTabControl.prototype.render = function render() {
  27. return React.createElement(
  28. Tabs,
  29. { selected: this.getSelectedId(), onChange: this._onChange.bind(this) },
  30. this._renderPanels()
  31. );
  32. };
  33. /**
  34. * Set the selected tab
  35. */
  36. VisTabControl.prototype.select = function select(tabId) {
  37. this.setState({ selectedTab: tabId || this.props.selectedTab });
  38. };
  39. /**
  40. * Get the selected tab identifier
  41. *
  42. * @return unique identifier of the selected tab
  43. */
  44. VisTabControl.prototype.getSelectedId = function getSelectedId() {
  45. return this.state.selectedTab;
  46. };
  47. VisTabControl.prototype._renderPanels = function _renderPanels() {
  48. // render each tab entries as a TabPanel with the given identifier and label
  49. return _.map(this.props.entries, function (entry) {
  50. return React.createElement(TabPanel, { key: entry.id, id: entry.id, label: entry.label });
  51. });
  52. };
  53. VisTabControl.prototype._onChange = function _onChange(tabId) {
  54. // set the selectedTab to state
  55. this.select(tabId);
  56. // find the tab entry of the selected tab and invoke the callback (if available)
  57. var entry = _.findWhere(this.props.entries, { id: tabId });
  58. if (entry && entry.onChange) {
  59. entry.onChange(tabId);
  60. }
  61. };
  62. return VisTabControl;
  63. }(React.Component);
  64. var VisTabs = View.extend({
  65. className: 'visTabsView',
  66. init: function init() {
  67. VisTabs.inherited('init', this, arguments);
  68. },
  69. /**
  70. * Attach the tabs to a DOM element
  71. *
  72. * @param {Element} element - The parent DOM element to place the tabs
  73. */
  74. placeAt: function placeAt(element) {
  75. // remove the previous render (if any)
  76. $(element).find('.visTabsView').remove();
  77. this.$el.prependTo(element);
  78. },
  79. /**
  80. * Create the tabs with the given array of tab entries
  81. * The tab entry is expected to be provided in the following form:
  82. * Entry: {
  83. * id: <unique identifier of the tab>
  84. * label: <display label of the tab>
  85. * onChange: <callback function called on tab selection> [optional]
  86. * }
  87. *
  88. * @param {Entry[]} entries - array of tab definition entries
  89. * @param {string} selectedTab - identifier of the default selected tab [optional]
  90. */
  91. createTabs: function createTabs(entries, selectedTab) {
  92. this.entries = entries;
  93. // if there is not selectedTab, simply start with selecting the first tab entry
  94. var initState = { selectedTab: selectedTab || (entries && entries.length ? entries[0].id : null) };
  95. this.ctrl = ReactDOM.render(React.createElement(VisTabControl, { entries: entries, state: initState }), this.el);
  96. },
  97. reset: function reset() {
  98. if (this.ctrl) {
  99. ReactDOM.unmountComponentAtNode(this.el);
  100. this.entries = null;
  101. this.ctrl = null;
  102. }
  103. },
  104. remove: function remove() {
  105. this.reset();
  106. VisTabs.inherited('remove', this, arguments);
  107. },
  108. /**
  109. * Get the number of tabs
  110. *
  111. * @return number of tabs entries rendered
  112. */
  113. getTabsCount: function getTabsCount() {
  114. return this.entries ? this.entries.length : 0;
  115. },
  116. /**
  117. * Select a tab with the given tab identifier
  118. *
  119. * @param {string} tabId - unique tab identifier to select
  120. */
  121. selectTab: function selectTab(tabId) {
  122. if (this.ctrl) {
  123. if (this.ctrl.getSelectedId() !== tabId) {
  124. this.ctrl.select(tabId);
  125. this.ctrl.render();
  126. }
  127. }
  128. }
  129. });
  130. return VisTabs;
  131. });
  132. //# sourceMappingURL=VisTabs.js.map