aboutsummaryrefslogtreecommitdiff
path: root/app/dashboard/static/js/linaro-base-1.0.6.js
blob: 6a36c3038ab81642673203a69bbf09192c65d784 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

var JSBase = (function() {
    'use strict';

    var csrftoken = $('meta[name=csrf-token]').attr('content'),
        errorsContainer = $('#errors-container'),
        defaultErrorReason = 'Data call failed',
        defaultTimeout = 10000;

    // Make sure the element ID starts with #.
    // `elementID`: The element ID to check.
    function checkIfID(elementID) {
        var localElement = elementID;

        if (localElement[0] !== '#') {
            localElement = '#' + localElement;
        }
        return localElement;
    }

    // Make sure the class name starts with a dot.
    // `className`: The name of the class to check.
    function checkIfClass(className) {
        var localName = className;

        if (localName[0] !== '.') {
            localName = '.' + localName;
        }
        return localName;
    }

     // Add error alerts at the top of the page in the default container.
     // `id`: The id of the element to add.
     // `code`: The error code (int).
     // `reason`: Option reason that will be added to the error message.
    function setErrorAlert(id, code, reason) {
        var localId = checkIfID(id),
            text = '';

        text = '<div id="' + localId + '" ' +
            'class="alert alert-danger alert-dismissable">' +
            '<button type="button" class="close" ' +
            'data-dismiss="alert" aria-hidden="true">&times;</button>';

        if (reason !== null && reason !== undefined) {
            text = text + reason + '<br/>';
        }

        text = text +
            'Error while loading data from the server (error code:&nbsp;' +
            code + ').&nbsp;' +
            'Please contact the website administrators.';

        text = text + '</div>';

        errorsContainer.append(text);
        $(localId).alert();
    }

    // Load static HTML content from an URL.
    // `elementID`: The ID of the element.
    // `contentURL`: The URL where the static content will be taken.
    function loadHTMLContent(elementID, contentURL) {
        var realID = checkIfID(elementID);
        $(realID).empty().load(contentURL);
    }

    // Replace content of an element based on its id.
    // `elementID`: The ID of the element to search.
    // `staticContent`: The content that the element will be replaced with.
    function replaceContentByID(elementID, staticContent) {
        var realID = checkIfID(elementID);
        $(realID).empty().append(staticContent);
    }

    // Remove an element by its ID from the DOM.
    // `elementID`: The ID of the element to remove.
    function removeElementByID(elementID) {
        var realID = checkIfID(elementID);
        $(realID).remove();
    }

    // Replace content of elements based on their class name.
    // It loops through all the elements removing their content and appending
    // the provided one.
    // `className`: The name of the class to search.
    // `staticContent`: The content that the elements will be replaced with.
    function replaceContentByClass(className, staticContent) {
        var realClass = checkIfClass(className);
        $(realClass).each(function() {
            $(this).empty().append(staticContent);
        });
    }

    // Remove hidden class from element and apply animation to show it.
    // `elementID`: The ID of the element to show.
    function showElementByID(elementID) {
        var realID = checkIfID(elementID);
        $(realID)
            .removeClass('hidden')
            .fadeIn('slow', 'linear');
    }

    // Remove CSS class from the specified element.
    // `elementID`: The ID of the element.
    // `cssClass`: The CSS class to remove or an Array of classes as string.
    function removeCssClassForID(elementID, cssClass) {
        var realID = checkIfID(elementID);
        if (cssClass.constructor === Array) {
            cssClass.forEach(function(element, index, arra) {
                $(realID).removeClass(element);
            });
        } else {
            $(realID).removeClass(cssClass);
        }
    }

    // Return an ajax promise.
    // `url`: The URL for the ajax call.
    // `method`: The type of ajax call, default to 'GET'.
    // `data`: The data to be passed to the ajax call.
    // `successFunction`: Optional function, or array of functions,
    // to be called on success.
    // `errorFunction`: Optional function to be call on error.
    // `errorReason`: Error message to be displayed.
    // `headers`: Optional headers to set for the ajax call.
    // `errorId`: ID to use for the error message div element.
    function createDeferredCall(url, method, data, successFunction,
        errorFunction, errorReason, headers, errorId) {

        var ajaxSettings, ajaxCall;

        if (method === null || method === undefined) {
            method = 'GET';
        }

        if (errorReason === null || errorReason === undefined) {
            errorReason = defaultErrorReason;
        }

        if (errorId === null || errorId === undefined) {
            errorId = '#error';
        } else {
            errorId = checkIfID(errorId);
        }

        ajaxSettings = {
            'type': method,
            'traditional': true,
            'cache': true,
            'dataType': 'json',
            'beforeSend': function(jqXHR) {
                jqXHR.setRequestHeader('X-CSRFToken', csrftoken);
            },
            'timeout': defaultTimeout,
            'statusCode': {
                400: function() {
                    setErrorAlert(errorId + '-400', 404, errorReason);
                },
                403: function() {
                    setErrorAlert(errorId + '-403', 403, errorReason);
                },
                404: function() {
                    setErrorAlert(errorId + '-404', 404, errorReason);
                },
                408: function() {
                    errorReason = errorReason + 'nbsp;(timeout)';
                    setErrorAlert(errorId + '-408', 408, errorReason);
                },
                500: function() {
                    setErrorAlert(errorId + '-500', 500, errorReason);
                }
            }
        };

        if (data !== null && data !== undefined) {
            ajaxSettings.data = data;
        }

        if (successFunction !== null && successFunction !== undefined) {
            ajaxSettings.success = successFunction;
        }

        if (errorFunction !== null && errorFunction !== undefined) {
            ajaxSettings.error = errorFunction;
        }

        if (headers !== null && headers !== undefined) {
            ajaxSettings.headers = headers;
        }

        ajaxCall = $.ajax(url, ajaxSettings);
        return ajaxCall;
    }

    /*
        Populate the sidebar navigation with the provided elements.
        The "Top" link, is already populated since it's available in the base
        template.

        elements: An array of objects. Each object must have the href, and name
        attributes set. An optional subnav attribute can be defined, and must
        be another array of similar objects, to add a navigation sub-level.
    */
    function populateSideBarNav(elements) {
        if (elements === undefined || elements === null) {
            return;
        }

        var sidebarNav = '',
            arrayLen = elements.length,
            element = null,
            i = 0,
            j = 0,
            subNav = null,
            subNavLen;

        // Append the stuff only if we have the element.
        // On mobile platforms the element is not available.
        if ($('#sidebar-nav').length !== 0) {
            sidebarNav = '<ul class="nav sidenav">' +
                '<li class="active"><a href="#top">Top</a></li>';

            for (i; i < arrayLen; i = i + 1) {
                element = elements[i];

                sidebarNav += '<li><a href="' + checkIfID(element.href) +
                    '">' + element.name + '</a>';

                // Add subnav links (only if really available).
                if (element.hasOwnProperty('subnav') &&
                        element.subnav !== null) {
                    subNav = element.subnav;
                    subNavLen = subNav.length;

                    sidebarNav += '<ul class="nav">';
                    for (j; j < subNavLen; j = j + 1) {
                        sidebarNav += '<li><a href="' +
                            checkIfID(subNav[j].href) +
                            '">' + subNav[j].name +
                            '</a></li>';
                    }

                    sidebarNav += '</ul></li>';
                }

                sidebarNav += '</li>';
            }

            sidebarNav += '</ul>';

            $('#sidebar-nav').empty().append(sidebarNav);
            $('[data-spy="scroll"]').each(function() {
                $(this).scrollspy('refresh');
            });
        }
    }

    /*
        Concatenate objects together in one single object.
    */
    function collectObjects() {
        var returnObject = {},
            len = arguments.length,
            arg,
            i = 0,
            key;

        for (i = 0; i < len; i = i + 1) {
            arg = arguments[i];

            if (typeof arg === 'object') {
                for (key in arg) {
                    if (arg.hasOwnProperty(key)) {
                        returnObject[key] = arg[key];
                    }
                }
            }
        }

        return returnObject;
    }

    // Enable keyboard hotkeys/shortcuts.
    function setHotKeys() {
        var selectSearch = function() {
                $('.input-sm').focus();
            },
            selectTableLength = function() {
                $('.length-menu .input-sm').focus();
            },
            goToHome = function() {
                window.location = $('#home-l')[0].href;
            },
            goToJob = function() {
                window.location = $('#job-l')[0].href;
            },
            goToBuild = function() {
                window.location = $('#build-l')[0].href;
            },
            goToBoot = function() {
                window.location = $('#boot-l')[0].href;
            },
            goToInfo = function() {
                window.location = $('#info-l')[0].href;
            },
            showHelp = function() {
                $('#modal-hotkeys').modal('show');
            };

        $(document).mapHotKeys(
            [{
                    key: '/',
                    action: selectSearch
                }, {
                    key: 'l',
                    action: selectTableLength
                },
                $.mapHotKeys.createSequence('s', 'h', $(document), showHelp),
                $.mapHotKeys.createSequence('g', 'h', $(document), goToHome),
                $.mapHotKeys.createSequence('g', 'j', $(document), goToJob),
                $.mapHotKeys.createSequence('g', 'b', $(document), goToBuild),
                $.mapHotKeys.createSequence('g', 't', $(document), goToBoot),
                $.mapHotKeys.createSequence('g', 'i', $(document), goToInfo)
            ]
        );
    }

    function translateCommitURL(commitURL, commitId) {
        var uriParser,
            hostName,
            knownGit,
            baseURL = '',
            urlPath,
            newCommitURL = '',
            translateRule,
            lenTranslate,
            i,
            urlTranslation = $('#url-translation').val();

        if ((commitURL !== null && commitURL !== undefined) &&
                (commitId !== null && commitId !== undefined) &&
                urlTranslation !== 'None') {

            urlTranslation = JSON.parse(urlTranslation);

            uriParser = new URI(commitURL);
            hostName = uriParser.hostname();
            urlPath = uriParser.path();

            if (urlTranslation.hasOwnProperty(hostName)) {
                knownGit = urlTranslation[hostName];
                translateRule = knownGit[3];
                lenTranslate = translateRule.length;

                for (i = 0; i < lenTranslate; i = i + 1) {
                    urlPath = urlPath.replace(
                        translateRule[i][0], translateRule[i][1]);
                }

                baseURL = new URI({
                    protocol: knownGit[0],
                    hostname: hostName,
                    path: knownGit[1].replace('%s', urlPath)
                });

                newCommitURL = new URI({
                    protocol: knownGit[0],
                    hostname: hostName,
                    path: knownGit[2].replace('%s', urlPath) + commitId
                });

                baseURL = baseURL.href();
                newCommitURL = newCommitURL.href();
            }
        }
        return [baseURL, newCommitURL];
    }

    // Set up the base functionalities common to (almost) all pages.
    function init() {
        setHotKeys();

        var body = $('body');

        body.tooltip({
            'selector': '[rel=tooltip]',
            'placement': 'auto top'
        });

        body.scrollspy({
            target: '#sidebar-nav'
        });

        $('.clickable-table tbody').on('click', 'tr', function() {
            var url = $(this).data('url');
            if (url) {
                window.location = url;
            }
        });

        $('.btn-group > .btn').click(function() {
            $(this).addClass('active').siblings().removeClass('active');
        });
    }

    /*
        Create a modal dialog with a single x button to close it.
    */
    function createLargeModalDialog(id, title, body) {
        var mDialog = '<div class="modal fade" tabindex="-1" ' +
            'role="dialog" aria-hidden="true" id="' + id + '">';

        mDialog += '<div class="modal-dialog modal-lg larger-modal">' +
            '<div class="modal-content">' +
            '<div class="modal-header">' +
            '<button type="button" class="close" ' +
            'data-dismiss="modal"' +
            'aria-hidden="true">&times;</button>' +
            '<h3 class="modal-title" id="' + id + '-title">' +
            title + '</h3>' +
            '<div class="modal-body">' +
            body +
            '</div></div></div></div></div>';

        return mDialog;
    }

    return {
        collectObjects: collectObjects,
        createDeferredCall: createDeferredCall,
        createLargeModalDialog: createLargeModalDialog,
        init: init,
        loadHTMLContent: loadHTMLContent,
        populateSideBarNav: populateSideBarNav,
        removeElementByID: removeElementByID,
        removeCssClassForID: removeCssClassForID,
        replaceContentByClass: replaceContentByClass,
        replaceContentByID: replaceContentByID,
        setErrorAlert: setErrorAlert,
        showElementByID: showElementByID,
        translateCommitURL: translateCommitURL
    };
}());

