summaryrefslogtreecommitdiff
path: root/assets/js/src/Popover.js
blob: 183d8488d9caa01ee909c36f10d7273c0a3dde9c (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Kanboard.Popover = function(app) {
    this.app = app;
};

Kanboard.Popover.prototype.listen = function() {
    var self = this;

    $(document).on("click", ".popover", function(e) {
        self.onClick(e);
    });

    $(document).on("click", ".close-popover", function(e) {
        self.close(e);
    });

    $(document).on("click", "#popover-container", function(e) {
        self.close(e);
    });

    $(document).on("click", "#popover-content", function(e) {
        e.stopPropagation();
    });
};

Kanboard.Popover.prototype.onClick = function(e) {
    e.preventDefault();
    e.stopPropagation();

    var target = e.currentTarget || e.target;
    var link = target.getAttribute("href");

    if (! link) {
        link = target.getAttribute("data-href");
    }

    if (link) {
        this.open(link);
    }
};

Kanboard.Popover.prototype.isOpen = function() {
    return $('#popover-container').size() > 0;
};

Kanboard.Popover.prototype.open = function(link) {
    var self = this;

    if (!self.isOpen()) {
        $.get(link, function(content) {
            $("body").prepend('<div id="popover-container"><div id="popover-content">' + content + '</div></div>');
            self.executeOnOpenedListeners();
        });
    }
};

Kanboard.Popover.prototype.close = function(e) {
    if (this.isOpen()) {
        if (e) {
            e.preventDefault();
        }

        $("#popover-container").remove();
        this.executeOnClosedListeners();
    }
};

Kanboard.Popover.prototype.ajaxReload = function(data, request, self) {
    var redirect = request.getResponseHeader("X-Ajax-Redirect");

    if (redirect === 'self') {
        window.location.reload();
    } else if (redirect && redirect.indexOf('#') > -1) {
        window.location = redirect.split('#')[0];
    } else if (redirect) {
        window.location = redirect;
    } else {
        $("#popover-content").html(data);
        $("#popover-content input[autofocus]").focus();
        self.executeOnOpenedListeners();
    }
};

Kanboard.Popover.prototype.executeOnOpenedListeners = function() {
    for (var className in this.app.controllers) {
        var controller = this.app.get(className);

        if (typeof controller.onPopoverOpened === "function") {
            controller.onPopoverOpened();
        }
    }

    this.afterOpen();
};

Kanboard.Popover.prototype.executeOnClosedListeners = function() {
    for (var className in this.app.controllers) {
        var controller = this.app.get(className);

        if (typeof controller.onPopoverClosed === "function") {
            controller.onPopoverClosed();
        }
    }
};

Kanboard.Popover.prototype.afterOpen = function() {
    var self = this;
    var popoverForm = $("#popover-content .popover-form");

    // Submit forms with Ajax request
    if (popoverForm) {
        popoverForm.on("submit", function(e) {
            e.preventDefault();

            $.ajax({
                type: "POST",
                url: popoverForm.attr("action"),
                data: popoverForm.serialize(),
                success: function(data, textStatus, request) {
                    self.ajaxReload(data, request, self);
                },
                beforeSend: function() {
                    var button = $('.popover-form button[type="submit"]');
                    button.html('<i class="fa fa-spinner fa-pulse"></i> ' + button.html());
                    button.attr("disabled", true);
                }
            });
        });
    }

    // Submit link with Ajax request
    $(document).on("click", ".popover-link", function(e) {
        e.preventDefault();

        $.ajax({
            type: "GET",
            url: $(this).attr("href"),
            success: function(data, textStatus, request) {
                self.ajaxReload(data, request, self);
            }
        });
    });

    // Autofocus fields (html5 autofocus works only with page onload)
    $("[autofocus]").each(function() {
        $(this).focus();
    });

    this.app.datePicker();
    this.app.autoComplete();
    this.app.tagAutoComplete();

    new Vue({
        el: '#popover-container'
    });
};