summaryrefslogtreecommitdiff
path: root/assets/js/src/Search.js
blob: 4fbfee460b3192c48f6e61279ed46a22736a8c0e (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
function Search(app) {
    this.app = app;
    this.keyboardShortcuts();
}

Search.prototype.focus = function() {
    // Place cursor at the end when focusing on the search box
    $(document).on("focus", "#form-search", function() {
        if ($("#form-search")[0].setSelectionRange) {
           $('#form-search')[0].setSelectionRange($('#form-search').val().length, $('#form-search').val().length);
        }
    });
};

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

    // Filter helper for search
    $(document).on("click", ".filter-helper", function (e) {
        e.preventDefault();

        var filter = $(this).data("filter");
        var appendFilter = $(this).data("append-filter");

        if (appendFilter) {
            filter = $("#form-search").val() + " " + appendFilter;
        }

        $("#form-search").val(filter);

        if ($('#board').length) {
            self.app.board.reloadFilters(filter);
        }
        else {
            $("form.search").submit();
        }
    });
};

Search.prototype.keyboardShortcuts = function() {
    var self = this;

    // Switch view mode for projects: go to the overview page
    Mousetrap.bind("v o", function(e) {
        var link = $(".view-overview");

        if (link.length) {
            window.location = link.attr('href');
        }
    });

    // Switch view mode for projects: go to the board
    Mousetrap.bind("v b", function(e) {
        var link = $(".view-board");

        if (link.length) {
            window.location = link.attr('href');
        }
    });

    // Switch view mode for projects: go to the calendar
    Mousetrap.bind("v c", function(e) {
        var link = $(".view-calendar");

        if (link.length) {
            window.location = link.attr('href');
        }
    });

    // Switch view mode for projects: go to the listing
    Mousetrap.bind("v l", function(e) {
        var link = $(".view-listing");

        if (link.length) {
            window.location = link.attr('href');
        }
    });

    // Switch view mode for projects: go to the gantt chart
    Mousetrap.bind("v g", function(e) {
        var link = $(".view-gantt");

        if (link.length) {
            window.location = link.attr('href');
        }
    });

    // Focus to the search field
    Mousetrap.bind("f", function(e) {
        e.preventDefault();
        var input = document.getElementById("form-search");

        if (input) {
            input.focus();
        }
    });

    // Reset to the search field
    Mousetrap.bind("r", function(e) {
        e.preventDefault();
        var reset = $(".filter-reset").data("filter");

        $("#form-search").val(reset);

        if ($('#board').length) {
            self.app.board.reloadFilters(reset);
        }
        else {
            $("form.search").submit();
        }
    });
};