Code coverage report for View/GraphsView/TableView.js

Statements: 94.23% (98 / 104)      Branches: 83.93% (47 / 56)      Functions: 100% (6 / 6)      Lines: 94.23% (98 / 104)      Ignored: none     

All files » View/GraphsView/ » TableView.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222                                                        1   4                 4 4 4       4 8 4 4 4 4           4 4 4 4 4   4 4 4 4         4 4           4 2 2   4 1 1   4 4 4 4 4     4 3 11 11 11 11       4   4 4 14 14 11   14 7   7   14 3 3     11     4     4 4 1     3 3 6 3 1     2       3 1     2       3             4 14 14 14 14 14           14 14 14 7   7   14 14           4   4           4   4 4     4 4       4 4     4 9 9 9 9 33 8   33   9     4        
/*jshint node: true */
'use strict';
 
/*
* Name :  TableView.js
* Module : FrontEnd::View::GraphsView
* Location : /frontend/app/View/GraphsView
*
* History :
* Version       Date        Programmer                  Description
* =================================================================================================
* 1.0.3         2015-06-24  Maria Giovanna Chinellato   Fix table sorting
*
* 1.0.2         2015-06-22  Maria Giovanna Chinellato   Fix appearance of table
*
* 1.0.1         2015-06-22  Maria Giovanna Chinellato   Fix update of data
*
* 1.0.0         2015-06-13  Francesco Rossetto          Tested
*
* 0.1.0         2015-06-05  Maria Giovanna Chinellato   Add function: init and setData, fix code   
*
* 0.0.2         2015-06-04  Maria Giovanna Chinellato   Add link function, observe and watch      
*
* 0.0.1         2015-06-02  Maria Giovanna Chinellato   Initial code      
* =================================================================================================
*/
 
 
angular.module('norris-nrti')
.directive('tableChart', function($compile){
    return { // attributo della direttiva
        restrict: 'E', // direttiva di tipo elemento (tag)
        replace: false,
        scope: {
            url: '@'
        },
        //bindToController: true,
        link: function(scope, element, attrs){
 
            attrs.$observe('url', function(value) {
                Eif (value) {
                    scope.$parent.socketConnection(value); // richiama la funzione del controller che permette di connettersi al server
                }
            });
 
            scope.$parent.$watch('changed', function(newValue, oldValue){
                if (newValue !== oldValue) {
                    scope.setData(); // chiama la funzione che organizza i dati da visualizzare
                    Eif (scope.$parent.changedP === true) {
                        scope.init(); // chiama la funzione init che crea la tabella
                        scope.$parent.changedP = false;
                    }
                }
            }, true);
 
            // crea il codice HTML da inserire nella pagina per creare la tabella
            scope.init = function(){
                element.empty();
                var border;
                var noBorder = 'class="table-condensed table-striped"';
                var headers = 'color: #000; background-color: #FFF;';
 
                var width;
                var height;
                Eif (scope.$parent.table.getWidth() !== 0){
                    width = ' ' + scope.$parent.table.getWidth() + 'px';
                }
                else{
                    width = '100%';
                }
                Eif (scope.$parent.table.getHeight() !== 0){
                    height = ' ' + scope.$parent.table.getHeight() + 'px';
                }
                else{
                    height = '100%';
                }
                
                if (scope.$parent.table.getAppearance().horizontalGrid !== undefined && scope.$parent.table.getAppearance().horizontalGrid !== null) {
                    border = 'border-top:' + scope.$parent.table.getAppearance().horizontalGrid.width + 'px solid ' + scope.$parent.table.getAppearance().horizontalGrid.color + ';';
                    border = border + 'border-bottom:' + scope.$parent.table.getAppearance().horizontalGrid.width + 'px solid ' + scope.$parent.table.getAppearance().horizontalGrid.color + ';';
                }
                if (scope.$parent.table.getAppearance().verticalGrid !== undefined && scope.$parent.table.getAppearance().verticalGrid !== null) {
                    border = border + 'border-left:' + scope.$parent.table.getAppearance().verticalGrid.width + 'px solid ' + scope.$parent.table.getAppearance().verticalGrid.color + ';';
                    border = border + 'border-right:' + scope.$parent.table.getAppearance().verticalGrid.width + 'px solid ' + scope.$parent.table.getAppearance().verticalGrid.color + ';';
                }
                var tableStyle = 'style="' + border + ' ';
                var dim = 'style="width:' + width + '; height:' + height + ';"';
                var str = scope.url.split('/');
                var id = str[str.length-1];
                var table = '<style>';
 
                // mette l'aspetto delle celle di riga pari e di riga dispari
                if(scope.$parent.table.getAppearance().rowOdd !== undefined && scope.$parent.table.getAppearance().rowEven !== undefined){
                    for (var td=0;td<scope.$parent.table.getHeaders().length;td++){
                        table = table + 'table #' + id +  ' tr:nth-child(odd) td:nth-child(' + (td+1) + '){ color: ' + scope.$parent.table.getAppearance().rowOdd.textColor[0] + '; ';
                        table = table + 'background-color: ' + scope.$parent.table.getAppearance().rowOdd.backgroundColor[td] + ';} ';
                        table = table + 'table #' + id +  ' tr:nth-child(even) td:nth-child(' + (td+1) + '){ color: ' + scope.$parent.table.getAppearance().rowEven.textColor[0] + '; ';
                        table = table + 'background-color: ' + scope.$parent.table.getAppearance().rowEven.backgroundColor[td] + ';}';  
                    }
                }
 
                table = table + '</style><div class="graphtitle">'+ scope.$parent.table.getTitle() +'</div><table id="' + id + '" ' + dim + ' st-table="displayed" st-safe-src="rowCollection" ';
 
                table = table + '<thead><tr>';
                for (var i=0; i<scope.$parent.table.getHeaders().length; i++) {
                    table = table + '<th ';
                    if (scope.$parent.table.getAppearance().headers !== undefined) {
                        headers = 'color:' + scope.$parent.table.getAppearance().headers.textColor[i] + '; background-color:' + scope.$parent.table.getAppearance().headers.backgroundColor[i] + '; ';
                    }
                    if ((scope.$parent.table.getAppearance().horizontalGrid !== undefined && scope.$parent.table.getAppearance().horizontalGrid !== null) || (scope.$parent.table.getAppearance().verticalGrid !== undefined && scope.$parent.table.getAppearance().verticalGrid !== null)) {
                        table = table + tableStyle + headers + ' " ';
                    } else {
                        table = table + noBorder + 'style="' + headers + ' " ';
                    }
                    if (scope.$parent.table.getSortable() === true && scope.$parent.table.getSort().column.length === 0) {
                        table = table + 'st-sort="record.'+ scope.$parent.table.getHeaders()[i] +'"';
                        table = table + '><a href="">'+ scope.$parent.table.getHeaders()[i] +'</a></th>';
                    }
                    else{
                        table = table + '>'+ scope.$parent.table.getHeaders()[i] +'</th>';
                    }
                }
                table = table + '</tr></thead>';
 
                // controlla se รจ abilitato qualche ordinamento da parte dello sviluppatore e ordina i record
                Eif (scope.$parent.table.getSort() !== null){
                    if (scope.$parent.table.getSort().column.length === 1){
                        table = table + '<tbody><tr ng-repeat="line in displayed | orderBy:\''+ scope.$parent.table.getSort().column[0] +'\'">';
                    }
                    else{
                        var order = ' | orderBy:[';
                        for (var s=0; s<scope.$parent.table.getSort().column.length; s++){
                            if (scope.$parent.table.getSort().ordering[s] === 'ASC'){
                                if (s === scope.$parent.table.getSort().column.length-1){
                                    order = order + '\'record.'+ scope.$parent.table.getSort().column[s] +'\'';
                                }
                                else{
                                    order = order + '\'record.'+ scope.$parent.table.getSort().column[s] +'\',';
                                }
                            }
                            else{
                                if (s === scope.$parent.table.getSort().column.length-1){
                                    order = order + '\'-record.'+ scope.$parent.table.getSort().column[s] +'\'';
                                }
                                else{
                                    order = order + '\'-record.'+ scope.$parent.table.getSort().column[s] +'\',';
                                }
                            }
                        }
                        table = table + '<tbody><tr ng-repeat="line in displayed' + order + ']">';
                    }
                }
                else{
                    table = table + '<tbody><tr ng-repeat="line in displayed">';
                }
                //i = 0;
                for (var j=0; j<scope.$parent.table.getHeaders().length; j++){
                  var cellBG;
                  var cellText;
                  Eif(isNaN(scope.$parent.table.getHeaders()[j])){
                    cellBG = 'background-color: {{line.appearance.' + scope.$parent.table.getHeaders()[j] + '.bg}};';
                    cellText = 'color: {{line.appearance.' + scope.$parent.table.getHeaders()[j] + '.text}};';
                  }
                  else{
                    cellBG = 'background-color: {{line.appearance[' + scope.$parent.table.getHeaders()[j] + '].bg}};';
                    cellText = 'color: {{line.appearance[' + scope.$parent.table.getHeaders()[j] + '].text}};';
                  }
                    var cellStyle = 'style="' + border + cellBG + cellText + '"';
                    table = table + '<td ';
                    if ((scope.$parent.table.getAppearance().horizontalGrid !== undefined && scope.$parent.table.getAppearance().horizontalGrid !== null) || (scope.$parent.table.getAppearance().verticalGrid !== undefined && scope.$parent.table.getAppearance().verticalGrid !== undefined)) {
                        table = table + cellStyle;
                    } else {
                        table = table + noBorder + cellStyle;
                    }
                    Eif(isNaN(scope.$parent.table.getHeaders()[j])){
                      table = table + '>{{line.record.'+ scope.$parent.table.getHeaders()[j] +'}}</td>';
                    }
                    else{
                      table = table + '>{{line.record['+ scope.$parent.table.getHeaders()[j] +']}}</td>';
                    }
                }
                table = table + '</tr></tbody>';
 
                table = table + '<tfoot><tr>' +
                                    '<td  style="background-color: #FFF;" colspan="5" class="text-center">' +
                                        '<div st-pagination="" st-items-by-page="itemsByPage" st-displayed-pages="5"></div>' +
                                    '</td>' +
                                '</tr></tfoot>';
 
                table = table + '</table>';
                
                var compiled = $compile(table)(scope);
                element.append(compiled);
            };
 
            scope.displayed = [].concat(scope.rowCollection);
            scope.rowCollection = [];
            //scope.appearance;
 
            // imposta i dati da visualizzare
            scope.setData = function(){
                scope.rowCollection = []; // array che contiene una copia dei dati per permettere paginazione e ordinamento con dati dinamici
                //scope.appearance = [];
                //var appearance = ;
                for (var k=0; k<scope.$parent.table.getFlowList().length; k++) {
                    for (var i=0; i<scope.$parent.table.getFlowList()[k].flow.getData().length; i++) {
                        var appearance = {};
                        var record = {};
                        for (var j=0; j<scope.$parent.table.getHeaders().length; j++) {
                            if (scope.$parent.table.getFlowList()[k].flow.getData()[i].appearance !== undefined){
                                appearance[scope.$parent.table.getHeaders()[j]] = scope.$parent.table.getFlowList()[k].flow.getData()[i].appearance[j];
                            }
                            record[scope.$parent.table.getHeaders()[j]] = scope.$parent.table.getFlowList()[k].flow.getData()[i].value[j];
                        }
                        scope.rowCollection.push({'appearance': appearance, 'record': record});
                    }
                }
                scope.itemsByPage = scope.$parent.table.getMaxItemsPage();
            };
        }
    };
});