summaryrefslogtreecommitdiff
path: root/assets/js/src/base.js
blob: d918e54a87f2c621798d8a8f02533a346b86d959 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Common functions
var Kanboard = (function() {

    jQuery(document).ready(function() {
        Kanboard.Init();
    });

    return {

        // Return true if the element#id exists
        Exists: function(id) {
            if (document.getElementById(id)) {
                return true;
            }

            return false;
        },

        // Open a popup on a link click
        Popover: function(e, callback) {
            e.preventDefault();
            e.stopPropagation();

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

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

            if (link) {
                Kanboard.OpenPopover(link, callback);
            }
        },

        // Display a popup
        OpenPopover: function(link, callback) {

            $.get(link, function(content) {

                $("body").append('<div id="popover-container"><div id="popover-content">' + content + '</div></div>');

                $("#popover-container").click(function() {
                    $(this).remove();
                });

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

                $(".close-popover").click(function(e) {
                    e.preventDefault();
                    $('#popover-container').remove();
                });

                Mousetrap.bind("esc", function() {
                    $('#popover-container').remove();
                });

                if (callback) {
                    callback();
                }
            });
        },

        // Return true if the page is visible
        IsVisible: function() {

            var property = "";

            if (typeof document.hidden !== "undefined") {
                property = "visibilityState";
            } else if (typeof document.mozHidden !== "undefined") {
                property = "mozVisibilityState";
            } else if (typeof document.msHidden !== "undefined") {
                property = "msVisibilityState";
            } else if (typeof document.webkitHidden !== "undefined") {
                property = "webkitVisibilityState";
            }

            if (property != "") {
                return document[property] == "visible";
            }

            return true;
        },

        // Save preferences in local storage
        SetStorageItem: function(key, value) {
            if (typeof(Storage) !== "undefined") {
                localStorage.setItem(key, value);
            }
        },

        GetStorageItem: function(key) {

            if (typeof(Storage) !== "undefined") {
                return localStorage.getItem(key);
            }

            return '';
        },

        // Generate Markdown preview
        MarkdownPreview: function(e) {

            e.preventDefault();

            var link = $(this);
            var nav = $(this).closest("ul");
            var write = $(".write-area");
            var preview = $(".preview-area");
            var textarea = $("textarea");

            var request = $.ajax({
                url: "?controller=app&action=preview",
                contentType: "application/json",
                type: "POST",
                processData: false,
                dataType: "html",
                data: JSON.stringify({
                    "text": textarea.val()
                })
            });

            request.done(function(data) {

                nav.find("li").removeClass("form-tab-selected");
                link.parent().addClass("form-tab-selected");

                preview.find(".markdown").html(data)
                preview.css("height", textarea.css("height"));
                preview.css("width", textarea.css("width"));

                write.hide();
                preview.show();
            });
        },

        // Show the Markdown textarea
        MarkdownWriter: function(e) {

            e.preventDefault();

            $(this).closest("ul").find("li").removeClass("form-tab-selected")
            $(this).parent().addClass("form-tab-selected");

            $(".write-area").show();
            $(".preview-area").hide();
        },

        // Check session and redirect to the login page if not logged
        CheckSession: function() {

            if (! $(".form-login").length) {
                $.ajax({
                    cache: false,
                    url: $("body").data("status-url"),
                    statusCode: {
                        401: function() {
                            window.location = $("body").data("login-url");
                        }
                    }
                });
            }
        },

        Init: function() {

            // Project select box
            $("#board-selector").chosen({
                width: 180
            });

            $("#board-selector").change(function() {
                window.location = $(this).attr("data-board-url").replace(/PROJECT_ID/g, $(this).val());
            });

            // Check the session every 60s
            window.setInterval(Kanboard.CheckSession, 60000);

            // Keyboard shortcuts
            Mousetrap.bind("mod+enter", function() {
                $("form").submit();
            });

            Mousetrap.bind("b", function(e) {
                e.preventDefault();
                $('#board-selector').trigger('chosen:open');
            });

            // Tooltip for column description
            $(".column-tooltip").tooltip({
                content: function() {
                    return '<div class="markdown">' + $(this).attr("title") + '</div>';
                }
            });

            $.datepicker.setDefaults($.datepicker.regional[$("body").data("js-lang")]);

            Kanboard.InitAfterAjax();
        },

        InitAfterAjax: function() {

            // Popover
            $(document).on("click", ".popover", Kanboard.Popover);

            // Datepicker
            $(".form-date").datepicker({
                showOtherMonths: true,
                selectOtherMonths: true,
                dateFormat: 'yy-mm-dd',
                constrainInput: false
            });

            // Markdown Preview for textareas
            $("#markdown-preview").click(Kanboard.MarkdownPreview);
            $("#markdown-write").click(Kanboard.MarkdownWriter);

            // Auto-select input fields
            $(".auto-select").focus(function() {
                $(this).select();
            });

            // Dropdown
            $(".dropit-submenu").hide();
            $('.dropdown').not(".dropit").dropit();
        }
    };

})();