|
// ==UserScript==
|
|
// @id redmine_totals
|
|
// @name Redmine Totals
|
|
// @version 0.1
|
|
// @namespace http://jamesarobertson.co.nz/
|
|
// @author James Robertson
|
|
// @description Redmine issue list totals
|
|
// @run-at document-end
|
|
// ==/UserScript==
|
|
|
|
function isDate(str) {
|
|
return !isNaN(Date.parse(str));
|
|
}
|
|
|
|
function isNumber(n) {
|
|
return !isNaN(parseFloat(n)) && isFinite(n);
|
|
}
|
|
|
|
function addRowValuesToTotals(row, totals) {
|
|
var cell_content;
|
|
var cell_value;
|
|
var cells = row.getElementsByTagName('td');
|
|
for (var col_num=2; col_num<cells.length; col_num++) {
|
|
cell_content = cells[col_num].innerHTML;
|
|
if (!isDate(cell_content)) {
|
|
cell_value = parseFloat(cell_content);
|
|
if (isNumber(cell_value)){
|
|
if(!totals[col_num].total) {
|
|
totals[col_num].total = 0;
|
|
totals[col_num].group_total = 0;
|
|
}
|
|
totals[col_num].total += cell_value;
|
|
totals[col_num].group_total += cell_value;
|
|
}
|
|
}
|
|
}
|
|
return totals;
|
|
}
|
|
|
|
function insertTotalsRow(table_body, totals, is_group_total, before_row) {
|
|
if(is_group_total==null) is_group_total = false;
|
|
|
|
var totals_row = document.createElement('tr');
|
|
|
|
totals_row.style.textAlign = 'center';
|
|
if (!is_group_total){
|
|
totals_row.style.borderTop = "1px solid #CCCCCC";
|
|
totals_row.style.fontWeight = 'bold';
|
|
}
|
|
|
|
var cell;
|
|
for (var col_num=1; col_num<totals.length; col_num++){
|
|
cell = document.createElement('td');
|
|
if (col_num == 1) {
|
|
cell.colSpan = 2;
|
|
if (is_group_total) {
|
|
cell.innerHTML = ' ';
|
|
} else {
|
|
cell.innerHTML = 'Total';
|
|
}
|
|
} else {
|
|
if (totals[col_num].total){
|
|
if(is_group_total) {
|
|
cell.innerHTML = totals[col_num].group_total;
|
|
cell.style.borderTop = "1px solid #CCCCCC";
|
|
} else {
|
|
cell.innerHTML = totals[col_num].total;
|
|
}
|
|
} else {
|
|
cell.innerHTML = ' ';
|
|
}
|
|
}
|
|
totals_row.appendChild(cell);
|
|
}
|
|
if(before_row != null){
|
|
table_body.insertBefore(totals_row, before_row);
|
|
} else {
|
|
table_body.appendChild(totals_row);
|
|
}
|
|
}
|
|
|
|
function main() {
|
|
var issues_table = document.getElementsByClassName('list issues')[0];
|
|
if(!issues_table) return;
|
|
|
|
var head_cells = issues_table.getElementsByTagName('th');
|
|
var table_body = issues_table.getElementsByTagName('tbody')[0];
|
|
var table_rows = table_body.getElementsByTagName('tr');
|
|
|
|
var totals = new Array();
|
|
var has_groups = false;
|
|
var has_multiple_groups = false;
|
|
|
|
for (var col_num=0; col_num<head_cells.length; col_num++) {
|
|
totals[col_num] = new Object();
|
|
}
|
|
|
|
for (var row_num=0; row_num<table_rows.length; row_num++) {
|
|
if(table_rows[row_num].className.indexOf('group') >= 0){
|
|
if (!has_groups) {
|
|
has_groups = true;
|
|
} else {
|
|
has_multiple_groups = true;
|
|
insertTotalsRow(table_body, totals, true, table_rows[row_num]);
|
|
for (var col_num=0; col_num<totals.length; col_num++){
|
|
if (totals[col_num].total){
|
|
totals[col_num].group_total = 0;
|
|
}
|
|
}
|
|
row_num++;
|
|
}
|
|
} else {
|
|
totals = addRowValuesToTotals(table_rows[row_num], totals);
|
|
}
|
|
}
|
|
|
|
if (has_multiple_groups) {
|
|
insertTotalsRow(table_body, totals, true);
|
|
}
|
|
insertTotalsRow(table_body, totals);
|
|
}
|
|
|
|
main();
|