Created
March 4, 2018 10:24
-
-
Save obecker/de34b04a18d4f438df144058cb348edf to your computer and use it in GitHub Desktop.
Input helper for Computer Futures worksheets - to be used with a browser extension like cjs (Custom JavaScript for websites)
This file contains 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
/****************************************************************************************** | |
* Input helper for Computer Futures worksheets | |
* - adds automatically missing zeros (9 -> 09:00) | |
* - computes total day working time and displays it also in human readable form (7.42 -> 7:25) | |
* - enables cursor navigation between input fields | |
* - removes all pre-filled entries from an empty worksheet | |
* (c) Oliver Becker, 2016-2018 | |
******************************************************************************************/ | |
(function() { | |
if (typeof(jQuery) === 'undefined') { | |
return; | |
} | |
/* ************************************ jQuery Caret ************************************ | |
https://github.com/accursoft/caret | |
Original code by Gideon Sireling | |
Minor refactorings by Oliver Becker | |
Copyright (c) 2009, Gideon Sireling | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above | |
copyright notice, this list of conditions and the following | |
disclaimer in the documentation and/or other materials provided | |
with the distribution. | |
* Neither the name of Gideon Sireling nor the names of other | |
contributors may be used to endorse or promote products derived | |
from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
(function($) { | |
$.fn.caret = function(pos) { | |
var target = this[0]; | |
var range1, range2, bookmark; | |
var isContentEditable = target && target.contentEditable === 'true'; | |
//get | |
if (arguments.length === 0) { | |
if (!target) { | |
return this; | |
} | |
//HTML5 | |
if (window.getSelection) { | |
//contenteditable | |
if (isContentEditable) { | |
target.focus(); | |
range1 = window.getSelection().getRangeAt(0), | |
range2 = range1.cloneRange(); | |
range2.selectNodeContents(target); | |
range2.setEnd(range1.endContainer, range1.endOffset); | |
return range2.toString().length; | |
} | |
//textarea | |
return target.selectionStart; | |
} | |
//IE<9 | |
if (document.selection) { | |
target.focus(); | |
//contenteditable | |
if (isContentEditable) { | |
range1 = document.selection.createRange(); | |
range2 = document.body.createTextRange(); | |
range2.moveToElementText(target); | |
range2.setEndPoint('EndToEnd', range1); | |
return range2.text.length; | |
} | |
//textarea | |
pos = 0; | |
range = target.createTextRange(); | |
range2 = document.selection.createRange().duplicate(); | |
bookmark = range2.getBookmark(); | |
range.moveToBookmark(bookmark); | |
while (range.moveStart('character', -1) !== 0) pos++; | |
return pos; | |
} | |
// Addition for jsdom support | |
if (target.selectionStart) | |
return target.selectionStart; | |
//not supported | |
return 0; | |
} else if (!target) return; | |
//set | |
if (pos == -1) | |
pos = this[isContentEditable? 'text' : 'val']().length; | |
//HTML5 | |
if (window.getSelection) { | |
//contenteditable | |
if (isContentEditable) { | |
target.focus(); | |
window.getSelection().collapse(target.firstChild, pos); | |
} | |
//textarea | |
else | |
target.setSelectionRange(pos, pos); | |
} | |
//IE<9 | |
else if (document.body.createTextRange) { | |
if (isContentEditable) { | |
var range = document.body.createTextRange(); | |
range.moveToElementText(target); | |
range.moveStart('character', pos); | |
range.collapse(true); | |
range.select(); | |
} else { | |
range = target.createTextRange(); | |
range.move('character', pos); | |
range.select(); | |
} | |
} | |
if (!isContentEditable) | |
target.focus(); | |
return this; | |
} | |
})(jQuery); | |
// ******************************** End of jQuery Caret ******************************** | |
var timeFormat = /^([01][0-9]|2[0-3]):([0-5][0-9])$/; | |
var hoursOnly = /^[0-9]{1,2}$/; | |
var missingZero = /^[0-9]:.*$/; | |
function diffMinutes(from, to) { | |
var diff = 0; | |
var fromMatch = from && from.match(timeFormat); | |
var toMatch = to && to.match(timeFormat); | |
if (fromMatch && toMatch) { | |
diff = parseInt(toMatch[1], 10) * 60 + | |
parseInt(toMatch[2], 10) - | |
parseInt(fromMatch[1], 10) * 60 - | |
parseInt(fromMatch[2], 10) ; | |
} | |
return diff; | |
} | |
function compute($row, changed) { | |
var $inputs = $row.find('td input') | |
var from0 = $inputs.eq(0).val(); | |
var to0 = $inputs.eq(1).val(); | |
var from1 = $inputs.eq(2).val(); | |
var to1 = $inputs.eq(3).val(); | |
var sum = diffMinutes(from0, to0) + diffMinutes(from1, to1); | |
var $field = $row.find('td span').add($row.find('td').last()).last(); | |
$field.text(sum | |
? (changed ? (sum / 60).toFixed(2) : $field.text()) + | |
'\u00a0\u00a0\u00a0(' + Math.floor(sum / 60) + ':' + ('0' + sum % 60).slice(-2) + ')' | |
: '') | |
.css('color', changed ? '#888' : '#000') | |
} | |
function navigate(input, rowDiff, colDiff) { | |
var $tr = $(input).parents('tr'); | |
var $trpar = $tr.parent(); | |
var colIndex = $tr.find('td input').index(input) + colDiff; | |
var rowIndex = $trpar.find('tr').index($tr) + rowDiff; | |
if (colIndex >= 0 && rowIndex >= 0) { | |
var pos = $(input).caret(); | |
$trpar.find('tr').eq(rowIndex).find('td input').eq(colIndex) | |
.focus().caret(colDiff < 0 ? -1 : colDiff > 0 ? 0 : pos); | |
} | |
return false; | |
} | |
$('.sheet td input:text').change(function() { | |
var $input = $(this); | |
var v = $input.val(); | |
if (hoursOnly.test(v)) { | |
v += ':00'; | |
} | |
if (missingZero.test(v)) { | |
v = '0' + v; | |
} | |
$input.toggleClass('red', v && !timeFormat.test(v)).val(v); | |
compute($input.parents('tr'), true); | |
}).keydown(function(event) { | |
var code = event.which; | |
if (code === 38) { // arrow up | |
return navigate(this, -1, 0); | |
} else if (code === 40) { // arrow down | |
return navigate(this, 1, 0); | |
} else if (code === 37 && $(this).caret() === 0) { // arrow left, cursor on first position | |
return navigate(this, 0, -1); | |
} else if (code === 39 && $(this).caret() == $(this).val().length) { // arrow right, cursor last pos | |
return navigate(this, 0, 1); | |
} | |
}); | |
var emptySheet = $('#cwsRight').length === 0; | |
$('.sheet tr').each(function() { | |
var $inputs = $('td input:text', this); | |
if (emptySheet) { | |
$inputs.val(''); | |
} else if ($inputs.length > 0) { | |
compute($(this), false); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment