UNPKG

nowrapper-json

Version:

nowrapper

312 lines (267 loc) 13.8 kB
var _typeof = require("@babel/runtime/helpers/typeof"); (function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "@babel/runtime/helpers/createClass", "@babel/runtime/helpers/possibleConstructorReturn", "@babel/runtime/helpers/getPrototypeOf", "@babel/runtime/helpers/assertThisInitialized", "@babel/runtime/helpers/inherits", "@babel/runtime/helpers/objectWithoutProperties", "@babel/runtime/helpers/extends", "@babel/runtime/helpers/classCallCheck", "@babel/runtime/helpers/defineProperty", "react", "prop-types", "../locale/dialog", "./DialogContent", "noform-json"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("@babel/runtime/helpers/createClass"), require("@babel/runtime/helpers/possibleConstructorReturn"), require("@babel/runtime/helpers/getPrototypeOf"), require("@babel/runtime/helpers/assertThisInitialized"), require("@babel/runtime/helpers/inherits"), require("@babel/runtime/helpers/objectWithoutProperties"), require("@babel/runtime/helpers/extends"), require("@babel/runtime/helpers/classCallCheck"), require("@babel/runtime/helpers/defineProperty"), require("react"), require("prop-types"), require("../locale/dialog"), require("./DialogContent"), require("noform-json")); } else { var mod = { exports: {} }; factory(mod.exports, global.createClass, global.possibleConstructorReturn, global.getPrototypeOf, global.assertThisInitialized, global.inherits, global.objectWithoutProperties, global._extends, global.classCallCheck, global.defineProperty, global.react, global.propTypes, global.dialog, global.DialogContent, global.noformJson); global.DialogFormFactory = mod.exports; } })((typeof globalThis === "undefined" ? "undefined" : _typeof(globalThis)) === "object" ? globalThis : (typeof self === "undefined" ? "undefined" : _typeof(self)) === "object" ? self : this, function (_exports, _createClass2, _possibleConstructorReturn2, _getPrototypeOf2, _assertThisInitialized2, _inherits2, _objectWithoutProperties2, _extends2, _classCallCheck2, _defineProperty2, _react, _propTypes, _dialog, _DialogContent, _noformJson) { "use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(_exports, "__esModule", { value: true }); _exports["default"] = void 0; _createClass2 = _interopRequireDefault(_createClass2); _possibleConstructorReturn2 = _interopRequireDefault(_possibleConstructorReturn2); _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf2); _assertThisInitialized2 = _interopRequireDefault(_assertThisInitialized2); _inherits2 = _interopRequireDefault(_inherits2); _objectWithoutProperties2 = _interopRequireDefault(_objectWithoutProperties2); _extends2 = _interopRequireDefault(_extends2); _classCallCheck2 = _interopRequireDefault(_classCallCheck2); _defineProperty2 = _interopRequireDefault(_defineProperty2); _react = _interopRequireDefault(_react); _propTypes = _interopRequireDefault(_propTypes); _dialog = _interopRequireDefault(_dialog); _DialogContent = _interopRequireDefault(_DialogContent); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } var noop = function noop() {}; var isPromise = function isPromise(content) { return Promise.resolve(content) === content; }; var DialogForm = function DialogForm(options, getDialogInstance) { var _this = this; (0, _classCallCheck2["default"])(this, DialogForm); (0, _defineProperty2["default"])(this, "hide", function () { var dialogInstance = _this.getDialogInstance(); dialogInstance.hide(); }); (0, _defineProperty2["default"])(this, "handleCancel", function () { var onCancel = _this.options.onCancel; if (onCancel && typeof onCancel === "function") { onCancel(); } _this.hide(); }); (0, _defineProperty2["default"])(this, "handleOk", function () { var _this$options = _this.options, _this$options$onOk = _this$options.onOk, onOk = _this$options$onOk === void 0 ? _this.hide : _this$options$onOk, enableValidate = _this$options.enableValidate; var values = {}; if (_this.dialogCore) values = _this.dialogCore.getValues(); var params = [values, _this.hide, _this.dialogCore]; if (enableValidate && _this.dialogCore) { return _this.dialogCore.validate(function (err) { if (!err) return onOk.apply(void 0, params); return null; }); } return onOk.apply(void 0, params); }); (0, _defineProperty2["default"])(this, "renderFooter", function (Button, opts) { var _ref = opts || {}, layout = _ref.layout, _ref$loading = _ref.loading, loading = _ref$loading === void 0 ? false : _ref$loading; var _ref2 = layout || {}, label = _ref2.label; var _this$options2 = _this.options, _this$options2$locale = _this$options2.locale, locale = _this$options2$locale === void 0 ? "en" : _this$options2$locale, footer = _this$options2.footer, btnLoadingPropsName = _this$options2.btnLoadingPropsName, footerAlign = _this$options2.footerAlign; var textMap = _dialog["default"][locale]; var _this$options$hasCanc = _this.options.hasCancel, hasCancel = _this$options$hasCanc === void 0 ? true : _this$options$hasCanc; var okText = _this.options.okText || textMap.ok; var cancelText = _this.options.cancelText || textMap.cancel; var footerElement = null; if (footer) { var footerOpts = Object.assign(opts || {}); footerOpts.ok = _this.handleOk; footerOpts.cancel = _this.handleCancel; if (_this.dialogCore) footerOpts.ctx = _this.dialogCore; footerElement = footer(_this.hide, footerOpts); } else { var styleProps = {}; var alignCls = ["left", "center", "right"].indexOf(footerAlign) !== -1 ? "align-".concat(footerAlign) : ""; if (footerAlign === "label" && label) { styleProps.style = { marginLeft: "".concat(Number(label / 24).toFixed(2) * 100, "%"), "float": "none" }; } footerElement = _react["default"].createElement(_noformJson.If, { when: function when(values, ctx) { return ctx.globalStatus !== "preview"; } }, _react["default"].createElement("div", (0, _extends2["default"])({ key: "footer", className: "noform-dialog-custom-btns ".concat(alignCls) }, styleProps), hasCancel ? _react["default"].createElement(ActionButton, { disabled: loading, key: "align-footer-cancel", btnLoadingPropsName: btnLoadingPropsName, btnOrigin: Button, onClick: _this.handleCancel }, cancelText) : null, _react["default"].createElement(ActionButton, { disabled: loading, key: "align-footer-ok", btnLoadingPropsName: btnLoadingPropsName, btnOrigin: Button, type: "primary", onClick: _this.handleOk }, okText))); } return footerElement; }); (0, _defineProperty2["default"])(this, "renderContent", function (Button) { var _this$options3 = _this.options, content = _this$options3.content, task = _this$options3.task; var onDialogMount = function onDialogMount(core) { _this.dialogCore = core; }; var footer = _this.renderFooter.bind(_this, Button); return _react["default"].createElement(_DialogContent["default"], { content: content, task: task, footer: footer, onMount: onDialogMount }); }); this.options = options; this.dialogCore = null; this.getDialogInstance = getDialogInstance; }; var DialogFormFactory = function DialogFormFactory(_ref3) { var _this2 = this; var _Dialog = _ref3.Dialog, _Button = _ref3.Button, _compatiMap = _ref3.compatiMap; (0, _classCallCheck2["default"])(this, DialogFormFactory); (0, _defineProperty2["default"])(this, "show", function (options) { var Dialog = _this2.Dialog, Button = _this2.Button, compatiMap = _this2.compatiMap; if (!Dialog || !Button) { throw Error("DialogForm initialize failed, make sure you have passed antd components"); } var closablePolifill = options.closablePolifill, title = options.title, className = options.className, width = options.width, others = (0, _objectWithoutProperties2["default"])(options, ["closablePolifill", "title", "className", "width"]); var dialogInstance = null; // 按钮loading属性 var btnLoadingPropsName = compatiMap.btnLoadingProps || "loading"; var dialogForm = new DialogForm(_objectSpread({}, options, { compatiMap: compatiMap, btnLoadingPropsName: btnLoadingPropsName }), function () { return dialogInstance; }); var content = dialogForm.renderContent(Button); // 入口属性 var entryProps = compatiMap.show(_objectSpread({}, options, { title: title, content: content })); if (entryProps.closablePolifill) { entryProps.title = _react["default"].createElement("div", null, title, entryProps.closablePolifill(function () { return dialogInstance; })); } dialogInstance = Dialog.show(_objectSpread({}, others, {}, entryProps)); dialogInstance = compatiMap.dialogInstance(dialogInstance); return dialogInstance; }); this.Dialog = _Dialog; this.Button = _Button; this.compatiMap = _compatiMap; var show = _Dialog.show, _others = (0, _objectWithoutProperties2["default"])(_Dialog, ["show"]); Object.keys(_others).forEach(function (key) { _this2[key] = _others[key]; }); }; var ActionButton = /*#__PURE__*/ function (_React$Component) { (0, _inherits2["default"])(ActionButton, _React$Component); function ActionButton(props, context) { var _this3; (0, _classCallCheck2["default"])(this, ActionButton); _this3 = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(ActionButton).call(this, props, context)); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this3), "enableLoading", function () { var onLoading = _this3.props.onLoading; if (onLoading) { onLoading(); } _this3.setState({ isLoading: true }); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this3), "disableLoading", function () { var offLoading = _this3.props.offLoading; if (offLoading) { offLoading(); } _this3.setState({ isLoading: false }); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this3), "handleAction", function () { var onClick = _this3.props.onClick; if (typeof onClick === "function") { _this3.enableLoading(); var actionResult = onClick(); if (isPromise(actionResult)) { actionResult.then(_this3.disableLoading, _this3.disableLoading)["catch"](_this3.disableLoading); } else { _this3.disableLoading(); } } }); _this3.state = { isLoading: false }; return _this3; } (0, _createClass2["default"])(ActionButton, [{ key: "render", value: function render() { var _this$props = this.props, onClick = _this$props.onClick, _this$props$btnLoadin = _this$props.btnLoadingPropsName, btnLoadingPropsName = _this$props$btnLoadin === void 0 ? "loading" : _this$props$btnLoadin, btnOrigin = _this$props.btnOrigin, others = (0, _objectWithoutProperties2["default"])(_this$props, ["onClick", "btnLoadingPropsName", "btnOrigin"]); var Button = btnOrigin; var isLoading = this.state.isLoading; var btnLoadingProps = (0, _defineProperty2["default"])({}, btnLoadingPropsName, isLoading); return _react["default"].createElement(Button, (0, _extends2["default"])({ onClick: this.handleAction }, others, btnLoadingProps)); } }]); return ActionButton; }(_react["default"].Component); (0, _defineProperty2["default"])(ActionButton, "propTypes", { onLoading: _propTypes["default"].func, offLoading: _propTypes["default"].func, onClick: _propTypes["default"].func, btnLoadingPropsName: _propTypes["default"].string, btnOrigin: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].func]) }); var _default = DialogFormFactory; _exports["default"] = _default; });