Completed
Push — master ( 80fdf3...351fbc )
by Vladimir
02:43
created

assets/js/src/servers.js (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
var crel = require('crel');
2
var tinysort = require('tinysort');
3
4
var $servers = $('.js-server');
5
6
/**
7
 * The JS object for servers
8
 *
9
 * @constructor
10
 * @param object {Node}
11
 */
12
function Server(object) {
13
    this.$object = $(object);
14
    this.url = baseURLNoHost + '/servers/status/' + this.$object.data('id');
15
    this.token = this.$object.data('token');
16
17
    // Alias for this object for callback functions
18
    var server = this;
19
20
    // Set up the 'force refresh' button
21
    this.$object.find('.js-refresh').click(function (event) {
22
        event.preventDefault();
23
24
        var $this = $(this);
25
26
        $this.addClass('fa-spin');
27
        server.updateServer(true, function () {
28
            $this.removeClass('fa-spin');
29
        });
30
    });
31
32
    this.updateCard = function (data) {
33
        this.$object.find('.js-server__last-update').html(data['last_update']);
0 ignored issues
show
['last_update'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
34
        this.$object.find('.js-server__player-count').html(data['player_count']);
0 ignored issues
show
['player_count'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
35
        this.$object.attr('data-player-count', data['player_count']);
0 ignored issues
show
['player_count'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
36
37
        // Statuses
38
        var status = (data['player_count'] > 0) ? 'active' : data['status'];
0 ignored issues
show
['player_count'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
['status'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
39
        this.$object.find('.js-server__status').attr('data-status', status);
40
    };
41
42
    /**
43
     * Enable the spinner element for the server card
44
     * @returns {Server}
45
     */
46
    this.startSpinners = function () {
47
        this.$object
48
            .find('.js-dimmable')
49
            .find('.dimmer, .spinner')
50
            .fadeIn('fast')
51
        ;
52
53
        return this;
54
    };
55
56
    /**
57
     * Hide the spinner element
58
     * @returns {Server}
59
     */
60
    this.stopSpinners = function () {
61
        this.$object
62
            .find('.js-dimmable')
63
            .find('.dimmer, .spinner')
64
            .fadeOut('fast')
65
        ;
66
67
        return this;
68
    };
69
70
    /**
71
     * Update the server card with information we're pulling via AJAX
72
     */
73
    this.updateServer = function (forceUpdate, onDoneCallback) {
74
        this.startSpinners();
75
76
        var urlCall = this.url;
77
        forceUpdate = (typeof forceUpdate === 'undefined') ? false : forceUpdate;
78
79
        if (forceUpdate) {
80
            urlCall += '?forced=1';
81
        }
82
83
        jQuery
84
            .post(urlCall, {
85
                token: this.token
86
            })
87
            .done(function(data) {
88
                server.updateCard(data);
89
                server.stopSpinners();
90
91
                (typeof onDoneCallback === 'function') && onDoneCallback();
0 ignored issues
show
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
92
            })
93
        ;
94
    }
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
95
}
96
97
function initialize() {
98
    // Hide any dimmers that might have been left
99
    $('.dimmer, .spinner').fadeOut('fast');
100
101
    var dimmer  = crel('div', { 'class': 'dimmer' });
102
    var spinner = crel('div', { 'class': 'spinner' }, 'Loading...');
103
104
    $('.js-dimmable')
105
        .prepend(dimmer)
106
        .prepend(spinner)
107
    ;
108
109
    loadAllServers();
110
}
111
112
function loadAllServers() {
113
    $servers.each(function () {
114
        new Server(this).updateServer();
115
    });
116
}
117
118
function sortServers() {
119
    var servers = $('.js-server');
120
121
    tinysort(servers, { attr: 'data-player-count', order: 'ASC' });
122
}
123
124
module.exports = function () {
125
    // If this page has no .js-server components, then no need to proceed
126
    if (!$servers.length) {
127
        return;
128
    }
129
130
    initialize();
131
    sortServers();
132
};
133