UNPKG

meteor-interface

Version:

Simple Content Management System to generate your administration interface for Meteor and React.

377 lines (315 loc) 16.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactMeteorData = require("meteor/react-meteor-data"); var _slugify = _interopRequireDefault(require("slugify")); var _reactSpring = require("react-spring"); var _semanticUiReact = require("semantic-ui-react"); var _styledComponents = _interopRequireDefault(require("styled-components")); var _Confirmation = _interopRequireDefault(require("../../components/Confirmation")); var _WidgetFormSelector = _interopRequireDefault(require("../components/WidgetFormSelector")); var _WidgetFormContext = require("../../../utils/contexts/WidgetFormContext"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _templateObject() { var data = _taggedTemplateLiteral(["\n h5.header {\n letter-spacing:2px;\n margin-bottom: 0;\n }\n .button {\n transition: all 0.3s ease-in !important;\n }\n \n .image-selector {\n .choice {\n display: flex;\n justify-content: space-between;\n flex-direction: column;\n .button {\n margin-bottom: 3px;\n }\n }\n .wrapper {\n display: flex;\n align-items: center;\n justify-content: space-around;\n }\n }\n .tinymce-selector .wrapper {\n padding: 0px !important;\n }\n .mce-panel.mce-tinymce {\n border: none !important;\n box-shadow: none;\n border-radius: 5px;\n overflow: hidden;\n }\n .mce-top-part::before {\n -webkit-box-shadow: none;\n -moz-box-shadow: none;\n box-shadow: none;\n }\n .incrementation {\n color: green;\n cursor: pointer;\n font-size: 12px;\n }\n .decrementation {\n color: red;\n cursor: pointer;\n font-size: 12px;\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) } })); } 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; } var ContentEdit = /*#__PURE__*/ function (_Component) { _inherits(ContentEdit, _Component); function ContentEdit() { var _getPrototypeOf2; var _this; _classCallCheck(this, ContentEdit); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(ContentEdit)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", { changes: false, loaded: false, itemState: {}, confirmation: false, confirmationObject: {} }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "componentWillReceiveProps", function (nextProps) { var _nextProps$item = nextProps.item, item = _nextProps$item === void 0 ? {} : _nextProps$item; if (!_this.state.loaded && item._id) { var keys = Object.keys(item); var itemState = {}; keys.map(function (key) { itemState[key] = item[key]; }); _this.setState({ itemState: itemState, loaded: true }); } }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "updateValue", function (e, _ref) { var name = _ref.name, value = _ref.value, checked = _ref.checked; var _this$state$itemState = _this.state.itemState, itemState = _this$state$itemState === void 0 ? {} : _this$state$itemState; itemState[name] = value || checked; _this.setState({ itemState: itemState, changes: true }); return value; }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "save", function () { var itemState = _this.state.itemState; var _this$props = _this.props, history = _this$props.history, _this$props$collectio = _this$props.collection, collection = _this$props$collectio === void 0 ? {} : _this$props$collectio, _this$props$item = _this$props.item, item = _this$props$item === void 0 ? {} : _this$props$item, root = _this$props.root; var method = "interface.update.".concat((0, _slugify.default)(collection.label, { lower: true })); _this.setState({ confirmation: false, loading: true }); var self = _assertThisInitialized(_assertThisInitialized(_this)); Meteor.call(method, { item: itemState }, function (error, result) { self.setState({ loading: false }); if (result) { var itemId = item._id || result; notify.success('Changes saved'); history.push("".concat(root, "/collections/").concat((0, _slugify.default)(collection.label, { lower: true }), "/").concat(itemId)); } else if (error) { notify.error(error.reason); } }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "delete", function () { var _this$props2 = _this.props, history = _this$props2.history, _this$props2$collecti = _this$props2.collection, collection = _this$props2$collecti === void 0 ? {} : _this$props2$collecti, _this$props2$item = _this$props2.item, item = _this$props2$item === void 0 ? {} : _this$props2$item, root = _this$props2.root; _this.setState({ confirmation: false, loading: true }); var self = _assertThisInitialized(_assertThisInitialized(_this)); var method = "interface.delete.".concat((0, _slugify.default)(collection.label, { lower: true })); Meteor.call(method, { itemId: item._id }, function (error, result) { self.setState({ loading: false }); if (result) { notify.success('Document deleted'); history.push("".concat(root, "/collections/").concat((0, _slugify.default)(collection.label, { lower: true }))); } else if (error) { notify.error(error.reason); } }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "toggleConfirmationSave", function (e) { e.preventDefault(); var confirmationObject = { title: 'Save and publish the document', text: 'You are about to save your modification and publish it immediatly', cancel: function cancel() { return _this.setState({ confirmation: !_this.state.confirmation }); }, confirm: _this.save }; _this.setState({ confirmation: !_this.state.confirmation, confirmationObject: confirmationObject }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "toggleConfirmationDelete", function (e) { e.preventDefault(); var confirmationObject = { title: 'Delete the document', text: 'You are about to delete this document, this cannot be undone.', cancel: function cancel() { return _this.setState({ confirmation: !_this.state.confirmation }); }, confirm: _this.delete }; _this.setState({ confirmation: !_this.state.confirmation, confirmationObject: confirmationObject }); }); return _this; } _createClass(ContentEdit, [{ key: "render", value: function render() { var _this2 = this; var _this$state = this.state, _this$state$itemState2 = _this$state.itemState, itemState = _this$state$itemState2 === void 0 ? {} : _this$state$itemState2, changes = _this$state.changes, loaded = _this$state.loaded, confirmationObject = _this$state.confirmationObject, confirmation = _this$state.confirmation, loading = _this$state.loading; var _this$props3 = this.props, _this$props3$item = _this$props3.item, item = _this$props3$item === void 0 ? {} : _this$props3$item, history = _this$props3.history, ready = _this$props3.ready, _this$props3$collecti = _this$props3.collection, collection = _this$props3$collecti === void 0 ? {} : _this$props3$collecti, firstField = _this$props3.firstField, _this$props3$config = _this$props3.config, config = _this$props3$config === void 0 ? {} : _this$props3$config, root = _this$props3.root; // Extract datas from config var _config$collections = config.collections, collections = _config$collections === void 0 ? [] : _config$collections; return _react.default.createElement(_WidgetFormContext.WidgetSelectorContext.Provider, { value: { collection: collection, item: itemState, ready: ready, updateValue: this.updateValue } }, _react.default.createElement(ContentEditStyle, null, _react.default.createElement(_reactSpring.Spring, { from: { opacity: 0, marginLeft: 600 }, to: { opacity: 1, marginLeft: 0 } }, function (styles) { return _react.default.createElement(_semanticUiReact.Segment, { style: _objectSpread({}, styles, { display: 'flex', justifyContent: 'space-between', alignItems: 'center' }) }, _react.default.createElement(_semanticUiReact.Header, { content: "Single item from ".concat(collection.label, ": ").concat(item._id ? 'EDITION' : 'NEW'), as: "h5" }), _react.default.createElement("div", null, item._id ? _react.default.createElement(_semanticUiReact.Button, { content: "DELETE", size: "mini", icon: "trash", onClick: _this2.toggleConfirmationDelete, color: "red", labelPosition: "left" }) : null, _react.default.createElement(_semanticUiReact.Button, { content: changes === false ? "SAVED" : "SAVE", size: "mini", icon: "save", onClick: changes === false ? null : _this2.toggleConfirmationSave, color: changes === false ? "grey" : "green", labelPosition: "left" }))); }), _react.default.createElement(_reactSpring.Spring, { from: { opacity: 0, marginTop: 600 }, to: { opacity: 1, marginTop: 0 } }, function (styles) { return _react.default.createElement(_semanticUiReact.Segment, { style: styles, color: "green" }, _react.default.createElement(_semanticUiReact.Form, { loading: loading }, _react.default.createElement(_WidgetFormSelector.default, { collection: collection, item: itemState, ready: ready, updateValue: _this2.updateValue }))); }), _react.default.createElement(_Confirmation.default, { confirmation: confirmation, loading: loading, confirmationObject: confirmationObject }))); } }]); return ContentEdit; }(_react.Component); var _default = (0, _reactMeteorData.withTracker)(function (_ref2) { var match = _ref2.match, _ref2$config = _ref2.config, config = _ref2$config === void 0 ? {} : _ref2$config; var _match$params = match.params, collectionSlug = _match$params.collectionSlug, itemId = _match$params.itemId; var _config$collections2 = config.collections, collections = _config$collections2 === void 0 ? [] : _config$collections2; var collection = null; collections.map(function (coll) { var slug = (0, _slugify.default)(coll.label, { lower: true }); if (slug === collectionSlug) collection = coll; }); var subscription = Meteor.subscribe("interface.one.".concat((0, _slugify.default)(collection.label, { lower: true })), { itemId: itemId }); var ready = subscription.ready(); var firstField = collection.fields[0].name; var item = collection.mongo.findOne({ _id: itemId }); return { collection: collection, ready: ready, item: item, firstField: firstField }; })(ContentEdit); exports.default = _default; var ContentEditStyle = _styledComponents.default.div(_templateObject());