-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharabic-timeago.js
217 lines (196 loc) · 8 KB
/
arabic-timeago.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2015, AliOsm
// https://fb.com/aliosm97
// https://github.com/AliOsm/arabic-timeago
// Released under the MIT license.
// https://github.com/pragmaticly/smart-time-ago/blob/master/LICENSE
(function() {
var TimeAgo;
TimeAgo = (function() {
function TimeAgo(element, options) {
this.startInterval = 60000;
this.init(element, options);
}
TimeAgo.prototype.init = function(element, options) {
this.$element = $(element);
this.options = $.extend({}, $.fn.timeago.defaults, options);
this.updateTime();
return this.startTimer();
};
TimeAgo.prototype.startTimer = function() {
var self;
self = this;
return this.interval = setInterval((function() {
return self.refresh();
}), this.startInterval);
};
TimeAgo.prototype.stopTimer = function() {
return clearInterval(this.interval);
};
TimeAgo.prototype.restartTimer = function() {
this.stopTimer();
return this.startTimer();
};
TimeAgo.prototype.refresh = function() {
this.updateTime();
return this.updateInterval();
};
TimeAgo.prototype.updateTime = function() {
var self;
self = this;
return this.$element.findAndSelf(this.options.selector).each(function() {
var timeAgoInWords;
timeAgoInWords = self.timeAgoInWords($(this).attr(self.options.attr));
return $(this).html(timeAgoInWords);
});
};
TimeAgo.prototype.updateInterval = function() {
var filter, newestTime, newestTimeInMinutes, newestTimeSrc;
if (this.$element.findAndSelf(this.options.selector).length > 0) {
if (this.options.dir === "up") {
filter = ":first";
} else if (this.options.dir === "down") {
filter = ":last";
}
newestTimeSrc = this.$element.findAndSelf(this.options.selector).filter(filter).attr(this.options.attr);
newestTime = this.parse(newestTimeSrc);
newestTimeInMinutes = this.getTimeDistanceInMinutes(newestTime);
if (newestTimeInMinutes >= 0 && newestTimeInMinutes <= 44 && this.startInterval !== 60000) {
this.startInterval = 60000;
return this.restartTimer();
} else if (newestTimeInMinutes >= 45 && newestTimeInMinutes <= 89 && this.startInterval !== 60000 * 22) {
this.startInterval = 60000 * 22;
return this.restartTimer();
} else if (newestTimeInMinutes >= 90 && newestTimeInMinutes <= 2519 && this.startInterval !== 60000 * 30) {
this.startInterval = 60000 * 30;
return this.restartTimer();
} else if (newestTimeInMinutes >= 2520 && this.startInterval !== 60000 * 60 * 12) {
this.startInterval = 60000 * 60 * 12;
return this.restartTimer();
}
}
};
TimeAgo.prototype.timeAgoInWords = function(timeString) {
var absolutTime;
absolutTime = this.parse(timeString);
return "" + this.options.lang.suffix + this.options.lang.prefixes.ago + (this.distanceOfTimeInWords(absolutTime));
};
TimeAgo.prototype.parse = function(iso8601) {
var timeStr;
timeStr = $.trim(iso8601);
timeStr = timeStr.replace(/\.\d\d\d+/, "");
timeStr = timeStr.replace(/-/, "/").replace(/-/, "/");
timeStr = timeStr.replace(/T/, " ").replace(/Z/, " UTC");
timeStr = timeStr.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2");
return new Date(timeStr);
};
TimeAgo.prototype.getTimeDistanceInMinutes = function(absolutTime) {
var timeDistance;
timeDistance = new Date().getTime() - absolutTime.getTime();
return Math.round((Math.abs(timeDistance) / 1000) / 60);
};
TimeAgo.prototype.distanceOfTimeInWords = function(absolutTime) {
var dim;
dim = this.getTimeDistanceInMinutes(absolutTime);
if (dim === 0) {
return "" + this.options.lang.prefixes.lt + " " + this.options.lang.units.minute;
} else if (dim === 1) {
return "" + this.options.lang.prefixes.about + " " + this.options.lang.units.minute;
} else if (dim === 2) {
return "" + this.options.lang.prefixes.about + " " + this.options.lang.units.two_minutes;
} else if (dim >= 3 && dim <= 10) {
return "" + dim + " " + this.options.lang.units.minutes;
} else if (dim >= 11 && dim <= 50) {
return "" + dim + " " + this.options.lang.units.minute;
} else if (dim >= 51 && dim <= 89) {
return "" + this.options.lang.prefixes.about + " " + this.options.lang.units.hour;
} else if (dim >= 90 && dim <= 149) {
return "" + this.options.lang.prefixes.about + " " + this.options.lang.units.two_hours;
} else if (dim >= 150 && dim <= 599) {
return "" + (Math.round(dim / 60)) + " " + this.options.lang.units.hours;
} else if (dim >= 600 && dim <= 1439) {
return "" + (Math.round(dim / 60)) + " " + this.options.lang.units.hour;
} else if (dim >= 1440 && dim <= 2519) {
return "" + this.options.lang.prefixes.about + " " + this.options.lang.units.day;
} else if (dim >= 2520 && dim <= 3959) {
return "" + this.options.lang.prefixes.about + " " + this.options.lang.units.two_days;
} else if (dim >= 3960 && dim <= 14399) {
return "" + (Math.round(dim / 1440)) + " " + this.options.lang.units.days;
} else if (dim >= 14400 && dim <= 43199) {
return "" + (Math.round(dim / 1440)) + " " + this.options.lang.units.day;
} else if (dim >= 43200 && dim <= 86399) {
return "" + this.options.lang.prefixes.about + " " + this.options.lang.units.month;
} else if (dim >= 86400 && dim <= 129599) {
return "" + this.options.lang.prefixes.about + " " + this.options.lang.units.two_months;
} else if (dim >= 129600 && dim <= 431999) {
return "" + (Math.round(dim / 43200)) + " " + this.options.lang.units.months;
} else if (dim >= 432000 && dim <= 525599) {
return "" + (Math.round(dim / 43200)) + " " + this.options.lang.units.month;
} else if (dim >= 525600 && dim <= 655199) {
return "" + this.options.lang.prefixes.about + " " + this.options.lang.units.year;
} else if (dim >= 655200 && dim <= 914399) {
return "" + this.options.lang.prefixes.over + " " + this.options.lang.units.year;
} else if (dim >= 914400 && dim <= 1051199) {
return "" + this.options.lang.prefixes.almost + " " + this.options.lang.units.two_years;
} else if (dim >= 1051200 && dim <= 5184000) {
return "" + (Math.round(dim / 525600)) + " " + this.options.lang.units.years;
} else {
return "" + (Math.round(dim / 525600)) + " " + this.options.lang.units.year;
}
};
return TimeAgo;
})();
$.fn.timeago = function(options) {
if (options == null) {
options = {};
}
return this.each(function() {
var $this, data;
$this = $(this);
data = $this.data("timeago");
if (!data) {
return $this.data("timeago", new TimeAgo(this, options));
} else if (typeof options === 'string') {
return data[options]();
}
});
};
$.fn.findAndSelf = function(selector) {
return this.find(selector).add(this.filter(selector));
};
$.fn.timeago.Constructor = TimeAgo;
$.fn.timeago.defaults = {
selector: 'time.timeago',
attr: 'datetime',
dir: 'up',
lang: {
units: {
second: "ثانية",
two_seconds: "ثانيتين",
seconds: "ثواني",
minute: "دقيقة",
two_minutes: "دقيقتين",
minutes: "دقائق",
hour: "ساعة",
two_hours: "ساعتين",
hours: "ساعات",
day: "يوم",
two_days: "يومين",
days: "أيام",
month: "شهر",
two_months: "شهرين",
months: "أشهر",
year: "سنة",
two_years: "سنتين",
years: "سنوات"
},
prefixes: {
lt: "أقل من",
about: "حوالي",
over: "أكثر من",
almost: "قرابة",
ago: ""
},
suffix: 'منذ '
}
};
}).call(this);