@ohu-mobile/core
Version:
236 lines (235 loc) • 10.5 kB
JavaScript
"use strict";
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.dialogProps = exports.default = void 0;
var _babelHelperVueJsxMergeProps = _interopRequireDefault(require("@vue/babel-helper-vue-jsx-merge-props"));
var _vueStrictProp = _interopRequireDefault(require("vue-strict-prop"));
var _Popup = _interopRequireDefault(require("../Popup"));
var _PopupWrapper = require("../Popup/PopupWrapper");
var _deepmerge = _interopRequireDefault(require("deepmerge"));
var _utils = require("./utils");
var _Button = _interopRequireDefault(require("../Button"));
var _Divider = _interopRequireDefault(require("../Divider"));
var _iconUtils = require("../_utils/icon-utils");
var _localeMixin = _interopRequireDefault(require("../_utils/localeMixin"));
var _variables = require("../_config/variables");
var _Image = _interopRequireDefault(require("../Image"));
var _defineComponent = require("../_utils/defineComponent");
var _excluded = ["icon", "title", "content", "cancelBtn", "okBtn", "image"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 defaultOKOptions(text) {
return {
type: 'ok',
text: text,
disabled: false
};
}
function defaultCancelOptions(text) {
return {
type: 'cancel',
text: text,
disabled: false,
color: _variables.$colorTextMinor
};
}
var dialogProps = exports.dialogProps = (0, _deepmerge.default)({
image: (0, _vueStrictProp.default)(String, Object).optional,
icon: (0, _vueStrictProp.default)(String, Object).optional,
title: String,
content: String,
cancelBtn: (0, _vueStrictProp.default)(String, Object).optional,
okBtn: (0, _vueStrictProp.default)(String, Object).optional,
actions: _vueStrictProp.default.ofType().default(function () {
return [];
}),
layout: _vueStrictProp.default.ofStringLiterals('row', 'column').default('row'),
closeAfterAsyncTaskCompleted: (0, _vueStrictProp.default)(Boolean).default(false)
}, _PopupWrapper.popupOutSideProps);
dialogProps.animate.default = 'zoom';
dialogProps.maskClosable.default = false;
var _default = exports.default = (0, _defineComponent.defineComponent)('dialog').mixin((0, _localeMixin.default)('OhuDialog')).create({
model: {
prop: 'visible',
event: 'visibleChange'
},
props: dialogProps,
data: function data() {
return {
actionBtns: [],
asyncActions: []
};
},
watch: {
actions: function actions() {
this.actionBtns = this.getActionBtns();
}
},
mounted: function mounted() {
this.actionBtns = this.getActionBtns();
},
methods: {
close: function close() {
var _this = this,
_this$$refs$popup;
if (this.asyncActions.length > 0 && this.closeAfterAsyncTaskCompleted) return;
this.asyncActions.map(function (action) {
if (action.loading === true) {
_this.$emit('abort', action);
}
_this.$set(action, 'loading', false);
});
this.asyncActions = [];
(_this$$refs$popup = this.$refs.popup) === null || _this$$refs$popup === void 0 || _this$$refs$popup.close();
},
getOKAction: function getOKAction() {
if (this.okBtn === null) return;
return (0, _utils.createActionOptions)(defaultOKOptions(this.$l.defaultOKText), this.okBtn);
},
getCancelAction: function getCancelAction() {
if (this.cancelBtn === null) return;
return (0, _utils.createActionOptions)(defaultCancelOptions(this.$l.defaultCancelText), this.cancelBtn);
},
getActionBtns: function getActionBtns() {
if (this.actions === null) return [];
var actionBtns = this.actions;
if (actionBtns.length === 0) {
var btns = [];
var cancelAction = this.getCancelAction();
if (cancelAction) btns.push(cancelAction);
var okAction = this.getOKAction();
if (okAction) btns.push(okAction);
actionBtns = btns;
}
return actionBtns;
},
createActionHandler: function createActionHandler(action) {
var _this2 = this;
var defaultHandler = function defaultHandler() {
action.type && _this2.$emit(action.type);
_this2.close();
};
if (action.type && !action.handle) return defaultHandler;
return function () {
action.type && _this2.$emit(action.type);
if (action.handle instanceof Function) {
_this2.asyncActions.push(action);
var returnValue = action.handle();
if (returnValue instanceof Promise) {
_this2.$set(action, 'loading', true);
returnValue.then(function (value) {
var index = _this2.asyncActions.indexOf(action);
if (index >= 0) {
_this2.$set(action, 'loading', false);
_this2.asyncActions.splice(index, 1);
if (value !== false) {
_this2.close();
}
}
});
} else {
if (returnValue !== false) {
_this2.close();
}
}
}
};
},
renderActions: function renderActions() {
var _this3 = this;
var h = this.$createElement;
var cls = this.$rootCls();
var eles = [];
this.actionBtns.forEach(function (action, index) {
var style = {
color: action.color
};
var handler = _this3.createActionHandler(action);
eles.push(h(_Button.default, {
"attrs": {
"inline": _this3.layout === 'row',
"link": true,
"type": "primary",
"loading": action.loading,
"disabled": action.loading ? true : action.disabled
},
"style": style,
"on": {
"click": handler
}
}, [action.text]));
if (index < _this3.actionBtns.length - 1) {
eles.push(h(_Divider.default, {
"attrs": {
"vertical": _this3.layout === 'row'
}
}));
}
});
return h("div", {
"class": cls.element('footer')
}, [h(_Divider.default), h("div", {
"class": cls.element('actions').is(this.layout)
}, [eles])]);
},
renderBody: function renderBody() {
var h = this.$createElement;
var icon;
if (this.icon) {
icon = (0, _iconUtils.getIcon)(this.$createElement, this.icon);
}
var $slots = this.$slots,
$rootCls = this.$rootCls,
title = this.title,
content = this.content;
var titleNode = $slots.title ? $slots.title : title;
var contentNode = $slots.default ? $slots.default : content;
var bodyCls = $rootCls().element('body');
return h("div", {
"class": bodyCls
}, [icon && h("div", {
"class": bodyCls.element('icon')
}, [icon]), titleNode && h("h1", {
"class": bodyCls.element('title')
}, [titleNode]), contentNode && h("p", {
"class": bodyCls.element('content')
}, [contentNode])]);
}
},
render: function render() {
var h = arguments[0];
var cls = this.$rootCls();
var _this$$props = this.$props,
icon = _this$$props.icon,
title = _this$$props.title,
content = _this$$props.content,
cancelBtn = _this$$props.cancelBtn,
okBtn = _this$$props.okBtn,
image = _this$$props.image,
popupProps = _objectWithoutProperties(_this$$props, _excluded);
var popupNodeData = {
props: _objectSpread(_objectSpread({}, popupProps), {}, {
targetClass: cls.addClasses(popupProps.targetClass)
}),
on: this.$listeners,
ref: 'popup'
};
return h(_Popup.default, (0, _babelHelperVueJsxMergeProps.default)([{}, popupNodeData]), [image && h("div", {
"class": cls.element('banner')
}, [typeof image === 'string' ? h(_Image.default, {
"attrs": {
"src": image
}
}) : h(_Image.default, {
"props": _objectSpread({}, image)
})]), this.renderBody(), this.renderActions()]);
}
});