reprogl

Форк
0
/
reading-time.js 
194 строки · 5.5 Кб
1
/*!
2

3
Name: Reading Time
4
Dependencies: jQuery
5
Author: Michael Lynch
6
Author URL: http://michaelynch.com
7
Date Created: August 14, 2013
8
Date Updated: June 10, 2014
9
Licensed under the MIT license
10

11
*/
12

13
;(function($) {
14

15
    $.fn.readingTime = function(options) {
16

17
        //return if no element was bound
18
        //so chained events can continue
19
        if(!this.length) {
20
            return this;
21
        }
22

23
        //define default parameters
24
        var defaults = {
25
            readingTimeTarget: '.eta',
26
            wordCountTarget: null,
27
            wordsPerMinute: 270,
28
            round: true,
29
            lang: 'en',
30
            lessThanAMinuteString: '',
31
            minShortFormString: '',
32
            prependTimeString: '',
33
            prependWordString: '',
34
            remotePath: null,
35
            remoteTarget: null
36
        }
37

38
        //define plugin
39
        var plugin = this;
40

41
        //define element
42
        var el = $(this);
43

44
        //merge defaults and options
45
        plugin.settings = $.extend({}, defaults, options);
46

47
        //define vars
48
        var readingTimeTarget = plugin.settings.readingTimeTarget;
49
        var wordCountTarget = plugin.settings.wordCountTarget;
50
        var wordsPerMinute = plugin.settings.wordsPerMinute;
51
        var round = plugin.settings.round;
52
        var lang = plugin.settings.lang;
53
        var lessThanAMinuteString = plugin.settings.lessThanAMinuteString;
54
        var prependTimeString = plugin.settings.prependTimeString;
55
        var prependWordString = plugin.settings.prependWordString;
56
        var minShortFormString = plugin.settings.minShortFormString
57
        var remotePath = plugin.settings.remotePath;
58
        var remoteTarget = plugin.settings.remoteTarget;
59

60

61
        //if lang is set to italian
62
        if(lang == 'it') {
63

64
            var lessThanAMinute = lessThanAMinuteString || "Meno di un minuto";
65

66
            var minShortForm = minShortFormString || 'min';
67

68
        //if lang is set to french
69
        } else if(lang == 'fr') {
70

71
            var lessThanAMinute = lessThanAMinuteString || "Moins d'une minute";
72

73
            var minShortForm = minShortFormString || 'min';
74

75
        //if lang is set to german
76
        } else if(lang == 'de') {
77

78
            var lessThanAMinute = lessThanAMinuteString || "Weniger als eine Minute";
79

80
            var minShortForm = minShortFormString || 'min';
81

82
        //if lang is set to spanish
83
        } else if(lang == 'es') {
84

85
            var lessThanAMinute = lessThanAMinuteString || "Menos de un minuto";
86

87
            var minShortForm = minShortFormString || 'min';
88

89
        //if lang is set to dutch
90
        } else if(lang == 'nl') {
91

92
            var lessThanAMinute = lessThanAMinuteString || "Minder dan een minuut";
93

94
            var minShortForm = minShortFormString || 'min';
95

96
        //default lang is english
97
        } else {
98

99
            var lessThanAMinute = lessThanAMinuteString || 'Less than a minute';
100

101
            var minShortForm = minShortFormString || 'min';
102

103
        }
104

105
        var setTime = function(text) {
106

107
            //split text by spaces to define total words
108
            var totalWords = text.trim().split(/\s+/g).length;
109

110
            //define words per second based on words per minute (wordsPerMinute)
111
            var wordsPerSecond = wordsPerMinute / 60;
112

113
            //define total reading time in seconds
114
            var totalReadingTimeSeconds = totalWords / wordsPerSecond;
115

116
            //define reading time in minutes
117
            //if round is set to true
118
            if(round === true) {
119

120
                var readingTimeMinutes = Math.round(totalReadingTimeSeconds / 60);
121

122
            //if round is set to false
123
            } else {
124

125
                var readingTimeMinutes = Math.floor(totalReadingTimeSeconds / 60);
126

127
            }
128

129
            //define remaining reading time seconds
130
            var readingTimeSeconds = Math.round(totalReadingTimeSeconds - readingTimeMinutes * 60);
131

132
            //if round is set to true
133
            if(round === true) {
134

135
                //if minutes are greater than 0
136
                if(readingTimeMinutes > 0) {
137

138
                    //set reading time by the minute
139
                    $(readingTimeTarget).text(prependTimeString + readingTimeMinutes + ' ' + minShortForm);
140

141
                } else {
142

143
                    //set reading time as less than a minute
144
                    $(readingTimeTarget).text(prependTimeString + lessThanAMinute);
145

146
                }
147

148
            //if round is set to false
149
            } else {
150

151
                //format reading time
152
                var readingTime = readingTimeMinutes + ':' + readingTimeSeconds;
153

154
                //set reading time in minutes and seconds
155
                $(readingTimeTarget).text(prependTimeString + readingTime);
156

157
            }
158

159
            //if word count container isn't blank or undefined
160
            if(wordCountTarget !== '' && wordCountTarget !== undefined) {
161

162
                //set word count
163
                $(wordCountTarget).text(prependWordString + totalWords);
164

165
            }
166

167
        };
168

169
        //for each element
170
        el.each(function() {
171

172
            //if remotePath and remoteTarget aren't null
173
            if(remotePath != null && remoteTarget != null) {
174

175
                //get contents of remote file
176
                $.get(remotePath, function(data) {
177

178
                    //set time using the remote target found in the remote file
179
                    setTime($('<div>').html(data).find(remoteTarget).text());
180

181
                });
182

183
            } else {
184

185
                //set time using the targeted element
186
                setTime(el.text());
187

188
            }
189

190
        });
191

192
    }
193

194
})(jQuery);

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.