summaryrefslogtreecommitdiff
path: root/assets/js/src/Popover.js
blob: b978c08767a6d976c09971a3cad2f340b681a8fe (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
function Popover(app) {
    this.app = app;
    this.router = new Router();
    this.router.addRoute('screenshot-zone', Screenshot);
}

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

Popover.prototype.open = function(link) {
    var self = this;
    self.app.dropdown.close();

    $.get(link, function(content) {
        $("body").append('<div id="popover-container"><div id="popover-content">' + content + '</div></div>');
        self.router.dispatch();
        self.app.refresh();
        self.afterOpen();
    });
};

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

    $('#popover-container').remove();
};

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

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

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

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

Popover.prototype.listen = function() {
    $(document).on("click", ".popover", this.onClick.bind(this));
    $(document).on("click", ".close-popover", this.close.bind(this));
    $(document).on("click", "#popover-container", this.close.bind(this));
    $(document).on("click", "#popover-content", function(e) { e.stopPropagation(); });
};

Popover.prototype.afterOpen = function() {
    var self = this;
    var taskForm = $("#task-form");

    if (taskForm) {
        taskForm.on("submit", function(e) {
            e.preventDefault();

            $.ajax({
                type: "POST",
                url: taskForm.attr("action"),
                data: taskForm.serialize(),
                success: function(data, textStatus, request) {
                    if (request.getResponseHeader("X-Ajax-Redirect")) {
                        window.location = request.getResponseHeader("X-Ajax-Redirect");
                    }
                    else {
                        $("#popover-content").html(data);
                        self.afterOpen();
                    }
                }
            });
        });
    }
};