mirror of
https://github.com/Lissy93/dashy.git
synced 2025-07-24 22:25:16 +02:00
⌛ Writes a click-outside directive to trigger closing of context menu
This commit is contained in:
parent
c1a8f5c032
commit
318c2602a0
10
src/main.js
10
src/main.js
@ -7,16 +7,18 @@ import VSelect from 'vue-select'; // Select dropdown component
|
|||||||
import VTabs from 'vue-material-tabs'; // Tab view component, used on the config page
|
import VTabs from 'vue-material-tabs'; // Tab view component, used on the config page
|
||||||
import Toasted from 'vue-toasted'; // Toast component, used to show confirmation notifications
|
import Toasted from 'vue-toasted'; // Toast component, used to show confirmation notifications
|
||||||
|
|
||||||
import { toastedOptions } from './utils/defaults';
|
import { toastedOptions } from '@/utils/defaults';
|
||||||
import Dashy from './App.vue';
|
import Dashy from '@/App.vue';
|
||||||
import router from './router';
|
import router from '@/router';
|
||||||
import registerServiceWorker from './registerServiceWorker';
|
import registerServiceWorker from '@/registerServiceWorker';
|
||||||
|
import clickOutside from '@/utils/ClickOutside';
|
||||||
|
|
||||||
Vue.use(VTooltip);
|
Vue.use(VTooltip);
|
||||||
Vue.use(VModal);
|
Vue.use(VModal);
|
||||||
Vue.use(VTabs);
|
Vue.use(VTabs);
|
||||||
Vue.use(Toasted, toastedOptions);
|
Vue.use(Toasted, toastedOptions);
|
||||||
Vue.component('v-select', VSelect);
|
Vue.component('v-select', VSelect);
|
||||||
|
Vue.directive('clickOutside', clickOutside);
|
||||||
|
|
||||||
Vue.config.productionTip = false;
|
Vue.config.productionTip = false;
|
||||||
|
|
||||||
|
37
src/utils/ClickOutside.js
Normal file
37
src/utils/ClickOutside.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* A simple Vue directive to trigger an event when the user
|
||||||
|
* clicks anywhere other than the specified element.
|
||||||
|
* Used to close context menu's popup menus and tips.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const instances = [];
|
||||||
|
|
||||||
|
function onDocumentClick(e, el, fn) {
|
||||||
|
const { target } = e;
|
||||||
|
if (el !== target && !el.contains(target)) {
|
||||||
|
fn(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
bind(element, binding) {
|
||||||
|
const el = element;
|
||||||
|
el.dataset.outsideClickIndex = instances.length;
|
||||||
|
|
||||||
|
const fn = binding.value;
|
||||||
|
const click = (e) => {
|
||||||
|
onDocumentClick(e, el, fn);
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener('click', click);
|
||||||
|
document.addEventListener('touchstart', click);
|
||||||
|
instances.push(click);
|
||||||
|
},
|
||||||
|
unbind(el) {
|
||||||
|
if (!el.dataset) return;
|
||||||
|
const index = el.dataset.outsideClickIndex;
|
||||||
|
const handler = instances[index];
|
||||||
|
document.removeEventListener('click', handler);
|
||||||
|
instances.splice(index, 1);
|
||||||
|
},
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user