縮小│font12
原始│font14
放大│font16
我的Flickr
我的收音機
我的地圖

日曆

<<             >>

2007年7月25日 星期三

為部落格加入日曆功能

在網路上看見部落格中有 "Archive Calendar"日曆的功能,於是搜尋引擎Google出動開始找尋相關資料終於發現,Abin's Tech Note的部落格中有介紹於是二話不說立即套用看看。

首先,在你的 Sidebar 上面要至少有一個 "Blog 存檔" (Archive) 的網頁元素 (Element),接下來去修改它的屬性,存檔頻率改成「每日」,勾選「先顯示最舊的文章」,樣式設成「Flat 清單」。因為 "Blog 存檔" 這個 Element 「看起來」只允許你存在一個,如果你想要有日曆而且原有的 Archive 也想保留,那就要去修改 HTML(不要展開小裝置範本),把下面這行複製一份,然後改一下 id(不重複就行),你就可以有兩個 "Blog 存檔",把新增的那個 Element 改成日曆就行了。

說的簡單做也不難可以試試看。
先找到下列語法然後COPY在貼於它的下一行記得要改一下 id
<b:widget id='BlogArchive1' locked='false' title='ARCHIVED POSTS' type='BlogArchive'/>
步驟二
接下來可能有點難
Calendar 用的 CSS (放在 標籤內就行,就是定義樣版的區段裡,請依照個人樣版修改,這次修改算是大變動,記得先完整備份你的 Template,這次要展開整個範本進行修改
/* Archive Calendar Styles */

#Calendar {

margin: 0px;

}

#CalendarTable table {

border-collapse: collapse;

padding: 0px;


border: 0px;

}

#CalendarTable table th {

padding: 1px;

color: $sidebarcolor;

margin: 0;

}


#CalendarTable table td {

height: 25px;

color: $sidebartextcolor;

text-align: center;

padding: 1px;

margin: 0;


}

#CalendarTable table td a {

display: block;

}

#CalendarTable .Today {

color: #fff;

background: $sidebarcolor;


}

#CalendarTable .Today a {

color: #fff;

}

#CalendarTable .Weekend {

color: #997777;

}

#Calendar .act {


color: #fff;

padding: 4px;

}


步驟三 貼入一堆 Javascript。(一樣是貼在 標籤裡,如果你之前也有放在一起就好)
<script type='text/javascript'>

//<![CDATA[

<!-- Script functions for generating Archive Calendar: ArchiveCalendar(), archiveurl(), YearMonth(), Calendar(), PrevMonth(), NextMonth(), function GoToday(), refreshTable() -->

function ArchiveCalendar() {

this.PrevMonth = PrevMonth;

this.NextMonth = NextMonth;


this.GoToday = GoToday;

this.refreshTable = refreshTable;

this.YearMonth = YearMonth;

this.Calendar = Calendar;

this.archiveurl = archiveurl;

this.week_label = new Array("一","二","三","四","五","六","日");


this.month_label = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");

this.month_days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

this.today = new Date();

this.cur_day = new Date();

this.blogarchivename = new Array ();

this.thisarchive = 0;


this.base = '';

}



function archiveurl(idx) {

url = this.base;

url += this.blogarchivename[idx].slice(0, 4) + '_' + this.blogarchivename[idx].slice(4, 6) + '_' + this.blogarchivename[idx].slice(6) + '_archive.html';

return url;


}



function YearMonth(date_obj) {

year = date_obj.getFullYear();

thisMonth = this.month_label[date_obj.getMonth()];

out = year+'-'+thisMonth;

return out;


}



function Calendar(date_obj)

