Completed
Push — develop ( 321035...7852a2 )
by Dylan
02:53
created

crawler_painter.add_status_row   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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