MediaWiki:Common.js
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
/* 倒计时 */
(function ($) {
'use strict';
var countdowns = [];
var NO_LEADING_ZEROS = 1,
SHORT_FORMAT = 2,
NO_ZEROS = 4;
function formatTime(unit, value, isShort) {
if (isShort) {
return value + ' ' + (unit === 'day' ? 'd' : unit[0]);
}
return value + ' ' + unit;
}
function output(i, diff) {
var delta, result, parts = [];
var isShort = Boolean(countdowns[i].opts & SHORT_FORMAT);
delta = diff % 60;
parts.unshift(formatTime('秒', delta, isShort));
diff = Math.floor(diff / 60);
delta = diff % 60;
parts.unshift(formatTime('分', delta, isShort));
diff = Math.floor(diff / 60);
delta = diff % 24;
parts.unshift(formatTime('小时', delta, isShort));
diff = Math.floor(diff / 24);
parts.unshift(formatTime('天', diff, isShort));
if (countdowns[i].opts & NO_LEADING_ZEROS) {
while (parts.length && parts[0].startsWith('0 ')) {
parts.shift();
}
}
if (countdowns[i].opts & NO_ZEROS) {
parts = parts.filter(function (part) {
return !part.startsWith('0 ');
});
}
if (parts.length) {
if (isShort) {
countdowns[i].node.text(parts.join(' '));
} else {
// 不在分和秒之间加 "和"
countdowns[i].node.text(parts.join(', '));
}
} else {
countdowns[i].node.text('0 秒');
}
}
function end(i) {
var c = countdowns[i].node.parent();
switch (c.data('end')) {
case 'remove':
c.remove();
return true;
case 'stop':
output(i, 0);
return true;
case 'toggle':
var toggle = c.data('toggle');
if (toggle && toggle === 'next') {
c.next().show();
c.hide();
return true;
}
if (toggle && $(toggle).length) {
$(toggle).show();
c.hide();
return true;
}
break;
case 'callback':
var callback = c.data('callback');
if (callback && typeof module[callback] === 'function') {
output(i, 0);
module[callback].call(c);
return true;
}
break;
}
countdowns[i].countup = true;
output(i, 0);
return false;
}
function update() {
var now = Date.now();
var countdownsToRemove = [];
$.each(countdowns.slice(0), function (i, countdown) {
var diff = Math.floor((countdown.date - now) / 1000);
if (diff <= 0 && !countdown.countup) {
if (end(i)) countdownsToRemove.push(i);
} else {
output(i, diff);
}
});
var x;
while ((x = countdownsToRemove.pop()) !== undefined) {
countdowns.splice(x, 1);
}
if (countdowns.length) {
setTimeout(update, 1000);
}
}
function getOptions(node) {
var text = node.parent().data('options'),
opts = 0;
if (text) {
if (/no-leading-zeros/.test(text)) {
opts |= NO_LEADING_ZEROS;
}
if (/short-format/.test(text)) {
opts |= SHORT_FORMAT;
}
if (/no-zeros/.test(text)) {
opts |= NO_ZEROS;
}
}
return opts;
}
function init() {
var countdown = $('.countdown:not(.handled)');
if (!countdown.length) return;
$('.nocountdown').hide();
countdown
.show()
.find('.countdowndate')
.each(function () {
var $this = $(this),
date = (new Date($this.text())).valueOf();
if (isNaN(date)) {
$this.text('错误的日期格式');
return;
}
countdowns.push({
node: $this,
opts: getOptions($this),
date: date,
});
});
countdown.addClass('handled');
if (countdowns.length) {
update();
}
}
// 初始化倒计时
init();
})(jQuery);