From fffa9ee5d2eba631b4b9ce3ea08971dc8f429f47 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 12 Jun 2010 12:08:50 -0700 Subject: [PATCH] massive refactoring. namespaced all functions and vars. tweaked behavior of control buttons. fixed various settings bugs. release version... v1.1 --- CountdownTimer.gadget/countdown_timer.css | 9 +- CountdownTimer.gadget/countdown_timer.js | 174 ++++++++++------------ CountdownTimer.gadget/gadget.xml | 6 +- CountdownTimer.gadget/global.js | 34 +++++ CountdownTimer.gadget/main.html | 12 +- CountdownTimer.gadget/settings.html | 6 +- CountdownTimer.gadget/settings.js | 26 +--- 7 files changed, 138 insertions(+), 129 deletions(-) diff --git a/CountdownTimer.gadget/countdown_timer.css b/CountdownTimer.gadget/countdown_timer.css index 60d90ff..f70f3b3 100755 --- a/CountdownTimer.gadget/countdown_timer.css +++ b/CountdownTimer.gadget/countdown_timer.css @@ -23,6 +23,13 @@ body { font-weight: bolder; } +#controls { + height:16px; + top:3px; + right:3px; + position:absolute; +} + img#restart { - margin-left: 6px; + margin-left: 4px; } \ No newline at end of file diff --git a/CountdownTimer.gadget/countdown_timer.js b/CountdownTimer.gadget/countdown_timer.js index a5f3379..7fc6b8a 100755 --- a/CountdownTimer.gadget/countdown_timer.js +++ b/CountdownTimer.gadget/countdown_timer.js @@ -7,114 +7,97 @@ System.Gadget.settingsUI = "settings.html"; System.Gadget.onSettingsClosed = onSettingsClosed; -var seconds = 0; -var countdown_timer = null; -var debug_mode = 0; +FTTA.countdownTimer.main.seconds = 0; +FTTA.countdownTimer.main.countdown_timer = null; +FTTA.countdownTimer.main.alarm_timer = null; - -function init() -{ - loadSettings(); -} - -function resetCountdown() -{ - pauseCountdown(); - - seconds = 60 * countdown_min; - document.getElementById('countdown_block').innerHTML = seconds / 60; -} - -function startCountdown() -{ - countdown(); - showPauseButton(); -} -function pauseCountdown(obj) -{ - clearTimeout(countdown_timer); - showPlayButton(); -} -function countdown() -{ - if (seconds > 0) { - seconds -= 1; - var time_statement = seconds > 60 ? (Math.floor(seconds / 60) + " MIN") : (seconds + " SEC"); - document.getElementById('countdown_block').innerHTML = time_statement; - document.getElementById('countdown_block').title = seconds + " seconds remain"; - updateProgressBar(); - countdown_timer = setTimeout('countdown()', 1000); - } else { - timesUp(0); +FTTA.countdownTimer.main.countdown = { + reset: function() { + this.pause(); + FTTA.countdownTimer.main.seconds = 60 * FTTA.countdownTimer.global.countdown_min; + document.getElementById('countdown_block').innerHTML = FTTA.countdownTimer.main.seconds / 60 + " MIN"; + FTTA.countdownTimer.ui.updateProgressBar(); + }, + start: function() { + this.run(); + FTTA.countdownTimer.ui.showPause(); + }, + pause: function(obj) { + clearTimeout(FTTA.countdownTimer.main.countdown_timer); + clearTimeout(FTTA.countdownTimer.main.alarm_timer); + FTTA.countdownTimer.ui.showPlay(); + }, + run: function () { + if (FTTA.countdownTimer.main.seconds > 0) { + FTTA.countdownTimer.main.seconds -= 1; + var time_statement = FTTA.countdownTimer.main.seconds > 60 ? (Math.floor(FTTA.countdownTimer.main.seconds / 60) + " MIN") : (FTTA.countdownTimer.main.seconds + " SEC"); + document.getElementById('countdown_block').innerHTML = time_statement; + document.getElementById('countdown_block').title = FTTA.countdownTimer.main.seconds + " seconds"; + FTTA.countdownTimer.ui.updateProgressBar(); + FTTA.countdownTimer.main.countdown_timer = setTimeout('FTTA.countdownTimer.main.countdown.run()', 1000); + } else { + FTTA.countdownTimer.ui.timesUp(0); + } } } -function updateProgressBar() -{ - var width = Math.round(seconds / (60*countdown_min) * 100); - document.getElementById('progress_bar').style.width = width + "%"; - - var tick = document.getElementById('countdown_tick'); - if (tick.style.color == '#666') { - tick.style.color = '#ccc'; - } else { - tick.style.color = '#666'; - } -} -function timesUp(i) -{ - if (i++ < max_alarm_notifies) { - System.Sound.playSound("sound/alarm.wav"); - document.getElementById('countdown_block').innerHTML = "TIMES UP"; +FTTA.countdownTimer.ui = { + updateProgressBar: function() { + var width = Math.round(FTTA.countdownTimer.main.seconds / (60*FTTA.countdownTimer.global.countdown_min) * 100); + document.getElementById('progress_bar').style.width = width + "%"; - setTimeout("timesUp("+i+");", 4000); - } else { - showPlayButton(); - } -} -function showPlayButton() -{ - if (document.getElementById('pause')) { - var pause_btn = document.getElementById('pause'); - pause_btn.onclick = function() { startCountdown(); } - pause_btn.src = "images/play.png"; - pause_btn.id = "play"; - } -} -function showPauseButton() -{ - if (document.getElementById('play')) { - var play_btn = document.getElementById('play'); - play_btn.onclick = function() { pauseCountdown(); } - play_btn.src = "images/pause.png"; - play_btn.id = "pause"; + var tick = document.getElementById('countdown_tick'); + + if (tick.style.color == '#666') { + tick.style.color = '#ccc'; + } else { + tick.style.color = '#666'; + } + }, + timesUp: function(i) { + if (i++ < FTTA.countdownTimer.global.num_audible_alarm) { + System.Sound.playSound("sound/alarm.wav"); + FTTA.countdownTimer.main.alarm_timer = setTimeout("FTTA.countdownTimer.ui.timesUp("+i+");", 4000); + } else { + this.showPlay(); + } + document.getElementById('countdown_block').innerHTML = "TIME'S UP"; + }, + showPlay: function() { + if (document.getElementById('pause')) { + var pause_btn = document.getElementById('pause'); + pause_btn.onclick = function() { FTTA.countdownTimer.main.countdown.start(); } + pause_btn.src = "images/play.png"; + pause_btn.id = "play"; + } + }, + showPause: function() { + if (document.getElementById('play')) { + var play_btn = document.getElementById('play'); + play_btn.onclick = function() { FTTA.countdownTimer.main.countdown.pause(); } + play_btn.src = "images/pause.png"; + play_btn.id = "pause"; + } } } -function debug(msg) + +FTTA.countdownTimer.main.init = function() { - if (debug_mode) { - //System.Debug.outputString(msg); - window.prompt(msg); - } + FTTA.countdownTimer.main.getSettings(); } -function loadSettings() +FTTA.countdownTimer.main.getSettings = function() { - var setting_min = 0; + FTTA.countdownTimer.global.getSettings(); + + // override global countdown if custom is set var custom_countdown = System.Gadget.Settings.readString("custom_countdown_min"); if (custom_countdown > 0) { - setting_min = custom_countdown; - } else { - setting_min = System.Gadget.Settings.readString("countdown_minutes"); + FTTA.countdownTimer.global.countdown_min = custom_countdown; } - var setting_alarm_notifs = System.Gadget.Settings.read("countdown_alarm_notifies"); - - max_alarm_notifies = setting_alarm_notifs == '' ? 2 : setting_alarm_notifs; - countdown_min = setting_min == '' ? 30 : setting_min; - - resetCountdown(); + FTTA.countdownTimer.main.countdown.reset(); } function onSettingsClosed(event) @@ -122,11 +105,6 @@ function onSettingsClosed(event) // User hits OK on the settings page. if (event.closeAction == event.Action.commit) { - loadSettings(); + FTTA.countdownTimer.main.getSettings(); } - // User hits Cancel on the settings page. - //else if (event.closeAction == event.Action.cancel) - //{ - // SetContentText("Cancelled"); - //} } diff --git a/CountdownTimer.gadget/gadget.xml b/CountdownTimer.gadget/gadget.xml index cb49d0f..46526a7 100755 --- a/CountdownTimer.gadget/gadget.xml +++ b/CountdownTimer.gadget/gadget.xml @@ -1,16 +1,16 @@ Countdown Timer - naraku.gadgets + ftta.gadgets 1.0.0.0 - + © 2010 - Simple Countdown Timer + A Simple, Configurable Countdown Timer diff --git a/CountdownTimer.gadget/global.js b/CountdownTimer.gadget/global.js index f9a7fa0..c76ff48 100755 --- a/CountdownTimer.gadget/global.js +++ b/CountdownTimer.gadget/global.js @@ -4,7 +4,41 @@ - - http://funtothinkabout.com */ +FTTA = { + countdownTimer: { + global: {}, + main: {}, + settings: {} + } +} +FTTA.countdownTimer.global.getSettings = function() +{ + // override Defaults HERE: + FTTA.countdownTimer.global.countdown_min = 30; + FTTA.countdownTimer.global.num_audible_alarm = 2; + FTTA.countdownTimer.global.presets_list_values = "1||5||15||30||60||240"; + + // leave these alone. + var first_run = System.Gadget.Settings.readString("countdown_first_run"); + if (System.Gadget.Settings.readString("countdown_first_run") != '') { + FTTA.countdownTimer.global.countdown_min = System.Gadget.Settings.read("countdown_minutes"); + FTTA.countdownTimer.global.presets_list_values = System.Gadget.Settings.read("countdown_presets"); + FTTA.countdownTimer.global.num_audible_alarm = System.Gadget.Settings.read("countdown_alarm_notifies"); + } + //+"\npresets:"+FTTA.countdownTimer.global.presets_list_values + debug("countdown_min:"+FTTA.countdownTimer.global.countdown_min+"\nnum alarms:"+FTTA.countdownTimer.global.num_audible_alarm); +} + function getTimeSuffix(seconds) { return seconds > 3600 ? "HR" : seconds > 60 ? "MIN" : "SEC"; } + +FTTA.countdownTimer.global.debug_mode = 0; +function debug(msg) +{ + if (FTTA.countdownTimer.global.debug_mode) { + //System.Debug.outputString(msg); + window.prompt(msg); + } +} diff --git a/CountdownTimer.gadget/main.html b/CountdownTimer.gadget/main.html index ec1ace1..ac6b786 100755 --- a/CountdownTimer.gadget/main.html +++ b/CountdownTimer.gadget/main.html @@ -7,16 +7,16 @@ Countdown Timer - - + + - + -
- - +
+ +
:
 
