Commits

Stefan Scherfke committed add131e

Modified timer behaviour when inputs are edited during a countdown. Editing the timer target during a countdown no longer resets the countdown.

Comments (0)

Files changed (1)

Tea Timer.wdgt/Scripts/Front.js

     for (i in buttons) {
         // Remember to add "i = parseInt(i);" if you’re ever going to add some
         // calculations with "i"!! :-X
-        
+
         button = buttons[i];
         $(button[0]).click(button[1]);
         $(button[0]).bind(mouse_events, {button: button[0]}, this.toggleButton);
     }
 
 	fields = Array(
-		'#timerTarget', 
-		'#readyIn', 
-		'#hours', 
-		'#minutes', 
+		'#timerTarget',
+		'#readyIn',
+		'#hours',
+		'#minutes',
 		'#seconds'
 	);
 	len = fields.length;
 	for (var i in fields) {
-        // If I don’t do parseInt(i), the expressions "(i + len - 1) % len" 
+        // If I don’t do parseInt(i), the expressions "(i + len - 1) % len"
         // and "(i + 1) % len" won’t be evaluated each iteration, but be
         // cached instead. :-X
         i = parseInt(i);
-        
+
 	    field = fields[i];
 	    input = field + 'Input';
-        
+
 	    $(field).bind('click', {editor: input}, this.showEdit);
 		$(input).bind('blur keypress', {display: field}, this.setText);
 		$(input).bind('keypress', {
 		    pre: fields[(i + len - 1) % len], // i - 1 would not always be > 0!
 		    next: fields[(i + 1) % len]}, this.cycleInputs);
-		    
+
 		if (i > 1)
 			$(input).keypress(this.raiseOrLowerValue);
 	}
     $(hide).css('visibility', 'hidden');
     $(show).css('visibility', 'visible');
 };
-	
+
 /**
  * Handles the onMouseDown and onMouseUp events for buttons.
  *
  * @param event: The js event.
  */
 Front.prototype.showEdit = function(event) {
-    // Stop the timer if it is running
-	if (timer.timerInterval)
-		$('#pause').click();
-		
 	var display = $(event.target);
 	var editor = $(event.data.editor);
-	
+
 	editor.attr('value', display.text());
 	editor.css('visibility', 'visible');
 	display.css('visiblity', 'hidden');
 	editor.get(0).focus();
-	
+
 	if (display.attr('id') != 'readyIn')
 		editor.get(0).select();
 };
 Front.prototype.setText = function(event) {
 	if (event.type == 'keypress' && event.keyCode != 13)
 		return;
-		
+
+    var timeFields = ['hours', 'minutes', 'seconds'];
 	var editor = $(event.target);
 	var display = $(event.data.display);
-	
+
 	if (editor.css('visibility') == 'visible') {
-		if (timer.timerInterval)
-			clearInterval(timerInterval);
-			
+        // Stop timer if the time fields are modified
+		if ($.inArray(display.attr('id'), timeFields) > -1 &&
+                timer.timerInterval)
+			clearInterval(timer.timerInterval);
+
+        // Set default values
 		if (editor.val().length < 1) {
 			if (editor.attr('id') == 'timerTargetInput')
 				editor.val(getLocalizedString('Something'));
 			else
 				editor.val('00');
 		}
-		
-		if (display.attr('id') == 'timerTarget' || 
+
+        // Check inputs
+		if (display.attr('id') == 'timerTarget' ||
 		        display.attr('id') == 'readyIn')
 			display.text(editor.val());
 		else {
 		    var val = Math.min(Math.max(editor.val(), 0), 59);
 		    display.text(front.formatTwoDigits(val));
 		}
-			
-			
+
+
+        // Update preferences
 		if (display.attr('id') == 'timerTarget' && window.widget)
 			setPrefForKey(editor.val(), widget.identifier, 'timerTarget');
 		if (display.attr('id') == 'readyIn' && window.widget)
 			setPrefForKey(editor.val(), widget.identifier, 'minutes');
 		if (display.attr('id') == 'seconds' && window.widget)
 			setPrefForKey(editor.val(), widget.identifier, 'seconds');
-			
-		timer.isPaused = false; // Timer will read the inputs again.
-			
+
+        if ($.inArray(display.attr('id'), timeFields) > -1)
+		    timer.isPaused = false; // Timer will read the inputs again.
+
 		display.css('visibility', 'visible');
 		editor.css('visibility', 'hidden');
 	}
-	// Start the timer if the user pressed enter.
-	if (event.keyCode == 13)
+
+	// Start the timer if the user pressed enter and if the timer was not
+    // running or the time was changed.
+	if (event.keyCode == 13 && (!timer.timerInterval ||
+                $.inArray(display.attr('id'), timeFields) > -1))
 		$('#start').click();
 };