bansho/app/components/table/table.js

152 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
angular.module('adagios.table', ['adagios.live',
'adagios.table.actionbar',
'adagios.filters',
'adagios.table.cell_host',
'adagios.table.cell_duration',
'adagios.table.cell_service_check',
'adagios.table.cell_last_check',
'adagios.table.cell_hosts_host',
'adagios.table.cell_host_address',
'adagios.table.cell_host_status'
])
.value('tableConfig', { cells: { 'text': [], 'name': [] },
apiName: '',
filters: {},
cellToFieldsMap: {},
toWrap: [],
noRepeat: []})
.controller('TableCtrl', ['$scope', 'getServices', 'tableConfig', 'processColumnRepeat',
function ($scope, getServices, tableConfig, processColumnRepeat) {
var requestFields = [],
filters = JSON.parse(tableConfig.filters),
i;
$scope.cellsName = tableConfig.cells.name;
$scope.cellsText = tableConfig.cells.text;
$scope.cellIndexes = [];
for (i = 0; i < $scope.cellsName.length; i += 1) {
$scope.cellIndexes.push(i);
}
angular.forEach($scope.cellsName, function (key) {
angular.forEach(tableConfig.cellToFieldsMap[key], function (_value) {
requestFields.push(_value);
});
});
getServices(requestFields, filters, tableConfig.apiName)
.success(function (data) {
console.log(tableConfig.toWrap[0]);
$scope.entries = processColumnRepeat(data, tableConfig.cellToFieldsMap[tableConfig.toWrap[0]][1], tableConfig.cellToFieldsMap[tableConfig.toWrap[0]]);
});
}])
.directive('adgTable', ['$http', '$compile', 'tableConfig', function ($http, $compile, tableConfig) {
return {
restrict: 'E',
compile: function () {
return function (scope, element, attrs) {
if (!attrs.cellsText || !attrs.cellsName || !attrs.apiName) {
throw new Error('<adg-table> "cells-text", "cells-name" and "api-name" attributes must be defined');
}
tableConfig.cells.text = attrs.cellsText.split(',');
tableConfig.cells.name = attrs.cellsName.split(',');
tableConfig.apiName = attrs.apiName;
tableConfig.toWrap = attrs.toWrap.split(',');
tableConfig.noRepeat = attrs.noRepeat.split(',');
if (!!attrs.filters) {
tableConfig.filters = attrs.filters;
}
var template = 'components/table/table.html';
$http.get(template, { cache: true })
.success(function (data) {
var elem = $compile(data)(scope);
element.append(elem);
});
};
}
};
}])
.directive('adgCell', ['$http', '$compile', function ($http, $compile) {
return {
restrict: 'A',
compile: function () {
return function (scope, element, attrs) {
if (!attrs.cellName) {
throw new Error('<adg-cell> "cell-name" attribute must be defined');
}
var template = 'components/table/cell_' + attrs.cellName + '/cell_' + attrs.cellName + '.html';
$http.get(template, { cache: true })
.success(function (data) {
var td = $compile(data)(scope);
// HACK : replaceWith is a necessary hack because <tr> only accepts <td> as a child
element.replaceWith(td);
});
};
}
};
}])
.service('processColumnRepeat', function() {
function clearFields(entry, fields) {
angular.forEach(fields, function (value) {
entry[value] = '';
});
};
return function (data, fieldToProcess, fields) {
var last = '',
actual = '',
entry = {},
first_child = false,
parent_found = false,
i;
console.log(fieldToProcess);
for (i = 0; i < data.length; i += 1) {
entry = data[i];
actual = entry[fieldToProcess];
console.log(entry.host_name + " " + entry[fieldToProcess] + " === " + last);
if (entry[fieldToProcess] === last) {
if (!data[i-1].has_child && !parent_found) {
data[i-1].has_child = 1;
data[i-1].child_class='state--hasChild';
entry.child_class='state--isChild';
parent_found = true;
} else {
entry.is_child = 1;
entry.child_class='state--isChild';
}
clearFields(entry, fields);
} else {
first_child = false;
parent_found = false;
}
last = actual;
}
return data;
}
});