Skip to content

Commit

Permalink
manual merging
Browse files Browse the repository at this point in the history
  • Loading branch information
siddii committed Jun 25, 2013
1 parent 8c7edf1 commit e55d93c
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 420 deletions.
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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


154 changes: 69 additions & 85 deletions app/js/timer.js
Original file line number Diff line number Diff line change
@@ -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('<span>{{millis}}</span>')($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('<span>{{millis}}</span>')($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();
}
};
});
$scope.start();
}
};
});
143 changes: 69 additions & 74 deletions examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@
<li><a href="index.html#basic-timer">Basic Example</a></li>
<li><a href="index.html#clock-timer">Clock Timer</a></li>
<li><a href="index.html#progressbar-timer">Progressbar Timer</a></li>
<li>
<a href="index.html#countdown-timer">Countdown Timer</a></li>
<li>
<a href="index.html#intervalevent-timer">Timer with Interval Event</a>
</li>
</ul>
</li>
<li ng-non-bindable><a href="index.html#markup">&lt;timer/&gt;</a></li>
Expand Down Expand Up @@ -116,15 +111,15 @@
</div>
</div>
<div class="container page-content">
<section id="angularjs-single-timer">
<h3>AngularJS - Single Timer</h3>
<ul class="nav nav-tabs" id="angularjs-single-timer-tab">
<li class="active"><a data-toggle="tab" href="#angularjs-single-timer-source">index.html</a></li>
<li><a data-toggle="tab" href="#angularjs-single-timer-demo">Demo</a></li>
</ul>

<div class="tab-content">
<div class="tab-pane active" id="angularjs-single-timer-source">
<section id="angularjs-single-timer">
<h3>AngularJS - Single Timer</h3>
<ul class="nav nav-tabs" id="angularjs-single-timer-tab">
<li class="active"><a data-toggle="tab" href="#angularjs-single-timer-source">index.html</a></li>
<li><a data-toggle="tab" href="#angularjs-single-timer-demo">Demo</a></li>
</ul>

<div class="tab-content">
<div class="tab-pane active" id="angularjs-single-timer-source">
<pre class="prettyprint linenums">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
Expand Down Expand Up @@ -160,22 +155,22 @@ <h3>AngularJS - Single Timer</h3>
&lt;br/&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<div class="tab-pane" id="angularjs-single-timer-demo">
<iframe src="examples/angularjs-single-timer.html" width="100%" height="350px"></iframe>
</div>
</div>
</section>
<div class="tab-pane" id="angularjs-single-timer-demo">
<iframe src="examples/angularjs-single-timer.html" width="100%" height="350px"></iframe>
</div>
</div>
</section>

<section id="angularjs-multiple-timer">
<h3>AngularJS - Multiple Timer</h3>
<ul class="nav nav-tabs" id="angularjs-multiple-timer-tab">
<li class="active"><a data-toggle="tab" href="#angularjs-multiple-timer-source">index.html</a></li>
<li><a data-toggle="tab" href="#angularjs-multiple-timer-demo">Demo</a></li>
</ul>
<section id="angularjs-multiple-timer">
<h3>AngularJS - Multiple Timer</h3>
<ul class="nav nav-tabs" id="angularjs-multiple-timer-tab">
<li class="active"><a data-toggle="tab" href="#angularjs-multiple-timer-source">index.html</a></li>
<li><a data-toggle="tab" href="#angularjs-multiple-timer-demo">Demo</a></li>
</ul>

<div class="tab-content">
<div class="tab-pane active" id="angularjs-multiple-timer-source">
<div class="tab-content">
<div class="tab-pane active" id="angularjs-multiple-timer-source">
<pre class="prettyprint linenums">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
Expand Down Expand Up @@ -212,22 +207,22 @@ <h3>AngularJS - Multiple Timer</h3>
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<div class="tab-pane" id="angularjs-multiple-timer-demo">
<iframe src="examples/angularjs-multiple-timers.html" width="100%" height="350px"></iframe>
</div>
</div>
</section>
<div class="tab-pane" id="angularjs-multiple-timer-demo">
<iframe src="examples/angularjs-multiple-timers.html" width="100%" height="350px"></iframe>
</div>
</div>
</section>

