diff --git a/README.md b/README.md index 951280d..2878b20 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,5 @@ # angular-timer — A simple AngularJS directive demonstrating re-usability & interoperability -Added intervalEvent attribute and an example - -Added intervalEvent which broadcasts a named event for each timer on the -$rootScope every interval. - -This allows periodic events that can be consumed, for instance, to save -a document occasionally. - +Sample AngularJS directive diff --git a/app/js/timer.js b/app/js/timer.js index 69a33b2..733c950 100644 --- a/app/js/timer.js +++ b/app/js/timer.js @@ -1,98 +1,82 @@ angular.module('timer', []) - .directive('timer', function ($rootScope, $timeout, $compile) { - return { - restrict: 'E', - replace: false, - scope: { - interval: '=interval', - countdownattr: '=countdown', - intervaleventattr: '=intervalevent' - }, - controller: function ($rootScope, $scope, $element) { - if ($element.html().trim().length === 0) { - $element.append($compile('{{millis}}')($scope)); - } + .directive('timer', function ($timeout, $compile) { + return { + restrict: 'E', + replace: false, + scope: { + interval: '=interval', + countdownattr: '=countdown' + }, + controller: function ($scope, $element) { + if ($element.html().trim().length === 0) { + $element.append($compile('{{millis}}')($scope)); + } - $scope.startTime = null; - $scope.timeoutId = null; - $scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined; - $scope.isRunning = false; - $scope.intervalEventCount = 0; - $scope.intervalEvent = $scope.intervaleventattr; + $scope.startTime = null; + $scope.timeoutId = null; + $scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined; + $scope.isRunning = false; - $scope.$on('timer-start', function (){ - $scope.start(); - }); + $scope.$on('timer-start', function () { + $scope.start(); + }); - $scope.$on('timer-resume', function (){ - $scope.resume(); - }); + $scope.$on('timer-resume', function () { + $scope.resume(); + }); - $scope.$on('timer-stop', function (){ - $scope.stop(); - }); - - //example only: consume specific events in your controller - //$scope.$on('event:save', function () { - //$scope.$on('event:refreshData', function () { - $scope.$on($scope.intervalEvent, function () { - $scope.intervalEventCount++; - $scope.intervalEventName = $scope.intervalEvent - }); - - function resetTimeout() { - if ($scope.timeoutId) { - $timeout.cancel($scope.timeoutId); - } - } + $scope.$on('timer-stop', function () { + $scope.stop(); + }); - $scope.start = $element[0].start = function () { - $scope.startTime = new Date(); - resetTimeout(); - tick(); - }; + function resetTimeout() { + if ($scope.timeoutId) { + $timeout.cancel($scope.timeoutId); + } + } - $scope.resume = $element[0].resume = function () { - resetTimeout(); - $scope.startTime = new Date() - ($scope.stoppedTime - $scope.startTime); - tick(); - }; + $scope.start = $element[0].start = function () { + $scope.startTime = new Date(); + resetTimeout(); + tick(); + }; - $scope.stop = $element[0].stop = function () { - $scope.stoppedTime = new Date(); - $timeout.cancel($scope.timeoutId); - $scope.timeoutId = null; - }; + $scope.resume = $element[0].resume = function () { + resetTimeout(); + $scope.startTime = new Date() - ($scope.stoppedTime - $scope.startTime); + tick(); + }; - $element.bind('$destroy', function () { - $timeout.cancel($scope.timeoutId); - }); + $scope.stop = $element[0].stop = function () { + $scope.stoppedTime = new Date(); + $timeout.cancel($scope.timeoutId); + $scope.timeoutId = null; + }; - var tick = function (){ - if ($scope.countdown > 0) { - $scope.countdown--; - } - else if ($scope.countdown <= 0) { - $scope.stop(); - } + $element.bind('$destroy', function () { + $timeout.cancel($scope.timeoutId); + }); - $scope.millis = new Date() - $scope.startTime; - $scope.seconds = Math.floor(($scope.millis / 1000) % 60) ; - $scope.minutes = Math.floor((($scope.millis / (1000*60)) % 60)); - $scope.hours = Math.floor((($scope.millis / (1000*60*60)) % 24)); - - if($scope.intervalEvent != undefined) { - $rootScope.$broadcast($scope.intervalEvent); - } - - $scope.timeoutId = $timeout(function () { - tick(); - }, $scope.interval); + var tick = function () { + if ($scope.countdown > 0) { + $scope.countdown--; + } + else if ($scope.countdown <= 0) { + $scope.stop(); + } - $scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis}); - }; + $scope.millis = new Date() - $scope.startTime; + $scope.seconds = Math.floor(($scope.millis / 1000) % 60); + $scope.minutes = Math.floor((($scope.millis / (1000 * 60)) % 60)); + $scope.hours = Math.floor((($scope.millis / (1000 * 60 * 60)) % 24)); + $scope.timeoutId = $timeout(function () { + tick(); + }, $scope.interval); + + $scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis}); + }; - $scope.start(); - } - }; - }); \ No newline at end of file + $scope.start(); + } + }; + }); \ No newline at end of file diff --git a/examples.html b/examples.html index 3637ec8..d76d559 100644 --- a/examples.html +++ b/examples.html @@ -81,11 +81,6 @@
  • Basic Example
  • Clock Timer
  • Progressbar Timer
  • -
  • - Countdown Timer
  • -
  • - Timer with Interval Event -
  • <timer/>
  • @@ -116,15 +111,15 @@
    -
    -

    AngularJS - Single Timer

    - - -
    -
    +
    +

    AngularJS - Single Timer

    + + +
    +
     <!DOCTYPE html>
     <html>
    @@ -160,22 +155,22 @@ 

    AngularJS - Single Timer

    <br/> </body> </html>
    -
    -
    - -
    -
    +
    + +
    +
    +
    -
    -

    AngularJS - Multiple Timer

    - +
    +

    AngularJS - Multiple Timer

    + -
    -
    +
    +
     <!DOCTYPE html>
     <html>
    @@ -212,22 +207,22 @@ 

    AngularJS - Multiple Timer

    </div> </body> </html>
    -
    -
    - -
    -
    +
    + +
    +
    + -
    -

    AngularJS - Polling Timers

    - +
    +

    AngularJS - Polling Timers

    + -
    -
    +
    +
     <!DOCTYPE html>
     <html>
    @@ -287,22 +282,22 @@ 

    AngularJS - Polling Timers

    <br/> </body> </html>
    -
    -
    - -
    -
    +
    + +
    + +
    -
    -

    JQuery Timer

    - +
    +

    JQuery Timer

    + -
    -
    +
    +
     <!DOCTYPE html>
     <html>
    @@ -331,22 +326,22 @@ 

    JQuery Timer

    <br/> </body> </html>
    -
    -
    - -
    -
    +
    + +
    + +
    -
    -

    Plain JavaScript

    - +
    +

    Plain JavaScript

    + -
    -
    +
    +
     <!DOCTYPE html>
     <html>
    @@ -374,17 +369,17 @@ 

    Plain JavaScript

    <br/> </body> </html>
    -
    -
    - -
    -
    +
    + +
    + +
    - + diff --git a/index.html b/index.html index 133a634..03c2216 100644 --- a/index.html +++ b/index.html @@ -77,9 +77,6 @@ Progressbar Timer
  • Countdown Timer
  • -
  • - Timer with Interval Event -
  • @@ -116,277 +113,234 @@
    -
    -

    - Introduction

    +
    +

    + Introduction

    -

    - Directives in AngularJS is a powerful way of building - reusable UI components. This simple project will serve as a sample/reference implementation - demonstrating its flexibilities by making it inter-operable across runtime (AngularJS, plain simple - JavaScript & jQuery)

    +

    + Directives in AngularJS is a powerful way of building + reusable UI components. This simple project will serve as a sample/reference implementation + demonstrating its flexibilities by making it inter-operable across runtime (AngularJS, plain simple + JavaScript & jQuery)

    -

    - For basic understanding of how directives work in AngularJS, please head to this developer guide.

    -
    -
    -

    - Basic Example

    +

    + For basic understanding of how directives work in AngularJS, please head to this developer guide.

    +
    +
    +

    + Basic Example

    -
    -

    - This simple directive <timer /> will start the timer with the default option of - ticking every 1 millisecond

    +
    +

    + This simple directive <timer /> will start the timer with the default option of + ticking every 1 millisecond

    -

    - -

    - - -
    -
    -

    - Timer with hours, minutes & seconds

    + + + + +
    + +
    +

    + Timer with hours, minutes & seconds

    -
    -

    - This markup <timer interval="1000">{{hours}} hours, {{minutes}} - minutes, {{seconds}} seconds.</timer> will run the clock timer ticking every second

    +
    +

    + This markup <timer interval="1000">{{hours}} hours, {{minutes}} + minutes, {{seconds}} seconds.</timer> will run the clock timer ticking every second

    -

    - {{hours}} hours, {{minutes}} minutes, {{seconds}} seconds. -

    - -
    -
    -

    - Progressbar Timer

    + {{hours}} hours, {{minutes}} minutes, {{seconds}} seconds. + + + +
    +
    +

    + Progressbar Timer

    -
    -

    - Timer directive along with Twitter Bootstrap's Progressbar will set the timer - on Progressbar control.

    - <timer interval="1000"><div class="progress - progress-striped active"> <div class="bar" style="width: {{seconds}}%;"></div> - </div></timer> +
    +

    + Timer directive along with Twitter Bootstrap's Progressbar will set the timer + on Progressbar control.

    + <timer interval="1000"><div class="progress + progress-striped active"> <div class="bar" style="width: {{seconds}}%;"></div> + </div></timer> -

    - -
    -
    -  
    -
    -
    -

    - - -
    -
    -

    - Countdown Timer

    + +
    +
    +  
    +
    +
    + + + + +
    +
    +

    + Countdown Timer

    -
    -

    - The countdown timer <timer interval="1000" countdown="100"/> - will start its countdown from 100 until it reaches 0 by ticking every second

    +
    +

    + The countdown timer <timer interval="1000" countdown="100"/> + will start its countdown from 100 until it reaches 0 by ticking every second

    -

    - {{countdown}} -

    -
    -
    -

    - Timer with Interval Event

    + {{countdown}} + + +
    +
    +

    + Markup

    -
    -

    - The interval event will fire 'your-named-event' on the $rootScope every interval. -

    -

    - Remember the single quotes around the event name. (Angular needs to know it is a string.)

    -

    - - <timer interval="1000" intervalEvent="'event:timer-interval-check-a-thing'">{{intervalEventName}}: {{intervalEventCount}}</timer> -

    -

    - - <timer interval="3000" intervalEvent="'event:timer-interval-save-document'">{{intervalEventName}}: {{intervalEventCount}}</timer> -

    -

    - {{intervalEventName}}: {{intervalEventCount}} -
    - {{intervalEventName}}: {{intervalEventCount}} -

    -
    -
    -
    -

    - Markup

    +

    + Timer directive can be declared using following options. By default, it will display milliseconds inside + span tag. It can also take template string to display user-defined formats.

    +

    - Timer directive can be declared using following options. By default, it will display milliseconds inside - span tag. It can also take template string to display user-defined formats.

    - -
    -

    - <timer interval="1000" />

    -
    -
    -

    - <timer interval="1000">{{hours}} hours, {{minutes}} minutes, - {{seconds}} seconds, {{millis}} milliseconds.</timer>

    -
    -
    -

    - - <timer interval="3000" intervalEvent="'event:timer-interval-save-document'">{{intervalEventName}}: {{intervalEventCount}}</timer> - -

    -
    -

    - Attributes

    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - Name - - Required - - Default value -
    - interval - - false - - 1 millisecond -
    - countdown - - false - -  
    - intervalEvent - - false - - The interval event will fire 'event:eventName' on the $rootScope every interval. -
    -

    - Methods

    - + <timer interval="1000" />

    +
    +

    - Following DOM methods can be invoked to timer. Prepend to timer- for scope based - events when calling from AngularJS controllers.

    - - - - - - - - - - - - - - - - - - - - - -
    - Method name - - Description -
    - start - - Starts the timer -
    - stop - - Stops the timer -
    - resume - - Resumes the timer. Will NOT reset the start time -
    -

    - Events

    + <timer interval="1000">{{hours}} hours, {{minutes}} minutes, + {{seconds}} seconds, {{millis}} milliseconds.</timer>

    +
    +

    + Attributes

    + + + + + + + + + + + + + + + + + + + + +
    + Name + + Required + + Default value +
    + interval + + false + + 1 millisecond +
    + countdown + + false + +  
    +

    + Methods

    - - - - - - - - - - - - - -
    - Event name - - Description -
    - timer-tick - - Tick event that gets emitted for every timer tick for specified interval. Please refer to Polling Timer - example for its usage. -
    -
    -
    -

    - Contributions welcome!

    +

    + Following DOM methods can be invoked to timer. Prepend to timer- for scope based + events when calling from AngularJS controllers.

    + + + + + + + + + + + + + + + + + + + + + +
    + Method name + + Description +
    + start + + Starts the timer +
    + stop + + Stops the timer +
    + resume + + Resumes the timer. Will NOT reset the start time +
    +

    + Events

    -

    - We welcome any or all kinds of contributions! Please submit pull requests or create issues to contribute to this - project :)

    -
    - + + + + + + + + + + + + + +
    + Event name + + Description +
    + timer-tick + + Tick event that gets emitted for every timer tick for specified interval. Please refer to Polling Timer + example for its usage. +
    + +
    +

    + Contributions welcome!

    + +

    + We welcome any or all kinds of contributions! Please submit pull requests or create issues to contribute to this + project :)

    +
    + - + \ No newline at end of file