UNPKG

polestar-ui-kit

Version:

## Install

287 lines (228 loc) 11.4 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 = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _uuid = require("uuid"); var _reactDraggable = _interopRequireDefault(require("react-draggable")); var _reactResizable = require("react-resizable"); var _Header = _interopRequireDefault(require("./Header")); var _Content = _interopRequireDefault(require("./Content")); var _Footer = _interopRequireDefault(require("./Footer")); var _zIndexManager = _interopRequireDefault(require("../helpers/zIndexManager")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } 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 WindowPortal = function (_Component) { _inherits(WindowPortal, _Component); var _super = _createSuper(WindowPortal); function WindowPortal(props) { var _this; _classCallCheck(this, WindowPortal); _this = _super.call(this, props); _this.onOk = function () { if (_this.props.onOk) { _this.props.onOk(); } }; _this.onCancel = function () { _this.onClose(); if (_this.props.onCancel) { _this.props.onCancel(); } }; _this.onMinimize = function () { _this.setState({ contentVisible: false }); if (_this.props.onMinimize) { _this.props.onMinimize(); } }; _this.onRestore = function () { _this.setState({ contentVisible: true }); if (_this.props.onRestore) { _this.props.onRestore(); } }; _this.onClose = function () { delete _zIndexManager["default"].components[_this.WindowPortalRef.current.id]; if (_this.props.onClose) { _this.props.onClose(); } }; _this.onResize = function (e, _ref) { var element = _ref.element, size = _ref.size; _this.setState({ width: size.width, height: size.height }); if (_this.props.onResize) { _this.props.onResize(e, { element: element, size: size }); } }; _this.getZIndex = function () { var el = _this.WindowPortalRef.current; return window.getComputedStyle(el, null).getPropertyValue('z-index'); }; _this.handleMouseDown = function () { _zIndexManager["default"].setZIndex(_this.WindowPortalRef, _this.getZIndex()); }; _this.renderFooter = function () { var _this$props = _this.props, footer = _this$props.footer, onOk = _this$props.onOk, onCancel = _this$props.onCancel, okLabel = _this$props.okLabel, cancelLabel = _this$props.cancelLabel; var contentVisible = _this.state.contentVisible; if (footer) { return _react["default"].createElement(_Footer["default"], { visible: contentVisible }, footer); } if (onOk || onCancel) { return _react["default"].createElement(_Footer["default"], { visible: contentVisible, onOk: onOk ? _this.onOk : null, onCancel: onCancel ? _this.onCancel : null, okLabel: okLabel, cancelLabel: cancelLabel }); } return null; }; _this.renderWindow = function () { var _this$props2 = _this.props, className = _this$props2.className, zIndex = _this$props2.zIndex, icon = _this$props2.icon, title = _this$props2.title, closable = _this$props2.closable, minimizable = _this$props2.minimizable, headerHeight = _this$props2.headerHeight, headerColor = _this$props2.headerColor, customButtons = _this$props2.customButtons, children = _this$props2.children; var _this$state = _this.state, width = _this$state.width, height = _this$state.height, contentVisible = _this$state.contentVisible; return _react["default"].createElement("div", { className: (0, _classnames["default"])('ps-window', className), style: { width: width, zIndex: zIndex }, ref: _this.WindowPortalRef, id: _this.uuid, role: "presentation", onMouseDown: _this.handleMouseDown }, _react["default"].createElement(_Header["default"], { icon: icon, title: title, closable: closable, minimizable: minimizable, height: headerHeight, color: headerColor, customButtons: customButtons, contentVisible: contentVisible, onMinimize: _this.onMinimize, onRestore: _this.onRestore, onClose: _this.onClose }), _react["default"].createElement(_Content["default"], { visible: contentVisible, height: height }, children), _this.renderFooter()); }; _this.state = { contentVisible: true, width: props.width, height: props.height }; _this.uuid = (0, _uuid.v4)(); _this.WindowPortalRef = _react["default"].createRef(); return _this; } _createClass(WindowPortal, [{ key: "componentDidMount", value: function componentDidMount() { console.log('componentDidMount zIndex', this.getZIndex()); _zIndexManager["default"].setZIndex(this.WindowPortalRef, this.getZIndex()); } }, { key: "render", value: function render() { var _this$props3 = this.props, open = _this$props3.open, minWidth = _this$props3.minWidth, minHeight = _this$props3.minHeight, maxWidth = _this$props3.maxWidth, maxHeight = _this$props3.maxHeight, position = _this$props3.position, modal = _this$props3.modal, resizable = _this$props3.resizable, draggable = _this$props3.draggable; var _this$state2 = this.state, width = _this$state2.width, height = _this$state2.height; var x = (!position.x || position.x === 0) && width ? (document.body.clientWidth - width) / 2 : position.x; var y = position.y; var window = resizable ? _react["default"].createElement(_reactResizable.Resizable, { className: "box", width: width, height: height, minConstraints: [minWidth, minHeight], maxConstraints: [maxWidth, maxHeight], onResize: this.onResize }, this.renderWindow()) : this.renderWindow(); window = draggable ? _react["default"].createElement(_reactDraggable["default"], { handle: ".ps-window-header", bounds: "body", position: null, defaultPosition: { x: x, y: y }, grid: [25, 25], onMouseDown: this.handleMouseDown }, window) : window; return _react["default"].createElement("div", { className: "ps-window-wrapper", style: { display: open ? null : 'none' } }, window, modal && _react["default"].createElement("div", { className: "ps-window-mask" })); } }]); return WindowPortal; }(_react.Component); var _default = WindowPortal; exports["default"] = _default;