| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  | import Vue from 'vue' | 
					
						
							| 
									
										
										
										
											2021-03-02 14:18:30 +08:00
										 |  |  | import {t} from '../_locale' | 
					
						
							| 
									
										
										
										
											2021-12-08 14:04:41 +08:00
										 |  |  | import Dialog from './dialog.vue' | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  | /* istanbul ignore next */ | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  | const noop = function() {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  | // all active instances
 | 
					
						
							|  |  |  | const instances = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Dialog factory | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {Object} props | 
					
						
							|  |  |  |  * @return {Dialog} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-09-04 15:09:13 +08:00
										 |  |  | const generate = function({ | 
					
						
							|  |  |  |   title = '', | 
					
						
							|  |  |  |   icon = '', | 
					
						
							|  |  |  |   iconSvg = false, | 
					
						
							|  |  |  |   content = '', | 
					
						
							|  |  |  |   closable = false, | 
					
						
							| 
									
										
										
										
											2021-01-15 11:17:58 +08:00
										 |  |  |   transition = 'md-bounce', | 
					
						
							| 
									
										
										
										
											2019-09-04 15:09:13 +08:00
										 |  |  |   btns = [], | 
					
						
							|  |  |  |   onShow = noop, | 
					
						
							|  |  |  |   onHide = noop, | 
					
						
							|  |  |  | }) { | 
					
						
							| 
									
										
										
										
											2018-12-10 15:43:15 +08:00
										 |  |  |   const DialogConstructor = Vue.extend(Dialog) | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |   const vm = new DialogConstructor({ | 
					
						
							|  |  |  |     propsData: { | 
					
						
							| 
									
										
										
										
											2019-09-04 15:09:13 +08:00
										 |  |  |       value: false, | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |       title, | 
					
						
							|  |  |  |       icon, | 
					
						
							| 
									
										
										
										
											2018-10-15 15:49:02 +08:00
										 |  |  |       iconSvg, | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |       content, | 
					
						
							|  |  |  |       closable, | 
					
						
							|  |  |  |       btns, | 
					
						
							| 
									
										
										
										
											2021-01-15 11:17:58 +08:00
										 |  |  |       transition, | 
					
						
							| 
									
										
										
										
											2019-01-21 18:13:41 +08:00
										 |  |  |       preventScroll: true, | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }).$mount() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  |   instances.push(vm) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |   vm.$on('input', val => { | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  |     /* istanbul ignore else */ | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |     if (!val) { | 
					
						
							|  |  |  |       vm.value = false | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   vm.$on('hide', () => { | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  |     const index = instances.indexOf(vm) | 
					
						
							|  |  |  |     /* istanbul ignore else */ | 
					
						
							|  |  |  |     if (index >= 0) { | 
					
						
							|  |  |  |       instances.splice(index, 1) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |     vm.$destroy() | 
					
						
							| 
									
										
										
										
											2019-09-04 15:09:13 +08:00
										 |  |  |     onHide() | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   vm.$on('show', () => { | 
					
						
							|  |  |  |     onShow() | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 15:09:13 +08:00
										 |  |  |   vm.value = true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |   return vm | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Dynamically create a confirm dialog | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {Object} props | 
					
						
							|  |  |  |  * @return {Dialog} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Dialog.confirm = ({ | 
					
						
							|  |  |  |   title = '', | 
					
						
							|  |  |  |   icon = '', | 
					
						
							| 
									
										
										
										
											2018-10-15 15:49:02 +08:00
										 |  |  |   iconSvg = false, | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  |   content = '', | 
					
						
							| 
									
										
										
										
											2021-03-02 14:18:30 +08:00
										 |  |  |   cancelText = t('md.dialog.cancel'), | 
					
						
							| 
									
										
										
										
											2018-11-30 20:21:57 +08:00
										 |  |  |   cancelWarning = false, | 
					
						
							| 
									
										
										
										
											2021-03-02 14:18:30 +08:00
										 |  |  |   confirmText = t('md.dialog.confirm'), | 
					
						
							| 
									
										
										
										
											2018-11-30 20:21:57 +08:00
										 |  |  |   confirmWarning = false, | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  |   closable = false, | 
					
						
							| 
									
										
										
										
											2021-01-15 11:17:58 +08:00
										 |  |  |   transition, | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  |   onConfirm = noop, | 
					
						
							| 
									
										
										
										
											2018-11-30 19:33:46 +08:00
										 |  |  |   onCancel = noop, | 
					
						
							| 
									
										
										
										
											2019-09-04 15:09:13 +08:00
										 |  |  |   onShow = noop, | 
					
						
							|  |  |  |   onHide = noop, | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  | }) => { | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |   const vm = generate({ | 
					
						
							|  |  |  |     title, | 
					
						
							|  |  |  |     icon, | 
					
						
							| 
									
										
										
										
											2018-10-15 15:49:02 +08:00
										 |  |  |     iconSvg, | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |     content, | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  |     closable, | 
					
						
							| 
									
										
										
										
											2021-01-15 11:17:58 +08:00
										 |  |  |     transition, | 
					
						
							| 
									
										
										
										
											2019-09-04 15:09:13 +08:00
										 |  |  |     onShow, | 
					
						
							|  |  |  |     onHide, | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |     btns: [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         text: cancelText, | 
					
						
							| 
									
										
										
										
											2018-11-30 20:21:57 +08:00
										 |  |  |         warning: cancelWarning, | 
					
						
							| 
									
										
										
										
											2018-11-30 19:33:46 +08:00
										 |  |  |         handler: /* istanbul ignore next */ () => { | 
					
						
							|  |  |  |           if (onCancel() !== false) { | 
					
						
							|  |  |  |             vm.close() | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |       }, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         text: confirmText, | 
					
						
							| 
									
										
										
										
											2018-11-30 20:21:57 +08:00
										 |  |  |         warning: confirmWarning, | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  |         handler: /* istanbul ignore next */ () => { | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |           if (onConfirm() !== false) { | 
					
						
							|  |  |  |             vm.close() | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return vm | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Dynamically create a alert dialog | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {Object} props | 
					
						
							|  |  |  |  * @return {Dialog} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-10-15 15:49:02 +08:00
										 |  |  | Dialog.alert = ({ | 
					
						
							|  |  |  |   title = '', | 
					
						
							|  |  |  |   icon = '', | 
					
						
							|  |  |  |   iconSvg = false, | 
					
						
							|  |  |  |   content = '', | 
					
						
							| 
									
										
										
										
											2021-03-02 14:18:30 +08:00
										 |  |  |   confirmText = t('md.dialog.confirm'), | 
					
						
							| 
									
										
										
										
											2018-10-15 15:49:02 +08:00
										 |  |  |   closable = false, | 
					
						
							| 
									
										
										
										
											2018-11-30 20:21:57 +08:00
										 |  |  |   warning = false, | 
					
						
							| 
									
										
										
										
											2021-01-15 11:17:58 +08:00
										 |  |  |   transition, | 
					
						
							| 
									
										
										
										
											2018-10-15 15:49:02 +08:00
										 |  |  |   onConfirm = noop, | 
					
						
							| 
									
										
										
										
											2019-09-04 15:09:13 +08:00
										 |  |  |   onShow = noop, | 
					
						
							|  |  |  |   onHide = noop, | 
					
						
							| 
									
										
										
										
											2018-10-15 15:49:02 +08:00
										 |  |  | }) => { | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |   const vm = generate({ | 
					
						
							|  |  |  |     title, | 
					
						
							|  |  |  |     icon, | 
					
						
							| 
									
										
										
										
											2018-10-15 15:49:02 +08:00
										 |  |  |     iconSvg, | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |     content, | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  |     closable, | 
					
						
							| 
									
										
										
										
											2021-01-15 11:17:58 +08:00
										 |  |  |     transition, | 
					
						
							| 
									
										
										
										
											2019-09-04 15:09:13 +08:00
										 |  |  |     onShow, | 
					
						
							|  |  |  |     onHide, | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |     btns: [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         text: confirmText, | 
					
						
							| 
									
										
										
										
											2018-11-30 20:21:57 +08:00
										 |  |  |         warning, | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  |         handler: /* istanbul ignore next */ () => { | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |           if (onConfirm() !== false) { | 
					
						
							|  |  |  |             vm.close() | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return vm | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Dynamically create a succeed dialog | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {Object} props | 
					
						
							|  |  |  |  * @return {Dialog} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  | Dialog.succeed = props => { | 
					
						
							| 
									
										
										
										
											2018-11-15 15:02:35 +08:00
										 |  |  |   props.icon = 'success-color' | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |   return Dialog.confirm(props) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Dynamically create a failed dialog | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {Object} props | 
					
						
							|  |  |  |  * @return {Dialog} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  | Dialog.failed = props => { | 
					
						
							| 
									
										
										
										
											2018-11-15 15:02:35 +08:00
										 |  |  |   props.icon = 'warn-color' | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  |   return Dialog.confirm(props) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 14:47:43 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Close all actived static dialogs | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @static | 
					
						
							|  |  |  |  * @return void | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Dialog.closeAll = () => { | 
					
						
							|  |  |  |   instances.forEach(instance => { | 
					
						
							|  |  |  |     instance.close() | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-26 16:04:04 +08:00
										 |  |  | export default Dialog |