Created
August 18, 2011 09:00
-
-
Save mpj/1153689 to your computer and use it in GitHub Desktop.
Converting timespan to string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name BetterTATMIN | |
// @namespace http://tat.se | |
// @version 0.0.1 | |
// @description Fixes some annoyances of the time reporting tool to make it nicer. | |
// @include http://www01mmxlabvm/* | |
// @copyright 2011, RIM TAT AB | |
// ==/UserScript== | |
var ENABLE_FREE_FRIDAYS = true; | |
function isDayEmpty(dayInt) { | |
var onClickStr = 'document.form.weekday['+dayInt.toString()+'].selected=true;'; | |
var $weekDayHeader = $('td[onclick="'+onClickStr+'"]'); | |
var $weekDay = $weekDayHeader.parents("table:first"); | |
return $weekDay.find('.report_item').length == 0; | |
} | |
function firstEmptyWeekDay() { | |
var i = 0; | |
while (i<6) { | |
if(isDayEmpty(i)) { | |
return i; | |
} | |
i++; | |
} | |
return null; | |
} | |
function isWeekLocked() { | |
return ($('*:contains("Week has not been approved")').length); | |
} | |
function toggleWeekLock() { | |
var saturday = 5; | |
if (isWeekLocked() || firstEmptyWeekDay() >= saturday || confirm("Monday through friday NOT reported. Lock week?")) { | |
$('input[name="cbLock"]').attr('checked', !isWeekLocked()); | |
$('input[name="submit-lock"]').click(); | |
} | |
} | |
function preSelectFirstEmptyWeekDay() { | |
// Guess the weekday depending on reported items on screen, | |
// and preselect it. | |
var weekDayToSelect = firstEmptyWeekDay(); | |
$('select[name="weekday"]').val(weekDayToSelect.toString()); | |
if (ENABLE_FREE_FRIDAYS) { | |
// Special for Mattias Petter Johansson, who is free on | |
// fridays. | |
var isFriday = weekDayToSelect == 4; | |
if (isFriday) { | |
// Preselect Leave of Absence | |
$('#projid').val(6).change(); | |
} | |
} | |
} | |
function createHelpBox(hours) { | |
hours = hours-32; | |
var boxhtml = '<div id="btmHelp" style="position:absolute;right:25px;top:0;' + | |
'background-color:#33ccff;font-family:Arial,Sans-serif;padding: 15px">' + | |
'</div>'; | |
$("body").append(boxhtml); | |
$("#btmHelp").html('<strong style="display:block">BetterTATMIN enabled</strong>'). | |
append('<strong style="display:block">Result this week: ' + hours.toString().replace(".",",") + '</strong>'). | |
append('<p><b>CTRL+R</b> Report button</p>'). | |
append('<p><b>CTRL+L</b> Toggle week lock</p>'). | |
append('<p><b>CTRL+E</b> Calculate time</p>'); | |
} | |
function setCookie(c_name,value,exdays) | |
{ | |
var exdate=new Date(); | |
exdate.setDate(exdate.getDate() + exdays); | |
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); | |
document.cookie=c_name + "=" + c_value; | |
} | |
function getCookie(c_name) | |
{ | |
var i,x,y,ARRcookies=document.cookie.split(";"); | |
for (i=0;i<ARRcookies.length;i++) | |
{ | |
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); | |
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); | |
x=x.replace(/^\s+|\s+$/g,""); | |
if (x==c_name) { | |
return unescape(y); | |
} | |
} | |
} | |
// Convert the minutes given to a string | |
// that can be used for the funky select box values. | |
function convertToFuckedValue(minutes) { | |
if (minutes >= 50) | |
return ".83"; | |
if (minutes >= 40) | |
return ".67"; | |
if (minutes >= 30) | |
return ".5"; | |
if (minutes >= 20) | |
return ".33"; | |
if (minutes >= 10) | |
return ".17"; | |
if (minutes == 0) | |
return "n"; | |
} | |
// Accepts string like "9:15" and converts it | |
// to an int of minutes. | |
function timeToMinutes(str) { | |
if (str.length <= 2) | |
str = str + ":00"; | |
var hours = parseInt(str.split(':')[0]); | |
var minutes = parseInt(str.split(':')[1]); | |
return minutes + (hours*60); | |
} | |
var $ = unsafeWindow.jQuery; | |
$(document).ready(function() { | |
// Set focus to weekday field on load. | |
$('#weekday').focus(); | |
// Preselect last selected project | |
if (getCookie('btm_projid') && !(ENABLE_FREE_FRIDAYS && getCookie('btm_projid').toString() == '6')) { | |
// Set values of the project boxes to their last values, and then trigger their change events. | |
$('#projid').val(getCookie('btm_projid')).change(); | |
$('select[name="subproject_id"]').val(getCookie('btm_subprojid')).change(); | |
} else { | |
$('#projid').val(44).change(); | |
} | |
// Preselect 8 hours | |
$('select[name="hours"]').val("8"); | |
// Preselect 0 minutes | |
$('select[name="minutes"]').val("n"); | |
$('select[name="weekday"]').val($('.report_item').length.toString()); | |
preSelectFirstEmptyWeekDay(); | |
$(document).bind('keydown', function(event) { | |
if (!event.ctrlKey) { | |
return true; | |
} | |
var rKey = 82; | |
var lKey = 76; | |
var eKey = 69; | |
if (event.keyCode == rKey) { | |
$('input[value="Report"]').click(); | |
return false; | |
} | |
if (event.keyCode == lKey) { | |
toggleWeekLock(); | |
return false; | |
} | |
if (event.keyCode == eKey) { | |
var now = new Date(); | |
var hour = now.getHours().toString(); | |
var minute = now.getMinutes().toString(); | |
var hourIn = prompt("In?", "9:00"); | |
var hourOut = prompt("Out?", hour+":"+minute); | |
var lunchMinutes = parseInt(prompt("Lunch minutes? Example: 60", "60")); | |
var minutesWorked = timeToMinutes(hourOut) - timeToMinutes(hourIn) - lunchMinutes; | |
var hoursWorked = Math.floor(minutesWorked/60.0); | |
minutesWorked = minutesWorked - (hoursWorked*60.0); | |
$('select[name="hours"]').val(hoursWorked.toString()); | |
$('select[name="minutes"]').val(convertToFuckedValue(minutesWorked)); | |
$("input.comment").val(hourIn + " to " + hourOut + " (" + lunchMinutes + " minute lunch)"); | |
return false; | |
} | |
}); | |
// Hijack checkreporttime to inject a setcookie | |
// that stores the selected project. | |
var oldFunction = checkreporttime; | |
checkreporttime = function() { | |
setCookie('btm_projid', $('#projid').val(), 5); | |
setCookie('btm_subprojid', $('select[name="subproject_id"]').val(), 5); | |
return oldFunction(); | |
} | |
// Calculate time total for the week | |
var totalMinutes = 0; | |
// class of td might be 'weekday' or 'weekday_today' | |
$("table.week td[class^='weekday']").each(function() { | |
// Don't count free days | |
if ($(this).text().indexOf("Leave of Absence") > 0) | |
return; | |
var strLoggedTime = $(this).find("table tbody tr:eq(5)").text().replace("Day total:", "").trim(); | |
// String is "8h 40min" | |
var indexOfH = strLoggedTime.indexOf("h"); | |
var indexOfMin = strLoggedTime.indexOf("min"); | |
var strHours= strLoggedTime.substring(0,indexOfH); | |
var strMinutes=strLoggedTime.substring(indexOfH+1,indexOfMin); | |
var hours = parseInt(strHours.trim()); | |
if (hours && hours != NaN) { | |
var minutes = 0; | |
if (indexOfMin > 0) { | |
minutes = parseInt(strMinutes.trim()); | |
} | |
totalMinutes += (hours * 60) + minutes; | |
console.log(strLoggedTime); | |
console.log(totalMinutes); | |
} | |
}); | |
var totalHours = Math.round((totalMinutes/60)*100)/100; | |
createHelpBox(totalHours); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment