Added functionality to display which view is currently active.
[taboo.git] / chrome / content / calendar.js
blobb5a0b765240225de4879575d39130b5405255627
1 /*
2 * Copyright 2007 Jesse Andrews, Manish Singh, Ian Fischer
4 * This file may be used under the terms of of the
5 * GNU General Public License Version 2 or later (the "GPL"),
6 * http://www.gnu.org/licenses/gpl.html
8 * Software distributed under the License is distributed on an "AS IS" basis,
9 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10 * for the specific language governing rights and limitations under the
11 * License.
14 function daysOf(year, month) {
15 // determine number of days in month
16 // by adding 1 month, then subtracting 1 second
17 // and looking at the current date
19 var date = new Date(year, month+1, 1);
20 return new Date(date - 1).getDate();
23 function Calendar(container) {
24 var self=this;
25 document.body.className = 'calendar';
27 var table = document.createElement('table');
28 container.appendChild(table);
30 function calDB() {
31 var _db = {};
33 this.add = function(tab) {
34 var date = new Date(tab.updated);
35 var Y = date.getFullYear();
36 var M = date.getMonth();
37 var D = date.getDate();
38 if (!_db[Y]) { _db[Y] = {}; }
39 if (!_db[Y][M]) { _db[Y][M] = {}; }
40 if (!_db[Y][M][D]) { _db[Y][M][D] = []; }
41 _db[Y][M][D].push(tab);
44 this.clear = function() {
45 _db = {};
48 this.getTabs = function(Y,M,D) {
49 try {
50 return _db[Y][M][D];
52 catch (e) {}
56 var db = new calDB();
58 function addTabsToTD(year, month, date, td) {
59 var tabs = db.getTabs(year, month, date);
60 if (tabs && tabs.length > 0) {
61 var img = document.createElement('img');
62 img.setAttribute('src', tabs[0].thumbURL);
63 td.appendChild(img);
64 td.onclick = function() {
65 var div = document.createElement('div');
66 div.setAttribute('class', 'tabs');
67 tabs.forEach(function(tab) {
68 var img = document.createElement('img');
69 img.setAttribute('src', tab.thumbURL);
70 img.setAttribute('title', tab.title);
71 img.onclick = function(event) {
72 SVC.open(tab.url, whereToOpenLink(event));
74 div.appendChild(img);
75 });
76 container.appendChild(div);
77 var remover = function(event) {
78 if (event.target != div) {
79 container.removeChild(div);
80 document.removeEventListener('click', remover, true);
83 document.addEventListener('click', remover, true);
86 else {
87 td.setAttribute('class', 'empty')
88 td.appendChild(document.createTextNode(date));
92 this.start = function() {
93 db.clear();
94 container.removeChild(table);
95 table = document.createElement('table');
96 container.appendChild(table);
99 this.finish = function(year, month) {
100 var year = year || new Date().getFullYear(); // default to current year
101 var month = month || new Date().getMonth(); // default to current month
102 var days = daysOf(year, month);
104 table.innerHTML = "<tr><th colspan='7' id='date_nav'><span id='nav_left'>&larr;</span>" + (month+1) + ' / ' + year + '<span id="nav_right">&rarr;</span></th></tr>' +
105 "<tr><th>SUN</th><th>MON</th><th>TUE</th><th>WED</th><th>THUR</th><th>FRI</th><th>SAT</th></tr>";
107 var left = document.getElementById('nav_left');
108 left.onclick = function() {
109 var new_month = month - 1;
110 var new_year = year;
111 if (new_month < 0) {
112 new_month = 11;
113 new_year = year - 1;
115 self.finish(new_year, new_month);
118 var right = document.getElementById('nav_right');
119 right.onclick = function() {
120 var new_month = month + 1;
121 var new_year = year;
122 if (new_month > 11) {
123 new_month = 0;
124 new_year = year + 1;
126 self.finish(new_year, new_month);
129 var tr = null;
131 for (var date=1; date<=days; date++) {
132 var curDate = new Date(year, month, date);
134 if (!tr) {
135 tr = document.createElement('tr');
136 for (var i=0; i<curDate.getDay(); i++) {
137 var td = document.createElement('td');
138 td.setAttribute('class', 'blank')
139 tr.appendChild(td);
141 table.appendChild(tr);
144 var td = document.createElement('td');
146 addTabsToTD(year, month, date, td);
148 tr.appendChild(td);
149 if (curDate.getDay() == 6) {
150 tr = null;
155 this.add = function(tab) {
156 db.add(tab);