Project

General

Profile

1
function plugin(UIkit) {
2

    
3
    if (plugin.installed) {
4
        return;
5
    }
6

    
7
    var { util, mixin } = UIkit;
8
    var {$, doc, fastdom, flipPosition, isTouch, isWithin, pointerDown, pointerEnter, pointerLeave, toJQuery} = util;
9

    
10
    var active;
11

    
12
    doc.on('click', e => {
13
        if (active && !isWithin(e.target, active.$el)) {
14
            active.hide();
15
        }
16
    });
17

    
18
    UIkit.component('tooltip', {
19

    
20
        attrs: true,
21

    
22
        mixins: [mixin.toggable, mixin.position],
23

    
24
        props: {
25
            delay: Number,
26
            container: Boolean,
27
            title: String
28
        },
29

    
30
        defaults: {
31
            pos: 'top',
32
            title: '',
33
            delay: 0,
34
            animation: ['uk-animation-scale-up'],
35
            duration: 100,
36
            cls: 'uk-active',
37
            clsPos: 'uk-tooltip',
38
            container: true,
39
        },
40

    
41
        init() {
42
            this.container = this.container === true && UIkit.container || this.container && toJQuery(this.container);
43
        },
44

    
45
        connected() {
46
            fastdom.mutate(() => this.$el.removeAttr('title').attr('aria-expanded', false));
47
        },
48

    
49
        disconnected() {
50
            this.hide();
51
        },
52

    
53
        methods: {
54

    
55
            show() {
56

    
57
                if (active === this) {
58
                    return;
59
                }
60

    
61
                if (active) {
62
                    active.hide();
63
                }
64

    
65
                active = this;
66

    
67
                clearTimeout(this.showTimer);
68

    
69
                this.tooltip = $(`<div class="${this.clsPos}" aria-hidden="true"><div class="${this.clsPos}-inner">${this.title}</div></div>`).appendTo(this.container);
70

    
71
                this.$el.attr('aria-expanded', true);
72

    
73
                this.positionAt(this.tooltip, this.$el);
74
                this.origin = this.getAxis() === 'y' ? `${flipPosition(this.dir)}-${this.align}` : `${this.align}-${flipPosition(this.dir)}`;
75

    
76
                this.showTimer = setTimeout(() => {
77
                    this.toggleElement(this.tooltip, true);
78

    
79
                    this.hideTimer = setInterval(() => {
80
                        if (!this.$el.is(':visible')) {
81
                            this.hide();
82
                        }
83
                    }, 150);
84

    
85
                }, this.delay);
86
            },
87

    
88
            hide() {
89

    
90
                if (this.$el.is('input') && this.$el[0] === document.activeElement) {
91
                    return;
92
                }
93

    
94
                active = active !== this && active || false;
95

    
96
                clearTimeout(this.showTimer);
97
                clearInterval(this.hideTimer);
98
                this.$el.attr('aria-expanded', false);
99
                this.toggleElement(this.tooltip, false);
100
                this.tooltip && this.tooltip.remove();
101
                this.tooltip = false;
102
            }
103

    
104
        },
105

    
106
        events: {
107
            [`focus ${pointerEnter} ${pointerDown}`](e) {
108
                if (e.type !== pointerDown || !isTouch(e)) {
109
                    this.show();
110
                }
111
            },
112
            'blur': 'hide',
113
            [pointerLeave](e) {
114
                if (!isTouch(e)) {
115
                    this.hide()
116
                }
117
            }
118
        }
119

    
120
    });
121

    
122
}
123

    
124
if (!BUNDLED && typeof window !== 'undefined' && window.UIkit) {
125
    window.UIkit.use(plugin);
126
}
127

    
128
export default plugin;
(4-4/5)