Project

General

Profile

1
function plugin(UIkit) {
2

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

    
7
    var {$, each, pointerEnter, pointerLeave, Transition} = UIkit.util;
8
    var containers = {};
9

    
10
    UIkit.component('notification', {
11

    
12
        functional: true,
13

    
14
        args: ['message', 'status'],
15

    
16
        defaults: {
17
            message: '',
18
            status: '',
19
            timeout: 5000,
20
            group: null,
21
            pos: 'top-center',
22
            onClose: null,
23
            clsClose: 'uk-notification-close'
24
        },
25

    
26
        created() {
27

    
28
            if (!containers[this.pos]) {
29
                containers[this.pos] = $(`<div class="uk-notification uk-notification-${this.pos}"></div>`).appendTo(UIkit.container);
30
            }
31

    
32
            this.$mount($(
33
                `<div class="uk-notification-message${this.status ? ` uk-notification-message-${this.status}` : ''}">
34
                    <a href="#" class="${this.clsClose}" data-uk-close></a>
35
                    <div>${this.message}</div>
36
                </div>`
37
            ).appendTo(containers[this.pos].show())[0]);
38

    
39
        },
40

    
41
        ready() {
42

    
43
            var marginBottom = parseInt(this.$el.css('margin-bottom'), 10);
44

    
45
            Transition.start(
46
                this.$el.css({opacity: 0, marginTop: -1 * this.$el.outerHeight(), marginBottom: 0}),
47
                {opacity: 1, marginTop: 0, marginBottom}
48
            ).then(() => {
49
                if (this.timeout) {
50
                    this.timer = setTimeout(this.close, this.timeout);
51
                    this.$el
52
                        .on(pointerEnter, () => clearTimeout(this.timer))
53
                        .on(pointerLeave, () => this.timer = setTimeout(this.close, this.timeout));
54
                }
55
            });
56

    
57
        },
58

    
59
        events: {
60

    
61
            click(e) {
62
                if ($(e.target).closest('a[href="#"]').length) {
63
                    e.preventDefault();
64
                }
65
                this.close();
66
            }
67

    
68
        },
69

    
70
        methods: {
71

    
72
            close(immediate) {
73

    
74
                var remove = () => {
75

    
76
                    this.onClose && this.onClose();
77
                    this.$el.trigger('close', [this]).remove();
78

    
79
                    if (!containers[this.pos].children().length) {
80
                        containers[this.pos].hide();
81
                    }
82

    
83
                };
84

    
85
                if (this.timer) {
86
                    clearTimeout(this.timer);
87
                }
88

    
89
                if (immediate) {
90
                    remove();
91
                } else {
92
                    Transition.start(this.$el, {opacity: 0, marginTop: -1 * this.$el.outerHeight(), marginBottom: 0}).then(remove)
93
                }
94
            }
95

    
96
        }
97

    
98
    });
99

    
100
    UIkit.notification.closeAll = function (group, immediate) {
101
        each(UIkit.instances, (_, component) => {
102
            if (component.$options.name === 'notification' && (!group || group === component.group)) {
103
                component.close(immediate);
104
            }
105
        })
106
    };
107

    
108
}
109

    
110
if (!BUNDLED && typeof window !== 'undefined' && window.UIkit) {
111
    window.UIkit.use(plugin);
112
}
113

    
114
export default plugin;
(2-2/5)