“User:Bhsd/widget/countdown.js”的版本间差异

删除598字节 、​ 2021年1月30日 (六) 02:06
无编辑摘要
//<nowiki>
// 用于[[Widget:Countdown]],可以使用ES6语法
/**
* @Function: 生成倒计时
* @Source: [[moegirl:Widget:Countdown]]
* @EditedBy: [[User:Bhsd]]
*/
"use strict";
/*global mw, $, moment, wgUCS*/
(function() => {
const mainfromNow = function($content) {
const monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始
fromNowconst $ele = function$(elethis) {,
const now = moment(),
then = $ele.data('target'),
before = ele.children().first(),
after = ele.children().last(),
isBefore = then.isBefore( now ),
[early, late] = isBefore ? [then, now] : [now, then],
textMonth$num = wgUCS$('个月<span>', {class: '個月countdown-num'}),
textHouryear = wgUCSlate.diff('小时' early, '小時year' );,
let year = isBefore ?month now= late.yeardiff() - thenearly.add(year(), : then.'year('), -'month' now.year(),
monthday = isBeforeyear ? now.month()null -: thenlate.monthdiff() : thenearly.add(month(, 'month'), -'day' now.month(),
dayhour = isBeforeyear || month ? now.date()null -: thenlate.datediff() : thenearly.dateadd(day, 'day'), -'hour' now.date(),
hourminute = isBeforeyear || month || day ? now.hour()null -: thenlate.hourdiff() : thenearly.add(hour(, 'hour'), -'minute' now.hour(),
minutesecond = isBeforeyear || month || day ? now.minute()null -: thenlate.minutediff() : thenearly.add(minute(, 'minute'), -'second' now.minute(),
secondresult += 60;[
second = isBefore ? now.second() - then.second() : then.second() - now.second(),
result... =year "";? [$num.clone().text( year ), '年'] : [],
... month ? [$num.clone().text( month ), mw.msg('widget-cd-mm')] : [],
if (second < 0) {
... day ? [$num.clone().text( day ), '天'] : [],
minute--;
... hour ? [$num.clone().text( hour ), mw.msg('widget-cd-hh')] : [],
second += 60;
... minute === null ? [$num.clone().text( minute ), '分'] : [],
}
... second === null ? [$num.clone().text( second ), '秒'] : []
if (minute < 0) {
hour--];
if $ele.toggleClass('isBefore', isBefore).children().eq(isBefore {? before0 : 1).find( '.countdown' ).html( result ); }
minute += 60;
}
if (hour < 0) {
day--;
hour += 24;
}
if (day < 0) {
month--;
if (early.month() === 1) { day += early.year() % 4 === 0 ? 29 : 28; }
else { day += monthsHave31Days.includes( early.month() ) ? 31 : 30; }
}
if (month < 0) {
year--;
month += 12;
}
if (year > 0) { result += `<span class="countdown-num">${year}</span>年`; }
if (month > 0) { result += `<span class="countdown-num">${month}</span>${textMonth}`; }
if (year === 0 && day > 0) { result += `<span class="countdown-num">${day}</span>天`; }
if (year === 0 && month === 0) {
if (hour > 0) { result += `<span class="countdown-num">${hour}</span>${textHour}`; }
if (day === 0) {
if (minute > 0 || hour > 0) { result += `<span class="countdown-num">${minute}</span>分`; }
result += `<span class="countdown-num">${second}</span>秒`;
}
}
ele.toggleClass('isBefore', isBefore);
if (isBefore) { before.find( '.countdown' ).html( result ); }
else { after.find( '.countdown' ).html( result ); }
},
run = function() { $( '.counting' ).each( fromNow ); },
mw.loader.using(main 'ext.gadget.site-lib'= ).then(($content) => {
const $nodes = $content.find( '.countingcountdownNode' ).each(function() { fromNow( $(this) ); });
}if ($nodes.length === 0) { return; }
mw.loader.using( ['moment', 'ext.gadget.site-lib'] ).then(() => {
},
mw.messages.set( wgUCS({ 'widget-cd-error': "时间格式错误!", 'widget-cd-mm': '个月', 'widget-cd-hh': '小时' },
main = function($content) {
{ 'widget-cd-error': "時間格式錯誤!", 'widget-cd-mm': '個月', 'widget-cd-hh': '小時' }) );
if ($content.find( '.countdownNode' ).length === 0) { return; }
mw.loader.using( 'moment' ) $nodes.theneach(function() => {
const $content.find(ele '.countdownNode'= ).each(function$(this) {,
const ele = $(this),
then = moment(this.title, 'YYYY-MM-DD HH:mm Z');
if (then.isValid()) { ele.data('target', then).addClass( 'counting' ); }
else { $ele.toggleClassdata('error countdownNodetarget', then).textaddClass( "(时间格式错误!)"'counting' ); }
.attr('title', then.format( 'YYYY年MM月DD日HH:mm ([UTC]Z)' ));
if (second < 0) { }
else { $ele.toggleClass('error countdownNode').text( mw.msg('widget-cd-error') ); }
});
ifconst ($contentcounting = $nodes.findfilter( '.counting' ).length === 0) { return; }
if (day$counting.length === 0) { return; }
console.log('Hook: wikipage.content,开始处理生成新增的倒计时');
run();
$counting.removeClass( 'countdownNode' ); // 新節點至少要執行過一次run()後才能移除countdownNode類
mw.countdown = mw.countdown || setInterval(() => { run(); }, 1000 ); // 防止重复setInterval
$content.find( '.counting' ).removeClass( 'countdownNode' );
mw.countdown = mw.countdown || setInterval(() => { run(); }, 1000 );
});
};,
console.log( 'setInterval: 等待jQuery加载完毕' );
const timerStart = Date.now(),
timer = setInterval(() => {
if (!window.jQuery) { return; }
clearInterval(timer);
mw.widget = mw.widget || {};
console.log(`End setInterval: jQuery加载完毕,用时 ${Date.now() - timerStart} ms`);
if (mw.hook( 'wikipagewidget.content' countdown).add($content => { main($content)return; });
console.log( 'End setInterval: 等待jQuery加载完毕,开始执行Widget:Countdown' );
mw.hook( 'wikipage.content' ).add( main );
mw.widget.countdown = true;
}, 500);
}) ();
16,874

个编辑