{

year = date_obj.getFullYear();

thisDay = this.today.getDate();

thisMonth = this.month_label[date_obj.getMonth()];

nDays = this.month_days[date_obj.getMonth()];


if (date_obj.getMonth() == 1 &&(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))

nDays = 29;

IsitNow = date_obj;

IsitNow.setDate(1);

startDay = IsitNow.getDay() - 1;


if (startDay < 0) startDay = 6;

var out='';

out+='<table>';

out+='<tr>';


for (var index=0;index<7;index++)

out+='<th style="width:25px;">'+ this.week_label[index]+'</th>';

out+='</tr>';

thisarchive=0;


while (thisarchive <= this.blogarchivename.length -1)

{

if(this.blogarchivename[thisarchive].slice(0,4) == year && this.blogarchivename[thisarchive].slice(4,6) == thisMonth)

break;

else


thisarchive++;

}

var tab_col=0;

for (index=0;index<startDay;index++) {

if (tab_col==0)

out+='<tr>';


out+='<td> </td>';

tab_col++;

}

var archiveday;

for (index=1;index<=nDays;index++) {


if (index<10) {

index_day = new Date(year+'/'+thisMonth+'/0'+index+' 00:01');

archiveday = '0' + index;

}

else {

index_day = new Date(year+'/'+thisMonth+'/'+index+' 00:01');


archiveday = index;

}

if (tab_col==0)

out+='<tr>';

if (index==thisDay && this.today.getMonth()==date_obj.getMonth() &amp;& this.today.getFullYear()==date_obj.getFullYear()) {


out+='<td class="Today">';

if (thisarchive < this.blogarchivename.length && this.blogarchivename[thisarchive].slice(4,6)==thisMonth &amp;& this.blogarchivename[thisarchive].slice(6,8)==archiveday) {

out+='<a href="'+this.archiveurl(thisarchive)+'" target="_top">'+index+'</a>';


thisarchive++;

}

else

out+=index;

out+='</td>';


}

else {

if (tab_col < 5)

out+='<td>';

else


out+='<td class="Weekend">';

if (thisarchive < this.blogarchivename.length && this.blogarchivename[thisarchive].slice(4,6)==thisMonth &amp;& this.blogarchivename[thisarchive].slice(6,8)==archiveday) {

out+='<a href="'+this.archiveurl(thisarchive)+'" target="_top">'+index+'</a>';


thisarchive++;

}

else

out+=index;

out+='</td>';


}

if (tab_col==6) {

out+='</tr>';

tab_col=0;

}


else

tab_col++;

}

if (tab_col>0) {

for (var si=0;si<(7-tab_col);si++) {


out+='<td> </td>';

}

out+='</tr>';

}


out+='</table>';

return out;

}



function PrevMonth() {

thisMonth = this.cur_day.getMonth()-1;

year = this.cur_day.getFullYear();


if (thisMonth<0) {

thisMonth = 11;

year = year-1;

}

thisMonth = this.month_label[thisMonth];

this.cur_day = new Date(year+'/'+thisMonth+'/1 00:01');


this.refreshTable();

}



function NextMonth() {

thisMonth = this.cur_day.getMonth()+1;

year = this.cur_day.getFullYear();

if (thisMonth>11) {


thisMonth = 0;

year = year+1;

}

thisMonth = this.month_label[thisMonth];

this.cur_day = new Date(year+'/'+thisMonth+'/1 00:01');

this.refreshTable();


}



function GoToday() {

this.cur_day = new Date();

this.refreshTable();

}



function refreshTable() {

document.getElementById('CalendarMonth').innerHTML = this.YearMonth(this.cur_day);


document.getElementById('CalendarTable').innerHTML = this.Calendar(this.cur_day);

}

//]]>

</script>


最後一個步驟,要修改範本顯示的版面!找到下面這一段:(如果你有兩個 Archive,要確認你找到的是要改的那一個)先了解範本顯示的版面位置去推算就可以知道。
<b:includable id='flat' var='data'>

<ul>

<b:loop values='data:data' var='i'>

<li class='archivedate'>

<a expr:href='data:i.url'><data:i.name/></a> (<data:i.post-count/>)


</li>

</b:loop>

</ul>

</b:includable>


整個取代改成下面這一段:
<b:includable id='flat' var='data'>

<!-- Generate Archive Calendar -->

<center>

<table border='0' cellpadding='0' cellspacing='0' id='Calendar'>

<caption>

<a href='javascript:;' onclick='Calendar.PrevMonth();' title='Previous Month'>&lt;< </a>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;


<a href='javascript:;' onclick='Calendar.GoToday();' title='Back to Today'> <span id='CalendarMonth'/> </a>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;

<a href='javascript:;' onclick='Calendar.NextMonth();' title='Next Month'> &gt;></a>


</caption>

<tr>

<td class='act' id='CalendarTable'> </td>

</tr>

</table>


<script type='text/javascript'>

Calendar = new ArchiveCalendar();

Calendar.base = &#39;http://你的部落格網址/&#39;;

var offset = Calendar.base.length;

<b:loop values='data:data' var='i'>


archUrl = &#39;<data:i.url/>&#39;.slice(offset);

Calendar.blogarchivename.push(archUrl.slice(0,4) + archUrl.slice(5,7) + archUrl.slice(8,10));

</b:loop>

Calendar.refreshTable();

</script>


</center>

</b:includable>

範本存檔,然後預覽看看,如果日曆有出來,就可以搞定收工。

0 Comments: