debian-mirror-gitlab/app/assets/javascripts/popovers/components/popovers.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.1 KiB
Vue
Raw Normal View History

2021-01-29 00:20:46 +05:30
<script>
2021-11-11 11:23:49 +05:30
import { GlPopover, GlSafeHtmlDirective } from '@gitlab/ui';
2021-01-29 00:20:46 +05:30
2021-03-08 18:12:59 +05:30
const newPopover = (element) => {
2021-01-29 00:20:46 +05:30
const { content, html, placement, title, triggers = 'focus' } = element.dataset;
return {
target: element,
content,
html,
placement,
title,
triggers,
};
};
export default {
components: {
GlPopover,
},
2021-11-11 11:23:49 +05:30
directives: {
SafeHtml: GlSafeHtmlDirective,
},
2021-01-29 00:20:46 +05:30
data() {
return {
popovers: [],
};
},
created() {
2021-03-08 18:12:59 +05:30
this.observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
2021-01-29 00:20:46 +05:30
mutation.removedNodes.forEach(this.dispose);
});
});
},
beforeDestroy() {
this.observer.disconnect();
},
methods: {
addPopovers(elements) {
const newPopovers = elements.reduce((acc, element) => {
if (this.popoverExists(element)) {
return acc;
}
const popover = newPopover(element);
this.observe(popover);
return [...acc, popover];
}, []);
this.popovers.push(...newPopovers);
},
observe(popover) {
this.observer.observe(popover.target.parentElement, {
childList: true,
});
},
dispose(target) {
if (!target) {
this.popovers = [];
} else {
2021-03-08 18:12:59 +05:30
const index = this.popovers.findIndex((popover) => popover.target === target);
2021-01-29 00:20:46 +05:30
if (index > -1) {
this.popovers.splice(index, 1);
}
}
},
popoverExists(element) {
2021-03-08 18:12:59 +05:30
return this.popovers.some((popover) => popover.target === element);
2021-01-29 00:20:46 +05:30
},
2021-11-11 11:23:49 +05:30
},
safeHtmlConfig: {
ADD_TAGS: ['use'], // to support icon SVGs
2021-01-29 00:20:46 +05:30
},
};
</script>
<template>
<div>
<gl-popover v-for="(popover, index) in popovers" :key="index" v-bind="popover">
<template #title>
2021-11-11 11:23:49 +05:30
<span v-if="popover.html" v-safe-html:[$options.safeHtmlConfig]="popover.title"></span>
2021-01-29 00:20:46 +05:30
<span v-else>{{ popover.title }}</span>
</template>
2021-11-11 11:23:49 +05:30
<span v-if="popover.html" v-safe-html:[$options.safeHtmlConfig]="popover.content"></span>
2021-01-29 00:20:46 +05:30
<span v-else>{{ popover.content }}</span>
</gl-popover>
</div>
</template>