diff --git a/CountdownTimer.gadget/settings.html b/CountdownTimer.gadget/settings.html index a4f0b02..196099c 100755 --- a/CountdownTimer.gadget/settings.html +++ b/CountdownTimer.gadget/settings.html @@ -7,12 +7,12 @@ Countdown Timer Settings - + - + Custom Countdown @@ -24,7 +24,7 @@

- For Tips and About Visit:
+ For Tips and More, Visit:
Fun To Think About \ No newline at end of file diff --git a/CountdownTimer.gadget/settings.js b/CountdownTimer.gadget/settings.js index bde6527..e755659 100755 --- a/CountdownTimer.gadget/settings.js +++ b/CountdownTimer.gadget/settings.js @@ -4,11 +4,10 @@ - - http://funtothinkabout.com */ -System.Gadget.onSettingsClosing = SettingsClosing; +System.Gadget.onSettingsClosing = FTTA.countdownTimer.settings.SettingsClosing; -function SettingsClosing(event) +function FTTA.countdownTimer.settings.SettingsClosing(event) { - // Save the settings if the user clicked OK. if (event.closeAction == event.Action.commit) { System.Gadget.Settings.write("countdown_first_run", false); @@ -41,35 +40,26 @@ function SettingsClosing(event) event.cancel = false; } -var countdown_min; -var presets_list_values; -var num_audible_alarm; -function loadSettings() +FTTA.countdownTimer.settings.getSettings = function() { - if (System.Gadget.Settings.read("countdown_first_run") == '') { - loadDefaults(); - } else { - countdown_min = System.Gadget.Settings.read("countdown_minutes"); - presets_list_values = System.Gadget.Settings.read("countdown_presets"); - num_audible_alarm = System.Gadget.Settings.read("countdown_alarm_notifies"); - } + FTTA.countdownTimer.global.getSettings(); var presets_list = document.getElementById('preset_list'); - presets_list_values = presets_list_values.split("||"); + presets_list_values = FTTA.countdownTimer.global.presets_list_values.split("||"); + // populate countdown minute presets for (i = 0; i < presets_list_values.length; i++) { var option = document.createElement('option'); option.appendChild(document.createTextNode(presets_list_values[i])); option.setAttribute("value", presets_list_values[i]); - if (countdown_min == presets_list_values[i]) { + if (FTTA.countdownTimer.global.countdown_min == presets_list_values[i]) { option.setAttribute("selected", "selected"); } presets_list.appendChild(option); } // populate number of alarm sounds - document.getElementById("num_audible_alarm").value = num_audible_alarm; + document.getElementById("num_audible_alarm").value = FTTA.countdownTimer.global.num_audible_alarm; return; } -