Skip to content

Commit 691da4a

Browse files
Paul LeclercqPaul Leclercq
authored andcommitted
merge
2 parents e70ad4a + 028b63d commit 691da4a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+46415
-29120
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ logs/*
55
build/
66
.DS_Store
77
/node_modules/
8-
*.log
8+
*.log

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ notifications:
77

88
before_script:
99
- npm install -g grunt-cli
10+
- npm install bower
11+
- bower install
1012

1113
script:
1214
- grunt tests

dist/CHANGELOG.md renamed to CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
##Change Log
1+
-##Change Log
22

33
###Version 1.2.0
44
* Reset method https://github.com/siddii/angular-timer/pull/46
@@ -63,4 +63,4 @@
6363
* 'auto-start' attribute name change to 'autostart' in support of Angular 1.2. See #14
6464

6565
###Version 1.0.3
66-
* Successful Bower integration!
66+
* Successful Bower integration!

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = function (grunt) {
3636
banner: '<%= meta.banner %>'
3737
},
3838
src: [
39-
'app/**/*.js'
39+
'app/**/*.js'
4040
],
4141
dest: '<%= dist_dir %>/<%= pkg.name %>.js'
4242
}

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@ With any of the following options...
77
* Install angular-timer using [Bower](http://bower.io) - `bower install angular-timer`
88
* Add ``timer`` to your list of modules
99

10+
### Requirements
11+
With Bower install :
12+
* Install humanize-duration using [Bower](http://bower.io) - `bower install humanize-duration`
13+
* Install momentjs using [Bower](http://bower.io) - `bower install momentjs`
14+
15+
And include these scripts in your webpage :
16+
* bower_components/momentjs/min/moment.min.js
17+
* bower_components/momentjs/min/locales.min.js
18+
* bower_components/humanize-duration/humanize-duration.js
19+
20+
1021
### Running locally
22+
Install all bower components - `bower install`
1123
Using [Grunt](http://gruntjs.com/) type `grunt` from command line, the default task will open index.html page in your
1224
default browser
1325

app/js/timer.js renamed to app/js/_timer.js

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ var timerModule = angular.module('timer', [])
1010
countdownattr: '=countdown',
1111
finishCallback: '&finishCallback',
1212
autoStart: '&autoStart',
13+
language: '@?',
1314
maxTimeUnit: '='
1415
},
15-
controller: ['$scope', '$element', '$attrs', '$timeout', '$interpolate', function ($scope, $element, $attrs, $timeout, $interpolate) {
16+
controller: ['$scope', '$element', '$attrs', '$timeout', 'I18nService', '$interpolate', function ($scope, $element, $attrs, $timeout, I18nService, $interpolate) {
1617

1718
// Checking for trim function since IE8 doesn't have it
1819
// If not a function, create tirm with RegEx to mimic native trim
@@ -27,6 +28,18 @@ var timerModule = angular.module('timer', [])
2728
//backward and forward compatibility.
2829
$scope.autoStart = $attrs.autoStart || $attrs.autostart;
2930

31+
32+
$scope.language = $scope.language || 'en';
33+
34+
//allow to change the language of the directive while already launched
35+
$scope.$watch('language', function() {
36+
i18nService.init($scope.language);
37+
});
38+
39+
//init momentJS i18n, default english
40+
var i18nService = new I18nService();
41+
i18nService.init($scope.language);
42+
3043
if ($element.html().trim().length === 0) {
3144
$element.append($compile('<span>' + $interpolate.startSymbol() + 'millis' + $interpolate.endSymbol() + '</span>')($scope));
3245
} else {
@@ -54,11 +67,11 @@ var timerModule = angular.module('timer', [])
5467
$scope.$on('timer-clear', function () {
5568
$scope.clear();
5669
});
57-
70+
5871
$scope.$on('timer-reset', function () {
5972
$scope.reset();
6073
});
61-
74+
6275
$scope.$on('timer-set-countdown', function (e, countdown) {
6376
$scope.countdown = countdown;
6477
});
@@ -76,8 +89,8 @@ var timerModule = angular.module('timer', [])
7689
});
7790

7891
$scope.start = $element[0].start = function () {
79-
$scope.startTime = $scope.startTimeAttr ? new Date($scope.startTimeAttr) : new Date();
80-
$scope.endTime = $scope.endTimeAttr ? new Date($scope.endTimeAttr) : null;
92+
$scope.startTime = $scope.startTimeAttr ? moment($scope.startTimeAttr) : moment();
93+
$scope.endTime = $scope.endTimeAttr ? moment($scope.endTimeAttr) : null;
8194
if (!$scope.countdown) {
8295
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
8396
}
@@ -91,7 +104,7 @@ var timerModule = angular.module('timer', [])
91104
if ($scope.countdownattr) {
92105
$scope.countdown += 1;
93106
}
94-
$scope.startTime = new Date() - ($scope.stoppedTime - $scope.startTime);
107+
$scope.startTime = moment().diff((moment($scope.stoppedTime).diff(moment($scope.startTime))));
95108
tick();
96109
$scope.isRunning = true;
97110
};
@@ -104,31 +117,37 @@ var timerModule = angular.module('timer', [])
104117

105118
$scope.clear = $element[0].clear = function () {
106119
// same as stop but without the event being triggered
107-
$scope.stoppedTime = new Date();
120+
$scope.stoppedTime = moment();
108121
resetTimeout();
109122
$scope.timeoutId = null;
110123
$scope.isRunning = false;
111124
};
112125

113126
$scope.reset = $element[0].reset = function () {
114-
$scope.startTime = $scope.startTimeAttr ? new Date($scope.startTimeAttr) : new Date();
115-
$scope.endTime = $scope.endTimeAttr ? new Date($scope.endTimeAttr) : null;
127+
$scope.startTime = $scope.startTimeAttr ? moment($scope.startTimeAttr) : moment();
128+
$scope.endTime = $scope.endTimeAttr ? moment($scope.endTimeAttr) : null;
116129
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
117130
resetTimeout();
118131
tick();
119132
$scope.isRunning = false;
120133
$scope.clear();
121134
};
122-
135+
123136
$element.bind('$destroy', function () {
124137
resetTimeout();
125138
$scope.isRunning = false;
126139
});
127140

141+
128142
function calculateTimeUnits() {
143+
var timeUnits = {}; //will contains time with units
144+
129145
if ($attrs.startTime !== undefined){
130-
$scope.millis = new Date() - new Date($scope.startTimeAttr);
146+
$scope.millis = moment().diff(moment($scope.startTimeAttr));
131147
}
148+
149+
timeUnits = i18nService.getTimeUnits($scope.millis);
150+
132151
// compute time values based on maxTimeUnit specification
133152
if (!$scope.maxTimeUnit || $scope.maxTimeUnit === 'day') {
134153
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
@@ -180,13 +199,16 @@ var timerModule = angular.module('timer', [])
180199
$scope.daysS = ($scope.days === 1)? '' : 's';
181200
$scope.monthsS = ($scope.months === 1)? '' : 's';
182201
$scope.yearsS = ($scope.years === 1)? '' : 's';
202+
203+
183204
// new plural-singular unit decision functions (for custom units and multilingual support)
184-
$scope.secondUnit = function(singleSecond, pluralSecond){if($scope.seconds === 1){if(singleSecond){return singleSecond;} return 'second';} if(pluralSecond){return pluralSecond;} return 'seconds';};
185-
$scope.minuteUnit = function(singleMinute, pluralMinute){if($scope.minutes === 1){if(singleMinute){return singleMinute;} return 'minute';} if(pluralMinute){return pluralMinute;} return 'minutes';};
186-
$scope.hourUnit = function(singleHour, pluralHour){if($scope.hours === 1){if(singleHour){return singleHour;} return 'hour';} if(pluralHour){return pluralHour;} return 'hours';};
187-
$scope.dayUnit = function(singleDay, pluralDay){if($scope.days === 1){if(singleDay){return singleDay;} return 'day';} if(pluralDay){return pluralDay;} return 'days';};
188-
$scope.monthUnit = function(singleMonth, pluralMonth){if($scope.months === 1){if(singleMonth){return singleMonth;} return 'month';} if(pluralMonth){return pluralMonth;} return 'months';};
189-
$scope.yearUnit = function(singleYear, pluralYear){if($scope.years === 1){if(singleYear){return singleYear;} return 'year';} if(pluralYear){return pluralYear;} return 'years';};
205+
$scope.secondUnit = timeUnits.seconds;
206+
$scope.minuteUnit = timeUnits.minutes;
207+
$scope.hourUnit = timeUnits.hours;
208+
$scope.dayUnit = timeUnits.days;
209+
$scope.monthUnit = timeUnits.months;
210+
$scope.yearUnit = timeUnits.years;
211+
190212
//add leading zero if number is smaller than 10
191213
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
192214
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
@@ -229,17 +251,16 @@ var timerModule = angular.module('timer', [])
229251
}
230252
calculateTimeUnits();
231253

232-
var tick = function () {
254+
var tick = function tick() {
233255

234-
$scope.millis = new Date() - $scope.startTime;
256+
$scope.millis = moment().diff($scope.startTime);
235257
var adjustment = $scope.millis % 1000;
236258

237259
if ($scope.endTimeAttr) {
238-
$scope.millis = $scope.endTime - new Date();
260+
$scope.millis = moment($scope.endTime).diff(moment());
239261
adjustment = $scope.interval - $scope.millis % 1000;
240262
}
241263

242-
243264
if ($scope.countdownattr) {
244265
$scope.millis = $scope.countdown * 1000;
245266
}
@@ -279,7 +300,7 @@ var timerModule = angular.module('timer', [])
279300
}
280301
}]
281302
};
282-
}]);
303+
}]);
283304

