UNPKG

@aibsweb/faceted-search

Version:
290 lines (243 loc) 12.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.LOAD_STATE = void 0; var _react = _interopRequireDefault(require("react")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _extends() { _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; }; return _extends.apply(this, arguments); } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var LOAD_STATE = { INITIALIZED: 'INITIALIZED', FETCHING: 'FETCHING', SUCCESS: 'SUCCESS', FAILURE: 'FAILURE' }; /** * DataTableHOCs * * Higher Order Components for enhancing data-table. * Static methods are react higher order components, i.e., a function that takes a component and returns a new component. */ exports.LOAD_STATE = LOAD_STATE; var DataTableHOCs = /*#__PURE__*/ function () { function DataTableHOCs() { _classCallCheck(this, DataTableHOCs); } _createClass(DataTableHOCs, null, [{ key: "withFetching", /** * A higher order component concerned with fetching and loading data. * Returns a class that renders a data table. * * @param {Class} Component A DataTable React.Component class. * @param {Class} Methods A class providing static methods and business logic for fetching data. */ value: function withFetching(Component, Methods) { return ( /*#__PURE__*/ function (_React$Component) { _inherits(_class, _React$Component); /** * A component that fetches and stores data and also renders a data table. * * @param {Object} props These are the properties for this component. * @param {Element} props.element The container element for this DataTable instance. */ function _class(props) { var _this; _classCallCheck(this, _class); _this = _possibleConstructorReturn(this, _getPrototypeOf(_class).call(this, props)); // initialize state _this.state = { data: [], loadState: LOAD_STATE.INITIALIZED }; return _this; } /** * React Lifecycle Method * @ignore */ _createClass(_class, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; var definitionFileName = this.props.element.dataset.tableDefinitionFile; this.fetchDefinitionFile(definitionFileName).then(function (tableDefinition) { return _this2.loadData(tableDefinition); })["catch"](function (err) { console.error(err); _this2.setState({ loadState: LOAD_STATE.FAILURE }); }); } /** * This returns a promise that resolves if data is succesfully stored in component state. * The promise rejects if no data is provided and data cannot be fetched from a url. * * @param {Object} tableDefinition includes schema for columns, and the option for data stored in an array. * If data is not provided, then a data url and query should be provided. */ }, { key: "loadData", value: function loadData(tableDefinition) { var _this3 = this; return new Promise(function (resolve, reject) { if (tableDefinition.data && tableDefinition.data.length) { // data is included in the table definition JSON file. _this3.setState(_objectSpread({}, tableDefinition, { loadState: LOAD_STATE.SUCCESS }), function () { return resolve(); }); } else if (tableDefinition['data-url'] && tableDefinition['query']) { // data must be fetched from a url using a query. _this3.fetchDataFromURL(tableDefinition).then(function (data) { _this3.setState(_objectSpread({}, tableDefinition, { data: data, loadState: LOAD_STATE.SUCCESS }), function () { return resolve(); }); })["catch"](function (err) { return reject(err); }); } else { console.error(tableDefinition); reject(new Error('Definition Error: no data.')); } }); } /** * Fetches the table definition file using a provided method passed into the HOC. * Methods.fetchDefinitionFile should return a promise that resolves to a table definition object, * or rejects with a error. * * @param {String} definitionFileName Name of the table definition file to fetch. * @returns {Promise<Object>} */ }, { key: "fetchDefinitionFile", value: function fetchDefinitionFile(definitionFileName) { return Methods.fetchDefinitionFile(definitionFileName); } /** * Fetches data from a url using a method passed into the HOC. * Methods.fetchDataFromURL should return a promise that resolves to data for a data table, * otherwise, rejects with an error. * * @param {Object} tableDefinition The table definition object with data url and query. * @returns {Promise<Object[]>} */ }, { key: "fetchDataFromURL", value: function fetchDataFromURL(tableDefinition) { return Methods.fetchDataFromURL(tableDefinition); } /** * Renders a loading spinner * @ignore */ }, { key: "renderLoading", value: function renderLoading() { return _react["default"].createElement("div", { className: "loading-spinner-wrapper" }, _react["default"].createElement("p", null, _react["default"].createElement("i", { className: "fas fa-spinner fa-spin" }))); } }, { key: "renderLoadFailed", value: function renderLoadFailed() { return _react["default"].createElement("div", { className: "load-failed-wrapper" }, _react["default"].createElement("p", null, 'There was a problem fetching this data.')); } /** * React Lifecycle Method * @ignore */ }, { key: "render", value: function render() { var loadState = this.state.loadState; if (loadState === LOAD_STATE.FETCHING || loadState === LOAD_STATE.INITIALIZED) { return this.renderLoading(); } if (loadState === LOAD_STATE.FAILURE) { return this.renderLoadFailed(); } return _react["default"].createElement(Component, _extends({}, this.props, { data: this.state.data, schema: this.state.schema })); } }]); return _class; }(_react["default"].Component) ); } /** * A higher order component concerned with creating custom cell content for a data table. * * @param {class} Component A DataTable React.Component class. * @param {class} Methods A class providing static methods for getting custom cell content. */ }, { key: "withCustomCellContent", value: function withCustomCellContent(Component, Methods) { return ( /*#__PURE__*/ function (_React$Component2) { _inherits(_class2, _React$Component2); function _class2() { _classCallCheck(this, _class2); return _possibleConstructorReturn(this, _getPrototypeOf(_class2).apply(this, arguments)); } _createClass(_class2, [{ key: "getCustomCellContentByType", /** * Creates custom cell content using a method passed into the HOC. * Methods.getCustomCellContentByType should determine the column type and decide what content needs to be returned. * * @param {Object} columnData Schema data for the cell. * @param {Object} rowData Data row for the table. * @returns {JSX | String} */ value: function getCustomCellContentByType(columnData, rowData) { return Methods.getCustomCellContentByType(columnData, rowData); } /** * React Lifecycle Method * @ignore */ }, { key: "render", value: function render() { return _react["default"].createElement(Component, _extends({}, this.props, { getCustomCellContentByType: this.getCustomCellContentByType })); } }]); return _class2; }(_react["default"].Component) ); } }]); return DataTableHOCs; }(); exports["default"] = DataTableHOCs;