UNPKG

13.1 kBJavaScriptView Raw
1(function webpackUniversalModuleDefinition(root, factory) {
2 if(typeof exports === 'object' && typeof module === 'object')
3 module.exports = factory(require("vue"));
4 else if(typeof define === 'function' && define.amd)
5 define("VueToast", ["vue"], factory);
6 else if(typeof exports === 'object')
7 exports["VueToast"] = factory(require("vue"));
8 else
9 root["VueToast"] = factory(root["Vue"]);
10})(this, function(__WEBPACK_EXTERNAL_MODULE__982__) {
11return /******/ (() => { // webpackBootstrap
12/******/ "use strict";
13/******/ var __webpack_modules__ = ({
14
15/***/ 982:
16/***/ ((module) => {
17
18module.exports = __WEBPACK_EXTERNAL_MODULE__982__;
19
20/***/ })
21
22/******/ });
23/************************************************************************/
24/******/ // The module cache
25/******/ var __webpack_module_cache__ = {};
26/******/
27/******/ // The require function
28/******/ function __webpack_require__(moduleId) {
29/******/ // Check if module is in cache
30/******/ var cachedModule = __webpack_module_cache__[moduleId];
31/******/ if (cachedModule !== undefined) {
32/******/ return cachedModule.exports;
33/******/ }
34/******/ // Create a new module (and put it into the cache)
35/******/ var module = __webpack_module_cache__[moduleId] = {
36/******/ // no module.id needed
37/******/ // no module.loaded needed
38/******/ exports: {}
39/******/ };
40/******/
41/******/ // Execute the module function
42/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
43/******/
44/******/ // Return the exports of the module
45/******/ return module.exports;
46/******/ }
47/******/
48/************************************************************************/
49/******/ /* webpack/runtime/define property getters */
50/******/ (() => {
51/******/ // define getter functions for harmony exports
52/******/ __webpack_require__.d = (exports, definition) => {
53/******/ for(var key in definition) {
54/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
55/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
56/******/ }
57/******/ }
58/******/ };
59/******/ })();
60/******/
61/******/ /* webpack/runtime/hasOwnProperty shorthand */
62/******/ (() => {
63/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
64/******/ })();
65/******/
66/************************************************************************/
67var __webpack_exports__ = {};
68// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
69(() => {
70
71// EXPORTS
72__webpack_require__.d(__webpack_exports__, {
73 "default": () => (/* binding */ src)
74});
75
76// UNUSED EXPORTS: ToastComponent, ToastPositions, useToast
77
78// EXTERNAL MODULE: external {"commonjs":"vue","commonjs2":"vue","amd":"vue","root":"Vue"}
79var external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_ = __webpack_require__(982);
80;// CONCATENATED MODULE: ./node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/dist/templateLoader.js??ruleSet[1].rules[2]!./node_modules/vue-loader/dist/index.js??ruleSet[1].rules[6].use[0]!./src/js/Component.vue?vue&type=template&id=1a4bf640
81
82
83const _hoisted_1 = /*#__PURE__*/(0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.createVNode)("div", {
84 class: "v-toast__icon"
85}, null, -1);
86
87function render(_ctx, _cache, $props, $setup, $data, $options) {
88 return (0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.openBlock)(), (0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.createBlock)(external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.Transition, {
89 "enter-active-class": _ctx.transition.enter,
90 "leave-active-class": _ctx.transition.leave
91 }, {
92 default: (0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.withCtx)(() => [(0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.withDirectives)((0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.createVNode)("div", {
93 ref: "root",
94 role: "alert",
95 class: ["v-toast__item", [`v-toast__item--${_ctx.type}`, `v-toast__item--${_ctx.position}`]],
96 onMouseover: _cache[1] || (_cache[1] = $event => _ctx.toggleTimer(true)),
97 onMouseleave: _cache[2] || (_cache[2] = $event => _ctx.toggleTimer(false)),
98 onClick: _cache[3] || (_cache[3] = (...args) => _ctx.whenClicked && _ctx.whenClicked(...args))
99 }, [_hoisted_1, (0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.createVNode)("p", {
100 class: "v-toast__text",
101 innerHTML: _ctx.message
102 }, null, 8, ["innerHTML"])], 34), [[external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.vShow, _ctx.isActive]])]),
103 _: 1
104 }, 8, ["enter-active-class", "leave-active-class"]);
105}
106;// CONCATENATED MODULE: ./src/js/Component.vue?vue&type=template&id=1a4bf640
107
108;// CONCATENATED MODULE: ./src/js/helpers.js
109
110function removeElement(el) {
111 if (typeof el.remove !== 'undefined') {
112 el.remove();
113 } else {
114 var _el$parentNode;
115
116 (_el$parentNode = el.parentNode) === null || _el$parentNode === void 0 ? void 0 : _el$parentNode.removeChild(el);
117 }
118}
119function createComponent(component, props, parentContainer, slots = {}) {
120 const vNode = (0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.h)(component, props, slots);
121 const container = document.createElement('div');
122 container.classList.add('v-toast--pending');
123 parentContainer.appendChild(container);
124 (0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.render)(vNode, container);
125 return vNode.component;
126}
127;// CONCATENATED MODULE: ./src/js/timer.js
128// https://stackoverflow.com/a/3969760
129class Timer {
130 constructor(callback, delay) {
131 this.startedAt = Date.now();
132 this.callback = callback;
133 this.delay = delay;
134 this.timer = setTimeout(callback, delay);
135 }
136
137 pause() {
138 this.stop();
139 this.delay -= Date.now() - this.startedAt;
140 }
141
142 resume() {
143 this.stop();
144 this.startedAt = Date.now();
145 this.timer = setTimeout(this.callback, this.delay);
146 }
147
148 stop() {
149 clearTimeout(this.timer);
150 }
151
152}
153;// CONCATENATED MODULE: ./src/js/positions.js
154/* harmony default export */ const positions = (Object.freeze({
155 TOP_RIGHT: 'top-right',
156 TOP: 'top',
157 TOP_LEFT: 'top-left',
158 BOTTOM_RIGHT: 'bottom-right',
159 BOTTOM: 'bottom',
160 BOTTOM_LEFT: 'bottom-left'
161}));
162;// CONCATENATED MODULE: ./node_modules/mitt/dist/mitt.mjs
163/* harmony default export */ function mitt(n){return{all:n=n||new Map,on:function(t,e){var i=n.get(t);i?i.push(e):n.set(t,[e])},off:function(t,e){var i=n.get(t);i&&(e?i.splice(i.indexOf(e)>>>0,1):n.set(t,[]))},emit:function(t,e){var i=n.get(t);i&&i.slice().map(function(n){n(e)}),(i=n.get("*"))&&i.slice().map(function(n){n(t,e)})}}}
164//# sourceMappingURL=mitt.mjs.map
165
166;// CONCATENATED MODULE: ./src/js/bus.js
167
168/* harmony default export */ const bus = (mitt());
169;// CONCATENATED MODULE: ./node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/dist/index.js??ruleSet[1].rules[6].use[0]!./src/js/Component.vue?vue&type=script&lang=js
170
171
172
173
174
175/* harmony default export */ const Componentvue_type_script_lang_js = ((0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.defineComponent)({
176 name: 'toast',
177 props: {
178 message: {
179 type: String,
180 required: true
181 },
182 type: {
183 type: String,
184 default: 'success'
185 },
186 position: {
187 type: String,
188 default: positions.BOTTOM_RIGHT,
189
190 validator(value) {
191 return Object.values(positions).includes(value);
192 }
193
194 },
195 duration: {
196 type: Number,
197 default: 3000
198 },
199 dismissible: {
200 type: Boolean,
201 default: true
202 },
203 onDismiss: {
204 type: Function,
205 default: () => {}
206 },
207 onClick: {
208 type: Function,
209 default: () => {}
210 },
211 queue: Boolean,
212 pauseOnHover: {
213 type: Boolean,
214 default: true
215 }
216 },
217
218 data() {
219 return {
220 isActive: false,
221 parentTop: null,
222 parentBottom: null,
223 isHovered: false
224 };
225 },
226
227 beforeMount() {
228 this.setupContainer();
229 },
230
231 mounted() {
232 this.showNotice();
233 bus.on('toast-clear', this.dismiss);
234 },
235
236 methods: {
237 setupContainer() {
238 this.parentTop = document.querySelector('.v-toast.v-toast--top');
239 this.parentBottom = document.querySelector('.v-toast.v-toast--bottom'); // No need to create them, they already exists
240
241 if (this.parentTop && this.parentBottom) return;
242
243 if (!this.parentTop) {
244 this.parentTop = document.createElement('div');
245 this.parentTop.className = 'v-toast v-toast--top';
246 }
247
248 if (!this.parentBottom) {
249 this.parentBottom = document.createElement('div');
250 this.parentBottom.className = 'v-toast v-toast--bottom';
251 }
252
253 const container = document.body;
254 container.appendChild(this.parentTop);
255 container.appendChild(this.parentBottom);
256 },
257
258 shouldQueue() {
259 if (!this.queue) return false;
260 return this.parentTop.childElementCount > 0 || this.parentBottom.childElementCount > 0;
261 },
262
263 dismiss() {
264 if (this.timer) this.timer.stop();
265 clearTimeout(this.queueTimer);
266 this.isActive = false; // Timeout for the animation complete before destroying
267
268 setTimeout(() => {
269 this.onDismiss.apply(null, arguments);
270 const wrapper = this.$refs.root; // unmount the component
271
272 (0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.render)(null, wrapper);
273 removeElement(wrapper);
274 }, 150);
275 },
276
277 showNotice() {
278 if (this.shouldQueue()) {
279 // Call recursively if should queue
280 this.queueTimer = setTimeout(this.showNotice, 250);
281 return;
282 }
283
284 const wrapper = this.$refs.root.parentElement;
285 this.correctParent.insertAdjacentElement('afterbegin', this.$refs.root);
286 removeElement(wrapper);
287 this.isActive = true;
288
289 if (this.duration) {
290 this.timer = new Timer(this.dismiss, this.duration);
291 }
292 },
293
294 whenClicked() {
295 if (!this.dismissible) return;
296 this.onClick.apply(null, arguments);
297 this.dismiss();
298 },
299
300 toggleTimer(newVal) {
301 if (!this.pauseOnHover || !this.timer) return;
302 newVal ? this.timer.pause() : this.timer.resume();
303 }
304
305 },
306 computed: {
307 correctParent() {
308 switch (this.position) {
309 case positions.TOP:
310 case positions.TOP_RIGHT:
311 case positions.TOP_LEFT:
312 return this.parentTop;
313
314 case positions.BOTTOM:
315 case positions.BOTTOM_RIGHT:
316 case positions.BOTTOM_LEFT:
317 return this.parentBottom;
318 }
319 },
320
321 transition() {
322 switch (this.position) {
323 case positions.TOP:
324 case positions.TOP_RIGHT:
325 case positions.TOP_LEFT:
326 return {
327 enter: 'v-toast--fade-in-down',
328 leave: 'v-toast--fade-out'
329 };
330
331 case positions.BOTTOM:
332 case positions.BOTTOM_RIGHT:
333 case positions.BOTTOM_LEFT:
334 return {
335 enter: 'v-toast--fade-in-up',
336 leave: 'v-toast--fade-out'
337 };
338 }
339 }
340
341 },
342
343 beforeUnmount() {
344 bus.off('toast-clear', this.dismiss);
345 }
346
347}));
348;// CONCATENATED MODULE: ./src/js/Component.vue?vue&type=script&lang=js
349
350;// CONCATENATED MODULE: ./src/js/Component.vue
351
352
353
354Componentvue_type_script_lang_js.render = render
355
356/* harmony default export */ const Component = (Componentvue_type_script_lang_js);
357;// CONCATENATED MODULE: ./src/js/api.js
358
359
360
361const useToast = (globalProps = {}) => {
362 return {
363 open(options) {
364 let message = null;
365 if (typeof options === 'string') message = options;
366 const defaultProps = {
367 message
368 };
369 const propsData = Object.assign({}, defaultProps, globalProps, options);
370 const instance = createComponent(Component, propsData, document.body);
371 return {
372 dismiss: instance.ctx.dismiss
373 };
374 },
375
376 clear() {
377 bus.emit('toast-clear');
378 },
379
380 success(message, options = {}) {
381 return this.open(Object.assign({}, {
382 message,
383 type: 'success'
384 }, options));
385 },
386
387 error(message, options = {}) {
388 return this.open(Object.assign({}, {
389 message,
390 type: 'error'
391 }, options));
392 },
393
394 info(message, options = {}) {
395 return this.open(Object.assign({}, {
396 message,
397 type: 'info'
398 }, options));
399 },
400
401 warning(message, options = {}) {
402 return this.open(Object.assign({}, {
403 message,
404 type: 'warning'
405 }, options));
406 },
407
408 default(message, options = {}) {
409 return this.open(Object.assign({}, {
410 message,
411 type: 'default'
412 }, options));
413 }
414
415 };
416};
417;// CONCATENATED MODULE: ./src/index.js
418
419
420
421const ToastPlugin = {
422 install: (app, options = {}) => {
423 let instance = useToast(options);
424 app.config.globalProperties.$toast = instance;
425 app.provide('$toast', instance);
426 }
427};
428/* harmony default export */ const src = (ToastPlugin);
429
430})();
431
432__webpack_exports__ = __webpack_exports__.default;
433/******/ return __webpack_exports__;
434/******/ })()
435;
436});
\No newline at end of file