284305
/* commonjs package manager support (eg componentjs) */
285306
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){

app/js/i18nService.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var app = angular.module('timer');
2+
3+
app.factory('I18nService', function() {
4+
5+
var I18nService = function() {};
6+
7+
I18nService.prototype.language = 'en';
8+
I18nService.prototype.timeHumanizer = {};
9+
10+
I18nService.prototype.init = function init(lang){
11+
this.language = lang;
12+
//moment init
13+
moment.locale(this.language);
14+
15+
//human duration init, using it because momentjs does not allow accurate time (
16+
// momentJS: a few moment ago, human duration : 4 seconds ago
17+
this.timeHumanizer = humanizeDuration.humanizer({
18+
language: this.language,
19+
halfUnit:false
20+
});
21+
};
22+
23+
/**
24+
* get time with units from momentJS i18n
25+
* @param {int} millis
26+
* @returns {{millis: string, seconds: string, minutes: string, hours: string, days: string, months: string, years: string}}
27+
*/
28+
I18nService.prototype.getTimeUnits = function getTimeUnits(millis) {
29+
var diffFromAlarm = Math.round(millis/1000) * 1000; //time in milliseconds, get rid of the last 3 ms value to avoid 2.12 seconds display
30+
31+
var time = {};
32+
33+
if (typeof this.timeHumanizer != 'undefined'){
34+
time = {
35+
'millis' : this.timeHumanizer(diffFromAlarm, { units: ["milliseconds"] }),
36+
'seconds' : this.timeHumanizer(diffFromAlarm, { units: ["seconds"] }),
37+
'minutes' : this.timeHumanizer(diffFromAlarm, { units: ["minutes", "seconds"] }) ,
38+
'hours' : this.timeHumanizer(diffFromAlarm, { units: ["hours", "minutes", "seconds"] }) ,
39+
'days' : this.timeHumanizer(diffFromAlarm, { units: ["days", "hours", "minutes", "seconds"] }) ,
40+
'months' : this.timeHumanizer(diffFromAlarm, { units: ["months", "days", "hours", "minutes", "seconds"] }) ,
41+
'years' : this.timeHumanizer(diffFromAlarm, { units: ["years", "months", "days", "hours", "minutes", "seconds"] })
42+
};
43+
}
44+
else {
45+
console.error('i18nService has not been initialized. You must call i18nService.init("en") for example');
46+
}
47+
48+
return time;
49+
};
50+
51+
return I18nService;
52+
});

bower.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@
99
"url": "git://github.com/siddii/angular-timer.git"
1010
},
1111
"dependencies": {
12-
"angular": ">= 1.0.7"
12+
"angular": ">= 1.0.7",
13+
"momentjs": "~2.9.0",
14+
"humanize-duration": "~2.4.0",
15+
"angular-mocks": "~1.3.13"
1316
},
1417
"devDependencies": {
15-
"bootstrap": "2.3.2",
16-
"angular-scenario": ">= 1.0.7",
17-
"angular-mocks": ">= 1.0.7"
18+
"bootstrap": "2.3.2",
19+
"angular-scenario": ">= 1.0.7",
20+
"angular-mocks": ">= 1.0.7"
1821
},
1922
"main": "./dist/angular-timer.js",
2023
"ignore": [
2124
"node_modules",
2225
"bower_components"
23-
]
26+
],
27+
"resolutions": {
28+
"angular": "~1.3.13",
29+
"humanize-duration": "~2.4.0"
30+
}
2431
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
{
22
"name": "angular-mocks",
3-
"version": "1.2.0-rc.2",
3+
"version": "1.3.13",
44
"main": "./angular-mocks.js",
5+
"ignore": [],
56
"dependencies": {
6-
"angular": "1.2.0-rc.2"
7+
"angular": "1.3.13"
78
},
89
"homepage": "https://github.com/angular/bower-angular-mocks",
9-
"_release": "1.2.0-rc.2",
10+
"_release": "1.3.13",
1011
"_resolution": {
1112
"type": "version",
12-
"tag": "v1.2.0-rc.2",
13-
"commit": "9bdf39463a7e59c35f4f6163853c8da4fbf81ea3"
13+
"tag": "v1.3.13",
14+
"commit": "8f53bfda01696e53549fe3266b0a4d220ea9bf77"
1415
},
1516
"_source": "git://github.com/angular/bower-angular-mocks.git",
16-
"_target": ">= 1.0.7",
17+
"_target": "~1.3.13",
1718
"_originalSource": "angular-mocks"
1819
}
Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
1-
bower-angular-mocks
2-
===================
1+
# packaged angular-mocks
32

