From a9080d82677a3da3d1e7614250a856eb12c95293 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sun, 30 Nov 2014 17:29:38 +0900 Subject: Move other clock helper functions into OO idiom --- js/main.js | 119 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 57 insertions(+), 62 deletions(-) diff --git a/js/main.js b/js/main.js index f8b4049..1381253 100644 --- a/js/main.js +++ b/js/main.js @@ -159,12 +159,23 @@ $.ionSound({ function MyFlipClock (jq, obj) { - var theClock = new FlipClock(jq, obj); - theClock.go = function (seconds) { + var thisClock = new FlipClock(jq, obj); + + thisClock.go = $.proxy(function (seconds) { this.setTime(seconds); this.start(); - }; - return theClock; + }, thisClock); + thisClock.reset = $.proxy(function () { + thisClock.stop(); + thisClock.setTime(0); + }, thisClock); + thisClock.custom = $.proxy(function () { + var minutes = parseInt(prompt('Number of minutes', '0')); + var seconds = parseInt(prompt('Number of seconds', '0')); + thisClock.go(minutes * 60 + seconds); + }, thisClock); + + return thisClock; } var timeWastingClock = MyFlipClock($('.time-wasting-clock'), { @@ -178,6 +189,38 @@ var timeWastingClock = MyFlipClock($('.time-wasting-clock'), { } }); +timeWastingClock.running = false; +timeWastingClock.reset = $.proxy(function () { + if (this.getTime() != 1) + { + if (confirm('Are you sure?')) + { + if (this.running) + { + $('#timeWastingClockGo').html('Start timer'); + this.stop(); + this.running = false; + } + $.jStorage.set('time_wasted', 0); + this.setTime(0); + } + } +}, timeWastingClock); +timeWastingClock.toggle = $.proxy(function () { + if (this.running) + { + $('#timeWastingClockGo').html('Start timer'); + this.stop(); + this.running = false; + } + else + { + $('#timeWastingClockGo').html('Stop timer'); + this.start(); + this.running = true; + } +}, timeWastingClock); + timeWastingClock.setTime($.jStorage.get("time_wasted", 0)); var $timeWastingClockRunning = false; @@ -192,25 +235,9 @@ var activityClock = MyFlipClock($('.activity-countdown'), { } }); -function timeWastingClockToggle() -{ - if ($timeWastingClockRunning) - { - $('#timeWastingClockGo').html('Start timer'); - timeWastingClock.stop(); - $timeWastingClockRunning = false; - } - else - { - $('#timeWastingClockGo').html('Stop timer'); - timeWastingClock.start(); - $timeWastingClockRunning = true; - } -} - -$(document).bind('keydown', 't', timeWastingClockToggle); -$(document).bind('keydown', 'j', timeWastingClockToggle); -$(document).bind('keydown', 'space', timeWastingClockToggle); +$(document).bind('keydown', 't', timeWastingClock.toggle); +$(document).bind('keydown', 'j', timeWastingClock.toggle); +$(document).bind('keydown', 'space', timeWastingClock.toggle); $(document).bind('keydown', 'd', toggleDateStyle); $(document).bind('keydown', 'k', function (){$.ionSound.play("klaxon");}); @@ -222,38 +249,6 @@ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } -function timeWastingClockReset() -{ - if (timeWastingClock.getTime() != 0) - { - var $timeWastingClockResetConfirm = confirm("Are you sure?"); - if ($timeWastingClockResetConfirm) - { - if ($timeWastingClockRunning) - { - $('#timeWastingClockGo').html('Start timer'); - timeWastingClock.stop(); - $timeWastingClockRunning = false; - } - $.jStorage.set("time_wasted", 0); - timeWastingClock.setTime(0); - } - } -} - -function activityClockReset() -{ - activityClock.stop(); - activityClock.setTime(0); -} - -function activityClockCustom() -{ - var minutes = prompt("Number of minutes", "0"); - var seconds = prompt("Number of seconds", "0"); - activityClockGo(parseInt(minutes) * 60 + parseInt(seconds)); -} - // mplungjan on stack overflow: http://stackoverflow.com/a/15397495 function nth(d) { if(d>3 && d<21) return 'th'; // thanks kennebec @@ -300,9 +295,9 @@ toggleDateStyle(); // expire chosen date style each day $.jStorage.setTTL("date_style", 86400); -$(document).bind('keydown', 's', timeWastingClockReset); -$(document).bind('keydown', 'r', activityClockReset); -$(document).bind('keydown', 'c', activityClockCustom); +$(document).bind('keydown', 's', timeWastingClock.reset); +$(document).bind('keydown', 'r', activityClock.reset); +$(document).bind('keydown', 'c', activityClock.custom); $(document).bind('keydown', '0', function (){activityClock.go(30);}); $(document).bind('keydown', '1', function (){activityClock.go(60);}); @@ -326,16 +321,16 @@ $(document).ready(function(){ $('#one-two-three').click(function (){$.ionSound.play("onetwothree");}); $('#timeWastingClockGo').button(); - $('#timeWastingClockGo').click(timeWastingClockToggle); + $('#timeWastingClockGo').click(timeWastingClock.toggle); $('#timeWastingClockReset').button(); - $('#timeWastingClockReset').click(timeWastingClockReset); + $('#timeWastingClockReset').click(timeWastingClock.reset); $('#activityClockReset').button(); - $('#activityClockReset').click(activityClockReset); + $('#activityClockReset').click(activityClock.reset); $('#activityClockCustom').button(); - $('#activityClockCustom').click(activityClockCustom); + $('#activityClockCustom').click(activityClock.custom); $('#activityClock30s').button(); $('#activityClock30s').click(function (){activityClock.go(30);}) -- cgit v1.2.3