Completed
Push — master ( 3f2ac7...f065be )
by Dylan
02:41
created

crawler_painter.add_row   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 21
rs 7.551
c 1
b 1
f 0
cc 7
nc 20
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
     */
70
    add_row: function(name, data){
71
        var cont    = this.get_container_by_name(name),
72
            table   = cont.find('tbody'),
73
            row     = $('<tr></tr>').appendTo(table),
74
            len     = table.find('tr').length;
75
76
        for(var d in data) row.append($('<td/>').append(data[d]));
77
78
        // Show icons if we have items
79
        if( len > 0 ){
80
            cont.find('.icon').fadeIn();
81
        }
82
83
        cont.find('.count').html(len);
84
85
        // Set the header colour
86
        if( cont.find('td div.alert-danger').length > 0 ) crawler_painter.set_type(name, 'error');
87
        else if( cont.find('td div.alert-warning').length > 0 ) crawler_painter.set_type(name, 'warning');
88
        else if( cont.find('td div.alert-info').length > 0 ) crawler_painter.set_type(name, 'info');
89
        else if( cont.find('td div.alert-success').length > 0 ) crawler_painter.set_type(name, 'success');
90
    },
91
92
    /**
93
     * Reset the data inside of the table for the container named {name}
94
     *
95
     * @param {string} name
96
     * @param {string|undefined} type
97
     * @return undefined
98
     */
99
    reset_table: function(name, type){
100
        var cont = this.get_container_by_name(name);
101
102
        cont.find('tbody tr').remove();
103
        cont.find('.count').html('');
104
        cont.find('.icon').hide();
105
106
        if( type != undefined ) this.set_type(name, type);
107
108
        return undefined;
109
    },
110
111
    /**
112
     * Create a status field to be used in the report rows
113
     *
114
     * @param {string} type
115
     * @param {string} text
116
     * @return {jQuery}
117
     */
118
    create_status: function(type, text){
119
        var ret = $('<div class="status-text alert"></div>');
120
        switch(type){
121
            case 'info':
122
                ret.addClass('alert-info');
123
                ret.append('<i class="glyphicon glyphicon-info-sign">&nbsp;</i>');
124
                break;
125
126
            case 'error':
127
                ret.addClass('alert-danger');
128
                ret.append('<i class="glyphicon glyphicon-exclamation-sign">&nbsp;</i>');
129
                break;
130
131
            case 'success':
132
                ret.addClass('alert-success');
133
                ret.append('<i class="glyphicon glyphicon-ok-sign">&nbsp;</i>');
134
                break;
135
136
            case 'warning':
137
                ret.addClass('alert-warning');
138
                ret.append('<i class="glyphicon glyphicon-warning-sign">&nbsp;</i>');
139
                break;
140
            default: return undefined;
141
        }
142
        ret.append(text);
143
        return ret;
144
    },
145
146
    /**
147
     * Return the container matching the provided name
148
     *
149
     * @param {string} name
150
     * @return {jQuery|undefined}
151
     */
152
    get_container_by_name: function(name){
153
        for(var c in this.containers) if(this.containers[c]['name'] == name) return this.containers[c]['container'];
154
        return undefined;
155
    },
156
157
    /**
158
     * Set the type of the test so it's colour changes according
159
     *
160
     * @param {string} name
161
     * @param {string} type
162
     */
163
    set_type: function(name, type){
164
        var cont = this.get_container_by_name(name);
165
        cont.removeClass('blue red green yellow purple');
166
        switch(type){
167
            case 'info': return cont.addClass('blue');
168
            case 'error': return cont.addClass('red');
169
            case 'success': return cont.addClass('green');
170
            case 'warning': return cont.addClass('yellow');
171
            default: return cont.addClass('purple');
172
        }
173
    },
174
175
    /**
176
     * Update the header stats
177
     */
178
    update_header: function(){
179
        $('#leftcount').html(crawler.que.length);
180
        $('#donecount').html(crawler.tested.length);
181
182
        if(crawler.que.length > 0 ) $('#analyzestatus').html('Analyzing');
183
        else if(crawler.que.length < 1 && crawler.tested.length > 0) $('#analyzestatus').html('Finished');
184
    },
185
186
    /**
187
     * Returns a link out of the passed url
188
     *
189
     * @param {string} url
190
     * @param {string|undefined} anchor
191
     * @returns {string}
192
     */
193
    create_link: function(url, anchor){
194
        anchor = (anchor) ? anchor : url;
195
        return '<a class="btn btn-link" href="'+url+'" title="'+anchor+'" target="_blank" rel="nofollow">'
196
            +'<span class="glyphicon glyphicon-new-window">&nbsp;</span>'+
197
            ((anchor.length > 29) ? anchor.substr(0, 27) + '...' : anchor)
198
            +'</a>';
199
    },
200
201
    /**
202
     * Initialize the painter
203
     */
204
    init:function(){
205
        for(var c in this.containers) $('#results_container').append(this.containers[c]['container']);
206
        crawler.on('CRAWL_FINISHED', this.update_header);
207
    }
208
};
209