Skip to content

Commit f5e8815

Browse files
export tad
1 parent 5e5e925 commit f5e8815

File tree

4 files changed

+102
-80
lines changed

4 files changed

+102
-80
lines changed

countdown.ts

+4-78
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,4 @@
1-
import {Component,FORM_DIRECTIVES} from 'angular2/angular2';
2-
3-
4-
@Component({
5-
selector: 'count-down',
6-
properties: [
7-
'units',
8-
'end'
9-
],
10-
directives: [FORM_DIRECTIVES],
11-
template: `<h1>{{displayString()}}</h1>
12-
<ng-content></ng-content>
13-
`
14-
})
15-
16-
17-
export class CountDown {
18-
units:any;
19-
end:any;
20-
21-
onInit() {
22-
setInterval(()=>this.displayString(), 1);
23-
}
24-
25-
displayString() {
26-
27-
if (typeof this.units === 'string') {
28-
this.units = this.units.split('|');
29-
}
30-
31-
32-
var dateDifference = new Date(this.end) - new Date();
33-
var lastUnit = this.units[this.units.length - 1],
34-
unitConstantForMillisecs = {
35-
weeks: (1000 * 60 * 60 * 24 * 7),
36-
days: (1000 * 60 * 60 * 24),
37-
hours: (1000 * 60 * 60),
38-
minutes: (1000 * 60),
39-
seconds: 1000,
40-
milliseconds: 1
41-
},
42-
unitsLeft = {},
43-
returnString = '',
44-
totalMillisecsLeft = dateDifference,
45-
i,
46-
unit:any;
47-
for (i in this.units) {
48-
if (this.units.hasOwnProperty(i)) {
49-
50-
unit = this.units[i].trim();
51-
if (unitConstantForMillisecs[unit.toLowerCase()] === false) {
52-
//$interval.cancel(countDownInterval);
53-
throw new Error('Cannot repeat unit: ' + unit);
54-
55-
}
56-
if (unitConstantForMillisecs.hasOwnProperty(unit.toLowerCase()) === false) {
57-
throw new Error('Unit: ' + unit + ' is not supported. Please use following units: weeks, days, hours, minutes, seconds, milliseconds');
58-
}
59-
60-
unitsLeft[unit] = totalMillisecsLeft / unitConstantForMillisecs[unit.toLowerCase()];
61-
62-
if (lastUnit === unit) {
63-
unitsLeft[unit] = Math.ceil(unitsLeft[unit]);
64-
} else {
65-
unitsLeft[unit] = Math.floor(unitsLeft[unit]);
66-
}
67-
totalMillisecsLeft -= unitsLeft[unit] * unitConstantForMillisecs[unit.toLowerCase()];
68-
unitConstantForMillisecs[unit.toLowerCase()] = false;
69-
70-
71-
returnString += ' ' + unitsLeft[unit] + ' ' + unit;
72-
}
73-
}
74-
return returnString;
75-
}
76-
77-
78-
}
1+
/**
2+
* Created by previousdeveloper on 14.11.2015.
3+
*/
4+
export * from './lib/countdown';

lib/countdown.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {Component,FORM_DIRECTIVES} from 'angular2/angular2';
2+
3+
4+
@Component({
5+
selector: 'count-down',
6+
properties: [
7+
'units',
8+
'end'
9+
],
10+
directives: [FORM_DIRECTIVES],
11+
template: `<h1>{{displayString()}}</h1>
12+
<ng-content></ng-content>
13+
`
14+
})
15+
16+
17+
export class CountDown {
18+
units:any;
19+
end:any;
20+
21+
onInit() {
22+
setInterval(()=>this.displayString(), 1);
23+
}
24+
25+
displayString() {
26+
27+
if (typeof this.units === 'string') {
28+
this.units = this.units.split('|');
29+
}
30+
31+
32+
var dateDifference = new Date(this.end) - new Date();
33+
var lastUnit = this.units[this.units.length - 1],
34+
unitConstantForMillisecs = {
35+
weeks: (1000 * 60 * 60 * 24 * 7),
36+
days: (1000 * 60 * 60 * 24),
37+
hours: (1000 * 60 * 60),
38+
minutes: (1000 * 60),
39+
seconds: 1000,
40+
milliseconds: 1
41+
},
42+
unitsLeft = {},
43+
returnString = '',
44+
totalMillisecsLeft = dateDifference,
45+
i,
46+
unit:any;
47+
for (i in this.units) {
48+
if (this.units.hasOwnProperty(i)) {
49+
50+
unit = this.units[i].trim();
51+
if (unitConstantForMillisecs[unit.toLowerCase()] === false) {
52+
//$interval.cancel(countDownInterval);
53+
throw new Error('Cannot repeat unit: ' + unit);
54+
55+
}
56+
if (unitConstantForMillisecs.hasOwnProperty(unit.toLowerCase()) === false) {
57+
throw new Error('Unit: ' + unit + ' is not supported. Please use following units: weeks, days, hours, minutes, seconds, milliseconds');
58+
}
59+
60+
unitsLeft[unit] = totalMillisecsLeft / unitConstantForMillisecs[unit.toLowerCase()];
61+
62+
if (lastUnit === unit) {
63+
unitsLeft[unit] = Math.ceil(unitsLeft[unit]);
64+
} else {
65+
unitsLeft[unit] = Math.floor(unitsLeft[unit]);
66+
}
67+
totalMillisecsLeft -= unitsLeft[unit] * unitConstantForMillisecs[unit.toLowerCase()];
68+
unitConstantForMillisecs[unit.toLowerCase()] = false;
69+
70+
71+
returnString += ' ' + unitsLeft[unit] + ' ' + unit;
72+
}
73+
}
74+
return returnString;
75+
}
76+
77+
78+
}

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "angular2-simple-countdown",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Simple Angular2 CountDown",
5-
"main": "countdown.ts",
5+
"main": "countdown.js",
6+
"typings": "./countdown.d.ts",
67
"scripts": {
78
"test": "echo \"Error: no test specified\" && exit 1"
89
},

tsconfig.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"noImplicitAny": true,
4+
"module": "commonjs",
5+
"target": "ES5",
6+
"emitDecoratorMetadata": true,
7+
"experimentalDecorators": true,
8+
"sourceMap": true,
9+
"declaration": true
10+
},
11+
"files": [
12+
"countdown.ts"
13+
],
14+
"exclude": [
15+
"node_modules"
16+
]
17+
}

0 commit comments

Comments
 (0)