Logo  

Home - Old Man Programmer

Displaying webapps/scheduler/schedule.js

function fillin(t, tab) {
  var color = tab.color;
  for (var i = 0; i < tab.events.length; i++) {
    var e = tab.events[i];
    for(var d = 0; d < 7; d++) {
      if (e.day[d] == 0) continue;
      for(var h = e.start.hour; h <= e.end.hour; h++) {
	smin = (h == e.start.hour? e.start.min/5 : 0);
	emin = (h == e.end.hour? e.end.min/5 : 12);
	for(var m = smin; m < emin; m++) {
	  t[d][h][m] = color;
	}
      }
    }
  }
}

function genTable()
{
  var t = new Array(7);
  for(var d = 0; d < 7; d++) {
    t[d] = new Array(24);
    for(var h = 0; h < 23; h++) {
      t[d][h] = new Array(0,0,0,0,0,0,0,0,0,0,0,0);
    }
  }
  for(var i = 0; i < _tabs.length; i++) {
    if (_tabs[i].name == "Main") continue;
    fillin(t, _tabs[i]);
  }
  return t;
}

function makeTable(w, tab, time) {
  var days = new Array("Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat");
  var header = true;
  for(var i = 0; i < tab.events.length; i++) {
    if (i) w.document.writeln("</table>", "<table id='schedule'>");
    
    var e = tab.events[i];
    w.document.write("<thead>\n", "<th></th>");
    for(var d = 0; d < 7; d++) {
      if (e.day[d]) w.document.write("<th>" + days[d] + "</th>");
    }
    w.document.writeln("</thead>", "<tbody>");
    for(var h = 0; h < 23; h++) {
      if (h < e.start.hour || h > e.end.hour) continue;
      var hour = h%12;
      if (hour == 0) hour = 12;
      hour += h < 12? " am" : " pm";
      w.document.write("<tr>", "<th rowspan=12>" + hour + "</th>");
      for(var tr = 0; tr < 12; tr++) {
	if (tr) w.document.write("</tr><tr height='5px'>");

	for(var d = 0; d < 7; d++) {
	  if (!e.day[d]) continue;
	  var last = 0;
	  var t = 0;
	  var color = time[d][h][0];
	  for(var m = 0; m < 12; m++) {
	    if (time[d][h][m] != color) {
	      if (last == tr) {
		w.document.write("<td rowspan="+(m-last)+ (color == 0? ">" : " style='background-color:#"+color+";'>"));
	      } else if (t > tr) break;
	      color = time[d][h][m];
	      last = m;
	      t++;
	    }
	  }
	  if (last == tr) {
	    w.document.write("<td rowspan="+(12-last)+ (color == 0? ">" : " style='background-color:#"+color+";'>"));
	  }
	}
      }
      w.document.writeln("</tr>");
    }
    w.document.writeln("</tbody>", "</thead>");
  }
  gen_legend(w);
}

function gen_legend(w) {
  w.document.writeln("</table>");
  w.document.writeln("<table id='legend'>");
  for(var i = 0; i < _tabs.length; i++) {
    var e = _tabs[i];
    if(e.name === "Main") continue;
    w.document.writeln("<tr>", "<td>", e.name, " (", e.description, ")</td>");
    w.document.write("<td width=60px style='background-color:#"+e.color+";'>\n");
    w.document.writeln("</tr>");
  }
  w.document.writeln("</table>");
}

function schedule() {
  if(disable_flag == true) return;
  save_tab();
  var name = document.getElementById("name").value;
  var w = window.open();
  var t = genTable();
  w.document.writeln(
    "<!DOCTYPE html>",
    "<html>",
      "<head>",
        "<title>" + name + "</title>",
	"<link rel='stylesheet' href='style.css' type='text/css'>",
      "</head>",
      "<body style='background: white;'>",
        "<h1 style='text-align: center;'>"+name+"</h1>",
	"<table id='schedule'>");
  makeTable(w, _tabs[0], t);
  w.document.writeln(
	"</table>",
	"</body>",
    "</html>");
  w.document.close();
  w.focus();
}