<section id="angularjs-polling-timer">
<h3>AngularJS - Polling Timers</h3>
<ul class="nav nav-tabs" id="angularjs-polling-timer-tab">
<li class="active"><a data-toggle="tab" href="#angularjs-polling-timer-source">index.html</a></li>
<li><a data-toggle="tab" href="#angularjs-polling-timer-demo">Demo</a></li>
</ul>
<section id="angularjs-polling-timer">
<h3>AngularJS - Polling Timers</h3>
<ul class="nav nav-tabs" id="angularjs-polling-timer-tab">
<li class="active"><a data-toggle="tab" href="#angularjs-polling-timer-source">index.html</a></li>
<li><a data-toggle="tab" href="#angularjs-polling-timer-demo">Demo</a></li>
</ul>

<div class="tab-content">
<div class="tab-pane active" id="angularjs-polling-timer-source">
<div class="tab-content">
<div class="tab-pane active" id="angularjs-polling-timer-source">
<pre class="prettyprint linenums">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
Expand Down Expand Up @@ -287,22 +282,22 @@ <h3>AngularJS - Polling Timers</h3>
&lt;br/&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<div class="tab-pane" id="angularjs-polling-timer-demo">
<iframe src="examples/angularjs-polling-timer.html" width="100%" height="350px"></iframe>
</div>
</div>
</section>
<div class="tab-pane" id="angularjs-polling-timer-demo">
<iframe src="examples/angularjs-polling-timer.html" width="100%" height="350px"></iframe>
</div>
</div>
</section>

<section id="jquery-timer">
<h3>JQuery Timer</h3>
<ul class="nav nav-tabs" id="jquery-timer-tab">
<li class="active"><a data-toggle="tab" href="#jquery-timer-source">index.html</a></li>
<li><a data-toggle="tab" href="#jquery-timer-demo">Demo</a></li>
</ul>
<section id="jquery-timer">
<h3>JQuery Timer</h3>
<ul class="nav nav-tabs" id="jquery-timer-tab">
<li class="active"><a data-toggle="tab" href="#jquery-timer-source">index.html</a></li>
<li><a data-toggle="tab" href="#jquery-timer-demo">Demo</a></li>
</ul>

<div class="tab-content">
<div class="tab-pane active" id="jquery-timer-source">
<div class="tab-content">
<div class="tab-pane active" id="jquery-timer-source">
<pre class="prettyprint linenums">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
Expand Down Expand Up @@ -331,22 +326,22 @@ <h3>JQuery Timer</h3>
&lt;br/&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<div class="tab-pane" id="jquery-timer-demo">
<iframe src="examples/jquery-timer.html" width="100%" height="350px"></iframe>
</div>
</div>
</section>
<div class="tab-pane" id="jquery-timer-demo">
<iframe src="examples/jquery-timer.html" width="100%" height="350px"></iframe>
</div>
</div>
</section>

<section id="plain-javascript">
<h3>Plain JavaScript</h3>
<ul class="nav nav-tabs" id="plain-javascript-tab">
<li class="active"><a data-toggle="tab" href="#plain-javascript-source">index.html</a></li>
<li><a data-toggle="tab" href="#plain-javascript-demo">Demo</a></li>
</ul>
<section id="plain-javascript">
<h3>Plain JavaScript</h3>
<ul class="nav nav-tabs" id="plain-javascript-tab">
<li class="active"><a data-toggle="tab" href="#plain-javascript-source">index.html</a></li>
<li><a data-toggle="tab" href="#plain-javascript-demo">Demo</a></li>
</ul>

<div class="tab-content">
<div class="tab-pane active" id="plain-javascript-source">
<div class="tab-content">
<div class="tab-pane active" id="plain-javascript-source">
<pre class="prettyprint linenums">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
Expand Down Expand Up @@ -374,17 +369,17 @@ <h3>Plain JavaScript</h3>
&lt;br/&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<div class="tab-pane" id="plain-javascript-demo">
<iframe src="examples/plain-javascript.html" width="100%" height="350px"></iframe>
</div>
</div>
</section>
<div class="tab-pane" id="plain-javascript-demo">
<iframe src="examples/plain-javascript.html" width="100%" height="350px"></iframe>
</div>
</div>
</section>


<footer>
<p>&copy; Siddique Hameed 2013</p>
</footer>
<footer>
<p>&copy; Siddique Hameed 2013</p>
</footer>

</div>

Expand Down
Loading

0 comments on commit e55d93c

Please sign in to comment.