UNPKG

vue-notifications

Version:
1 lines 2.77 kB
var DEFAULT_MESSAGE_TYPES;!function(DEFAULT_MESSAGE_TYPES){DEFAULT_MESSAGE_TYPES.error="error",DEFAULT_MESSAGE_TYPES.warn="warn",DEFAULT_MESSAGE_TYPES.info="info",DEFAULT_MESSAGE_TYPES.success="success"}(DEFAULT_MESSAGE_TYPES||(DEFAULT_MESSAGE_TYPES={}));const VueNotifications={types:{error:DEFAULT_MESSAGE_TYPES.error,warn:DEFAULT_MESSAGE_TYPES.warn,info:DEFAULT_MESSAGE_TYPES.info,success:DEFAULT_MESSAGE_TYPES.success},propertyName:"notifications",config:{type:DEFAULT_MESSAGE_TYPES.info,timeout:3e3},pluginOptions:{},installed:!1,install(vueConstructor,pluginOptions){if(this.installed)throw console.error("VueNotifications: plugin already installed");const mixin=makeMixin();vueConstructor.mixin(mixin),this.setPluginOptions(pluginOptions),this.installed=!0},setPluginOptions(pluginOptions){this.pluginOptions=pluginOptions}};function getValues(config,vueApp){let result={type:DEFAULT_MESSAGE_TYPES.info};return Object.keys(config).forEach(field=>{result="cb"===field?{...result,[field]:config[field].bind(vueApp)}:{...result,[field]:"function"==typeof config[field]?config[field].call(vueApp):config[field]}}),result}function showMessage(config,vueApp){const valuesObj=getValues(config,vueApp);(VueNotifications.pluginOptions[valuesObj.type]?VueNotifications.pluginOptions[valuesObj.type]:console.log)(valuesObj,vueApp),config.cb&&config.cb()}function setMethod(vueApp,name,componentOptions){let{methods:methods}=componentOptions;methods||(methods={}),methods[name]?console.warn(`VueNotifications: trying to create method which is already exist: ${name}`):methods[name]=makeMethod(vueApp,name,componentOptions)}function makeMethod(vueApp,methodName,componentOptions){return config=>{const pluginOptions=componentOptions[VueNotifications.propertyName],methodConfig=pluginOptions?pluginOptions[methodName]:{};showMessage({...VueNotifications.config,...methodConfig,...config},vueApp)}}function initVueNotificationPlugin(vueApp,notifications){notifications&&(Object.keys(notifications).forEach(name=>setMethod(vueApp,name,vueApp.$options)),vueApp.$emit("vue-notifications-initiated"))}function unlinkVueNotificationPlugin(vueApp,notifications){if(!notifications)return;const{methods:methods}=vueApp.$options;methods&&(Object.keys(notifications).forEach(name=>{methods[name]&&(methods[name]=void 0,delete methods[name])}),vueApp.$emit("vue-notifications-unlinked"))}function makeMixin(){return{beforeCreate(){const notificationsField=this.$options[VueNotifications.propertyName];notificationsField&&initVueNotificationPlugin(this,notificationsField)},beforeDestroy:function(){unlinkVueNotificationPlugin(this,this.$options[VueNotifications.propertyName])}}}"undefined"!=typeof window&&window.Vue&&window.Vue.use(VueNotifications);export default VueNotifications;