From 8853df582333d0838bfe822b2d59dc810569fc00 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Mon, 1 Dec 2014 08:56:58 +0900 Subject: attempt to make MyFlipClock a jQuery plugin --- js/main.js | 54 +++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/js/main.js b/js/main.js index e5981dd..e1f3433 100644 --- a/js/main.js +++ b/js/main.js @@ -149,40 +149,43 @@ $.ionSound({ preload: true }); -function MyFlipClock (obj, options) -{ - options.autoStart = false; - FlipClock.Factory.call(this, obj, 0, options); - this.go = function (seconds) { - this.setTime(seconds); - this.start(); - }; - this.reset = function () { - this.stop(); - this.setTime(0); - }; - this.custom = function () { - var minutes = parseInt(prompt('Number of minutes', '0')); - var seconds = parseInt(prompt('Number of seconds', '0')); - this.go(minutes * 60 + seconds); +(function ($) { + MyFlipClock = function(obj, digit, options) { + if(digit instanceof Object && digit instanceof Date === false) { + options = digit; + digit = 0; + } + return new MyFlipClock.Factory(obj, digit, options); }; -} -MyFlipClock.prototype = new FlipClock.Factory; + MyFlipClock.Factory = FlipClock.Factory.extend({ + autoStart:false, + go:function (seconds) { + this.setTime(seconds); + this.start(); + }, + reset:function () { + this.stop(); + this.setTime(0); + }, + custom:function () { + var minutes = parseInt(prompt('Number of minutes', '0')); + var seconds = parseInt(prompt('Number of seconds', '0')); + this.go(minutes * 60 + seconds); + } + }); +}(jQuery)); -var timeWastingClock = new MyFlipClock($('.time-wasting-clock'), { +var timeWastingClock = new MyFlipClock($('.time-wasting-clock'),{ callbacks:{ interval:function () { $.ionSound.play("button_tiny"); - if (timeWastingClock) - var time = timeWastingClock.getTime().time; - else - time = 0; + var time = timeWastingClock.getTime().time; $.jStorage.set("time_wasted", time); } } }); -timeWastingClock.setTime($.jStorage.get("time_wasted", 0)); +timeWastingClock.setTime($.jStorage.get("time_wasted", 0)); timeWastingClock.running = false; timeWastingClock.reset = $.proxy(function () { if (this.getTime() != 1) @@ -192,7 +195,7 @@ timeWastingClock.reset = $.proxy(function () { if (this.running) { $('#timeWastingClockGo').html('Start timer'); - this.stop(); + timeWastingClock.stop(); this.running = false; } $.jStorage.set('time_wasted', 0); @@ -223,6 +226,7 @@ var activityClock = new MyFlipClock($('.activity-countdown'), { } } }); +activityClock.countdown = true; $(document).bind('keydown', 't', timeWastingClock.toggle); $(document).bind('keydown', 'j', timeWastingClock.toggle); -- cgit v1.2.3