-
Notifications
You must be signed in to change notification settings - Fork 4
/
readingtime.js
86 lines (71 loc) · 2.16 KB
/
readingtime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
angular.module('ngReadingTime', [])
.service('readingTime', function() {
var formats = {
value_only: function(time) {
return time;
},
text_only: function(time) {
var result = (time.readableStr || 'readable in') + ' ';
if (!time.minutes && !time.seconds)
result += ' ' + (time.aMomentStr || 'a moment');
if (time.minutes)
result += time.minutes + ' ' + (time.minutesStr || 'minutes ');
if (time.minutes && time.seconds)
result += ', '
if (time.seconds)
result += time.seconds + ' ' + (time.secondsStr || 'seconds');
return result;
}
};
return {
get: function(text, custom) {
if (!text) text = '';
var words = text.trim().split(/\s+/g).length,
wps = (custom.wordsPerMinute || 210) / 60,
rts = words / wps;
var format = (custom.format || 'text_only');
var m = Math.floor(rts / 60),
s = Math.round(rts - m * 60);
var out = formats[format].call(this, {
aMomentStr: custom.aMomentStr,
readableStr: custom.readableStr,
minutesStr: custom.minutesStr,
secondsStr: custom.secondsStr,
minutes: m,
seconds: s
});
return out;
}
}
})
.directive('ngReadingTime', ['readingTime', function(readingTime) {
return {
restrict: 'A',
scope: {
textToRead: '=',
wordsPerMinute: '@?',
aMomentStr: '@?',
readableStr: '@?',
minutesStr: '@?',
secondsStr: '@?',
format: '@?'
},
replace: true,
template: '<span>{{txt}}</span>',
link: function(scope, element, attrs) {
scope.$watch(function() {
return scope.textToRead;
}, function (text) {
scope.txt = readingTime.get(text, {
wordsPerMinute: scope.wordsPerMinute,
format: scope.format,
aMomentStr: scope.aMomentStr,
readableStr: scope.readableStr,
minutesStr: scope.minutesStr,
secondsStr: scope.secondsStr
});
});
}
}
}]);