/*
    Return a custom date in ISO format.
    The format returned is: YYYY-MM-DD
*/
Date.prototype.getCustomISODate = function() {
    'use strict';
    var year = this.getUTCFullYear().toString(),
        month = (this.getUTCMonth() + 1).toString(),
        day = this.getUTCDate().toString();

    month = month[1] ? month : '0' + month[0];
    day = day[1] ? day : '0' + day[0];

    return year + '-' + month + '-' + day;
};

/*
    Return a custom date in ISO format, based on UTC time.
    The format returned is: YYYY-MM-DD HH:MM:SS UTC
*/
Date.prototype.getCustomISOFormat = function() {
    'use strict';
    var year = this.getUTCFullYear().toString(),
        month = (this.getUTCMonth() + 1).toString(),
        day = this.getUTCDate().toString(),
        hour = this.getUTCHours().toString(),
        minute = this.getUTCMinutes().toString(),
        seconds = this.getUTCSeconds().toString();

    month = month[1] ? month : '0' + month[0];
    day = day[1] ? day : '0' + day[0];

    hour = hour[1] ? hour : '0' + hour[0];
    minute = minute[1] ? minute : '0' + minute[0];
    seconds = seconds[1] ? seconds : '0' + seconds[0];

    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute +
        ':' + seconds + ' UTC';
};

/*
    Return a custom time representation. This is mostly useful to calulate
    elapsed time.
    The full format returned is: X hours Y min. Z sec. T mill.

    If one of the values for hours, minutes, seconds or milliseconds is 0,
    it will not be returned.
*/
Date.prototype.getCustomTime = function() {
    'use strict';
    var localHours = this.getUTCHours(),
        localMinutes = this.getUTCMinutes(),
        localSeconds = this.getUTCSeconds(),
        localMilliseconds = this.getMilliseconds(),
        localTime = '';

    if (localHours !== 0) {
        localTime += localHours.toString() + ' hours ';
    }

    if (localMinutes !== 0) {
        localTime += localMinutes.toString() + ' min. ';
    }

    if (localSeconds !== 0) {
        localTime += localSeconds.toString() + ' sec. ';
    }

    if (localMilliseconds !== 0) {
        localTime += localMilliseconds.toString() + ' mill.';
    }

    if (!localTime) {
        localTime = '0';
    }

    return localTime.trim();
};