'use strict';

var dashboardApp = angular.module('dashboardApp', [
    'dashboardControllers'
  ]
).directive('simpleTable', function() {
       return {
            restrict: 'A',
            link: function($scope, $element, $attrs) {
                var data = $scope.$eval($attrs.data);
                var colsWatcher = function(newCol, oldCol) {
                    if (newCol !== oldCol) {
                        $element.html("");
                        var cols = $.extend([], newCol);
                        var header = "<thead><tr>";
                        angular.forEach(cols, function(c) {
                            if (c.headerClass != undefined) {
                                header += "<th class=\"" + c.headerClass + "\">" + c.label +"</th>";
                            } else {
                                header += "<th>" + c.label +"</th>";
                            }
                        });
                        header += "</tr></thead>";
                        $element.append(header);
                    }
                };
                $scope.$watch(data.columns, colsWatcher);

                var rowsWatcher = function(newRow, oldRow) {
                    if (newRow !== oldRow) {
                        var rows = $.extend([], newRow);
                        var body = "<tbody>";
                        angular.forEach(rows, function(row) {
                            body += "<tr>";
                            angular.forEach(row, function(cell, field) {
                                if (field != "key") {
                                    body += "<td style=\"text-align:right\">" + cell +"</td>";
                                } else {
                                    body += "<td>" + cell +"</td>";
                                }
                            });
                            body += "</tr>";
                        });
                        body += "</tbody>";
                        $element.append(body);
                    }
                };
                $scope.$watch(data.rows, rowsWatcher);
            }
       }
}).directive('simpleGroupedTable', function() {
       return {
            restrict: 'A',
            link: function($scope, $element, $attrs) {
                var data = $scope.$eval($attrs.data);

                //FIXME: duplicite helper as in controllers.js
                var formatNumber = function(value) {
                    if (value == "") {
                        return value;
                    } else {
                        if (value % 1 != 0) {
                            return numeral(value).format('0,0.0000');
                        } else {
                            return numeral(value).format('0,0');
                        }
                    }
                };

                var colsWatcher = function(newCol, oldCol) {
                    if (newCol !== oldCol) {
                        $element.html("");
                        var cols = $.extend([], newCol);
                        var header = "<thead><tr>";
                        angular.forEach(cols, function(c) {
                            if (c.headerClass != undefined) {
                                header += "<th class=\"" + c.headerClass + "\">" + c.label +"</th>";
                            } else {
                                header += "<th>" + c.label +"</th>";
                            }
                        });
                        header += "</tr></thead>";
                        $element.append(header);
                    }
                };
                $scope.$watch(data.columns, colsWatcher);

                var rowsWatcher = function(newRow, oldRow) {
                    if (newRow !== oldRow) {
                        var groups = $.extend([], newRow);
                        var body = "<tbody>";
                        angular.forEach(groups, function(group) {
                            body += "<tr>";
                            body += "<td colspan=\"9\"><strong>" + group.label + "</strong></td>";
                            body += "</tr>";
                            angular.forEach(group.items, function(row) {
                                body += "<tr>";
                                angular.forEach(row, function(cell, field) {
                                    if (field != "key") {
                                        body += "<td style=\"text-align:right\">" + cell +"</td>";
                                    } else {
                                        body += "<td colspan=\"2\" style=\"padding-left: 25px;\">" + cell +"</td>";
                                    }
                                });
                                body += "</tr>";
                            });
                        });
                        body += "</tbody>"
                        $element.append(body);
                    }
                };
                $scope.$watch(data.rows, rowsWatcher);
            }
       }
});
@@ -701,32 +701,58 @@
   header += %22%3Cth
+ title=%5C%22%22 + c.label + %22%5C%22
  class=%5C%22%22 + c.h
@@ -865,32 +865,58 @@
   header += %22%3Cth
+ title=%5C%22%22 + c.label + %22%5C%22
 %3E%22 + c.label +%22%3C
@@ -1806,16 +1806,39 @@
  += %22%3Ctd
+ title=%5C%22%22 + cell + %22%5C%22
 %3E%22 + cel
@@ -3329,16 +3329,42 @@
  += %22%3Cth
+ title=%5C%22%22 + c.label + %22%5C%22
  class=%5C
@@ -3493,16 +3493,42 @@
  += %22%3Cth
+ title=%5C%22%22 + c.label + %22%5C%22
 %3E%22 + c.l
@@ -4153,32 +4153,62 @@
     body += %22%3Ctd
+ title=%5C%22%22 + group.label + %22%5C%22
  colspan=%5C%229%5C%22%3E%3C
@@ -4788,15 +4788,38 @@
 ft: 
-25
+10
 px;%5C%22
+ title=%5C%22%22 + cell + %22%5C%22
 %3E%22 +

Add tooltips for keys and columns names