|
本館粉絲專頁 |
|
|
使用者:Digipunk/FC/func.js:修訂版本之間的差異
台灣棒球維基館
< 使用者:Digipunk | FC
小 (test) |
|||
(由1名用戶作出的12個中途修訂版本未被顯示) | |||
第1行: | 第1行: | ||
− | + | /* | |
− | + | Firefox: new Date(str) 的 str 需為 y-m-d | |
− | + | */ | |
− | + | ||
− | + | var DGPKJS_ymp = []; // 避免在同一頁重複執行相同月份的 ymp | |
− | + | ||
− | + | ||
//-------------------------------------------------- | //-------------------------------------------------- | ||
第12行: | 第10行: | ||
function DGPKJS_FC() { | function DGPKJS_FC() { | ||
var FC = document.getElementsByClassName('DGPK-FC-JS'); | var FC = document.getElementsByClassName('DGPK-FC-JS'); | ||
− | for (var i | + | for (var i = FC.length - 1; i >= 0; i--) { |
try { | try { | ||
var attr = JSON.parse(FC[i].innerText.replace(/[\r\n]/g, '')); | var attr = JSON.parse(FC[i].innerText.replace(/[\r\n]/g, '')); | ||
第35行: | 第33行: | ||
attr.id = (attr.id || 'DGPK-FC') + '-' + i; | attr.id = (attr.id || 'DGPK-FC') + '-' + i; | ||
− | __dgpkjs_fc_init(attr) | + | if (__dgpkjs_fc_init(attr) == true) { |
− | + | __dgpkjs_fc_create(attr, FC[i]); | |
+ | } | ||
+ | |||
+ | // FC[i].remove(); | ||
} catch (e) { | } catch (e) { | ||
console.log(e); | console.log(e); | ||
第45行: | 第46行: | ||
function __dgpkjs_fc_init(attr) { | function __dgpkjs_fc_init(attr) { | ||
var tmp, dd; | var tmp, dd; | ||
+ | var ymp_auto = false; | ||
dd = new Date(); | dd = new Date(); | ||
第50行: | 第52行: | ||
attr.yy = null; | attr.yy = null; | ||
− | if ( | + | if (!attr.arg1 && (tmp = attr.pagename.match(/^(\d+)年\/(\d+)月$/)) != null) { |
attr.yy = parseInt(tmp[1]); | attr.yy = parseInt(tmp[1]); | ||
attr.mm = parseInt(tmp[2]); | attr.mm = parseInt(tmp[2]); | ||
+ | attr.ymp = 1; | ||
+ | ymp_auto = true; | ||
} | } | ||
if (attr.yy == null) { | if (attr.yy == null) { | ||
第67行: | 第71行: | ||
// normalize from and to | // normalize from and to | ||
if (attr.from) { | if (attr.from) { | ||
− | + | attr.from = new Date(attr.from); | |
− | + | if (!(attr.from instanceof Date) || isNaN(attr.from)) { | |
− | + | attr.from = null; | |
− | attr.from = | + | |
} | } | ||
} | } | ||
if (attr.to) { | if (attr.to) { | ||
− | + | attr.to = new Date(attr.to); | |
− | + | if (!(attr.to instanceof Date) || isNaN(attr.to)) { | |
− | + | attr.to = null; | |
− | attr.to = | + | |
} | } | ||
} | } | ||
第83行: | 第85行: | ||
// current month | // current month | ||
attr.ym = attr.yy + '/' + attr.mm; | attr.ym = attr.yy + '/' + attr.mm; | ||
+ | |||
+ | if (ymp_auto == true) { | ||
+ | if (DGPKJS_ymp.indexOf(attr.ym) >= 0) { | ||
+ | return false; | ||
+ | } | ||
+ | DGPKJS_ymp.push(attr.ym); | ||
+ | } | ||
// previous month | // previous month | ||
第158行: | 第167行: | ||
} | } | ||
− | attr.first = new Date(attr.ym); | + | attr.first = new Date(attr.ym + '/1'); |
attr.wday = (attr.first.getDay() + 7 - attr.human) % 7; | attr.wday = (attr.first.getDay() + 7 - attr.human) % 7; | ||
attr.first.setDate(1 + offset_first * 7 - attr.wday); | attr.first.setDate(1 + offset_first * 7 - attr.wday); | ||
− | attr.last = new Date(attr['n-ym']); | + | attr.last = new Date(attr['n-ym'] + '/1'); |
attr.last.setDate(0 + offset_last * 7); | attr.last.setDate(0 + offset_last * 7); | ||
第171行: | 第180行: | ||
attr.rows = Math.ceil(((attr.last - attr.first) / (86400 * 1000) + 1) / 7); | attr.rows = Math.ceil(((attr.last - attr.first) / (86400 * 1000) + 1) / 7); | ||
} | } | ||
+ | |||
+ | // title - might contain wiki links | ||
+ | if (attr.title && (tmp = attr.title.match(/\[\[(#[^\|\]]+)\|([^\]]+)\]\]/)) != null) { | ||
+ | attr.title = '<A HREF="' + dgpk_wikilink(tmp[1]) + '">' + tmp[2] + '</A>'; | ||
+ | } | ||
+ | |||
+ | return true; | ||
} | } | ||
第187行: | 第203行: | ||
html += '<TR CLASS="header' + attr.ymp + '"' + stl + '>'; | html += '<TR CLASS="header' + attr.ymp + '"' + stl + '>'; | ||
if (attr.ymp == 1) { // Y/M Page | if (attr.ymp == 1) { // Y/M Page | ||
− | html += '<TD><A HREF="' + attr.localurl + | + | html += '<TD><A HREF="' + dgpk_wikilink(attr.localurl + attr['p-ym'].replace('/', '年/') + '月') + '">←</A></TD>'; |
− | html += '<TD><A HREF="' + attr.localurl + | + | html += '<TD><A HREF="' + dgpk_wikilink(attr.localurl + attr['n-ym'].replace('/', '年/') + '月') + '">→</A></TD>'; |
html += '<TD COLSPAN="3" STYLE="color:white"><tt>' + attr.ym + '</tt></TD>'; | html += '<TD COLSPAN="3" STYLE="color:white"><tt>' + attr.ym + '</tt></TD>'; | ||
− | html += '<TD><A HREF="' + | + | html += '<TD><A HREF="' + dgpk_wikilink('#' + attr.mm + '月28日') + '">▼</A></TD>'; |
html += '<TD><A HREF="#top">▲</A></TD>'; | html += '<TD><A HREF="#top">▲</A></TD>'; | ||
} else { | } else { | ||
html += '<TD COLSPAN="7"' + stl + '>' + (attr.title || '<tt>' + attr.ym + '</tt>'); | html += '<TD COLSPAN="7"' + stl + '>' + (attr.title || '<tt>' + attr.ym + '</tt>'); | ||
if (attr['days-left'] > 0) { | if (attr['days-left'] > 0) { | ||
− | html += '<tt> <A HREF="' + attr.localurl + | + | html += '<tt> <A HREF="' + dgpk_wikilink(attr.localurl + attr.pagename); |
− | html += (attr.localurl.indexOf('?') >= 0 ? '&' : '?') + 'action=purge#' + (attr.id || '') + '" STYLE="color:yellow;font-style:italic">('; | + | html += (attr.localurl.indexOf('?') >= 0 ? '&' : '?') + 'action=purge#' + (attr.id || '') + '"'; |
+ | html += ' STYLE="color:yellow;font-style:italic;' + (attr.st.countdown || '') +'">('; | ||
html += attr['days-left'] + ' day' + (attr['days-left'] > 1 ? 's' : '') + ' left)</A></tt>'; | html += attr['days-left'] + ' day' + (attr['days-left'] > 1 ? 's' : '') + ' left)</A></tt>'; | ||
} | } | ||
第242行: | 第259行: | ||
if (attr.ymp) { | if (attr.ymp) { | ||
if (dd.getMonth() == mm2) { | if (dd.getMonth() == mm2) { | ||
− | html += '<A HREF="' + | + | html += '<A HREF="' + dgpk_wikilink('#' + attr.mm + '月' + day + '日') + '">'; |
html += '<SPAN CLASS="link' + today_cls + '">' + day + '</SPAN></A>'; | html += '<SPAN CLASS="link' + today_cls + '">' + day + '</SPAN></A>'; | ||
} else { | } else { | ||
第261行: | 第278行: | ||
} | } | ||
if (link != null) { | if (link != null) { | ||
− | + | // if (link.substr(0, 1) != '#') link = '#' + link; | |
− | html += '<A HREF="' + | + | if (link.substr(0, 1) != '#' && link.substr(0, 4) != 'http') { |
+ | link = attr.localurl + link; | ||
+ | } | ||
+ | html += '<A HREF="' + dgpk_wikilink(link) + '"><SPAN CLASS="num link' + today_cls + '"'; | ||
html += ' STYLE="' + (attr.st.num || '') + ';' + stl + ';' + today_stl + '">' + day + '</SPAN></A>'; | html += ' STYLE="' + (attr.st.num || '') + ';' + stl + ';' + today_stl + '">' + day + '</SPAN></A>'; | ||
} else { | } else { | ||
− | if (dd >= attr.from && dd <= attr.to) { | + | if (attr.from && attr.to && dd >= attr.from && dd <= attr.to) { |
html += '<SPAN CLASS="num mark' + today_cls + '"'; | html += '<SPAN CLASS="num mark' + today_cls + '"'; | ||
html += ' STYLE="' + (attr.st.num || '') + ';' + (attr.st.mark || '') + ';' + today_stl + '">' + day + '</SPAN>'; | html += ' STYLE="' + (attr.st.num || '') + ';' + (attr.st.mark || '') + ';' + today_stl + '">' + day + '</SPAN>'; | ||
第292行: | 第312行: | ||
document.getElementById('TBL-' + attr.id).style.opacity = 1; | document.getElementById('TBL-' + attr.id).style.opacity = 1; | ||
}, 1); | }, 1); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } |
2021年1月29日 (五) 13:33的最新修訂版本
/* Firefox: new Date(str) 的 str 需為 y-m-d */ var DGPKJS_ymp = []; // 避免在同一頁重複執行相同月份的 ymp //-------------------------------------------------- // FC (floating calendar) //-------------------------------------------------- function DGPKJS_FC() { var FC = document.getElementsByClassName('DGPK-FC-JS'); for (var i = FC.length - 1; i >= 0; i--) { try { var attr = JSON.parse(FC[i].innerText.replace(/[\r\n]/g, '')); for (var j in attr) { if (typeof attr[j] == 'object') { for (var k in attr[j]) { if (!isNaN(attr[j][k])) { attr[j][k] = parseInt(attr[j][k]); } else if (typeof attr[j][k] == 'string') { attr[j][k] = decodeURIComponent(attr[j][k].replace(/\+/g, ' ')).trim(); } } } else { if (!isNaN(attr[j])) { attr[j] = parseInt(attr[j]); } else if (typeof attr[j] == 'string') { attr[j] = decodeURIComponent(attr[j].replace(/\+/g, ' ')).trim(); } } } attr.id = (attr.id || 'DGPK-FC') + '-' + i; if (__dgpkjs_fc_init(attr) == true) { __dgpkjs_fc_create(attr, FC[i]); } // FC[i].remove(); } catch (e) { console.log(e); } } } function __dgpkjs_fc_init(attr) { var tmp, dd; var ymp_auto = false; dd = new Date(); attr.today = new Date(dd.getFullYear() + '/' + (dd.getMonth() + 1) + '/' + dd.getDate()); // to avoid the trouble of 'H:i:s ms' attr.yy = null; if (!attr.arg1 && (tmp = attr.pagename.match(/^(\d+)年\/(\d+)月$/)) != null) { attr.yy = parseInt(tmp[1]); attr.mm = parseInt(tmp[2]); attr.ymp = 1; ymp_auto = true; } if (attr.yy == null) { if (attr.arg1) { tmp = attr.arg1.split('/'); attr.yy = parseInt(tmp[0]); attr.mm = parseInt(tmp[1]); } else { attr.yy = attr.today.getFullYear(); attr.mm = attr.today.getMonth() + 1; } } // normalize from and to if (attr.from) { attr.from = new Date(attr.from); if (!(attr.from instanceof Date) || isNaN(attr.from)) { attr.from = null; } } if (attr.to) { attr.to = new Date(attr.to); if (!(attr.to instanceof Date) || isNaN(attr.to)) { attr.to = null; } } // current month attr.ym = attr.yy + '/' + attr.mm; if (ymp_auto == true) { if (DGPKJS_ymp.indexOf(attr.ym) >= 0) { return false; } DGPKJS_ymp.push(attr.ym); } // previous month if (attr.mm == 1) { attr['p-ym'] = (parseInt(attr.yy) - 1) + '/12'; } else { attr['p-ym'] = attr.yy + '/' + (attr.mm - 1); } // next month if (attr.mm == 12) { attr['n-ym'] = (parseInt(attr.yy) + 1) + '/1'; } else { attr['n-ym'] = attr.yy + '/' + (attr.mm + 1); } // misc attr['days-left'] = 0; if (attr.countdown && attr.from) { attr['days-left'] = Math.floor((attr.from - attr.today) / (86400 * 1000)); if (attr['days-left'] <= 0) attr['days-left'] = 0; } // calc the first and last days of the calendar var offset_first, offset_last; offset_first = 0; offset_last = 0; if (attr.ymp == 1) { attr['show-pn'] = 0; } else { attr['show-pn'] = 1; switch (attr.more) { case 'p': offset_first = -1; break; case 'pp': offset_first = -2; break; case 'n': offset_last = 1; break; case 'nn': offset_last = 2; break; case 'pn': case 'np': offset_first = -1; offset_last = 1; break; default: switch (attr.shift) { case 'p': offset_first = -1; offset_last = -1; break; case 'pp': offset_first = -2; offset_last = -2; break; case 'n': offset_first = 1; offset_last = 1; break; case 'nn': offset_first = 2; offset_last = 2; break; default: attr['show-pn'] = 0; break; } break; } } attr.first = new Date(attr.ym + '/1'); attr.wday = (attr.first.getDay() + 7 - attr.human) % 7; attr.first.setDate(1 + offset_first * 7 - attr.wday); attr.last = new Date(attr['n-ym'] + '/1'); attr.last.setDate(0 + offset_last * 7); if (attr.ymp == 0 && attr['show-pn'] == 0 && attr.row6 == 1) { attr.rows = 6; attr['show-pn'] = 1; } else { attr.rows = Math.ceil(((attr.last - attr.first) / (86400 * 1000) + 1) / 7); } // title - might contain wiki links if (attr.title && (tmp = attr.title.match(/\[\[(#[^\|\]]+)\|([^\]]+)\]\]/)) != null) { attr.title = '<A HREF="' + dgpk_wikilink(tmp[1]) + '">' + tmp[2] + '</A>'; } return true; } function __dgpkjs_fc_create(attr, fc) { var stl, tmp, html = ''; // outer div stl = (attr.st.div ? ' STYLE="' + attr.st.div + '"' : ''); html += '<DIV ID="' + (attr.id || '') + '" CLASS="DGPK-FC DGPK-FC' + attr.ymp + '"' + stl + '>'; html += '<TABLE ID="TBL-' + attr.id + '" BORDER="0" CELLSPACING="0" CELLPADDING="2" STYLE="width:100%;opacity:0.1;transition:opacity 1s">'; //------------------------ // title //------------------------ stl = (attr.st.header ? ' STYLE="' + attr.st.header + '"' : ''); html += '<TR CLASS="header' + attr.ymp + '"' + stl + '>'; if (attr.ymp == 1) { // Y/M Page html += '<TD><A HREF="' + dgpk_wikilink(attr.localurl + attr['p-ym'].replace('/', '年/') + '月') + '">←</A></TD>'; html += '<TD><A HREF="' + dgpk_wikilink(attr.localurl + attr['n-ym'].replace('/', '年/') + '月') + '">→</A></TD>'; html += '<TD COLSPAN="3" STYLE="color:white"><tt>' + attr.ym + '</tt></TD>'; html += '<TD><A HREF="' + dgpk_wikilink('#' + attr.mm + '月28日') + '">▼</A></TD>'; html += '<TD><A HREF="#top">▲</A></TD>'; } else { html += '<TD COLSPAN="7"' + stl + '>' + (attr.title || '<tt>' + attr.ym + '</tt>'); if (attr['days-left'] > 0) { html += '<tt> <A HREF="' + dgpk_wikilink(attr.localurl + attr.pagename); html += (attr.localurl.indexOf('?') >= 0 ? '&' : '?') + 'action=purge#' + (attr.id || '') + '"'; html += ' STYLE="color:yellow;font-style:italic;' + (attr.st.countdown || '') +'">('; html += attr['days-left'] + ' day' + (attr['days-left'] > 1 ? 's' : '') + ' left)</A></tt>'; } html += '</TD>'; } html += '</TR>'; //------------------------ // header //------------------------ var WDAYS = ['日', '一', '二', '三', '四', '五', '六', '日']; stl = (attr.st.wday ? ' STYLE="' + attr.st.wday + '"' : ''); html += '<TR CLASS="wday">'; if (!attr.human) { html += '<TH' + stl + '>日</TH>'; } for (var i = 1; i < 7; i++) { html += '<TH' + stl + '>' + WDAYS[i] + '</TH>'; } if (attr.human) { html += '<TH' + stl + '>日</TH>'; } html += '</TR>'; //------------------------ // day rows //------------------------ var dd = attr.first; var mm = parseInt(attr.mm); var mm2 = mm - 1; var today_cls, today_stl; for (var week = 0; week < attr.rows; week++) { html += '<TR CLASS="days' + attr.ymp + '">'; for (var wday = 0; wday < 7; wday ++) { if (dd.getTime() === attr.today.getTime()) { today_cls = ' today'; today_stl = attr.st.today || ''; } else { today_cls = ''; today_stl = ''; } var day = dd.getDate(); html += '<TD CLASS="day' + (dd.getMonth() != mm2 ? ' day-pn' : '') + '">'; if (attr.ymp) { if (dd.getMonth() == mm2) { html += '<A HREF="' + dgpk_wikilink('#' + attr.mm + '月' + day + '日') + '">'; html += '<SPAN CLASS="link' + today_cls + '">' + day + '</SPAN></A>'; } else { html += ' '; } } else { if (dd.getMonth() == mm2 || attr['show-pn']) { var link = null, stl; if (dd.getMonth() == mm2 && attr.links[day]) { link = attr.links[day]; stl = attr.st.link || ''; } else if (dd.getMonth() < mm2 && attr['p-links'][day]) { link = attr['p-links'][day]; stl = attr.st['link-pn'] || ''; } else if (dd.getMonth() > mm2 && attr['n-links'][day]) { link = attr['n-links'][day]; stl = attr.st['link-pn'] || ''; } if (link != null) { // if (link.substr(0, 1) != '#') link = '#' + link; if (link.substr(0, 1) != '#' && link.substr(0, 4) != 'http') { link = attr.localurl + link; } html += '<A HREF="' + dgpk_wikilink(link) + '"><SPAN CLASS="num link' + today_cls + '"'; html += ' STYLE="' + (attr.st.num || '') + ';' + stl + ';' + today_stl + '">' + day + '</SPAN></A>'; } else { if (attr.from && attr.to && dd >= attr.from && dd <= attr.to) { html += '<SPAN CLASS="num mark' + today_cls + '"'; html += ' STYLE="' + (attr.st.num || '') + ';' + (attr.st.mark || '') + ';' + today_stl + '">' + day + '</SPAN>'; } else { html += '<SPAN CLASS="num' + today_cls + '"'; html += ' STYLE="' + (attr.st.num || '') + ';' + today_stl + '">' + day + '</SPAN>'; } } } else { html += ' '; } } html += '</TD>'; dd.setDate(day + 1); } html += '</TR>'; } // done html += '</TABLE>'; html += '</DIV>'; fc.insertAdjacentHTML("afterend", html); setTimeout(function() { document.getElementById('TBL-' + attr.id).style.opacity = 1; }, 1); }