UNPKG

polestar-ui-kit

Version:

## Install

206 lines (157 loc) 9.87 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 _reactDom = _interopRequireDefault(require("react-dom")); var _propTypes = _interopRequireDefault(require("prop-types")); var _WindowPortal = _interopRequireDefault(require("./components/WindowPortal")); 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 _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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } 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 isReact16 = _reactDom["default"].createPortal !== undefined; var createPortal = isReact16 ? _reactDom["default"].createPortal : _reactDom["default"].unstable_renderSubtreeIntoContainer; var getParentElement = function getParentElement(parentSelector) { return parentSelector(); }; var Window = function (_Component) { _inherits(Window, _Component); var _super = _createSuper(Window); function Window(props) { var _this; _classCallCheck(this, Window); _this = _super.call(this, props); _this.open = function () { _this.createContainer(); _this.setState({ open: true }); }; _this.close = function () { _this.setState({ open: false }); if (_this.props.destroyOnClose === true) { _this.destroy(); } if (_this.props.onClose) { _this.props.onClose(); } }; _this.destroy = function () { _this.isInit = false; if (_this.container) { _this.container.parentNode.removeChild(_this.container); _this.container = null; } }; _this.createContainer = function () { if (_this.container) return; _this.isInit = true; _this.container = document.createElement('div'); var parent = getParentElement(_this.props.parentSelector); parent.appendChild(_this.container); }; _this.state = { open: false }; _this.windowRef = _react["default"].createRef(); _this.isInit = false; return _this; } _createClass(Window, [{ key: "render", value: function render() { var _this$props = this.props, destroyOnClose = _this$props.destroyOnClose, onClose = _this$props.onClose, rest = _objectWithoutProperties(_this$props, ["destroyOnClose", "onClose"]); var open = this.state.open; if (!this.isInit) return null; return !(destroyOnClose === true && open === false) ? (open || this.windowRef) && this.container && createPortal(_react["default"].createElement(_WindowPortal["default"], _extends({ ref: this.windowRef, onClose: this.close }, rest, { open: open })), this.container) : null; } }]); return Window; }(_react.Component); Window.propTypes = { className: _propTypes["default"].string, icon: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].element]), title: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].element]), closable: _propTypes["default"].bool, minimizable: _propTypes["default"].bool, maximizable: _propTypes["default"].bool, resizable: _propTypes["default"].bool, draggable: _propTypes["default"].bool, modal: _propTypes["default"].bool, width: _propTypes["default"].number, height: _propTypes["default"].number, minWidth: _propTypes["default"].number, minHeight: _propTypes["default"].number, maxWidth: _propTypes["default"].number, maxHeight: _propTypes["default"].number, headerHeight: _propTypes["default"].number, headerColor: _propTypes["default"].string, customButtons: _propTypes["default"].array, footer: _propTypes["default"].element, position: _propTypes["default"].shape({ x: _propTypes["default"].number, y: _propTypes["default"].number }), zIndex: _propTypes["default"].number, parentSelector: _propTypes["default"].func, destroyOnClose: _propTypes["default"].bool, onMinimize: _propTypes["default"].func, onRestore: _propTypes["default"].func, onClose: _propTypes["default"].func, onOk: _propTypes["default"].func, onCancel: _propTypes["default"].func }; Window.defaultProps = { title: 'Untitled', modal: false, closable: true, minimizable: true, resizable: true, draggable: true, width: 520, height: 500, minWidth: 300, minHeight: 200, maxWidth: 1200, maxHeight: 800, position: { x: 0, y: 100 }, parentSelector: function parentSelector() { return document.body; } }; var _default = Window; exports["default"] = _default;