UNPKG

9.18 kBSource Map (JSON)View Raw
1{"version":3,"file":"v-offline.js","sources":["../src/VOffline.vue","../node_modules/vue-runtime-helpers/dist/normalize-component.mjs","../src/install.ts"],"sourcesContent":["<template>\n <div :class=\"wrapperClass\">\n <slot />\n </div>\n</template>\n\n<script lang=\"ts\">\n import Ping from 'ping.js';\n import {\n ref,\n Ref,\n computed,\n onBeforeUnmount,\n PropType,\n ComputedRef,\n SetupContext,\n defineComponent,\n } from '@vue/composition-api';\n import { VOfflineProps } from '../types';\n\n export default defineComponent({\n name: 'VOffline',\n props: {\n onlineClass: {\n type: String as PropType<string>,\n required: false,\n default: '',\n },\n offlineClass: {\n type: String as PropType<string>,\n required: false,\n default: '',\n },\n pingUrl: {\n type: String as PropType<string>,\n required: false,\n default: 'https://google.com',\n },\n },\n setup(props: VOfflineProps, { emit }: SetupContext) {\n // Local state\n const isOnline: Ref<boolean> = ref(navigator.onLine || false);\n const events: Ref<string[]> = ref(['online', 'offline', 'load']);\n const url: Ref<string> = ref(props.pingUrl || 'https://google.com');\n\n // Local computed\n const wrapperClass: ComputedRef<string> = computed(() => {\n if (isOnline.value) {\n return typeof props.onlineClass === 'string' ? props.onlineClass : '';\n } else {\n return typeof props.offlineClass === 'string'\n ? props.offlineClass\n : '';\n }\n });\n\n /**\n * Created lifecycle hook\n */\n events.value.forEach((event) => window.addEventListener(event, check));\n\n /**\n * Before unmount lifecycle hook\n */\n onBeforeUnmount(() => {\n // Cleanup of the event listeners\n events.value.forEach((event) =>\n window.removeEventListener(event, check),\n );\n });\n\n // Local functions\n /**\n * Pings the URL and emits an\n * detected online/offline event.\n *\n * @returns {Promise<void>}\n */\n async function check(): Promise<void> {\n const p = new Ping();\n try {\n const ping = await p.ping(url.value);\n if (ping || navigator.onLine) {\n isOnline.value = true;\n emit('detected-condition', isOnline.value);\n }\n } catch (error) {\n if (error || !navigator.onLine) {\n isOnline.value = false;\n emit('detected-condition', isOnline.value);\n }\n }\n }\n\n return {\n wrapperClass,\n };\n },\n });\n</script>\n","function normalizeComponent(template, style, script, scopeId, isFunctionalTemplate, moduleIdentifier /* server only */, shadowMode, createInjector, createInjectorSSR, createInjectorShadow) {\r\n if (typeof shadowMode !== 'boolean') {\r\n createInjectorSSR = createInjector;\r\n createInjector = shadowMode;\r\n shadowMode = false;\r\n }\r\n // Vue.extend constructor export interop.\r\n const options = typeof script === 'function' ? script.options : script;\r\n // render functions\r\n if (template && template.render) {\r\n options.render = template.render;\r\n options.staticRenderFns = template.staticRenderFns;\r\n options._compiled = true;\r\n // functional template\r\n if (isFunctionalTemplate) {\r\n options.functional = true;\r\n }\r\n }\r\n // scopedId\r\n if (scopeId) {\r\n options._scopeId = scopeId;\r\n }\r\n let hook;\r\n if (moduleIdentifier) {\r\n // server build\r\n hook = function (context) {\r\n // 2.3 injection\r\n context =\r\n context || // cached call\r\n (this.$vnode && this.$vnode.ssrContext) || // stateful\r\n (this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext); // functional\r\n // 2.2 with runInNewContext: true\r\n if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {\r\n context = __VUE_SSR_CONTEXT__;\r\n }\r\n // inject component styles\r\n if (style) {\r\n style.call(this, createInjectorSSR(context));\r\n }\r\n // register component module identifier for async chunk inference\r\n if (context && context._registeredComponents) {\r\n context._registeredComponents.add(moduleIdentifier);\r\n }\r\n };\r\n // used by ssr in case component is cached and beforeCreate\r\n // never gets called\r\n options._ssrRegister = hook;\r\n }\r\n else if (style) {\r\n hook = shadowMode\r\n ? function (context) {\r\n style.call(this, createInjectorShadow(context, this.$root.$options.shadowRoot));\r\n }\r\n : function (context) {\r\n style.call(this, createInjector(context));\r\n };\r\n }\r\n if (hook) {\r\n if (options.functional) {\r\n // register for functional component in vue file\r\n const originalRender = options.render;\r\n options.render = function renderWithStyleInjection(h, context) {\r\n hook.call(context);\r\n return originalRender(h, context);\r\n };\r\n }\r\n else {\r\n // inject component registration as beforeCreate hook\r\n const existing = options.beforeCreate;\r\n options.beforeCreate = existing ? [].concat(existing, hook) : [hook];\r\n }\r\n }\r\n return script;\r\n}\n\nexport default normalizeComponent;\n//# sourceMappingURL=normalize-component.mjs.map\n","import VueCompositionApi from '@vue/composition-api';\nimport { VueConstructor } from 'vue';\nimport VOffline from './VOffline.vue';\n\nlet installed = false;\n\nconst install = {\n install(Vue: VueConstructor): void {\n if (installed) return;\n Vue.use(VueCompositionApi);\n Vue.component('VOffline', VOffline);\n installed = true;\n },\n};\n\nexport default install;\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAoBA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;EAEA;EACA;EACA;EACA;;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;EAEA;EACA;EACA;EACA;EACA;;EClGA,SAAS,kBAAkB,CAAC,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,oBAAoB,UAAU,EAAE,cAAc,EAAE,iBAAiB,EAAE,oBAAoB,EAAE;EAC7L,IAAI,IAAI,OAAO,UAAU,KAAK,SAAS,EAAE;EACzC,QAAQ,iBAAiB,GAAG,cAAc,CAAC;EAC3C,QAAQ,cAAc,GAAG,UAAU,CAAC;EACpC,QAAQ,UAAU,GAAG,KAAK,CAAC;EAC3B,KAAK;EACL;EACA,IAAI,MAAM,OAAO,GAAG,OAAO,MAAM,KAAK,UAAU,GAAG,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC;EAC3E;EACA,IAAI,IAAI,QAAQ,IAAI,QAAQ,CAAC,MAAM,EAAE;EACrC,QAAQ,OAAO,C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