Commits

Stefan Scherfke committed 7855a3b

Rewind works.

  • Participants
  • Parent commits cdbf79c

Comments (0)

Files changed (12)

Tea Timer.wdgt/Images/alarmclock_stop.png

Removed
Old image

Tea Timer.wdgt/Images/alarmclock_stop_clicked.png

Removed
Old image

Tea Timer.wdgt/Images/rew.png

Removed
Old image

Tea Timer.wdgt/Images/rew_clicked.png

Removed
Old image

Tea Timer.wdgt/Images/rewind.png

Added
New image

Tea Timer.wdgt/Images/rewind_clicked.png

Added
New image

Tea Timer.wdgt/Images/stop.png

Added
New image

Tea Timer.wdgt/Images/stop_clicked.png

Added
New image

Tea Timer.wdgt/Scripts/Front.js

 }
 
 Front.prototype._init = function() {
-    mouse_events = 'mouseover mouseout mousedown mouseup'
+    mouse_events = 'mouseover mouseout mousedown mouseup';
     buttons = Array(
-        Array('#start', timer.run),
-        Array('#pause', timer.pause),
-        Array('#rew', timer.rewind),
-        Array('#stop', timer.stopAlarm)
+        Array('#start', function() {
+            front.stopAlarm();
+            timer.run();
+            front.toggleVisibility('#start', '#pause');
+            front.toggleVisibility('#icon', '#rewind');
+        }),
+        Array('#pause', function() {
+            timer.pause();
+            front.toggleVisibility('#pause', '#start')
+        }),
+        Array('#rewind', function() {
+            timer.rewind();
+        }),
+        Array('#stop', function() {
+            front.stopAlarm();
+        })
     );
-    for (button in buttons) {
+    for (i in buttons) {
+        button = buttons[i];
         $(button[0]).click(button[1]);
         $(button[0]).bind(mouse_events, {button: button[0]}, this.toggleButton);
     }
 			$('#' + val + 'Input').keypress(this.raiseOrLowerValue);
 	}
 };
+
+/**
+ * Stop the alarm.
+ */
+Front.prototype.stopAlarm = function() {
+    timer.stopAlarm();
+    front.toggleVisibility('#stop', '#icon');
+};
+
+
+/**
+ * Hide one element and show the other.
+ *
+ * @param hide: Element to hide
+ * @param show: Element to show
+ */
+Front.prototype.toggleVisibility = function(hide, show) {
+    $(hide).css('visibility', 'hidden');
+    $(show).css('visibility', 'visible');
+};
+
 	
 /**
  * Handles the onMouseDown and onMouseUp events for buttons.
  */
 Front.prototype.toggleButton = function(event) {
     pressed = (event.type == 'mouseover' || event.type == 'mousedown');
-    # button[1:].upper() + pressed?
-};
-
-Front.prototype.setStartButtonPressed = function(event) {
-	pressed = (event.type == 'mouseover' || event.type == 'mousedown');
-	play = timer.timerInterval ? false : true	;
-	front.setStartButton(play, pressed);
-};
-
-/**
- * Sets either the play or the pause button to the widget.
- *
- * @param play: Set the play button if true, else the pause button.
- */
-Front.prototype.setStartButton = function(play, pressed) {
-	img = 'Images/'
-	img += play ? 'start' : 'pause';
-	img += pressed ? '_clicked' : '';
-	img += '.png';
-	$('#start').attr('src', img);
+    filename = 'Images/' + event.data.button.slice(1);
+    if (pressed)
+        filename += '_clicked';
+    filename += '.png';
+    $(event.data.button).attr('src', filename);
 };
 
 /**
  * Show the stop alarm button.
  */
