UNPKG

vue-toast-notification

Version:
510 lines (457 loc) 15.8 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("vue")); else if(typeof define === 'function' && define.amd) define("VueToast", ["vue"], factory); else if(typeof exports === 'object') exports["VueToast"] = factory(require("vue")); else root["VueToast"] = factory(root["Vue"]); })(typeof self !== 'undefined' ? self : this, function(__WEBPACK_EXTERNAL_MODULE__0__) { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); /******/ } /******/ }; /******/ /******/ // define __esModule on exports /******/ __webpack_require__.r = function(exports) { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ /******/ // create a fake namespace object /******/ // mode & 1: value is a module id, require it /******/ // mode & 2: merge all properties of value into the ns /******/ // mode & 4: return value when already ns object /******/ // mode & 8|1: behave like require /******/ __webpack_require__.t = function(value, mode) { /******/ if(mode & 1) value = __webpack_require__(value); /******/ if(mode & 8) return value; /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; /******/ var ns = Object.create(null); /******/ __webpack_require__.r(ns); /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); /******/ return ns; /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 3); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE__0__; /***/ }), /* 1 */, /* 2 */, /* 3 */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; __webpack_require__.r(__webpack_exports__); // CONCATENATED MODULE: ./node_modules/vue-loader/lib/loaders/templateLoader.js??vue-loader-options!./node_modules/vue-loader/lib??vue-loader-options!./src/js/Component.vue?vue&type=template&id=fb815198& var render = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('transition',{attrs:{"enter-active-class":_vm.transition.enter,"leave-active-class":_vm.transition.leave}},[_c('div',{directives:[{name:"show",rawName:"v-show",value:(_vm.isActive),expression:"isActive"}],staticClass:"toast",class:[("toast-" + _vm.type), ("is-" + _vm.position)],attrs:{"role":"alert"},on:{"click":_vm.whenClicked}},[_c('div',{staticClass:"toast-icon"}),_vm._v(" "),_c('p',{staticClass:"toast-text"},[_vm._v(_vm._s(_vm.message))])])])} var staticRenderFns = [] // CONCATENATED MODULE: ./src/js/Component.vue?vue&type=template&id=fb815198& // CONCATENATED MODULE: ./src/js/helpers.js var removeElement = function removeElement(el) { if (typeof el.remove !== 'undefined') { el.remove(); } else { el.parentNode.removeChild(el); } }; var hasWindow = function hasWindow() { return typeof window !== 'undefined'; }; var HTMLElement = hasWindow() ? window.HTMLElement : Object; // EXTERNAL MODULE: external {"commonjs":"vue","commonjs2":"vue","amd":"vue","root":"Vue"} var external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_ = __webpack_require__(0); var external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_default = /*#__PURE__*/__webpack_require__.n(external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_); // CONCATENATED MODULE: ./src/js/bus.js var bus = new external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_default.a(); /* harmony default export */ var js_bus = (bus); // CONCATENATED MODULE: ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options!./src/js/Component.vue?vue&type=script&lang=js& // // // // // // // // // // // // // // // // /* harmony default export */ var Componentvue_type_script_lang_js_ = ({ name: 'toast', props: { message: { type: String, required: true }, type: { type: String, "default": 'success' }, position: { type: String, "default": 'bottom-right' }, duration: { type: Number, "default": 3000 }, dismissible: { type: Boolean, "default": true }, onClose: { type: Function, "default": function _default() {} }, onClick: { type: Function, "default": function _default() {} }, queue: Boolean, container: { type: [Object, Function, HTMLElement], "default": null } }, data: function data() { return { isActive: false, parentTop: null, parentBottom: null }; }, beforeMount: function beforeMount() { this.setupContainer(); }, mounted: function mounted() { this.showNotice(); js_bus.$on('toast.clear', this.close); }, methods: { setupContainer: function setupContainer() { this.parentTop = document.querySelector('.notices.is-top'); this.parentBottom = document.querySelector('.notices.is-bottom'); // No need to create them, they already exists if (this.parentTop && this.parentBottom) return; if (!this.parentTop) { this.parentTop = document.createElement('div'); this.parentTop.className = 'notices is-top'; } if (!this.parentBottom) { this.parentBottom = document.createElement('div'); this.parentBottom.className = 'notices is-bottom'; } var container = this.container || document.body; container.appendChild(this.parentTop); container.appendChild(this.parentBottom); var containerParentClass = 'is-custom-parent'; if (this.container) { this.parentTop.classList.add(containerParentClass); this.parentBottom.classList.add(containerParentClass); } }, shouldQueue: function shouldQueue() { if (!this.queue) return false; return this.parentTop.childElementCount > 0 || this.parentBottom.childElementCount > 0; }, close: function close() { var _arguments = arguments, _this = this; clearTimeout(this.timer); this.isActive = false; // Timeout for the animation complete before destroying setTimeout(function () { _this.onClose.apply(null, _arguments); _this.$destroy(); removeElement(_this.$el); }, 150); }, showNotice: function showNotice() { var _this2 = this; if (this.shouldQueue()) { // Call recursively if should queue setTimeout(function () { return _this2.showNotice(); }, 250); return; } this.correctParent.insertAdjacentElement('afterbegin', this.$el); this.isActive = true; this.timer = setTimeout(function () { return _this2.close(); }, this.duration); }, whenClicked: function whenClicked() { if (!this.dismissible) return; this.onClick.apply(null, arguments); this.close(); } }, computed: { correctParent: function correctParent() { switch (this.position) { case 'top-right': case 'top': case 'top-left': return this.parentTop; case 'bottom-right': case 'bottom': case 'bottom-left': return this.parentBottom; } }, transition: function transition() { switch (this.position) { case 'top-right': case 'top': case 'top-left': return { enter: 'fadeInDown', leave: 'fadeOut' }; case 'bottom-right': case 'bottom': case 'bottom-left': return { enter: 'fadeInUp', leave: 'fadeOut' }; } } } }); // CONCATENATED MODULE: ./src/js/Component.vue?vue&type=script&lang=js& /* harmony default export */ var js_Componentvue_type_script_lang_js_ = (Componentvue_type_script_lang_js_); // CONCATENATED MODULE: ./node_modules/vue-loader/lib/runtime/componentNormalizer.js /* globals __VUE_SSR_CONTEXT__ */ // IMPORTANT: Do NOT use ES2015 features in this file (except for modules). // This module is a runtime utility for cleaner component module output and will // be included in the final webpack user bundle. function normalizeComponent ( scriptExports, render, staticRenderFns, functionalTemplate, injectStyles, scopeId, moduleIdentifier, /* server only */ shadowMode /* vue-cli only */ ) { // Vue.extend constructor export interop var options = typeof scriptExports === 'function' ? scriptExports.options : scriptExports // render functions if (render) { options.render = render options.staticRenderFns = staticRenderFns options._compiled = true } // functional template if (functionalTemplate) { options.functional = true } // scopedId if (scopeId) { options._scopeId = 'data-v-' + scopeId } var hook if (moduleIdentifier) { // server build hook = function (context) { // 2.3 injection context = context || // cached call (this.$vnode && this.$vnode.ssrContext) || // stateful (this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext) // functional // 2.2 with runInNewContext: true if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') { context = __VUE_SSR_CONTEXT__ } // inject component styles if (injectStyles) { injectStyles.call(this, context) } // register component module identifier for async chunk inferrence if (context && context._registeredComponents) { context._registeredComponents.add(moduleIdentifier) } } // used by ssr in case component is cached and beforeCreate // never gets called options._ssrRegister = hook } else if (injectStyles) { hook = shadowMode ? function () { injectStyles.call(this, this.$root.$options.shadowRoot) } : injectStyles } if (hook) { if (options.functional) { // for template-only hot-reload because in that case the render fn doesn't // go through the normalizer options._injectStyles = hook // register for functioal component in vue file var originalRender = options.render options.render = function renderWithStyleInjection (h, context) { hook.call(context) return originalRender(h, context) } } else { // inject component registration as beforeCreate hook var existing = options.beforeCreate options.beforeCreate = existing ? [].concat(existing, hook) : [hook] } } return { exports: scriptExports, options: options } } // CONCATENATED MODULE: ./src/js/Component.vue /* normalize component */ var component = normalizeComponent( js_Componentvue_type_script_lang_js_, render, staticRenderFns, false, null, null, null ) /* harmony default export */ var Component = (component.exports); // CONCATENATED MODULE: ./src/js/api.js var api_Api = function Api(Vue) { var globalOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return { open: function open(options) { var message; if (typeof options === 'string') message = options; var defaultOptions = { message: message }; var propsData = Object.assign({}, defaultOptions, globalOptions, options); return new (Vue.extend(Component))({ el: document.createElement('div'), propsData: propsData }); }, clear: function clear() { js_bus.$emit('toast.clear'); }, success: function success(message) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return this.open(Object.assign({}, { message: message, type: 'success' }, options)); }, error: function error(message) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return this.open(Object.assign({}, { message: message, type: 'error' }, options)); }, info: function info(message) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return this.open(Object.assign({}, { message: message, type: 'info' }, options)); }, warning: function warning(message) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return this.open(Object.assign({}, { message: message, type: 'warning' }, options)); }, "default": function _default(message) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return this.open(Object.assign({}, { message: message, type: 'default' }, options)); } }; }; /* harmony default export */ var api = (api_Api); // CONCATENATED MODULE: ./src/index.js var src_Plugin = function Plugin(Vue) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var methods = api(Vue, options); Vue.$toast = methods; Vue.prototype.$toast = methods; }; Component.install = src_Plugin; /* harmony default export */ var src = __webpack_exports__["default"] = (Component); /***/ }) /******/ ])["default"]; });