angular.module('app').directive('comparisonTable', ['$state', function($state) { function tblHeader(facets) { var r1 = ['ShipBuild']; var r2 = []; for (var i = 0, l = facets.length; i < l; i++) { if (facets[i].active) { var f = facets[i]; var p = f.props; var pl = p.length; r1.push('', f.lbls[j], ''); } } r1.push('>', f.title, ''); } } r1.push(''); r1.push(r2.join('')); r1.push(''); return r1.join(''); } function tblBody(facets, builds) { var body = []; if (builds.length === 0) { return 'No builds added to comparison!'); var href = $state.href('outfit', { shipId: b.id, code: b.code, bn: b.buildName }); body.push('', b.name, ''); body.push('', b.buildName, ''); for (var j = 0, fl = facets.length; j < fl; j++) { if (facets[j].active) { var f = facets[j]; var p = f.props; for (var k = 0, pl = p.length; k < pl; k++) { body.push('', f.fmt(b[p[k]]), ' ', f.unit, ''); } } } body.push(''); } return body.join(''); } return { restrict: 'A', link: function(scope, element) { var header = angular.element(''); var body = angular.element(''); element.append(header); element.append(body); var updateAll = function() { header.html(tblHeader(scope.facets)); body.html(tblBody(scope.facets, scope.builds)); }; scope.$watchCollection('facets', updateAll); scope.$watch('tblUpdate', updateAll); scope.$watchCollection('builds', function() { body.html(tblBody(scope.facets, scope.builds)); }); } }; }]);