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

删除320字节 、​ 2021年2月10日 (三) 06:40
无编辑摘要
 
(未显示同一用户的5个中间版本)
//<nowiki>
// 用于[[Widget:Countdown]],可以使用ES6语法
/**
* @Function: 生成倒计时
* @Source: [[moegirl:Widget:Countdown]]
* @Dependencies: moment, ext.gadget.site-lib
* @EditedBy: [[User:Bhsd]]
*/
"use strict";
/*global mw, $, moment, wgUCS*/
(function() => {
let counter, $num;
const monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始
const fromNow = function(ele) { // 处理一个 .counting 节点,显示时间
const now$ele = moment$(this),
thennow = ele.datamoment('target'), // now必须是一个新的moment对象
beforethen = moment( $ele.childrendata('target').first( ), // 必需复制一个moment对象再进行操作
after = ele.children().last(),
isBefore = then.isBefore( now ),
[early, late] = isBefore ? [then, now] : [now, then],
textMonthyear = wgUCSlate.diff('个月' early, '個月year' ),
textHourmonth = wgUCSlate.diff( early.add(year, '小时year'), '小時month' );,
let year day = isBeforeyear ? now.year()null -: thenlate.yeardiff() : thenearly.yearadd(month, 'month'), -'day' now.year(),
monthhour = isBeforeyear ?|| now.month() -? then.month()null : thenlate.monthdiff( early.add(day, 'day'), -'hour' now.month(),
dayminute = isBeforeyear || month || day ? now.date()null -: thenlate.datediff() : thenearly.dateadd(hour, 'hour'), -'minute' now.date(),
hoursecond = isBeforeyear || month || day ? now.hour()null -: thenlate.hourdiff() : thenearly.houradd(minute, 'minute'), -'second' now.hour(),
result = "";[
minute = isBefore ? now.minute() - then.minute() : then.minute() - now.minute(),
second... = isBeforeyear ? now[$num.secondclone() - then.secondtext() :year then.second(), -'年'] now.second(): [],
... month ? [$num.clone().text( month ), mw.msg('widget-cd-mm')] : [],
result = "";
... day ? [$num.clone().text( day ), '天'] : [],
if (second < 0) {
... hour ? [$num.clone().text( hour ), mw.msg('widget-cd-hh')] : [],
minute--;
... minute !== null ? [$num.clone().text( minute ), '分'] : [],
second += 60;
... second !== null ? [$num.clone().text( second ), '秒'] : []
}
if (minute < 0) {];
if $ele.toggleClass('isBefore', isBefore).children().eq(isBefore {? before0 : 1).find( '.countdown' ).html( result ); }
hour--;
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 = () => { $( '.counting' ).each( fromNow ); }, // 每秒循环一遍所有 .counting 节点
run = function() {
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' ); }
// 以本地时间替换title,之后交给[[Widget:Abbr]]就好
.attr('title', then.format( 'llll ([UTC]Z' ).slice(0, -3) + ')');
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
counter = counter || setInterval(run, 1000); // 防止重复setInterval
$content.find( '.counting' ).removeClass( 'countdownNode' );
mw.countdown = mw.countdown || setInterval(() => { run(); }, 1000 );
});
},
ifhandler (hour= < 0() => {
$num result += `$('<span>', {class=": 'countdown-num">${second'}</span>秒`);
mw.widget = mw.widget || {};
if (mw.widget.countdown) { return; } // 不要和mw.countdown搞混
mw.hook( 'wikipage.content' ).add($content => { main($content); });
mw.widget.countdown = true;
};
if (!window.jQuery) { returnhandler(); }
console.log( 'setInterval: 等待jQuery加载完毕' );
else { window.addEventListener( 'jquery', handler ); }
const timerStart = Date.now(),
timer = setInterval(() => {
if (!window.jQuery) { return; }
clearInterval(timer);
console.log(`End setInterval: jQuery加载完毕,用时 ${Date.now() - timerStart} ms`);
mw.hook( 'wikipage.content' ).add($content => { main($content); });
}, 500);
}) ();
//</nowiki>
// [[category:jQuery小部件]] {{DEFAULTSORT:Countdown.js}}
16,874

个编辑