本館粉絲專頁

使用者:Digipunk/FC/func.js

分享此網頁到Facebook
分享此網頁到Plurk
分享此網頁到百度搜藏
分享此網頁到Twitter
分享此網頁到Del.icio.us
台灣棒球維基館
< 使用者:Digipunk‎ | FC
在2018年7月26日 (四) 01:19由Digipunk對話 | 貢獻所做的修訂版本

跳轉到: 導覽搜尋

注意:保存設置後,要清掉瀏覽器的緩存才能生效:Mozilla:刷新(或用快鍵Ctrl-R),IE / Opera: Ctrl-F5, Safari: Cmd-R, Konqueror Ctrl-R。

window.addEventListener("load", function(event) {
  try {
    DGPKJS_FC();
  } catch (e) {
    console.log(e);
  }
}, false);
 
//--------------------------------------------------
// FC (floating calendar)
//--------------------------------------------------
function DGPKJS_FC() {
  var FC = document.getElementsByClassName('DGPK-FC-JS');
  for (var i = 0, ii = FC.length; i < ii; 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;
 
      __dgpkjs_fc_init(attr);
      __dgpkjs_fc_create(attr, FC[i]);
    } catch (e) {
      console.log(e);
    }
  }
}
 
function __dgpkjs_fc_init(attr) {
  var tmp, dd;
 
  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.ymp == 1 && !attr.arg1 && (tmp = attr.pagename.match(/^(\d+)年\/(\d+)月/)) != null) {
    attr.yy = parseInt(tmp[1]);
    attr.mm = parseInt(tmp[2]);
  }
  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) {
    try {
      attr.from = new Date(attr.from);
    } catch (e) {
      attr.from = new Date('1970/1');
    }
  }
  if (attr.to) {
    try {
      attr.to = new Date(attr.to);
    } catch (e) {
      attr.to = new Date('1970/1');
    }
  }
 
  // current month
  attr.ym  = attr.yy + '/' + attr.mm;
 
  // 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);
  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']);
  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);
  }
}
 
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="' + attr.localurl + __dgpkjs_fc_wikilink(attr['p-ym'].replace('/', '年/') + '月') + '">←</A></TD>';
    html += '<TD><A HREF="' + attr.localurl + __dgpkjs_fc_wikilink(attr['n-ym'].replace('/', '年/') + '月') + '">→</A></TD>';
    html += '<TD COLSPAN="3" STYLE="color:white"><tt>' + attr.ym + '</tt></TD>';
    html += '<TD><A HREF="' + __dgpkjs_fc_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>&nbsp;<A HREF="' + attr.localurl + __dgpkjs_fc_wikilink(attr.pagename);
      html += (attr.localurl.indexOf('?') >= 0 ? '&' : '?') + 'action=purge#' + (attr.id || '') + '" STYLE="color:yellow;font-style:italic">(';
      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="' + __dgpkjs_fc_wikilink('#' + attr.mm + '月' + day + '日') + '">';
          html += '<SPAN CLASS="link' + today_cls + '">' + day + '</SPAN></A>';
        } else {
          html += '&nbsp;';
        }
      } 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;
            html += '<A HREF="' + __dgpkjs_fc_wikilink(link) + '"><SPAN CLASS="num link' + today_cls + '"';
            html += ' STYLE="' + (attr.st.num || '') + ';' + stl + ';' + today_stl + '">' + day + '</SPAN></A>';
          } else {
            if (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 += '&nbsp;';
        }
      }
      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);
}
 
function __dgpkjs_fc_wikilink(str) {
  var pos = str.indexOf('#');
  if (pos < 0) {
    return encodeURI(str);
  } else {
    return encodeURI(str.substr(0, pos)) + '#' + encodeURIComponent(str.substr(pos + 1)).replace(/%/g, '.');
  }
}