{"version":3,"file":"v-offline.cjs","sources":["../src/components/VOffline.vue","../src/install.ts"],"sourcesContent":["<script lang=\"ts\">\n  import Ping from 'ping.js';\n  import type { ComputedRef, PropType, Ref } from 'vue';\n  import { computed, defineComponent, h, onBeforeUnmount, ref } from 'vue';\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    emits: ['detected-condition'],\n    setup(props, { slots, emit }) {\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       * @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 () => h('div', { class: wrapperClass.value }, slots);\n    },\n  });\n</script>\n","import { App as Application } from 'vue';\nimport VOffline from './components/VOffline.vue';\n\nlet installed = false;\n\nconst install = (app: Application) => {\n  if (!installed) {\n    app.component('VOffline', VOffline);\n    installed = true;\n  }\n};\n\nexport default install;\n"],"names":["_sfc_main","defineComponent","props","slots","emit","isOnline","ref","events","url","wrapperClass","computed","event","check","onBeforeUnmount","p","Ping","error","h","installed","install","app","VOffline","install$1"],"mappings":";;;;6IAKEA,EAAeC,kBAAgB,CAC7B,KAAM,WACN,MAAO,CACL,YAAa,CACX,KAAM,OACN,SAAU,GACV,QAAS,EACX,EACA,aAAc,CACZ,KAAM,OACN,SAAU,GACV,QAAS,EACX,EACA,QAAS,CACP,KAAM,OACN,SAAU,GACV,QAAS,oBACX,CACF,EACA,MAAO,CAAC,oBAAoB,EAC5B,MAAMC,EAAO,CAAE,MAAAC,EAAO,KAAAC,GAAQ,CAE5B,MAAMC,EAAyBC,EAAA,IAAI,UAAU,QAAU,EAAK,EACtDC,EAAwBD,EAAAA,IAAI,CAAC,SAAU,UAAW,MAAM,CAAC,EACzDE,EAAmBF,EAAA,IAAIJ,EAAM,SAAW,oBAAoB,EAG5DO,EAAoCC,EAAAA,SAAS,IAC7CL,EAAS,MACJ,OAAOH,EAAM,aAAgB,SAAWA,EAAM,YAAc,GAE5D,OAAOA,EAAM,cAAiB,SACjCA,EAAM,aACN,EAEP,EAKMK,EAAA,MAAM,QAASI,GAAU,OAAO,iBAAiBA,EAAOC,CAAK,CAAC,EAKrEC,EAAAA,gBAAgB,IAAM,CAEpBN,EAAO,MAAM,QAASI,GACpB,OAAO,oBAAoBA,EAAOC,CAAK,CAAA,CACzC,CACD,EAQD,eAAeA,GAAuB,CAC9B,MAAAE,EAAI,IAAIC,EACV,GAAA,EACW,MAAMD,EAAE,KAAKN,EAAI,KAAK,GACvB,UAAU,UACpBH,EAAS,MAAQ,GACZD,EAAA,qBAAsBC,EAAS,KAAK,SAEpCW,EAAO,EACVA,GAAS,CAAC,UAAU,UACtBX,EAAS,MAAQ,GACZD,EAAA,qBAAsBC,EAAS,KAAK,EAE7C,CACF,CAEO,MAAA,IAAMY,EAAAA,EAAE,MAAO,CAAE,MAAOR,EAAa,KAAA,EAASN,CAAK,CAC5D,CACF,CAAC,EC9EH,IAAIe,EAAY,GAEhB,MAAMC,EAAWC,GAAqB,CAC/BF,IACCE,EAAA,UAAU,WAAYC,CAAQ,EACtBH,EAAA,GAEhB,EAEAI,EAAeH"}