Completed
Push — develop ( 7852a2...d7f552 )
by Dylan
03:02
created

crawler_painter.add_row   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 16
rs 9.4285
c 2
b 1
f 0
cc 3
nc 4
nop 2
1
const crawler_painter = {
2
3
    containers: [],
4
5
    /**
6
     * Create a result table for the provided tests
7
     *
8
     * @param {string} name
9
     * @param {string} title
10
     * @param {Array} headers
11
     * @return {jQuery}
12
     */
13
    create: function(name, title, headers){
14
        var container       = $('<div class="infobox" id="'+name+'"></div>'),
15
            header          = $('<div class="header clearfix"></div>'),
16
            count           = $('<div class="count left"></div>'),
17
            export_button   = $('<div class="icon export glyphicon glyphicon-export right"></div>').hide(),
18
            toggle_button   = $('<div class="icon toggle glyphicon glyphicon-arrow-down right"></div>').hide(),
19
            title_bar       = $('<h2 class="left">'+title+'</h2>'),
20
            table_cont      = $('<div class="tableCont" style="display:none;"></div>'),
21
            thead           = $('<tr></tr>'),
22
            table           = $('<table class="table table-hover table-condensed"></table>').append(['<thead></thead>', '<tbody></tbody>']);
23
24
        for(var h in headers) thead.append('<th>'+headers[h]+'</th>');
25
        table.find('thead').append(thead);
26
27
        header.append([count, title_bar, toggle_button, export_button]);
28
        table_cont.append(table);
29
        container.append([header, table_cont]);
30
31
        toggle_button.click(function(){
32
            crawler.trigger('TOGGLED', [name]);
33
            var $this   = $(this),
34
                css     = ($this.hasClass('glyphicon-arrow-down')) ? 'glyphicon-arrow-up' : 'glyphicon-arrow-down';
35
            $this.parents('.infobox').find('.tableCont').slideToggle();
36
            $this.removeClass('glyphicon-arrow-up glyphicon-arrow-down');
37
            $this.addClass(css);
38
        });
39
40
        export_button.click(function(){
41
            crawler.trigger('BEFORE_EXPORT', [name]);
42
            var $this       = $(this),
43
                rows        = $this.parents( '.infobox' ).first().find( 'table tr' ),
44
                csvContent  = "data:text/csv;charset=utf-8,";
45
46
            $.each( rows, function(){
47
                var item = [];
48
                $.each( $(this).find( 'th, td' ), function(){ item.push( $(this).text() ); });
49
                csvContent += item.join(',') + "\n";
50
            });
51
52
            var link = document.createElement( 'a' );
53
            link.setAttribute( 'href', encodeURI( csvContent ) );
54
            link.setAttribute( 'download', name + '.csv' );
55
            link.click();
56
            crawler.trigger('AFTER_EXPORT', [name]);
57
        });
58
59
        this.containers.push({'name': name, 'container': container});
60
        return container;
61
    },
62
63
    /**
64
     * Add a row of data to the container which matches
65
     * the name provided
66
     *
67
     * @param {string} name
68
     * @param {Array} data
69
     * @return undefined
70
     */
71
    add_row: function(name, data){
72
        var cont    = this.get_container_by_name(name),
73
            table   = cont.find('tbody'),
74
            row     = $('<tr></tr>').appendTo(table),
75
            len     = table.find('tr').length;
76
77
        for(var d in data) row.append($('<td/>').append(data[d]));
78
79
        // Show icons if we have items
80
        if( len > 0 ){
81
            cont.find('.icon').fadeIn();
82
        }
83
84
        cont.find('.count').html(len);
85
        return this.set_type_by_data(name);
86
    },
87
88
    /**
89
     * Update the type of container named cont according to it's data
90
     *
91
     * @param {string} cont
0 ignored issues
show
Documentation introduced by
The parameter cont does not exist. Did you maybe forget to remove this comment?
Loading history...
92
     * @return undefined
93
     */
94
    set_type_by_data: function(name){
95
        var cont = this.get_container_by_name(name);
96
        if( cont.find('td div.alert-danger').length > 0 ) return crawler_painter.set_type(name, 'error');
97
        else if( cont.find('td div.alert-warning').length > 0 ) return crawler_painter.set_type(name, 'warning');
98
        else if( cont.find('td div.alert-info').length > 0 ) return crawler_painter.set_type(name, 'info');
99
        else if( cont.find('td div.alert-success').length > 0 ) return crawler_painter.set_type(name, 'success');
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if cont.find("td div.alert-success").length > 0 is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
100
    },
101
102
    /**
103
     * Add a row that only has a status
104
     *
105
     * @param {string} cont
106
     * @param {string} type
107
     * @param {string} string
108
     * @return undefined
109
     */
110
    add_status_row: function(cont, type, string){
111
        return crawler_painter.add_row(cont, [ this.create_status(type, string) ]);
112
    },
113
114
    /**
115
     * Reset the data inside of the table for the container named {name}
116
     *
117
     * @param {string} name
118
     * @param {string|undefined} type
119
     * @return undefined
120
     */
121
    reset_table: function(name, type){
122
        var cont = this.get_container_by_name(name);
123
124
        cont.find('tbody tr').remove();
125
        cont.find('.count').html('');
126
        cont.find('.icon').hide();
127
128
        if( type != undefined ) this.set_type(name, type);
129
130
        return undefined;
131
    },
132
133
    /**
134
     * Create a status field to be used in the report rows
135
     *
136
     * @param {string} type
137
     * @param {string} text
138
     * @return {jQuery}
139
     */
140
    create_status: function(type, text){
141
        var ret = $('<div class="status-text alert"></div>');
142
        switch(type){
143
            case 'info':
144
                ret.addClass('alert-info');
145
                ret.append('<i class="glyphicon glyphicon-info-sign">&nbsp;</i>');
146
                break;
147
148
            case 'error':
149
                ret.addClass('alert-danger');
150
                ret.append('<i class="glyphicon glyphicon-exclamation-sign">&nbsp;</i>');
151
                break;
152
153
            case 'success':
154
                ret.addClass('alert-success');
155
                ret.append('<i class="glyphicon glyphicon-ok-sign">&nbsp;</i>');
156
                break;
157
158
            case 'warning':
159
                ret.addClass('alert-warning');
160
                ret.append('<i class="glyphicon glyphicon-warning-sign">&nbsp;</i>');
161
                break;
162
            default: return undefined;
163
        }
164
        ret.append(text);
165
        return ret;
166
    },
167
168
    /**
169
     * Return the container matching the provided name
170
     *
171
     * @param {string} name
172
     * @return {jQuery|undefined}
173
     */
174
    get_container_by_name: function(name){
175
        for(var c in this.containers) if(this.containers[c]['name'] == name) return this.containers[c]['container'];
176
        return undefined;
177
    },
178
179
    /**
180
     * Set the type of the test so it's colour changes according
181
     *
182
     * @param {string} name
183
     * @param {string} type
184
     * @return undefined
185
     */
186
    set_type: function(name, type){
187
        var cont = this.get_container_by_name(name);
188
        cont.removeClass('blue red green yellow purple');
189
190
        switch(type){
191
            case 'info': return cont.addClass('blue');
192
            case 'error': return cont.addClass('red');
193
            case 'success': return cont.addClass('green');
194
            case 'warning': return cont.addClass('yellow');
195
            default: return cont.addClass('purple');
196
        }
197
198
        return undefined;
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
199
    },
200
201
    /**
202
     * Update the header stats
203
     */
204
    update_header: function(){
205
        $('#leftcount').html(crawler.que.length);
206
        $('#donecount').html(crawler.tested.length);
207
208
        if(crawler.que.length > 0 ) $('#analyzestatus').html('Analyzing');
209
        else if(crawler.que.length < 1 && crawler.tested.length > 0) $('#analyzestatus').html('Finished');
210
    },
211
212
    /**
213
     * Returns a link out of the passed url
214
     *
215
     * @param {string} url
216
     * @param {string|undefined} anchor
217
     * @returns {string}
218
     */
219
    create_link: function(url, anchor){
220
        anchor = (anchor) ? anchor : url;
221
        return '<a class="btn btn-link" href="'+url+'" title="'+anchor+'" target="_blank" rel="nofollow">'
222
            +'<span class="glyphicon glyphicon-new-window">&nbsp;</span>'+
223
            ((anchor.length > 29) ? anchor.substr(0, 27) + '...' : anchor)
224
            +'</a>';
225
    },
226
227
    /**
228
     * Initialize the painter
229
     */
230
    init:function(){
231
        for(var c in this.containers) $('#results_container').append(this.containers[c]['container']);
232
        crawler.on('CRAWL_FINISHED', this.update_header);
233
    }
234
};
235