Dialog.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default,
  21. dialogProps: () => dialogProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_vue2 = require("vue");
  26. var import_utils = require("../utils");
  27. var import_shared = require("../popup/shared");
  28. var import_popup = require("../popup");
  29. var import_button = require("../button");
  30. var import_action_bar = require("../action-bar");
  31. var import_action_bar_button = require("../action-bar-button");
  32. const [name, bem, t] = (0, import_utils.createNamespace)("dialog");
  33. const dialogProps = (0, import_utils.extend)({}, import_shared.popupSharedProps, {
  34. title: String,
  35. theme: String,
  36. width: import_utils.numericProp,
  37. message: [String, Function],
  38. callback: Function,
  39. allowHtml: Boolean,
  40. className: import_utils.unknownProp,
  41. transition: (0, import_utils.makeStringProp)("van-dialog-bounce"),
  42. messageAlign: String,
  43. closeOnPopstate: import_utils.truthProp,
  44. showCancelButton: Boolean,
  45. cancelButtonText: String,
  46. cancelButtonColor: String,
  47. cancelButtonDisabled: Boolean,
  48. confirmButtonText: String,
  49. confirmButtonColor: String,
  50. confirmButtonDisabled: Boolean,
  51. showConfirmButton: import_utils.truthProp,
  52. closeOnClickOverlay: Boolean
  53. });
  54. const popupInheritKeys = [...import_shared.popupSharedPropKeys, "transition", "closeOnPopstate"];
  55. var stdin_default = (0, import_vue2.defineComponent)({
  56. name,
  57. props: dialogProps,
  58. emits: ["confirm", "cancel", "keydown", "update:show"],
  59. setup(props, {
  60. emit,
  61. slots
  62. }) {
  63. const root = (0, import_vue2.ref)();
  64. const loading = (0, import_vue2.reactive)({
  65. confirm: false,
  66. cancel: false
  67. });
  68. const updateShow = (value) => emit("update:show", value);
  69. const close = (action) => {
  70. var _a;
  71. updateShow(false);
  72. (_a = props.callback) == null ? void 0 : _a.call(props, action);
  73. };
  74. const getActionHandler = (action) => () => {
  75. if (!props.show) {
  76. return;
  77. }
  78. emit(action);
  79. if (props.beforeClose) {
  80. loading[action] = true;
  81. (0, import_utils.callInterceptor)(props.beforeClose, {
  82. args: [action],
  83. done() {
  84. close(action);
  85. loading[action] = false;
  86. },
  87. canceled() {
  88. loading[action] = false;
  89. }
  90. });
  91. } else {
  92. close(action);
  93. }
  94. };
  95. const onCancel = getActionHandler("cancel");
  96. const onConfirm = getActionHandler("confirm");
  97. const onKeydown = (0, import_vue2.withKeys)((event) => {
  98. var _a, _b;
  99. if (event.target !== ((_b = (_a = root.value) == null ? void 0 : _a.popupRef) == null ? void 0 : _b.value)) {
  100. return;
  101. }
  102. const onEventType = {
  103. Enter: props.showConfirmButton ? onConfirm : import_utils.noop,
  104. Escape: props.showCancelButton ? onCancel : import_utils.noop
  105. };
  106. onEventType[event.key]();
  107. emit("keydown", event);
  108. }, ["enter", "esc"]);
  109. const renderTitle = () => {
  110. const title = slots.title ? slots.title() : props.title;
  111. if (title) {
  112. return (0, import_vue.createVNode)("div", {
  113. "class": bem("header", {
  114. isolated: !props.message && !slots.default
  115. })
  116. }, [title]);
  117. }
  118. };
  119. const renderMessage = (hasTitle) => {
  120. const {
  121. message,
  122. allowHtml,
  123. messageAlign
  124. } = props;
  125. const classNames = bem("message", {
  126. "has-title": hasTitle,
  127. [messageAlign]: messageAlign
  128. });
  129. const content = (0, import_utils.isFunction)(message) ? message() : message;
  130. if (allowHtml && typeof content === "string") {
  131. return (0, import_vue.createVNode)("div", {
  132. "class": classNames,
  133. "innerHTML": content
  134. }, null);
  135. }
  136. return (0, import_vue.createVNode)("div", {
  137. "class": classNames
  138. }, [content]);
  139. };
  140. const renderContent = () => {
  141. if (slots.default) {
  142. return (0, import_vue.createVNode)("div", {
  143. "class": bem("content")
  144. }, [slots.default()]);
  145. }
  146. const {
  147. title,
  148. message,
  149. allowHtml
  150. } = props;
  151. if (message) {
  152. const hasTitle = !!(title || slots.title);
  153. return (0, import_vue.createVNode)("div", {
  154. "key": allowHtml ? 1 : 0,
  155. "class": bem("content", {
  156. isolated: !hasTitle
  157. })
  158. }, [renderMessage(hasTitle)]);
  159. }
  160. };
  161. const renderButtons = () => (0, import_vue.createVNode)("div", {
  162. "class": [import_utils.BORDER_TOP, bem("footer")]
  163. }, [props.showCancelButton && (0, import_vue.createVNode)(import_button.Button, {
  164. "size": "large",
  165. "text": props.cancelButtonText || t("cancel"),
  166. "class": bem("cancel"),
  167. "style": {
  168. color: props.cancelButtonColor
  169. },
  170. "loading": loading.cancel,
  171. "disabled": props.cancelButtonDisabled,
  172. "onClick": onCancel
  173. }, null), props.showConfirmButton && (0, import_vue.createVNode)(import_button.Button, {
  174. "size": "large",
  175. "text": props.confirmButtonText || t("confirm"),
  176. "class": [bem("confirm"), {
  177. [import_utils.BORDER_LEFT]: props.showCancelButton
  178. }],
  179. "style": {
  180. color: props.confirmButtonColor
  181. },
  182. "loading": loading.confirm,
  183. "disabled": props.confirmButtonDisabled,
  184. "onClick": onConfirm
  185. }, null)]);
  186. const renderRoundButtons = () => (0, import_vue.createVNode)(import_action_bar.ActionBar, {
  187. "class": bem("footer")
  188. }, {
  189. default: () => [props.showCancelButton && (0, import_vue.createVNode)(import_action_bar_button.ActionBarButton, {
  190. "type": "warning",
  191. "text": props.cancelButtonText || t("cancel"),
  192. "class": bem("cancel"),
  193. "color": props.cancelButtonColor,
  194. "loading": loading.cancel,
  195. "disabled": props.cancelButtonDisabled,
  196. "onClick": onCancel
  197. }, null), props.showConfirmButton && (0, import_vue.createVNode)(import_action_bar_button.ActionBarButton, {
  198. "type": "danger",
  199. "text": props.confirmButtonText || t("confirm"),
  200. "class": bem("confirm"),
  201. "color": props.confirmButtonColor,
  202. "loading": loading.confirm,
  203. "disabled": props.confirmButtonDisabled,
  204. "onClick": onConfirm
  205. }, null)]
  206. });
  207. const renderFooter = () => {
  208. if (slots.footer) {
  209. return slots.footer();
  210. }
  211. return props.theme === "round-button" ? renderRoundButtons() : renderButtons();
  212. };
  213. return () => {
  214. const {
  215. width,
  216. title,
  217. theme,
  218. message,
  219. className
  220. } = props;
  221. return (0, import_vue.createVNode)(import_popup.Popup, (0, import_vue.mergeProps)({
  222. "ref": root,
  223. "role": "dialog",
  224. "class": [bem([theme]), className],
  225. "style": {
  226. width: (0, import_utils.addUnit)(width)
  227. },
  228. "tabindex": 0,
  229. "aria-labelledby": title || message,
  230. "onKeydown": onKeydown,
  231. "onUpdate:show": updateShow
  232. }, (0, import_utils.pick)(props, popupInheritKeys)), {
  233. default: () => [renderTitle(), renderContent(), renderFooter()]
  234. });
  235. };
  236. }
  237. });