dialog.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import { isObject, toCamel } from '../common/utils';
  11. const { prefix } = config;
  12. const name = `${prefix}-dialog`;
  13. let Dialog = class Dialog extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.options = {
  17. multipleSlots: true,
  18. addGlobalClass: true,
  19. };
  20. this.externalClasses = [
  21. `${prefix}-class`,
  22. `${prefix}-class-content`,
  23. `${prefix}-class-confirm`,
  24. `${prefix}-class-cancel`,
  25. `${prefix}-class-action`,
  26. ];
  27. this.properties = props;
  28. this.data = {
  29. prefix,
  30. classPrefix: name,
  31. buttonVariant: 'text',
  32. };
  33. this.observers = {
  34. 'confirmBtn, cancelBtn'(confirm, cancel) {
  35. const { prefix, classPrefix, buttonLayout } = this.data;
  36. const rect = { buttonVariant: 'text' };
  37. const useBaseVariant = [confirm, cancel].some((item) => isObject(item) && item.variant && item.variant !== 'text');
  38. const buttonMap = { confirm, cancel };
  39. const cls = [`${classPrefix}__button`];
  40. const externalCls = [];
  41. if (useBaseVariant) {
  42. rect.buttonVariant = 'base';
  43. cls.push(`${classPrefix}__button--${buttonLayout}`);
  44. }
  45. else {
  46. cls.push(`${classPrefix}__button--text`);
  47. externalCls.push(`${classPrefix}-button`);
  48. }
  49. Object.keys(buttonMap).forEach((key) => {
  50. const btn = buttonMap[key];
  51. const base = {
  52. block: true,
  53. class: [...cls, `${classPrefix}__button--${key}`],
  54. externalClass: [...externalCls, `${prefix}-class-${key}`],
  55. variant: rect.buttonVariant,
  56. };
  57. if (key === 'cancel' && rect.buttonVariant === 'base') {
  58. base.theme = 'light';
  59. }
  60. if (typeof btn === 'string') {
  61. rect[`_${key}`] = Object.assign(Object.assign({}, base), { content: btn });
  62. }
  63. else if (btn && typeof btn === 'object') {
  64. rect[`_${key}`] = Object.assign(Object.assign({}, base), btn);
  65. }
  66. });
  67. this.setData(Object.assign({}, rect));
  68. },
  69. };
  70. this.methods = {
  71. onTplButtonTap(e) {
  72. var _a, _b;
  73. const evtType = e.type;
  74. const { type, extra } = e.target.dataset;
  75. const button = this.data[`_${type}`];
  76. const cbName = `bind${evtType}`;
  77. if (type === 'action') {
  78. this.onActionTap(extra);
  79. return;
  80. }
  81. if (typeof button[cbName] === 'function') {
  82. const closeFlag = button[cbName](e);
  83. if (closeFlag) {
  84. this.close();
  85. }
  86. }
  87. const hasOpenType = 'openType' in button;
  88. if (!hasOpenType && ['confirm', 'cancel'].includes(type)) {
  89. (_a = this[toCamel(`on-${type}`)]) === null || _a === void 0 ? void 0 : _a.call(this, type);
  90. }
  91. if (evtType !== 'tap') {
  92. const success = ((_b = e.detail) === null || _b === void 0 ? void 0 : _b.errMsg.indexOf('ok')) > -1;
  93. this.triggerEvent(success ? 'open-type-event' : 'open-type-error-event', e.detail);
  94. }
  95. },
  96. onConfirm() {
  97. this.triggerEvent('confirm');
  98. if (this._onComfirm) {
  99. this._onComfirm();
  100. this.close();
  101. }
  102. },
  103. onCancel() {
  104. this.triggerEvent('close', { trigger: 'cancel' });
  105. this.triggerEvent('cancel');
  106. if (this._onCancel) {
  107. this._onCancel();
  108. this.close();
  109. }
  110. },
  111. onClose() {
  112. this.triggerEvent('close', { trigger: 'close-btn' });
  113. this.close();
  114. },
  115. close() {
  116. this.setData({ visible: false });
  117. },
  118. overlayClick() {
  119. if (this.properties.closeOnOverlayClick) {
  120. this.triggerEvent('close', { trigger: 'overlay' });
  121. }
  122. this.triggerEvent('overlayClick');
  123. },
  124. onActionTap(index) {
  125. this.triggerEvent('action', { index });
  126. if (this._onAction) {
  127. this._onAction({ index });
  128. this.close();
  129. }
  130. },
  131. openValueCBHandle(e) {
  132. this.triggerEvent('open-type-event', e.detail);
  133. },
  134. openValueErrCBHandle(e) {
  135. this.triggerEvent('open-type-error-event', e.detail);
  136. },
  137. };
  138. }
  139. };
  140. Dialog = __decorate([
  141. wxComponent()
  142. ], Dialog);
  143. export default Dialog;