Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e068451

Browse files
committed
timeago 升级
1 parent d73341e commit e068451

File tree

4 files changed

+77
-32
lines changed

4 files changed

+77
-32
lines changed

static/js/common.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,12 @@ SG.registerAtEvent = function(isAt, isEmoji, selector) {
173173
}
174174

175175
jQuery(document).ready(function($) {
176-
// timeago:3 天之内才显示 timeago
176+
// timeago:100 天之内才显示 timeago
177+
$.timeago.settings.cutoff = 1000*60*60*24*100;
177178

179+
// 历史原因,其他 js 使用了。(当时版本 timeago 不支持 cutoff)
178180
// time 的格式 2014-10-02 11:40:01
179181
SG.timeago = function(time) {
180-
var ago = new Date(time),
181-
now = new Date();
182-
183-
if (now - ago > 3 * 86400 * 1000) {
184-
return time;
185-
}
186-
187182
return $.timeago(time);
188183
};
189184

static/js/libs/jquery.timeago.js

100644100755
Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
* updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
44
*
55
* @name timeago
6-
* @version 1.1.0
6+
* @version 1.5.4
77
* @requires jQuery v1.2.3+
88
* @author Ryan McGeary
99
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
1010
*
1111
* For usage and examples, visit:
1212
* http://timeago.yarp.com/
1313
*
14-
* Copyright (c) 2008-2013, Ryan McGeary (ryan -[at]- mcgeary [*dot*] org)
14+
* Copyright (c) 2008-2017, Ryan McGeary (ryan -[at]- mcgeary [*dot*] org)
1515
*/
1616

1717
(function (factory) {
1818
if (typeof define === 'function' && define.amd) {
1919
// AMD. Register as an anonymous module.
2020
define(['jquery'], factory);
21+
} else if (typeof module === 'object' && typeof module.exports === 'object') {
22+
factory(require('jquery'));
2123
} else {
2224
// Browser globals
2325
factory(jQuery);
@@ -39,12 +41,17 @@
3941
$.extend($.timeago, {
4042
settings: {
4143
refreshMillis: 60000,
44+
allowPast: true,
4245
allowFuture: false,
46+
localeTitle: false,
47+
cutoff: 0,
48+
autoDispose: true,
4349
strings: {
4450
prefixAgo: null,
4551
prefixFromNow: null,
4652
suffixAgo: "ago",
4753
suffixFromNow: "from now",
54+
inPast: 'any moment now',
4855
seconds: "less than a minute",
4956
minute: "about a minute",
5057
minutes: "%d minutes",
@@ -60,7 +67,12 @@
6067
numbers: []
6168
}
6269
},
70+
6371
inWords: function(distanceMillis) {
72+
if (!this.settings.allowPast && ! this.settings.allowFuture) {
73+
throw 'timeago allowPast and allowFuture settings can not both be set to false.';
74+
}
75+
6476
var $l = this.settings.strings;
6577
var prefix = $l.prefixAgo;
6678
var suffix = $l.suffixAgo;
@@ -71,6 +83,10 @@
7183
}
7284
}
7385

86+
if (!this.settings.allowPast && distanceMillis >= 0) {
87+
return this.settings.strings.inPast;
88+
}
89+
7490
var seconds = Math.abs(distanceMillis) / 1000;
7591
var minutes = seconds / 60;
7692
var hours = minutes / 60;
@@ -99,12 +115,14 @@
99115
if ($l.wordSeparator === undefined) { separator = " "; }
100116
return $.trim([prefix, words, suffix].join(separator));
101117
},
118+
102119
parse: function(iso8601) {
103120
var s = $.trim(iso8601);
104121
s = s.replace(/\.\d+/,""); // remove milliseconds
105122
s = s.replace(/-/,"/").replace(/-/,"/");
106123
s = s.replace(/T/," ").replace(/Z/," UTC");
107124
s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
125+
s = s.replace(/([\+\-]\d\d)$/," $100"); // +09 -> +0900
108126
return new Date(s);
109127
},
110128
datetime: function(elem) {
@@ -121,36 +139,67 @@
121139
// init is default when no action is given
122140
// functions are called with context of a single element
123141
var functions = {
124-
init: function(){
142+
init: function() {
143+
functions.dispose.call(this);
125144
var refresh_el = $.proxy(refresh, this);
126145
refresh_el();
127146
var $s = $t.settings;
128147
if ($s.refreshMillis > 0) {
129-
setInterval(refresh_el, $s.refreshMillis);
148+
this._timeagoInterval = setInterval(refresh_el, $s.refreshMillis);
149+
}
150+
},
151+
update: function(timestamp) {
152+
var date = (timestamp instanceof Date) ? timestamp : $t.parse(timestamp);
153+
$(this).data('timeago', { datetime: date });
154+
if ($t.settings.localeTitle) {
155+
$(this).attr("title", date.toLocaleString());
130156
}
157+
refresh.apply(this);
131158
},
132-
update: function(time){
133-
$(this).data('timeago', { datetime: $t.parse(time) });
159+
updateFromDOM: function() {
160+
$(this).data('timeago', { datetime: $t.parse( $t.isTime(this) ? $(this).attr("datetime") : $(this).attr("title") ) });
134161
refresh.apply(this);
162+
},
163+
dispose: function () {
164+
if (this._timeagoInterval) {
165+
window.clearInterval(this._timeagoInterval);
166+
this._timeagoInterval = null;
167+
}
135168
}
136169
};
137170

138171
$.fn.timeago = function(action, options) {
139172
var fn = action ? functions[action] : functions.init;
140-
if(!fn){
173+
if (!fn) {
141174
throw new Error("Unknown function name '"+ action +"' for timeago");
142175
}
143176
// each over objects here and call the requested function
144-
this.each(function(){
177+
this.each(function() {
145178
fn.call(this, options);
146179
});
147180
return this;
148181
};
149182

150183
function refresh() {
184+
var $s = $t.settings;
185+
186+
//check if it's still visible
187+
if ($s.autoDispose && !$.contains(document.documentElement,this)) {
188+
//stop if it has been removed
189+
$(this).timeago("dispose");
190+
return this;
191+
}
192+
151193
var data = prepareData(this);
194+
152195
if (!isNaN(data.datetime)) {
153-
$(this).text(inWords(data.datetime));
196+
if ( $s.cutoff === 0 || Math.abs(distance(data.datetime)) < $s.cutoff) {
197+
$(this).text(inWords(data.datetime));
198+
} else {
199+
if ($(this).attr('title').length > 0) {
200+
$(this).text($(this).attr('title'));
201+
}
202+
}
154203
}
155204
return this;
156205
}
@@ -160,7 +209,9 @@
160209
if (!element.data("timeago")) {
161210
element.data("timeago", { datetime: $t.datetime(element) });
162211
var text = $.trim(element.text());
163-
if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
212+
if ($t.settings.localeTitle) {
213+
element.attr("title", element.data('timeago').datetime.toLocaleString());
214+
} else if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
164215
element.attr("title", text);
165216
}
166217
}

static/js/libs/jquery.timeago.zh-CN.js

100644100755
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
// Simplified Chinese
22
jQuery.timeago.settings.strings = {
33
prefixAgo: null,
4-
prefixFromNow: null,
5-
suffixAgo: "",
6-
suffixFromNow: "刚刚",
4+
prefixFromNow: "从现在开始",
5+
suffixAgo: "之前",
6+
suffixFromNow: null,
77
seconds: "不到1分钟",
8-
minute: "约1分钟",
8+
minute: "大约1分钟",
99
minutes: "%d分钟",
10-
hour: "约1小时",
11-
hours: "%d小时",
10+
hour: "大约1小时",
11+
hours: "大约%d小时",
1212
day: "1天",
1313
days: "%d天",
14-
month: "约1个月",
14+
month: "大约1个月",
1515
months: "%d月",
16-
year: "约1年",
16+
year: "大约1年",
1717
years: "%d年",
1818
numbers: [],
19-
wordSeparator: "",
20-
formatter: function(prefix, words, suffix) { return [prefix, words, suffix].join(""); }
21-
};
19+
wordSeparator: ""
20+
};

template/common/layout.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@
239239
<script type="text/javascript" src="/static/js/libs/jquery-1.11.1.min.js"></script>
240240
<!--<script src="//apps.bdimg.com/libs/bootstrap/3.2.0/js/bootstrap.min.js"></script>-->
241241
<script type="text/javascript" src="/static/js/libs/bootstrap-3.2.0.min.js"></script>
242-
<script src="/static/js/libs/jquery.timeago.js"></script>
243-
<script src="/static/js/libs/jquery.timeago.zh-CN.js"></script>
242+
<script src="/static/js/libs/jquery.timeago.js?v=1.5.4"></script>
243+
<script src="/static/js/libs/jquery.timeago.zh-CN.js?v=1.5.4"></script>
244244
<script src="/static/js/libs/md5.js"></script>
245245
<script type="text/javascript">
246246
var isHttps = {{.is_https}},
@@ -252,7 +252,7 @@
252252
}
253253
var GLaunchTime = {{timestamp .app.LaunchTime}}*1000;
254254
</script>
255-
<script src="/static/js/common.js?v=1.1"></script>
255+
<script src="/static/js/common.js?v=1.2"></script>
256256
{{template "js" .}}
257257
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jsrender/0.9.84/jsrender.min.js"></script>
258258
<script type="text/javascript">

0 commit comments

Comments
 (0)