Created
January 13, 2014 15:45
-
-
Save joshdoe/8402515 to your computer and use it in GitHub Desktop.
Chrome and Firefox/Greasemonkey user script that allows arrow keys to move between cells on the ATAAPS labor screen, shows leave information when hovering over link, and other enhancements.
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== | |
// @match https://ataaps.csd.disa.mil/* | |
// @description Allows arrow keys to move between cells on the ATAAPS labor screen, shows leave information when hovering over link, and other enhancements. | |
// @name ATAAPS Interface Enhancements | |
// ==/UserScript== | |
function addJQuery(callback) { | |
var script = document.createElement("script"); | |
script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"); | |
script.addEventListener('load', function() { | |
var script = document.createElement("script"); | |
script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();"; | |
document.body.appendChild(script); | |
}, false); | |
document.body.appendChild(script); | |
} | |
// the guts of this userscript | |
function main() { | |
// Note, jQ replaces $ to avoid conflicts. | |
// only modify the labor screen | |
if (jQ("#laborForm").length == 0) | |
return; | |
function almostEq(a, b) { | |
if (a == b || Math.abs(a - b) < 0.000001) return true; | |
else return false; | |
} | |
// disable autocomplete, as it gets in the way of moving to the next row | |
jQ("input[type='text']").attr('autocomplete', 'off') | |
jQ("input[type='text']").blur(function (e) { | |
var input = jQ(this); | |
// parse string to number | |
var v = parseFloat(input.val(), 10); | |
if (!v) return; | |
// split number into whole and fractional parts | |
var frac = v - Math.floor(v); | |
var whole = v - frac; | |
// convert from normal hour fractions to ATAAPS hour representation | |
if (almostEq(frac, 0.25)) frac = 0.15; | |
else if (almostEq(frac, 0.5)) frac = 0.30; | |
else if (almostEq(frac, 0.75)) frac = 0.45; | |
// ensure fractional part is in 15 minute increments, change border if not | |
if (almostEq(frac, 0) || almostEq(frac, 0.15) || almostEq(frac, 0.30) || almostEq(frac, 0.45)) { | |
input.val((whole + frac).toFixed(2)); | |
// restore border to normal (TODO: better way?) | |
input.css("border", "initial"); | |
input.css("border", "2px inset"); | |
} else { | |
input.css("border", "2px solid red"); | |
} | |
}); | |
jQ("input[type='text']").keyup(function (e) { | |
var input = jQ(this), | |
td = input.closest("td"), | |
next; | |
switch (e.which) { | |
case 37: | |
next = td.prev().find("input"); | |
break; | |
case 39: | |
next = td.next().find("input"); | |
break; | |
case 38: | |
// make robust to other elements besides td in each tr | |
tdIndex = input.closest("tr").find("td").index(td); | |
next = input.closest("tr").prev().find("td:eq(" + tdIndex + ")").find("input"); | |
break; | |
case 40: | |
// make robust to other elements besides td in each tr | |
tdIndex = input.closest("tr").find("td").index(td); | |
next = input.closest("tr").next().find("td:eq(" + tdIndex + ")").find("input"); | |
break; | |
} | |
if (next) { | |
next.focus().select(); | |
} else { | |
// only allow numbers and period | |
var newval = this.value.replace(/[^0-9\.]/g, ''); | |
if (this.value != newval) this.value = newval; | |
} | |
}); | |
// show leave info when hovering over "View Leave" link | |
$("a:contains('View')").hover(function () { | |
// Hover over code | |
var title = "Please wait while leave information loads"; | |
$(this).data('tipText', title).removeAttr('title'); | |
$('<p class="tooltip"></p>') | |
.text(title) | |
.css({'display': 'none', 'position': 'absolute'}) | |
.appendTo('body') | |
.fadeIn('slow') | |
.load('ControllerServlet?operation=LeaveView table table table:eq(2)'); | |
}, function () { | |
// Hover out code | |
$(this).attr('title', $(this).data('tipText')); | |
$('.tooltip').remove(); | |
}).mousemove(function (e) { | |
var mousex = e.pageX + 20; //Get X coordinates | |
var mousey = e.pageY + 10; //Get Y coordinates | |
$('.tooltip') | |
.css({ | |
top: mousey, | |
left: mousex | |
}) | |
}); | |
} | |
// load jQuery and execute the main function | |
addJQuery(main); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment