Skip to content

Instantly share code, notes, and snippets.

@rvazquezglez
Last active August 29, 2015 14:26
Show Gist options
  • Save rvazquezglez/e1d30f35abc4cc8cdb69 to your computer and use it in GitHub Desktop.
Save rvazquezglez/e1d30f35abc4cc8cdb69 to your computer and use it in GitHub Desktop.
/// Credits: FRU
(function () {
"use strict";
function makeRowSelector(rowIndex) {
return 'tr:nth-child(' + (2 + rowIndex) + ')';
}
function makeHoursColumnSelector(rowIndex) {
return 'td:nth-child(' + (6 + rowIndex) + ')';
}
function getTimeSheetTable() {
return $('div.ui-popup.ui-popup_active_true iframe').contents().find('#ctl00_mainArea_pnlUpd');
}
function getTotalRows(timeSheetTable) {
return timeSheetTable.find('tr').length - 2;
}
function getTpNumberFromLink(link) {
return link ? link.replace(/^.*\/([^/]+)$/, '$1') : '';
}
function getTpNumber(timeSheetTable, rowIndex) {
var linkLocationInRow = 'td:nth-child(3) a:nth-child(1)',
link = timeSheetTable.find(makeRowSelector(rowIndex) + ' ' + linkLocationInRow).attr('href');
return getTpNumberFromLink(link);
}
function getTpHours(timeSheetTable, rowIndex) {
var inputElement,
columnIndex,
totalHours = 0,
inputSelector;
for (columnIndex = 0; columnIndex < 7; columnIndex++) {
inputSelector = makeRowSelector(rowIndex) + ' ' + makeHoursColumnSelector(columnIndex) + ' input';
inputElement = timeSheetTable.find(inputSelector);
totalHours += inputElement ? parseFloat(inputElement.val()) : 0;
}
return totalHours;
}
function fillTpMap(timeSheetTable) {
var tpMap = {},
totalRows = getTotalRows(timeSheetTable),
rowIndex;
for (var rowIndex = 0; rowIndex < totalRows; rowIndex++) {
var tp = getTpNumber(timeSheetTable, rowIndex);
tpMap[tp] = getTpHours(timeSheetTable, rowIndex);
}
return tpMap;
}
function getEntries(tpMap) {
return $.map(tpMap, function (v, k) { return {tp: k, hours: v} });
}
function getWorkedTpEntries(tpMap) {
return getEntries(tpMap).filter(function (e) { return e.hours > 0});
}
function getWorkedTpNumbers(workedTpEntries) {
return workedTpEntries.map(function (x) { return x.tp; }).reduce(function (a, v) { return a + ", " + v; });
}
function getTotalHours(workedTpEntries) {
return workedTpEntries.map(function (x) { return x.hours; }).reduce( function (a, v) { return a + v; });
}
function getDetailedHours(workedTpEntries) {
return workedTpEntries.reduce(function (a, v) { a[v.tp] = v.hours; return a; }, {});
}
function makeResultsMap(workedTpEntries) {
return {
"tp numbers": getWorkedTpNumbers(workedTpEntries),
"total hours": getTotalHours(workedTpEntries),
"detailed hours": getDetailedHours(workedTpEntries)
};
}
var results = makeResultsMap(getWorkedTpEntries(fillTpMap(getTimeSheetTable())));
console.log(JSON.stringify(results, undefined, 2));
alert(JSON.stringify(results, undefined, 2));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment