UNPKG

9.18 kBSource Map (JSON)View Raw
1{"version":3,"file":"v-offline.cjs.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;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AClGA,SAAS,kBAAkB,CAAC,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,oBAAoB,UAAU,EAAE,cAAc,EAAE,iBAAiB,EAAE,oBAAoB,EAAE;AAC7L,IAAI,IAAI,OAAO,UAAU,KAAK,SAAS,EAAE;AACzC,QAAQ,iBAAiB,GAAG,cAAc,CAAC;AAC3C,QAAQ,cAAc,GAAG,UAAU,CAAC;AACpC,QAAQ,UAAU,GAAG,KAAK,CAAC;AAC3B,KAAK;AACL;AACA,IAAI,MAAM,OAAO,GAAG,OAAO,MAAM,KAAK,UAAU,GAAG,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC;AAC3E;AACA,IAAI,IAAI,QAAQ,IAAI,QAAQ,CAAC,MAAM,EAAE;AACrC,QAAQ,O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