2009-05-14

[筆記] 結合發表文章的日曆模組

今天在瀏覽網站的時候,看到Abin's Tech Note裡面有記載結合發表文章的日曆模組,可以讓我的sidebar變得簡潔點,同時也很方便依照日期找尋文章。
  1. 在]]></b:skin>之前輸入日曆外觀顏色的 CSS 樣式定義,如果對顏色、字型大小、靠左靠右有特別需求的,請自行改下面的樣式。
    /* Feed Calendar Styles */
    #Calendar {
    margin: 0px;
    }
    #Calendar .act {
    color: #fff;
    padding: 4px;
    }
    #CalendarTable table {
    border-collapse: collapse;
    padding: 0px;
    border: 0px;
    }
    #CalendarTable table th {
    padding: 1px;
    color: #777;
    margin: 0;
    }
    #CalendarTable table td {
    height: 25px;
    color: #999;
    text-align: center;
    padding: 1px;
    margin: 0;
    }
    #CalendarTable table td a {
    display: block;
    }
    #CalendarTable .Today {
    color: #fff;
    background: #777;
    }
    #CalendarTable .Today a {
    color: #fff;
    }
    #CalendarTable .Weekend {
    color: #997777;
    }


  2. 在]]></b:skin>之後,</head>之前加入下面的Java Script。
    <script type='text/javascript'>
    //<![CDATA[
    <!-- Script functions for generating Feed Calendar: generateCalendar(), collectPost(), BrowsePrev(), BrowseNext(), BackToday() -->
    var baseURL = '';
    var currentDay = new Date();
    var today = new Date();
    var monthLabels = new Array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12');
    var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    var weekLabels = new Array('一','二','三','四','五','六','日');

    function generateCalendar(){
    var thisYear = currentDay.getFullYear();
    var thisMonth = monthLabels[currentDay.getMonth()];
    var thisDay = today.getDate();
    var nDays = monthDays[currentDay.getMonth()];
    if (currentDay.getMonth() == 1 &&(((thisYear % 4 == 0) && (thisYear % 100 != 0)) || (thisYear % 400 == 0)))
    nDays = 29;
    var IsitNow = currentDay;
    IsitNow.setDate(1);
    var startDay = IsitNow.getDay() - 1;
    if (startDay < 0)
    startDay = 6;
    var sCalendarCode = '<table><tr>';
    for (var index=0;index<7;index++)
    sCalendarCode+='<th style="width:25px;">'+ weekLabels[index]+'</th>';
    sCalendarCode+='</tr>';
    var nTableCol=0;
    for (index=0;index<startDay;index++) {
    if (nTableCol == 0)
    sCalendarCode += '<tr>';
    sCalendarCode+='<td> </td>';
    nTableCol++;
    }
    for (index=1;index<=nDays;index++) {
    if (nTableCol==0)
    sCalendarCode+='<tr>';
    if (index==thisDay && today.getMonth()==currentDay.getMonth() && today.getFullYear()==currentDay.getFullYear())
    sCalendarCode+='<td id="Day'+index+'" class="Today">';
    else {
    if (nTableCol < 5)
    sCalendarCode+='<td id="Day'+index+'">';
    else
    sCalendarCode+='<td id="Day'+index+'" class="Weekend">';
    }
    sCalendarCode+=index;
    sCalendarCode+='</td>';

    if (nTableCol==6) {
    sCalendarCode+='</tr>';
    nTableCol=0;
    }
    else
    nTableCol++;
    }
    if (nTableCol>0) {
    for (index=0;index<(7-nTableCol);index++) {
    sCalendarCode+='<td> </td>';
    }
    sCalendarCode+='</tr>';
    }
    sCalendarCode+='</table>';
    document.getElementById('CalendarTable').innerHTML = sCalendarCode;

    var sFeedURL = baseURL + '/feeds/posts/summary?orderby=published&published-min='+thisYear+'-'+thisMonth+'-01T00:00:00&published-max='+thisYear+'-'+thisMonth+'-31T23:59:59&max-results=50&alt=json-in-script&callback=collectPost';
    var script = document.createElement('script');
    document.getElementById('CalendarCaption').innerHTML = '<span class="loading">Loading <blink>...</blink></span>';
    script.setAttribute('src', sFeedURL);
    script.setAttribute('type', 'text/javascript');
    document.documentElement.firstChild.appendChild(script);
    }

    function collectPost(json) {
    document.getElementById('CalendarCaption').innerHTML = currentDay.getFullYear()+'-'+monthLabels[currentDay.getMonth()];
    var entries = json.feed.entry;
    var nDay = 0, nCount = 0, nActual = 0;
    var posts = new Array();
    for (var i = 0, post; post = entries[i]; i++) {
    nDay = parseInt(post.published.$t.substr(8,2),10);
    if (i>0&&nDay==parseInt(entries[i-1].published.$t.substr(8,2),10)) {
    var actualDay = post.published.$t.substr(0,10);
    var actualTimezone = post.published.$t.substr(23,6);;
    posts[nActual-1][1] = posts[nActual-1][1]+', '+post.title.$t;
    posts[nActual-1][2] = baseURL +'/search?updated-min='+actualDay+'T00%3A00%3A00'+encodeURIComponent(actualTimezone)+'&updated-max='+actualDay+'T23%3A59%3A59'+encodeURIComponent(actualTimezone);
    } else {
    posts[nActual] = new Array(3);
    posts[nActual][0] = nDay;
    posts[nActual][1] = post.title.$t;
    var j = 0;
    while (j < post.link.length && post.link[j].rel != "alternate")
    j++;
    posts[nActual][2] = post.link[j].href;
    nActual++;
    }
    }
    for (i=0;i<nActual;i++) {
    posts[i][1] = posts[i][1].replace('\"', '"').replace('\'', ''');
    document.getElementById('Day'+posts[i][0]).innerHTML = '<a title="'+posts[i][1]+'" href="'+posts[i][2]+'" target="blank_">'+posts[i][0]+'</a>';
    }
    }

    function BrowsePrev() {
    var thisMonth = currentDay.getMonth()-1;
    var thisYear = currentDay.getFullYear();
    if (thisMonth<0) {
    thisMonth = 11;
    thisYear = thisYear-1;
    }
    thisMonth = monthLabels[thisMonth];
    currentDay = new Date(thisYear+'/'+thisMonth+'/1 00:01');
    generateCalendar();
    }

    function BrowseNext() {
    var thisMonth = currentDay.getMonth()+1;
    var thisYear = currentDay.getFullYear();
    if (thisMonth>11) {
    thisMonth = 0;
    thisYear = thisYear+1;
    }
    thisMonth = monthLabels[thisMonth];
    currentDay = new Date(thisYear+'/'+thisMonth+'/1 00:01');
    generateCalendar();
    }

    function BackToday() {
    currentDay = new Date();
    generateCalendar();
    }
    //]]>
    </script>

    樣板原始碼的修改到此先存檔。

  3. 接下來安排這個新日曆模組的位置。換到「網頁元素」的設定,在你想置放日曆的地方新增一個網頁元素,選擇 HTML/JavaScript 類型。接下來給個標題,然後貼入以下的 HTML 程式:
    <center>
    <table border="0" id="Calendar" cellpadding="0" cellspacing="0">
    <caption>
    <a href="#" onclick="javascript:BrowsePrev();return false;" title="Previous Month"><<</a>     
    <a href="#" onclick="javascript:BackToday();return false;" title="Back to Today"> <span id="CalendarCaption"> </span></a>     
    <a href="#" onclick="javascript:BrowseNext();return false;" title="Next Month">>></a>
    </caption>
    <tr>
    <td id="CalendarTable" class="act"> </td>
    </tr>
    </table>
    <script type="text/javascript">
    generateCalendar();
    </script>
    </center>

    存檔以後,就大功告成啦!

>

參考資料:Abin's Tech Note:結合發表文章的日曆模組

標籤:

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