yariksav / vuetify-dialog

Easy dialogs in Vuetify.js framework
MIT License
195 stars 48 forks source link

Clear all dialogs and notifications #64

Open mohammad-zr opened 4 years ago

mohammad-zr commented 4 years ago

Is there anyway to clear and remove all dialogs and notifications programmatically?

AbdelrhmanAbdelhamed commented 4 years ago

Please If there any, I'd like to know how as this might come in handy in many situations

mohammad-zr commented 4 years ago

@yariksav do you have any plan to implement this feature?

AbdelrhmanAbdelhamed commented 4 years ago

@Esniper90 I managed to do it using the shown and destroyed events to keep track of the dialogs.

    let _dialogs = []

    Vue.prototype.$dialog.on('shown', ({ dialog }) => _dialogs.push(dialog))

    Vue.prototype.$dialog.on(
      'destroyed',
      ({ dialog }) => (_dialogs = _dialogs.filter((_dialog) => _dialog.id !== dialog.id))
    )

    Vue.prototype.$dialog.clearDialogs = () => {
      _dialogs.forEach((_dialog) => _dialog.remove())
      _dialogs = []
    }

   // Then you can call this.$dialog.clearDialogs() inside any component
maxmartynov commented 3 years ago

Based on the @AbdelrhmanAbdelhamed code. For nuxt.js

// @/plugins/vuetify-dialog/vuetify-dialog.client.js
import Vue from 'vue'

export default () => {
  const _dialogs = []

  Vue.prototype.$dialog.on('shown', ({dialog}) => _dialogs.push(dialog))

  Vue.prototype.$dialog.on('destroyed', ({dialog}) => {
    const index = _dialogs.findIndex((_dialog) => _dialog.id === dialog.id)
    _dialogs.splice(index, 1)
  })

  Vue.prototype.$dialog.clearAllDialogs = () => {
    _dialogs.forEach((_dialog) => _dialog.remove())
    _dialogs.length = 0
  }
}
// @/plugins/vuetify-dialog/vuetify-dialog.d.ts
import Vue from 'vue'
import {VuetifyDialog as VuetifyDialogOrig} from 'vuetify-dialog'

declare module 'vue/types/vue' {
  interface VuetifyDialog extends VuetifyDialogOrig {
    clearAllDialogs: () => void
  }

  interface Vue {
    $dialog: VuetifyDialog
  }
}
// nuxt.config.js
// ...
    plugins: [
      {src: '@/plugins/vuetify-dialog/vuetify-dialog.client.js'},
    ],
 // ...