All files / src/components/u-tooltip.vue v-tooltip.js

5.88% Statements 1/17
0% Branches 0/16
0% Functions 0/5
5.88% Lines 1/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50            7x                                                                                      
import Vue from 'vue';
 
/**
 * 给任意元素添加`<u-tooltip>`工具提示效果
 * @value 需要绑定的内容
 */
const directive = {
    handle(binding) {
        const data = {
            content: binding.value,
            trigger: 'hover',
            placement: 'bottom',
            size: 'normal',
            followCursor: binding.modifiers.cursor,
        };
 
        Object.keys(binding.modifiers).forEach((key) => {
            if (['click', 'hover', 'right-click', 'double-click', 'manual'].includes(key))
                data.trigger = key;
            if (/^(top|bottom|left|right)(-start|-end)?$/.test(key))
                data.placement = key;
            if (/^(mini|small|normal|large|huge|auto)(-(mini|small|normal|large|huge|auto))?$/.test(key))
                data.size = key;
        });
 
        return data;
    },
    bind(el, binding) {
        const data = el['v-tooltip'] = Object.assign(el['v-tooltip'] || {}, directive.handle(binding));
 
        const UTooltip = Vue.component('UTooltip') || Vue.component('u-tooltip');
        el.tooltipVM = new UTooltip({
            propsData: Object.assign({
                reference: el,
                enableContentScroll: true,
            }, data),
        }).$mount();
    },
    update(el, binding) {
        const data = el['v-tooltip'] = Object.assign(el['v-tooltip'] || {}, directive.handle(binding));
        if (el.tooltipVM)
            Object.assign(el.tooltipVM, data);
    },
    unbind(el, binding) {
        el.tooltipVM && el.tooltipVM.$destroy();
    },
};
 
export default directive;