Completed
Push — master ( 090c72...ff33da )
by Dylan
04:20 queued 01:54
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.event_handler.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.event_handler.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.event_handler.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} name
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 ) this.set_type(name, 'error');
97
        else if( cont.find('td div.alert-warning').length > 0 ) this.set_type(name, 'warning');
98
        else if( cont.find('td div.alert-info').length > 0 ) this.set_type(name, 'info');
99
        else if( cont.find('td div.alert-success').length > 0 ) this.set_type(name, 'success');
100
        return undefined;
101
    },
102
103
    /**
104
     * Create a status field to be used in the report rows
105
     *
106
     * @param {string} type
107
     * @param {string} text
108
     * @return {jQuery}
109
     */
110
    create_status: function(type, text){
111
        var ret = $('<div class="status-text alert"></div>');
112
        switch(type){
113
            case 'info':
114
                ret.addClass('alert-info');
115
                ret.append('<i class="glyphicon glyphicon-info-sign">&nbsp;</i>');
116
                break;
117
118
            case 'error':
119
                ret.addClass('alert-danger');
120
                ret.append('<i class="glyphicon glyphicon-exclamation-sign">&nbsp;</i>');
121
                break;
122
123
            case 'success':
124
                ret.addClass('alert-success');
125
                ret.append('<i class="glyphicon glyphicon-ok-sign">&nbsp;</i>');
126
                break;
127
128
            case 'warning':
129
                ret.addClass('alert-warning');
130
                ret.append('<i class="glyphicon glyphicon-warning-sign">&nbsp;</i>');
131
                break;
132
            default: return undefined;
133
        }
134
        ret.append(text);
135
        return ret;
136
    },
137
138
    /**
139
     * Return the container matching the provided name
140
     *
141
     * @param {string} name
142
     * @return {jQuery|undefined}
143
     */
144
    get_container_by_name: function(name){
145
        for(var c in this.containers) if(this.containers[c]['name'] == name) return this.containers[c]['container'];
146
        return undefined;
147
    },
148
149
    /**
150
     * Set the type of the test so it's colour changes according
151
     *
152
     * @param {string} name
153
     * @param {string} type
154
     * @return undefined
155
     */
156
    set_type: function(name, type){
157
        var cont = this.get_container_by_name(name);
158
        cont.removeClass('blue red green yellow purple');
159
160
        switch(type){
161
            case 'info': return cont.addClass('blue');
162
            case 'error': return cont.addClass('red');
163
            case 'success': return cont.addClass('green');
164
            case 'warning': return cont.addClass('yellow');
165
            default: return cont.addClass('purple');
166
        }
167
    },
168
169
    /**
170
     * Update the header stats
171
     */
172
    update_header: function(){
173
        $('#leftcount').html(crawler.que.length);
174
        $('#donecount').html(crawler.tested.length);
175
176
        if(crawler.que.length > 0 ) $('#analyzestatus').html('Analyzing');
177
        else if(crawler.que.length < 1 && crawler.tested.length > 0) $('#analyzestatus').html('Finished');
178
    },
179
180
    /**
181
     * Returns a link out of the passed url
182
     *
183
     * @param {string} url
184
     * @param {string|undefined} anchor
185
     * @returns {string}
186
     */
187
    create_link: function(url, anchor){
188
        anchor = (anchor) ? anchor : url;
189
        return '<a class="btn btn-link" href="'+url+'" title="'+anchor+'" target="_blank" rel="nofollow">'
190
            +'<span class="glyphicon glyphicon-new-window">&nbsp;</span>'+
191
            ((anchor.length > 29) ? anchor.substr(0, 27) + '...' : anchor)
192
            +'</a>';
193
    },
194
195
    /**
196
     * Initialize the painter
197
     */
198
    init:function(){
199
        for(var c in this.containers) $('#results_container').append(this.containers[c]['container']);
200
        crawler.event_handler.on('CRAWL_FINISHED', this.update_header);
201
    }
202
};
203