{"version":3,"file":"index.cjs","sources":["../../../components/loading/index.ts"],"sourcesContent":["import { createApp, createVNode, render } from 'vue'\n\nimport Component from './loading.vue'\nimport { proxyExposed, unrefElement } from '@vexip-ui/hooks'\nimport { destroyObject, isClient } from '@vexip-ui/utils'\n\nimport type { App, MaybeRef } from 'vue'\nimport type { MaybeInstance } from '@vexip-ui/hooks'\nimport type { LoadingInstance, LoadingOptions, LoadingState } from './symbol'\n\nexport type { LoadingState, LoadingOptions }\n\ntype InitLoadingOptions = Omit<LoadingOptions, 'percent'>\n\nexport class LoadingManager {\n  name: string\n  defaults: InitLoadingOptions\n\n  private _mountedApp: App<unknown> | null\n  private _instance: LoadingInstance | null\n  private _innerApp: App<unknown> | null\n  private _container: HTMLElement | null\n  private _wrapper: HTMLElement | SVGElement | null\n  private _mountedEl: HTMLElement | null\n\n  constructor(options: InitLoadingOptions = {}) {\n    this._mountedApp = null\n    this._instance = null\n    this._innerApp = null\n    this._container = null\n    this._wrapper = null\n    this._mountedEl = null\n    this.name = 'Loading'\n    this.defaults = {}\n\n    this.config(options)\n  }\n\n  open(percent: number): void\n  open(options: LoadingOptions): void\n  open(options: number | LoadingOptions) {\n    if (!isClient) return\n\n    if (typeof options === 'number') {\n      options = { percent: options }\n    }\n\n    this._getInstance()!.startLoading({ ...this.defaults, ...options })\n  }\n\n  close() {\n    this.open(100)\n  }\n\n  config(options: InitLoadingOptions) {\n    this.defaults = { ...this.defaults, ...options }\n  }\n\n  clone() {\n    const manager = new LoadingManager(this.defaults)\n\n    manager._mountedApp = this._mountedApp\n\n    return manager\n  }\n\n  destroy() {\n    this._mountedEl && this._wrapper?.removeChild(this._mountedEl)\n    this._innerApp?.unmount()\n    this._container && render(null, this._container)\n    destroyObject(this)\n  }\n\n  isDestroyed() {\n    return false\n  }\n\n  install(app: App, options: InitLoadingOptions & { property?: string } = {}) {\n    const { property, ...others } = options\n\n    this.config(others)\n    this._mountedApp = app\n\n    if (property || !app.config.globalProperties.$loading) {\n      app.config.globalProperties[property || '$loading'] = this\n    }\n  }\n\n  transferTo(target: MaybeRef<string | MaybeInstance>) {\n    if (!isClient) return\n\n    const el = unrefElement(target)\n\n    if (el) {\n      this._wrapper = el\n\n      if (this._instance) {\n        this._mountedEl && this._wrapper.appendChild(this._mountedEl)\n      } else {\n        this._getInstance()\n      }\n    }\n  }\n\n  private _getInstance() {\n    if (!this._instance && isClient) {\n      if (!this._mountedApp) {\n        console.warn('[vexip-ui:Loading]: App missing, the plugin maybe not installed.')\n\n        this._container = document.createElement('div')\n        this._innerApp = createApp(Component)\n        this._instance = this._innerApp.mount(this._container) as LoadingInstance\n      } else {\n        const vnode = createVNode(Component, null, null)\n\n        this._container = document.createElement('div')\n        vnode.appContext = this._mountedApp._context\n\n        render(vnode, this._container)\n\n        this._instance = proxyExposed<LoadingInstance>(vnode)\n      }\n\n      this._mountedEl = this._container.firstElementChild as HTMLElement\n      ;(this._wrapper || document.body).appendChild(this._mountedEl)\n    }\n\n    return this._instance\n  }\n}\n\nexport const Loading = new LoadingManager()\n"],"names":["LoadingManager","options","__publicField","isClient","manager","_a","_b","render","destroyObject","app","property","others","target","el","unrefElement","createApp","Component","vnode","createVNode","proxyExposed","Loading"],"mappings":"wYAcO,MAAMA,CAAe,CAW1B,YAAYC,EAA8B,GAAI,CAV9CC,EAAA,aACAA,EAAA,iBAEQA,EAAA,oBACAA,EAAA,kBACAA,EAAA,kBACAA,EAAA,mBACAA,EAAA,iBACAA,EAAA,mBAGN,KAAK,YAAc,KACnB,KAAK,UAAY,KACjB,KAAK,UAAY,KACjB,KAAK,WAAa,KAClB,KAAK,SAAW,KAChB,KAAK,WAAa,KAClB,KAAK,KAAO,UACZ,KAAK,SAAW,CAAC,EAEjB,KAAK,OAAOD,CAAO,CAAA,CAKrB,KAAKA,EAAkC,CAChCE,EAAAA,WAED,OAAOF,GAAY,WACXA,EAAA,CAAE,QAASA,CAAQ,GAG1B,KAAA,eAAgB,aAAa,CAAE,GAAG,KAAK,SAAU,GAAGA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