UNPKG

reactware

Version:

Reactware is an open source toolkit for developing desktop PWA with HTML, CSS, and JS

594 lines (486 loc) 20.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _styledComponents = _interopRequireWildcard(require("styled-components")); var _ResizeHandle = _interopRequireDefault(require("./ResizeHandle")); var Styles = _interopRequireWildcard(require("../styles")); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } 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 _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 _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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } 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 _templateObject15() { var data = _taggedTemplateLiteral(["\n position: absolute;\n top: 0;\n left: 0;\n right: 0;\n bottom: 0;\n "]); _templateObject15 = function _templateObject15() { return data; }; return data; } function _templateObject14() { var data = _taggedTemplateLiteral(["\n ", "\n"]); _templateObject14 = function _templateObject14() { return data; }; return data; } function _templateObject13() { var data = _taggedTemplateLiteral([" \n cursor: ", "-resize; \n "]); _templateObject13 = function _templateObject13() { return data; }; return data; } function _templateObject12() { var data = _taggedTemplateLiteral(["\n height: 100%;\n top: 0;\n right: -1px;\n &:active { ", " }\n "]); _templateObject12 = function _templateObject12() { return data; }; return data; } function _templateObject11() { var data = _taggedTemplateLiteral(["\n width: 4px;\n height: 4px;\n position: absolute;\n top: 100%;\n right: 0px;\n margin-top: -2px;\n margin-right: -1px;\n &:active { ", " }\n "]); _templateObject11 = function _templateObject11() { return data; }; return data; } function _templateObject10() { var data = _taggedTemplateLiteral(["\n top: 100%;\n width: 100%;\n margin-top: -2px;\n &:active { ", " }\n "]); _templateObject10 = function _templateObject10() { return data; }; return data; } function _templateObject9() { var data = _taggedTemplateLiteral(["\n width: 4px;\n height: 4px;\n position: absolute;\n top: 100%;\n margin-top: -2px;\n margin-left: -1px;\n &:active { ", " }\n "]); _templateObject9 = function _templateObject9() { return data; }; return data; } function _templateObject8() { var data = _taggedTemplateLiteral(["\n height: 100%;\n top: 0;\n left: -1px;\n &:active { ", " }\n "]); _templateObject8 = function _templateObject8() { return data; }; return data; } function _templateObject7() { var data = _taggedTemplateLiteral(["\n position: absolute;\n "]); _templateObject7 = function _templateObject7() { return data; }; return data; } function _templateObject6() { var data = _taggedTemplateLiteral(["\n width: 4px;\n height: 4px;\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n"]); _templateObject6 = function _templateObject6() { return data; }; return data; } function _templateObject5() { var data = _taggedTemplateLiteral(["\n grid-area: head; \n grid-area: body; \n background: #535353;\n border: 1px solid #282828;\n border-top: 0;\n height: 100%;\n display: flex;\n"]); _templateObject5 = function _templateObject5() { return data; }; return data; } function _templateObject4() { var data = _taggedTemplateLiteral(["\n ", ";\n padding: 3px 5px;\n grid-area: head; \n background:gray;\n border: 1px solid #282828;\n border-bottom: 0;\n background-image: linear-gradient(#474747 0%, #393939 5%, #323232 100%);\n color: white;\n border-top-left-radius: 3px;\n border-top-right-radius: 3px;\n"]); _templateObject4 = function _templateObject4() { return data; }; return data; } function _templateObject3() { var data = _taggedTemplateLiteral(["\n -filter: blur(1px);\n opacity: 1;\n "]); _templateObject3 = function _templateObject3() { return data; }; return data; } function _templateObject2() { var data = _taggedTemplateLiteral(["\n box-shadow: 0px 0px 5px rgba(0,0,0,0.25);\n "]); _templateObject2 = function _templateObject2() { return data; }; return data; } function _templateObject() { var data = _taggedTemplateLiteral(["\n ", "\n ", "\n \n display: flex;\n flex-direction: column;\n position: absolute;\n\n box-shadow: 0px 0px rgba(0,0,0,0);\n transition: box-shadow 200ms;\n\n ", "\n\n ", "\n\n"]); _templateObject = function _templateObject() { return data; }; return data; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } var WindowStyle = _styledComponents.default.div(_templateObject(), Styles.font.size, Styles.other.unselectable, function (props) { return props.active && (0, _styledComponents.css)(_templateObject2()); }, function (props) { return !props.active && (0, _styledComponents.css)(_templateObject3()); }); var HeaderStyle = _styledComponents.default.div(_templateObject4(), Styles.font.size); var BodyStyle = _styledComponents.default.div(_templateObject5()); var ResizeStyled = _styledComponents.default.div(_templateObject6(), function (props) { return props.area && (0, _styledComponents.css)(_templateObject7()); }, function (props) { return props.area === 'left' && (0, _styledComponents.css)(_templateObject8(), Styles.other.fullwidth); }, function (props) { return props.area === 'left-bottom' && (0, _styledComponents.css)(_templateObject9(), Styles.other.fullwidth); }, function (props) { return props.area === 'bottom' && (0, _styledComponents.css)(_templateObject10(), Styles.other.fullwidth); }, function (props) { return props.area === 'right-bottom' && (0, _styledComponents.css)(_templateObject11(), Styles.other.fullwidth); }, function (props) { return props.area === 'right' && (0, _styledComponents.css)(_templateObject12(), Styles.other.fullwidth); }, function (props) { return props.direction && (0, _styledComponents.css)(_templateObject13(), props.direction); }); var FocusStyled = _styledComponents.default.div(_templateObject14(), function (props) { return !props.active && (0, _styledComponents.css)(_templateObject15()); }); var Window = /*#__PURE__*/ function (_React$Component) { _inherits(Window, _React$Component); function Window(props) { var _this; _classCallCheck(this, Window); _this = _possibleConstructorReturn(this, _getPrototypeOf(Window).call(this, props)); _this.state = { top: props.top, left: props.left, width: props.width, height: props.height, area: null, clientY: null, clientX: null }; _this.onMouseMove = _this.onMouseMove.bind(_assertThisInitialized(_assertThisInitialized(_this))); _this.onMouseUp = _this.onMouseUp.bind(_assertThisInitialized(_assertThisInitialized(_this))); _this.onMouseDown = _this.onMouseDown.bind(_assertThisInitialized(_assertThisInitialized(_this))); return _this; } _createClass(Window, [{ key: "onMouseMove", value: function onMouseMove(event) { var _this$state = this.state, area = _this$state.area, top = _this$state.top, left = _this$state.left, right = _this$state.right, height = _this$state.height, width = _this$state.width, SclientX = _this$state.clientX, SclientY = _this$state.clientY; var _this$props = this.props, minheight = _this$props.minheight, maxheight = _this$props.maxheight, minwidth = _this$props.minwidth, maxwidth = _this$props.maxwidth; var clientY = event.clientY, clientX = event.clientX; switch (area) { case 'left': { var tleft = left + (clientX - SclientX); var twidth = width - (clientX - SclientX); if (twidth <= minwidth) { this.setState({ left: left + (width - minwidth), width: minwidth }); break; } if (maxwidth && twidth >= maxwidth) { this.setState({ left: left + (width - maxwidth), width: maxwidth }); break; } this.setState({ left: tleft, width: twidth, clientX: clientX }); break; } case 'right': { var _twidth = width + (clientX - SclientX); if (_twidth <= minwidth) { this.setState({ width: minwidth }); break; } this.setState({ width: width + (clientX - SclientX), clientX: clientX }); break; } case 'bottom': { var theight = height + (clientY - SclientY); if (theight <= minheight) { this.setState({ height: minheight }); break; } if (maxheight && theight >= maxheight) { this.setState({ height: maxheight }); break; } this.setState({ height: theight, clientY: clientY }); break; } case 'left-bottom': { var _tleft = left + (clientX - SclientX); var _twidth2 = width - (clientX - SclientX); var _theight = height + (clientY - SclientY); var state = {}; if (_twidth2 <= minwidth) { state = { left: left + (width - minwidth), width: minwidth }; } else if (maxwidth && _twidth2 >= maxwidth) { state = { left: left + (width - maxwidth), width: maxwidth }; } else { state = { left: _tleft, width: _twidth2, clientX: clientX }; } if (_theight <= minheight) { state = _objectSpread({}, state, { height: minheight }); } else if (maxheight && _theight >= maxheight) { state = _objectSpread({}, state, { height: maxheight }); } else { state = _objectSpread({}, state, { height: _theight, clientY: clientY }); } if (state) { this.setState(_objectSpread({}, state)); break; } this.setState({ left: left + (clientX - SclientX), width: width - (clientX - SclientX), height: height + (clientY - SclientY), clientX: clientX, clientY: clientY }); break; } case 'right-bottom': { var _twidth3 = width + (clientX - SclientX); var _theight2 = height + (clientY - SclientY); var _state = {}; if (_twidth3 <= minwidth) { _state = { width: minwidth }; } else if (maxwidth && _twidth3 >= maxwidth) { _state = { width: maxwidth }; } else { _state = { width: _twidth3, clientX: clientX }; } if (_theight2 <= minheight) { _state = _objectSpread({}, _state, { height: minheight }); } else if (maxheight && _theight2 >= maxheight) { _state = _objectSpread({}, _state, { height: maxheight }); } else { _state = _objectSpread({}, _state, { height: _theight2, clientY: clientY }); } if (_state) { this.setState(_objectSpread({}, _state)); break; } this.setState({ width: width + (clientX - SclientX), height: height + (clientY - SclientY), clientX: clientX, clientY: clientY }); break; } case 'header': this.setState({ left: left + (clientX - SclientX), top: top + (clientY - SclientY), clientX: clientX, clientY: clientY }); break; default: break; } } }, { key: "onMouseUp", value: function onMouseUp(event) { // console.log('onMouseUp', event) window.removeEventListener('mousemove', this.onMouseMove); window.removeEventListener('mouseup', this.onMouseUp); } }, { key: "onMouseDown", value: function onMouseDown(event, area) { // console.log('onMouseDown', event) var onClick = this.props.onClick; var clientY = event.clientY, clientX = event.clientX; window.addEventListener('mouseup', this.onMouseUp); window.addEventListener('mousemove', this.onMouseMove); this.setState({ clientY: clientY, clientX: clientX, area: area }); onClick(); } }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, index = _this$props2.index, active = _this$props2.active, header = _this$props2.header, children = _this$props2.children, _this$state2 = this.state, width = _this$state2.width, height = _this$state2.height, top = _this$state2.top, left = _this$state2.left; return _react.default.createElement(WindowStyle, { active: active, style: { width: width, height: height, top: top, left: left, zIndex: index } }, _react.default.createElement(HeaderStyle, { onMouseDown: function onMouseDown(event) { return _this2.onMouseDown(event, 'header'); } }, header), _react.default.createElement(BodyStyle, null, children), _react.default.createElement(ResizeStyled, { area: "left", direction: "e", onMouseDown: function onMouseDown(event) { return _this2.onMouseDown(event, 'left'); } }), _react.default.createElement(ResizeStyled, { area: "bottom", direction: "n", onMouseDown: function onMouseDown(event) { return _this2.onMouseDown(event, 'bottom'); } }), _react.default.createElement(ResizeStyled, { area: "left-bottom", direction: "ne", onMouseDown: function onMouseDown(event) { return _this2.onMouseDown(event, 'left-bottom'); } }), _react.default.createElement(ResizeStyled, { area: "right", direction: "e", onMouseDown: function onMouseDown(event) { return _this2.onMouseDown(event, 'right'); } }), _react.default.createElement(ResizeStyled, { area: "right-bottom", direction: "nw", onMouseDown: function onMouseDown(event) { return _this2.onMouseDown(event, 'right-bottom'); } }), _react.default.createElement(FocusStyled, { active: active, onMouseDown: function onMouseDown(event) { return _this2.onMouseDown(event, 'header'); } })); } }]); return Window; }(_react.default.Component); exports.default = Window; _defineProperty(Window, "propTypes", { top: _propTypes.default.number, left: _propTypes.default.number, width: _propTypes.default.number, minwidth: _propTypes.default.number, maxwidth: _propTypes.default.number, height: _propTypes.default.number, minheight: _propTypes.default.number, maxheight: _propTypes.default.number, index: _propTypes.default.number.isRequired, active: _propTypes.default.bool, header: _propTypes.default.any, //eslint-disable-line children: _propTypes.default.any, //eslint-disable-line onClick: _propTypes.default.func }); _defineProperty(Window, "defaultProps", { top: 50, left: 50, width: 350, minwidth: 150, maxwidth: 400, height: 200, minheight: 150, maxheight: 300, active: false, header: null, children: null, onClick: function onClick() {} });