UNPKG

polestar-ui-kit

Version:

## Install

286 lines (218 loc) 10.8 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; 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); } require("core-js/modules/es.date.to-string"); require("core-js/modules/es.object.get-prototype-of"); require("core-js/modules/es.object.set-prototype-of"); require("core-js/modules/es.object.to-string"); require("core-js/modules/es.reflect.construct"); require("core-js/modules/es.regexp.to-string"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _polestarUtils = require("polestar-utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } 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; } 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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var LeftSide = function (_React$Component) { _inherits(LeftSide, _React$Component); var _super = _createSuper(LeftSide); function LeftSide(props) { var _this; _classCallCheck(this, LeftSide); _this = _super.call(this, props); _this.setWidth = function (width) { _this.width = typeof width === 'number' ? "".concat(width, "px") : width; _this.contentRef.current.style.width = _this.width; _this.resizerRef.current.style.left = _this.width; if (_this.props.expandToggleButton) { _this.expandToggleButtonRef.current.style.left = _this.width; } if (_this.props.onResize) { _this.props.onResize(_this.width); } }; _this.getWidth = function () { return _this.width; }; _this.expand = function () { var isBool = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (_this.state.hidden) { throw new Error('LeftSide is null. showLeft() method call'); } if (isBool) { _this.setWidth(_this.tempWidth || _this.width); _this.resizerRef.current.style.display = 'block'; if (_this.props.onExpand) { _this.props.onExpand(_this.width); } } else { _this.tempWidth = _this.width; _this.setWidth(0); _this.resizerRef.current.style.display = 'none'; if (_this.props.onCollapse) { _this.props.onCollapse(); } } _this.isExpand = isBool; if (_this.props.expandToggleButton) { if (_this.isExpand) { _this.expandToggleButtonRef.current.classList.remove('ps-layout-leftside-toggle-button-expand'); _this.expandToggleButtonRef.current.classList.add('ps-layout-leftside-toggle-button-collapse'); } else { _this.expandToggleButtonRef.current.classList.remove('ps-layout-leftside-toggle-button-collapse'); _this.expandToggleButtonRef.current.classList.add('ps-layout-leftside-toggle-button-expand'); } } }; _this.toggleExpand = function () { _this.expand(!_this.isExpand); }; _this.open = function () { var isBool = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; var callback = arguments.length > 1 ? arguments[1] : undefined; _this.setState({ hidden: !isBool }, callback); }; _this.isShow = function () { return !_this.state.hidden; }; _this.handleMouseDown = function (e) { if (!_this.resizeActiveFlag && _this.props.resizable) { if (_this.resizerRef.current.setCapture) { _this.resizerRef.current.setCapture(); } else { document.addEventListener('mouseup', _this.handleMouseUp, true); document.addEventListener('mousemove', _this.handleMouseMove, true); e.preventDefault(); } _this.resizeActiveFlag = true; } }; _this.handleMouseUp = function (e) { if (_this.resizeActiveFlag) { _this.setWidth(_this.resizerRef.current.style.left); if (_this.resizerRef.current.releaseCapture) { _this.resizerRef.current.releaseCapture(); } else { document.removeEventListener('mouseup', _this.handleMouseUp, true); document.removeEventListener('mousemove', _this.handleMouseMove, true); e.preventDefault(); } _this.resizeActiveFlag = false; _this.saveContentWidth(_this.contentRef.current.style.width); } }; _this.handleMouseMove = function (e) { var _this$props = _this.props, minWidth = _this$props.minWidth, maxWidth = _this$props.maxWidth; if (_this.resizeActiveFlag) { if (e.clientX >= minWidth && e.clientX <= maxWidth) { _this.resizerRef.current.style.left = "".concat(e.clientX, "px"); _this.setWidth(_this.resizerRef.current.style.left); if (_this.props.expandToggleButton) { _this.expandToggleButtonRef.current.style.left = "".concat(e.clientX, "px"); } if (!_this.resizerRef.current.releaseCapture) { e.preventDefault(); } } } }; _this.handleExpandToggleButton = function () { _this.toggleExpand(); }; _this.saveContentWidth = function (width) { _polestarUtils.Util.setCookie('leftContentWidth', width, 365); }; _this.renderExpandToggleButton = function () { var cls = _this.isExpand ? 'ps-layout-leftside-toggle-button-collapse' : 'ps-layout-leftside-toggle-button-expand'; return _this.props.expandToggleButton ? _react["default"].createElement("div", { className: "ps-layout-leftside-toggle-button ".concat(cls), ref: _this.expandToggleButtonRef, onClick: _this.handleExpandToggleButton, onKeyDown: function onKeyDown() { return undefined; }, role: "presentation" }) : null; }; _this.state = { hidden: false }; _this.contentRef = _react["default"].createRef(); _this.resizerRef = _react["default"].createRef(); _this.expandToggleButtonRef = _react["default"].createRef(); _this.resizeActiveFlag = false; _this.isExpand = true; return _this; } _createClass(LeftSide, [{ key: "componentDidMount", value: function componentDidMount() { var _this$props2 = this.props, width = _this$props2.width, resizable = _this$props2.resizable; if (this.state.hidden) return; var cookieWidth = _polestarUtils.Util.getCookie('leftContentWidth'); if (cookieWidth) { this.setWidth(cookieWidth); } else if (width) { this.setWidth("".concat(width, "px")); } if (!resizable) { this.resizerRef.current.style.cursor = 'default'; } } }, { key: "render", value: function render() { var _this$props3 = this.props, style = _this$props3.style, children = _this$props3.children; return !this.state.hidden ? _react["default"].createElement("div", { className: "ps-layout-leftside" }, _react["default"].createElement("div", { className: "ps-layout-leftside-content", ref: this.contentRef, style: style }, children), _react["default"].createElement("div", { className: "ps-layout-leftside-resizer", ref: this.resizerRef, onMouseDown: this.handleMouseDown, role: "presentation" }), this.renderExpandToggleButton()) : null; } }]); return LeftSide; }(_react["default"].Component); LeftSide.propTypes = { resizable: _propTypes["default"].bool, width: _propTypes["default"].number, minWidth: _propTypes["default"].number, maxWidth: _propTypes["default"].number, expandToggleButton: _propTypes["default"].bool, onExpand: _propTypes["default"].func, onCollapse: _propTypes["default"].func, onResize: _propTypes["default"].func }; LeftSide.defaultProps = { resizable: true, minWidth: 50, maxWidth: 450, expandToggleButton: true }; var _default = LeftSide; exports["default"] = _default;