{"version":3,"file":"index.mjs","sources":["../../../components/notice/index.ts"],"sourcesContent":["import { createApp, createVNode, markRaw, render } from 'vue'\n\nimport Component from './notice.vue'\nimport { proxyExposed, unrefElement } from '@vexip-ui/hooks'\nimport { destroyObject, isClient, isNull, isObject, noop, toNumber } from '@vexip-ui/utils'\n\nimport type { App, MaybeRef } from 'vue'\nimport type { MaybeInstance } from '@vexip-ui/hooks'\nimport type {\n  Key,\n  NoticeConfig,\n  NoticeInstance,\n  NoticeOptions,\n  NoticePlacement,\n  NoticeType\n} from './symbol'\n\nexport type { NoticeType, NoticePlacement, NoticeOptions }\n\ntype FuzzyOptions = string | NoticeOptions\ntype ManagerOptions = { marker?: boolean, duration?: number, placement?: NoticePlacement } & Record<\n  string,\n  unknown\n>\n\ninterface AipMethod {\n  (options: NoticeOptions): () => void,\n  (title: string, duration?: number): () => void,\n  (title: string, content: string, duration?: number): () => void,\n  /** @internal */\n  (options: FuzzyOptions, duration?: number): () => void\n}\n\nconst placementWhiteList: NoticePlacement[] = [\n  'top-right',\n  'top-left',\n  'bottom-right',\n  'bottom-left'\n]\n\nlet count = 1\n\nfunction getKey() {\n  return `notice-${count++}`\n}\n\nexport class NoticeManager {\n  name: string\n  defaults: Record<string, unknown>\n\n  open: AipMethod\n  primary: AipMethod\n  info: AipMethod\n  success: AipMethod\n  warning: AipMethod\n  error: AipMethod\n\n  private _mountedApp: App<unknown> | null\n  private _instance: NoticeInstance | 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: ManagerOptions = {}) {\n    options = {\n      ...options,\n      marker: !!options.marker,\n      duration: options.duration ? toNumber(options.duration) : 4000\n    }\n\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 = 'Notice'\n    this.defaults = {}\n\n    this.config(options)\n\n    this.open = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open(null, title, content, duration)\n    }\n\n    this.primary = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open('primary', title, content, duration)\n    }\n\n    this.info = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open('info', title, content, duration)\n    }\n\n    this.success = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open('success', title, content, duration)\n    }\n\n    this.warning = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open('warning', title, content, duration)\n    }\n\n    this.error = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open('error', title, content, duration)\n    }\n  }\n\n  judge(state: boolean, success: string, error: string, duration?: number): void\n  judge(state: boolean, success: NoticeOptions, error: string, duration?: number): void\n  judge(state: boolean, success: string, error: NoticeOptions, duration?: number): void\n  judge(state: boolean, success: NoticeOptions, error: NoticeOptions): void\n  judge(\n    state: boolean,\n    success: string | NoticeOptions,\n    error: string | NoticeOptions,\n    duration?: number\n  ) {\n    if (state) {\n      this.success(success, duration)\n    } else {\n      this.error(error, duration)\n    }\n  }\n\n  close(key: Key) {\n    if (isNull(key)) {\n      this.clear()\n    } else {\n      this._getInstance()?.remove(key)\n    }\n  }\n\n  config({ placement, ...others }: NoticeConfig & NoticeOptions) {\n    if (placement) {\n      this._getInstance()?.config({\n        placement: placementWhiteList.includes(placement) ? placement : placementWhiteList[0]\n      })\n    }\n\n    this.defaults = { ...this.defaults, ...others }\n  }\n\n  clone() {\n    const manager = new NoticeManager(this.defaults)\n\n    manager._mountedApp = this._mountedApp\n\n    return manager\n  }\n\n  clear() {\n    this._getInstance()?.clear()\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: ManagerOptions & { property?: string } = {}) {\n    const { property, ...others } = options\n\n    this.config(others)\n    this._mountedApp = app\n\n    if (property || !app.config.globalProperties.$notice) {\n      app.config.globalProperties[property || '$notice'] = 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:Notice]: 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 NoticeInstance\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<NoticeInstance>(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  private _open(\n    type: null | NoticeType,\n    title: FuzzyOptions,\n    content?: string | number,\n    _duration?: number\n  ) {\n    if (!isClient) {\n      return noop\n    }\n\n    let options: NoticeOptions\n\n    if (isObject(title)) {\n      options = title\n    } else {\n      if (typeof content === 'number') {\n        options = { title, duration: content }\n      } else if (!content) {\n        options = { title, duration: _duration }\n      } else {\n        options = { title, content, duration: _duration }\n      }\n    }\n\n    const key = options.key ?? getKey()\n    const notice = this._getInstance()!\n\n    let timer: ReturnType<typeof setTimeout>\n\n    const userCloseFn = options.onClose\n    const onClose = () => {\n      clearTimeout(timer)\n\n      if (typeof userCloseFn === 'function') {\n        return userCloseFn()\n      }\n    }\n\n    const userEnterFn = options.onEnter\n    const onEnter = () => {\n      if (options.liveOnEnter) {\n        clearTimeout(timer)\n      }\n\n      if (typeof userEnterFn === 'function') {\n        return userEnterFn()\n      }\n    }\n\n    const userLeaveFn = options.onLeave\n    const onLeave = () => {\n      if (options.liveOnEnter) {\n        clearTimeout(timer)\n        setDelayClose()\n      }\n\n      if (typeof userLeaveFn === 'function') {\n        return userLeaveFn()\n      }\n    }\n\n    const item: NoticeOptions = {\n      ...this.defaults,\n      ...options,\n      key,\n      type: type ?? options.type,\n      onClose,\n      onEnter,\n      onLeave\n    }\n\n    if (item.icon && typeof item.icon !== 'function') {\n      item.icon = markRaw(item.icon)\n    }\n\n    notice.add(item)\n    setDelayClose()\n\n    function setDelayClose() {\n      const duration = typeof item.duration === 'number' ? item.duration : 4000\n\n      if (duration >= 500) {\n        timer = setTimeout(() => {\n          notice.remove(key)\n        }, duration)\n      }\n    }\n\n    return () => {\n      clearTimeout(timer)\n      notice.remove(key)\n    }\n  }\n}\n\nexport const Notice = new NoticeManager()\n"],"names":["placementWhiteList","count","getKey","NoticeManager","options","__publicField","toNumber","title","content","duration","state","success","error","key","isNull","_a","placement","others","manager","_b","render","destroyObject","app","property","target","isClient","el","unrefElement","createApp","Component","vnode","createVNode","proxyExposed","type","_duration","noop","isObject","notice","timer","userCloseFn","onClose","userEnterFn","onEnter","userLeaveFn","onLeave","setDelayClose","item","markRaw","Notice"],"mappings":";;;;;;;;AAiCA,MAAMA,IAAwC;AAAA,EAC5C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,IAAIC,IAAQ;AAEZ,SAASC,IAAS;AAChB,SAAO,UAAUD,GAAO;AAC1B;AAEO,MAAME,EAAc;AAAA,EAkBzB,YAAYC,IAA0B,IAAI;AAjB1C,IAAAC,EAAA;AACA,IAAAA,EAAA;AAEA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AAEQ,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AAGI,IAAAD,IAAA;AAAA,MACR,GAAGA;AAAA,MACH,Q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