4-
angular-mocks.js bower repo
3+
This repo is for distribution on `npm` and `bower`. The source for this module is in the
4+
[main AngularJS repo](https://github.com/angular/angular.js/tree/master/src/ngMock).
5+
Please file issues and pull requests against that repo.
6+
7+
## Install
8+
9+
You can install this package either with `npm` or with `bower`.
10+
11+
### npm
12+
13+
```shell
14+
npm install angular-mocks
15+
```
16+
17+
The mocks are then available at `node_modules/angular-mocks/angular-mocks.js`.
18+
19+
Note that this package is not in CommonJS format, so doing `require('angular-mocks')` will
20+
return `undefined`.
21+
22+
### bower
23+
24+
```shell
25+
bower install angular-mocks
26+
```
27+
28+
The mocks are then available at `bower_components/angular-mocks/angular-mocks.js`.
29+
30+
## Documentation
31+
32+
Documentation is available on the
33+
[AngularJS docs site](https://docs.angularjs.org/guide/unit-testing).
34+
35+
## License
36+
37+
The MIT License
38+
39+
Copyright (c) 2010-2012 Google, Inc. http://angularjs.org
40+
41+
Permission is hereby granted, free of charge, to any person obtaining a copy
42+
of this software and associated documentation files (the "Software"), to deal
43+
in the Software without restriction, including without limitation the rights
44+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
45+
copies of the Software, and to permit persons to whom the Software is
46+
furnished to do so, subject to the following conditions:
47+
48+
The above copyright notice and this permission notice shall be included in
49+
all copies or substantial portions of the Software.
50+
51+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
52+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
53+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
54+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
55+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
56+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
57+
THE SOFTWARE.

0 commit comments

Comments
 (0)