2018-05-04 14:51:41 +08:00
|
|
|
(function($) {
|
|
|
|
$.fn.applyLocalDates = function(repeat) {
|
|
|
|
function _formatTimezone(timezone) {
|
|
|
|
return timezone.replace("_", " ").split("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
function processElement($element, options) {
|
|
|
|
repeat = repeat || true;
|
|
|
|
|
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:45:32 +08:00
|
|
|
var relativeTime;
|
|
|
|
if (options.forceTimezone) {
|
|
|
|
relativeTime = moment
|
|
|
|
.tz(options.date + " " + options.time, options.forceTimezone)
|
|
|
|
.utc();
|
|
|
|
} else {
|
|
|
|
relativeTime = moment.utc(options.date + " " + options.time);
|
|
|
|
}
|
2018-05-04 14:51:41 +08:00
|
|
|
|
2018-10-09 22:45:32 +08:00
|
|
|
if (relativeTime < moment().utc()) {
|
|
|
|
if (options.recurring) {
|
|
|
|
var parts = options.recurring.split(".");
|
|
|
|
var count = parseInt(parts[0], 10);
|
|
|
|
var type = parts[1];
|
|
|
|
var diff = moment().diff(relativeTime, type);
|
|
|
|
var add = Math.ceil(diff + count);
|
2018-05-04 14:51:41 +08:00
|
|
|
|
2018-10-09 22:45:32 +08:00
|
|
|
relativeTime = relativeTime.add(add, type);
|
|
|
|
} else {
|
|
|
|
$element.addClass("past");
|
|
|
|
}
|
2018-05-04 14:51:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var previews = options.timezones.split("|").map(function(timezone) {
|
2018-09-07 11:32:44 +08:00
|
|
|
var dateTime = relativeTime
|
|
|
|
.tz(timezone)
|
|
|
|
.format(options.format || "LLL");
|
|
|
|
|
2018-05-04 14:51:41 +08:00
|
|
|
var timezoneParts = _formatTimezone(timezone);
|
|
|
|
|
|
|
|
if (dateTime.match(/TZ/)) {
|
|
|
|
return dateTime.replace("TZ", timezoneParts.join(": "));
|
|
|
|
} else {
|
|
|
|
var output = timezoneParts[0];
|
|
|
|
if (timezoneParts[1]) {
|
|
|
|
output += " (" + timezoneParts[1] + ")";
|
|
|
|
}
|
|
|
|
output += " " + dateTime;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-11 22:19:40 +08:00
|
|
|
var displayTimezone = moment.tz.guess();
|
|
|
|
var relativeTime = relativeTime.tz(displayTimezone);
|
2018-09-12 21:54:36 +08:00
|
|
|
|
2018-08-30 18:38:05 +08:00
|
|
|
if (
|
|
|
|
options.format !== "YYYY-MM-DD HH:mm:ss" &&
|
|
|
|
relativeTime.isBetween(
|
|
|
|
moment().subtract(1, "day"),
|
|
|
|
moment().add(2, "day")
|
|
|
|
)
|
|
|
|
) {
|
2018-08-30 17:25:36 +08:00
|
|
|
relativeTime = relativeTime.calendar();
|
|
|
|
} else {
|
|
|
|
relativeTime = relativeTime.format(options.format);
|
|
|
|
}
|
2018-05-04 14:51:41 +08:00
|
|
|
|
|
|
|
var html = "<span>";
|
|
|
|
html += "<i class='fa fa-globe d-icon d-icon-globe'></i>";
|
2018-08-27 17:19:30 +08:00
|
|
|
html += "<span class='relative-time'></span>";
|
2018-05-04 14:51:41 +08:00
|
|
|
html += "</span>";
|
|
|
|
|
2018-09-07 11:24:05 +08:00
|
|
|
var joinedPreviews = previews.join("\n");
|
2018-05-22 19:58:06 +08:00
|
|
|
|
2018-09-12 21:54:36 +08:00
|
|
|
var displayedTime = relativeTime.replace(
|
|
|
|
"TZ",
|
2018-10-11 22:19:40 +08:00
|
|
|
_formatTimezone(displayTimezone).join(": ")
|
2018-09-12 21:54:36 +08:00
|
|
|
);
|
|
|
|
|
2018-05-04 14:51:41 +08:00
|
|
|
$element
|
|
|
|
.html(html)
|
2018-05-22 19:58:06 +08:00
|
|
|
.attr("title", joinedPreviews)
|
|
|
|
.attr("data-tooltip", joinedPreviews)
|
2018-10-09 15:04:42 +08:00
|
|
|
.addClass("cooked-date")
|
2018-08-27 17:19:30 +08:00
|
|
|
.find(".relative-time")
|
2018-09-12 21:54:36 +08:00
|
|
|
.text(displayedTime);
|
2018-05-04 14:51:41 +08:00
|
|
|
|
|
|
|
if (repeat) {
|
|
|
|
this.timeout = setTimeout(function() {
|
|
|
|
processElement($element, options);
|
|
|
|
}, 10000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.each(function() {
|
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
var options = {};
|
|
|
|
options.format = $this.attr("data-format");
|
|
|
|
options.date = $this.attr("data-date");
|
|
|
|
options.time = $this.attr("data-time");
|
|
|
|
options.recurring = $this.attr("data-recurring");
|
2018-10-12 05:41:57 +08:00
|
|
|
options.timezones = $this.attr("data-timezones");
|
2018-09-12 21:54:36 +08:00
|
|
|
options.forceTimezone = $this.attr("data-force-timezone");
|
2018-05-04 14:51:41 +08:00
|
|
|
|
|
|
|
processElement($this, options);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
})(jQuery);
|