Last active
April 17, 2018 15:14
-
-
Save tatat/a8da6b18ae4dcd6297cd9726c860cab9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
/** | |
* @param {Vue} Vue | |
* @param {any} options | |
*/ | |
install(Vue, options) { | |
/** | |
* @typedef {object} MessageBox | |
* @property {boolean} confirmed - To be set true if confirmed, otherwise false | |
* @property {VueComponent} instance - Instance of VueComponent for $msgbox() | |
* @property {() => Promise.<string>} close - Close message box | |
*/ | |
/** | |
* @name Vue#$messageBox | |
* @method | |
* @example | |
* const messageBox = await vm.$messageBox(options) | |
* | |
* try { | |
* if (messageBox.confirmed) { | |
* await onConfirmed() | |
* } else { | |
* await onCancelled() | |
* } | |
* } finally { | |
* await messageBox.close() | |
* } | |
* @param {any} options - To be passed to $msgbox() except "beforeClose" | |
* @return {Promise.<MessageBox>} | |
*/ | |
Vue.prototype.$messageBox = function(options = {}) { | |
return new Promise((resolve, reject) => { | |
if (!this.$msgbox) | |
return reject(new Error('element-ui is required')) | |
let _action | |
const promiseClosed = this.$msgbox({ | |
...options, | |
beforeClose(action, instance, done) { | |
_action = action | |
resolve({ | |
confirmed: action === 'confirm', | |
instance, | |
close: () => { | |
done() | |
return promiseClosed | |
} | |
}) | |
} | |
}).catch(error => { | |
if (error !== 'cancel') | |
throw error | |
}).then(() => _action) | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment