main.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. const defaults = {
  2. title: null,
  3. message: '',
  4. type: '',
  5. showInput: false,
  6. showClose: true,
  7. modalFade: true,
  8. lockScroll: true,
  9. closeOnClickModal: true,
  10. closeOnPressEscape: true,
  11. closeOnHashChange: true,
  12. inputValue: null,
  13. inputPlaceholder: '',
  14. inputType: 'text',
  15. inputPattern: null,
  16. inputValidator: null,
  17. inputErrorMessage: '',
  18. showConfirmButton: true,
  19. showCancelButton: false,
  20. confirmButtonPosition: 'right',
  21. confirmButtonHighlight: false,
  22. cancelButtonHighlight: false,
  23. confirmButtonText: '',
  24. cancelButtonText: '',
  25. confirmButtonClass: '',
  26. cancelButtonClass: '',
  27. customClass: '',
  28. beforeClose: null,
  29. dangerouslyUseHTMLString: false,
  30. center: false,
  31. roundButton: false
  32. };
  33. import Vue from 'vue';
  34. import msgboxVue from './main.vue';
  35. import merge from 'element-ui/src/utils/merge';
  36. import { isVNode } from 'element-ui/src/utils/vdom';
  37. const MessageBoxConstructor = Vue.extend(msgboxVue);
  38. let currentMsg, instance;
  39. let msgQueue = [];
  40. const defaultCallback = action => {
  41. if (currentMsg) {
  42. let callback = currentMsg.callback;
  43. if (typeof callback === 'function') {
  44. if (instance.showInput) {
  45. callback(instance.inputValue, action);
  46. } else {
  47. callback(action);
  48. }
  49. }
  50. if (currentMsg.resolve) {
  51. if (action === 'confirm') {
  52. if (instance.showInput) {
  53. currentMsg.resolve({ value: instance.inputValue, action });
  54. } else {
  55. currentMsg.resolve(action);
  56. }
  57. } else if (action === 'cancel' && currentMsg.reject) {
  58. currentMsg.reject(action);
  59. }
  60. }
  61. }
  62. };
  63. const initInstance = () => {
  64. instance = new MessageBoxConstructor({
  65. el: document.createElement('div')
  66. });
  67. instance.callback = defaultCallback;
  68. };
  69. const showNextMsg = () => {
  70. if (!instance) {
  71. initInstance();
  72. }
  73. instance.action = '';
  74. if (!instance.visible || instance.closeTimer) {
  75. if (msgQueue.length > 0) {
  76. currentMsg = msgQueue.shift();
  77. let options = currentMsg.options;
  78. for (let prop in options) {
  79. if (options.hasOwnProperty(prop)) {
  80. instance[prop] = options[prop];
  81. }
  82. }
  83. if (options.callback === undefined) {
  84. instance.callback = defaultCallback;
  85. }
  86. let oldCb = instance.callback;
  87. instance.callback = (action, instance) => {
  88. oldCb(action, instance);
  89. showNextMsg();
  90. };
  91. if (isVNode(instance.message)) {
  92. instance.$slots.default = [instance.message];
  93. instance.message = null;
  94. } else {
  95. delete instance.$slots.default;
  96. }
  97. ['modal', 'showClose', 'closeOnClickModal', 'closeOnPressEscape', 'closeOnHashChange'].forEach(prop => {
  98. if (instance[prop] === undefined) {
  99. instance[prop] = true;
  100. }
  101. });
  102. document.body.appendChild(instance.$el);
  103. Vue.nextTick(() => {
  104. instance.visible = true;
  105. });
  106. }
  107. }
  108. };
  109. const MessageBox = function(options, callback) {
  110. if (Vue.prototype.$isServer) return;
  111. if (typeof options === 'string' || isVNode(options)) {
  112. options = {
  113. message: options
  114. };
  115. if (typeof arguments[1] === 'string') {
  116. options.title = arguments[1];
  117. }
  118. } else if (options.callback && !callback) {
  119. callback = options.callback;
  120. }
  121. if (typeof Promise !== 'undefined') {
  122. return new Promise((resolve, reject) => { // eslint-disable-line
  123. msgQueue.push({
  124. options: merge({}, defaults, MessageBox.defaults, options),
  125. callback: callback,
  126. resolve: resolve,
  127. reject: reject
  128. });
  129. showNextMsg();
  130. });
  131. } else {
  132. msgQueue.push({
  133. options: merge({}, defaults, MessageBox.defaults, options),
  134. callback: callback
  135. });
  136. showNextMsg();
  137. }
  138. };
  139. MessageBox.setDefaults = defaults => {
  140. MessageBox.defaults = defaults;
  141. };
  142. MessageBox.alert = (message, title, options) => {
  143. if (typeof title === 'object') {
  144. options = title;
  145. title = '';
  146. } else if (title === undefined) {
  147. title = '';
  148. }
  149. return MessageBox(merge({
  150. title: title,
  151. message: message,
  152. $type: 'alert',
  153. closeOnPressEscape: false,
  154. closeOnClickModal: false
  155. }, options));
  156. };
  157. MessageBox.confirm = (message, title, options) => {
  158. if (typeof title === 'object') {
  159. options = title;
  160. title = '';
  161. } else if (title === undefined) {
  162. title = '';
  163. }
  164. return MessageBox(merge({
  165. title: title,
  166. message: message,
  167. $type: 'confirm',
  168. showCancelButton: true
  169. }, options));
  170. };
  171. MessageBox.prompt = (message, title, options) => {
  172. if (typeof title === 'object') {
  173. options = title;
  174. title = '';
  175. } else if (title === undefined) {
  176. title = '';
  177. }
  178. return MessageBox(merge({
  179. title: title,
  180. message: message,
  181. showCancelButton: true,
  182. showInput: true,
  183. $type: 'prompt'
  184. }, options));
  185. };
  186. MessageBox.close = () => {
  187. instance.doClose();
  188. instance.visible = false;
  189. msgQueue = [];
  190. currentMsg = null;
  191. };
  192. export default MessageBox;
  193. export { MessageBox };