UNPKG

@carbon/ibm-cloud-cognitive-cdai

Version:
335 lines (333 loc) 14 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof3 = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireWildcard(require("react")); var _bg = _interopRequireDefault(require("./assets/bg403.svg")); var _bg2 = _interopRequireDefault(require("./assets/bg404.svg")); var _bgCustom = _interopRequireDefault(require("./assets/bgCustom.svg")); var _carbonComponentsReact = require("carbon-components-react"); var _propTypes = require("prop-types"); var _carbonComponents = require("carbon-components"); var _settings = require("../../globals/js/settings"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof3(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } // // Copyright IBM Corp. 2020, 2020 // // This source code is licensed under the Apache-2.0 license found in the // LICENSE file in the root directory of this source tree. // var IdeHTTPErrors = exports.default = /*#__PURE__*/function (_Component) { function IdeHTTPErrors(_props) { var _this; (0, _classCallCheck2.default)(this, IdeHTTPErrors); _this = _callSuper(this, IdeHTTPErrors, [_props]); // Section rendering (0, _defineProperty2.default)(_this, "renderTitle", function () { return _this.renderSection('title'); }); (0, _defineProperty2.default)(_this, "renderDescription", function () { return _this.renderSection('description'); }); (0, _defineProperty2.default)(_this, "renderDetails", function () { return _this.renderSection('details'); }); (0, _defineProperty2.default)(_this, "renderLinks", function () { var _this2 = _this, linksPropIsObject = _this2.linksPropIsObject, linksProp = _this2.props.links, renderSection = _this2.renderSection; if (!linksProp) { return; } var links = linksPropIsObject ? [linksProp] : linksProp; return renderSection('links', /*#__PURE__*/_react.default.createElement("ul", null, links.map(function (_ref) { var text = _ref.text, url = _ref.url; return /*#__PURE__*/_react.default.createElement("li", { className: "".concat(_this.className, "-links--link"), key: "".concat(text, ":").concat(url) }, /*#__PURE__*/_react.default.createElement(_carbonComponentsReact.Link, { href: url }, text)); }))); }); // Background rendering (0, _defineProperty2.default)(_this, "renderBackground", function (isInline) { var _this3 = _this, getImage = _this3.getImage, isLargeViewport = _this3.isLargeViewport, renderSection = _this3.renderSection, state = _this3.state; if (!state || isInline && isLargeViewport || !isInline && !isLargeViewport) { return null; } var className = 'image-container'; var sectionClassName = isInline ? "inline-".concat(className) : className; var background = getImage(isInline); return renderSection(sectionClassName, background); }); (0, _defineProperty2.default)(_this, "getImage", function (isInline) { var _this4 = _this, src = _this4.background, componentClassName = _this4.className; var className = isInline ? "".concat(componentClassName, "-inline-image") : "".concat(componentClassName, "-image"); return /*#__PURE__*/_react.default.createElement("img", { alt: "", className: className, src: src }); }); (0, _defineProperty2.default)(_this, "handleResize", function () { _this.setState({ width: _this.pageWidth.current.offsetWidth }); }); // Layout (0, _defineProperty2.default)(_this, "renderSection", function (sectionClassName, children) { if (!sectionClassName) { return null; } var _this5 = _this, className = _this5.className, gridClasses = _this5.gridClasses, props = _this5.props, renderRow = _this5.renderRow; return renderRow(/*#__PURE__*/_react.default.createElement("div", { className: "".concat(className, "-").concat(sectionClassName, " ").concat(gridClasses) }, children ? children : props[sectionClassName])); }); (0, _defineProperty2.default)(_this, "renderRow", function (children) { return /*#__PURE__*/_react.default.createElement("div", { className: "".concat(_this.gridPrefix, "--row") }, children); }); // Validation (0, _defineProperty2.default)(_this, "validateProps", function () { var _this6 = _this, assertBackgroundIsValid = _this6.assertBackgroundIsValid, assertIsType = _this6.assertIsType, assertLinksIsValid = _this6.assertLinksIsValid, _this6$props = _this6.props, background = _this6$props.background, description = _this6$props.description, details = _this6$props.details, links = _this6$props.links, title = _this6$props.title; background && assertBackgroundIsValid(); description && assertIsType('string', 'description', description); details && assertIsType('string', 'details', details); links && assertLinksIsValid(); title && assertIsType('string', 'title', title); }); (0, _defineProperty2.default)(_this, "assertBackgroundIsValid", function () { var _this7 = _this, background = _this7.props.background, throwError = _this7.throwError; if (background !== 403 && background !== 404) { throwError("background prop must be 403 or 404, got ".concat(background)); } }); (0, _defineProperty2.default)(_this, "assertLinksIsValid", function () { var _this8 = _this, assertIsValidLinkObject = _this8.assertIsValidLinkObject, linksPropIsArray = _this8.linksPropIsArray, linksPropIsObject = _this8.linksPropIsObject, links = _this8.props.links, throwError = _this8.throwError; if (linksPropIsObject) { assertIsValidLinkObject(links); } else if (linksPropIsArray) { if (!links.length) { return throwError('links prop defined as an array must contain at least 1 item'); } links.map(function (link) { return assertIsValidLinkObject(link); }); } else { throwError("links prop must be an object or array of objects with text+url props, got ".concat(JSON.stringify(links))); } }); (0, _defineProperty2.default)(_this, "assertIsType", function (type, propName, prop) { var propType = (0, _typeof2.default)(prop); if (propType !== type) { _this.throwError("".concat(propName, " prop must be type ").concat(type, ", got ").concat(propType)); } }); (0, _defineProperty2.default)(_this, "assertIsValidLinkObject", function (link) { var _this9 = _this, assertIsType = _this9.assertIsType; var text = link.text, url = link.url; assertIsType('string', 'links.text', text); assertIsType('string', 'links.url', url); }); (0, _defineProperty2.default)(_this, "throwError", function (msg) { throw new Error("".concat(_this.constructor.name, ": ").concat(msg)); }); _this.pageWidth = /*#__PURE__*/_react.default.createRef(); return _this; } (0, _inherits2.default)(IdeHTTPErrors, _Component); return (0, _createClass2.default)(IdeHTTPErrors, [{ key: "componentDidMount", value: function componentDidMount() { var handleResize = this.handleResize; handleResize(); window.addEventListener('resize', handleResize); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener('resize', this.handleResize); } }, { key: "render", value: function render() { var className = this.className, gridPrefix = this.gridPrefix, pageWidth = this.pageWidth, renderBackground = this.renderBackground, renderDescription = this.renderDescription, renderDetails = this.renderDetails, renderLinks = this.renderLinks, renderTitle = this.renderTitle, validateProps = this.validateProps; validateProps(); return /*#__PURE__*/_react.default.createElement("div", { className: className }, /*#__PURE__*/_react.default.createElement("div", { className: "".concat(gridPrefix, "--grid ").concat(gridPrefix, "--grid--full-width"), ref: pageWidth }, renderBackground(), renderTitle(), renderDescription(), renderDetails(), renderLinks(), renderBackground(true))); } }, { key: "background", get: function get() { switch (this.props.background) { case 403: return _bg.default; case 404: return _bg2.default; default: return _bgCustom.default; } } }, { key: "isLargeViewport", get: function get() { var state = this.state; if (!state) { return false; } var width = state.width; // NOTE @simon Hardcoded value 672 is per Carbon guidelines, see // https://www.carbondesignsystem.com/guidelines/2x-grid/basics#breakpoints var mediumViewportWidth = 672; return width >= mediumViewportWidth; } }, { key: "gridClasses", get: function get() { var gridPrefix = this.gridPrefix; var column = function column(viewportSize, columnsToSpan) { return "".concat(gridPrefix, "--col-").concat(viewportSize, "-").concat(columnsToSpan); }; var offset = function offset(viewportSize, columnsToSpan) { return "".concat(gridPrefix, "--offset-").concat(viewportSize, "-").concat(columnsToSpan); }; return [offset('md', 3), column('md', 4), offset('md', 1), offset('max', 6), column('max', 6), offset('max', 4)].join(' '); } }, { key: "gridPrefix", get: function get() { return _carbonComponents.settings.prefix; } }, { key: "linksPropIsArray", get: function get() { return Array.isArray(this.props.links); } }, { key: "linksPropIsObject", get: function get() { var links = this.props.links; return links && Object.prototype.hasOwnProperty.call(links, 'text') && Object.prototype.hasOwnProperty.call(links, 'url'); } // Class scope helpers }, { key: "className", get: function get() { return "".concat(_settings.idePrefix, "-http-errors"); } }]); }(_react.Component); (0, _defineProperty2.default)(IdeHTTPErrors, "propTypes", { /** * Alternative background image to render for 403 and 404 pages. Must equal * 403 or 404 if defined. A generic image is rendered if not defined. */ background: _propTypes.number, /** * Second row of the text section used to display a pre-translated * description for the HTTP status code message */ description: _propTypes.string, /** * Third row of the text section used to display a pre-translated details * related to the HTTP status code message and description */ details: _propTypes.string, /** * Fourth row of the text section used to display a list of links to * relevant sections or documentation */ links: (0, _propTypes.oneOfType)([ /** * A single link object */ (0, _propTypes.shape)({ /** * The pre-translated text to display for the link */ text: _propTypes.string.isRequired, /** * The link's destination */ url: _propTypes.string.isRequired }), /** * An array of links objects */ (0, _propTypes.arrayOf)((0, _propTypes.shape)({ /** * The pre-translated text to display for the link */ text: _propTypes.string.isRequired, /** * The link's destination */ url: _propTypes.string.isRequired }))]), /** * First row of the text section used to display a pre-translated HTTP * status code message */ title: _propTypes.string }); (0, _defineProperty2.default)(IdeHTTPErrors, "defaultProps", { title: 'CHANGE ME', description: 'CHANGE ME' });