-Front.prototype.setStopAlarmButton = function() {
-	$('#icon').attr('src', 'Images/alarmclock_stop.png');
-	$('#icon').click(function(event){
-		timer.stopClicked = true;
-		$('#icon').attr('src', 'Images/alarmclock.png');
-		$('#icon').unbind('mouseover mouseout mousedown mouseup');
-	});
-	$('#icon').bind('mouseover mousedown', function(event){
-		$('#icon').attr('src', 'Images/alarmclock_stop_clicked.png');
-	});
-	$('#icon').bind('mouseout mouseup', function(event){
-		$('#icon').attr('src', 'Images/alarmclock_stop.png');
-	});
+Front.prototype.showStopAlarmButton = function() {
+    front.toggleVisibility('#icon', '#stop');
 };
 
-
 /**
  * Show the text input field to set an event or the time.
  *
  * Reset the display to the given time.
  */
 Front.prototype.resetDisplayTo = function(hours, minutes, seconds) {
+    front.toggleVisibility('#pause', '#start');
+    front.toggleVisibility('#rewind', '#icon');
 	$('#hours').text(hours);
 	$('#minutes').text(minutes);
 	$('#seconds').text(seconds);

Tea Timer.wdgt/Scripts/Timer.js

 }
 
 /**
- * This function is beeing executed if the start/pause button is pressed.
- * It either pauses the current countdown or starts a new one with the
- * currently displayed target time.
+ * Run the countdown.
  */
-Timer.prototype.startOrPauseTimer = function(event) {
-	if (timer.timerInterval) {
-		clearInterval(timer.timerInterval);
-		timer.timerInterval = null;
-		timer._isPaused = true
-		front.setStartButton(true, false);
+Timer.prototype.run = function() {
+    var h, m, s;
+    
+    if (!timer._isPaused) {
+		timer.hours = $('#hours').text()
+		timer.minutes = $('#minutes').text()
+		timer.seconds = $('#seconds').text()
+		h = parseInt(timer.hours, 10);
+		m = parseInt(timer.minutes, 10);
+		s = parseInt(timer.seconds, 10);  
+    }
+    else {
+        timer._isPaused = false;
+        h = timer.remainingHours;
+        m = timer.remainingMinutes;
+        s = timer.remainingSeconds;
+    }
+	m += h * 60;
+	s += m * 60;
+	timer._targetTime = Date.parse(new Date()) + s * 1000;
+	timer._isTargetReached = false; 
+
+	// Check if target time has already been passed.
+	if (timer._targetTime > Date.parse(new Date())) {
+		timer.timerInterval = setInterval('timer.runTimer()', 1000);
 	}
 	else {
-	    var h, m, s;
-	    
-	    if (!timer._isPaused) {
-    		timer.hours = $('#hours').text()
-    		timer.minutes = $('#minutes').text()
-    		timer.seconds = $('#seconds').text()
-    		h = parseInt(timer.hours, 10);
-    		m = parseInt(timer.minutes, 10);
-    		s = parseInt(timer.seconds, 10);  
-	    }
-	    else {
-	        timer._isPaused = false;
-	        h = timer.remainingHours;
-	        m = timer.remainingMinutes;
-	        s = timer.remainingSeconds;
-	    }
-		m += h * 60;
-		s += m * 60;
-		timer._targetTime = Date.parse(new Date()) + s * 1000;
-		timer._isTargetReached = false; 
-	
-		// Check if target time has already been passed.
-		if (timer._targetTime > Date.parse(new Date())) {
-			front.setStartButton(false, false);
-			timer.timerInterval = setInterval('timer.runTimer()', 1000);
-		}
-		else {
-			front.resetDisplay();
-			front.setStartButton(true, false);
-		}
+		front.resetDisplay();
 	}
 };
 
 /**
+ * Pause the countdown.
+ */
+Timer.prototype.pause = function() {
+	clearInterval(timer.timerInterval);
+	timer.timerInterval = null;
+	timer._isPaused = true
+};
+
+/**
+ * Rewind the countdown – set it back to it initial value.
+ */
+Timer.prototype.rewind = function() {
+	clearInterval(timer.timerInterval);
+	timer.timerInterval = null;
+	timer._isTargetReached = false;
+	timer._isPaused = false;
+	front.resetDisplayTo(timer.hours, timer.minutes, timer.seconds);
+};
+
+/**
+ * Stop the alarm.
+ */
+Timer.prototype.stopAlarm = function() {
+    timer.stopClicked = true;
+};
+
+
+/**
  * The run timer action first calculates the remaining time, then updates
  * the displayed time and finally checks wheter to alarm the user or not.
  */
 		timer._isTargetReached = true;
 		timer._isPaused = false;
 		front.resetDisplayTo(timer.hours, timer.minutes, timer.seconds);
-		front.setStartButton(true, false);
 		if (alarm.keepAlarming)
-			front.setStopAlarmButton();
+			front.showStopAlarmButton();
 			timer.stopClicked = false;
         timer.alarm();
 	}
 		$('#alarm').append('<embed name="soundPlayer" id="soundPlayer" '
 				+ 'src="/System/Library/Sounds/' + alarm.sound + '.aiff" '
 				+ 'autostart="false" hidden="true" enablejavascript="true" />');
-		$('#soundPlayer').get(0).Play();
+        $('#soundPlayer').get(0).Play();
 	}
 	if (window.widget && (!type || type == 'growl') && alarm.growl) {
 		widget.system('/usr/bin/env python2.5 growl.py "' + title + '" "' 

Tea Timer.wdgt/TeaTimer.css

 .editable { cursor: text; }
 
 /* Icon positions */
-#icon, #start, #pause, #rew, #stop, #update #yes, #update #no {
+#icon, #start, #pause, #rewind, #stop, #update #yes, #update #no {
 	margin: 0px;
 	position: absolute;
 	top: 38px;
 	width: 44px;
 	height: 44px;
 }
-#icon, #rew, #stop, #update #no { left: 28px; }
-#pause, #rew, #stop { visibility: hidden;}
+#icon, #rewind, #stop, #update #no { left: 28px; }
+#pause, #rewind, #stop { visibility: hidden;}
 
 /*******************************************************************************
  * Front side

Tea Timer.wdgt/TeaTimer.html

 	<img id="icon" src="Images/alarmclock.png" />
 	<img id="start" src="Images/start.png" />
 	<img id="pause" src="Images/pause.png" />
-	<img id="rewind" src="Images/rew.png" />
+	<img id="rewind" src="Images/rewind.png" />
 	<img id="stop" src="Images/stop.png" />
 	<div id="timerTarget" class="editable normal">Tea</div>
 	<input type="text" id="timerTargetInput" class="input" maxlength="20" />