');
tbody.append(tr);
}
// Insert day cell.
td = $('').attr({ id: 'kronolithMonth' + dateString })
.addClass('kronolithMonthDay')
.jqmData('date', dateString);
if (day.getMonth() != date.getMonth()) {
td.addClass('kronolithMinicalEmpty');
}
// Highlight today.
if (day.dateString() == today.dateString()) {
td.addClass('kronolithToday');
}
td.html(day.getDate());
tr.append(td);
day.next().day();
}
},
/**
* Parses a date attribute string into a Date object.
*
* For other strings use Date.parse().
*
* @param string date A yyyyMMdd date string.
*
* @return Date A date object.
*/
parseDate: function(date)
{
var d = new Date(date.substr(0, 4), date.substr(4, 2) - 1, date.substr(6, 2));
if (date.length == 12) {
d.setHours(date.substr(8, 2));
d.setMinutes(date.substr(10, 2));
}
return d;
},
storeCache: function(events, calendar, dates, createCache)
{
events = events || {};
//calendar[0] == type, calendar[1] == calendar name
calendar = calendar.split('|');
if (!KronolithMobile.ecache[calendar[0]]) {
if (!createCache) {
return;
}
KronolithMobile.ecache[calendar[0]] = {};
}
if (!KronolithMobile.ecache[calendar[0]][calendar[1]]) {
if (!createCache) {
return;
}
KronolithMobile.ecache[calendar[0]][calendar[1]] = {};
}
var calHash = KronolithMobile.ecache[calendar[0]][calendar[1]];
// Create empty cache entries for all dates.
if (!!dates) {
var day = dates[0].clone(), date;
while (!day.isAfter(dates[1])) {
date = day.dateString();
if (!calHash[date]) {
if (!createCache) {
return;
}
if (!KronolithMobile.cacheStart || KronolithMobile.cacheStart.isAfter(day)) {
KronolithMobile.cacheStart = day.clone();
}
if (!KronolithMobile.cacheEnd || KronolithMobile.cacheEnd.isBefore(day)) {
KronolithMobile.cacheEnd = day.clone();
}
calHash[date] = {};
}
day.add(1).day();
}
}
var cal = calendar.join('|');
$.each(events, function(key, date) {
// We might not have a cache for this date if the event lasts
// longer than the current view
if (typeof calHash[key] == 'undefined') {
return;
}
// Store useful information in event objects.
$.each(date, function(k, event) {
event.calendar = cal;
event.start = Date.parse(event.s);
event.end = Date.parse(event.e);
event.sort = event.start.toString('HHmmss')
+ (240000 - parseInt(event.end.toString('HHmmss'), 10)).toPaddedString(6);
event.id = k;
});
// Store events in cache.
$.extend(calHash[key], date);
});
},
/**
* Return all events for a single day from all displayed calendars merged
* into a single hash.
*
* @param string date A yyyymmdd date string.
*
* @return Hash An event hash which event ids as keys and event objects as
* values.
*/
getCacheForDate: function(date, calendar)
{
if (calendar) {
var cals = calendar.split('|');
return KronolithMobile.ecache[cals[0]][cals[1]][date];
}
var events = {};
$.each(KronolithMobile.ecache, function(key, type) {
$.each(type, function(id, cal) {
if (!Kronolith.conf.calendars[key][id].show) {
return;
}
if (typeof cal[date] != 'undefined') {
$.extend(events, cal[date]);
}
});
});
return events;
},
/**
* Handle swipe events for the current view.
*/
handleSwipe: function(map)
{
switch (KronolithMobile.view) {
case 'day':
if (map.type == 'swipeleft') {
KronolithMobile.showNextDay();
} else {
KronolithMobile.showPrevDay();
}
break;
case 'month':
if (map.type == 'swipeleft') {
KronolithMobile.showNextMonth();
} else {
KronolithMobile.showPrevMonth();
}
}
},
/**
* Event handler for the pagebeforechange event that implements loading of
* deep-linked pages.
*
* @param object e Event object.
* @param object data Event data.
*/
toPage: function(e, data)
{
switch (data.options.parsedUrl.view) {
case 'minical-next':
KronolithMobile.showNextMonth();
e.preventDefault();
break;
case 'minical-prev':
KronolithMobile.showPrevMonth();
e.preventDefault();
break;
case 'nextday':
KronolithMobile.showNextDay();
e.preventDefault();
break;
case 'prevday':
KronolithMobile.showPrevDay();
e.preventDefault();
break;
}
},
/**
*/
loadPage: function()
{
switch (HordeMobile.currentPage()) {
case 'monthview':
KronolithMobile.view = "month";
// (re)build the minical only if we need to
if (!$("#kronolithMinicalDate").jqmData("date") ||
($("#kronolithMinicalDate").jqmData("date").toString("M") != KronolithMobile.date.toString("M"))) {
KronolithMobile.moveToMonth(KronolithMobile.date);
}
break;
case 'overview':
KronolithMobile.view = "overview";
if (!KronolithMobile.haveOverview) {
KronolithMobile.loadEvents(KronolithMobile.date, KronolithMobile.date.clone().addDays(7), "overview");
KronolithMobile.haveOverview = true;
}
break;
case null:
break;
case 'dayview':
default:
KronolithMobile.view = "day";
$("#kronolithDayDate").html(KronolithMobile.date.toString("ddd") + " " + KronolithMobile.date.toString("d"));
KronolithMobile.loadEvents(KronolithMobile.date, KronolithMobile.date, "day");
break;
}
},
onDocumentReady: function()
{
KronolithMobile.date = new Date();
// Build list of calendars we want.
$.each(Kronolith.conf.calendars, function(key, value) {
$.each(value, function(cal, info) {
if (info.show) {
KronolithMobile.calendars.push([key, cal]);
}
});
});
// Bind click and swipe events
$('body').bind('swipeleft', KronolithMobile.handleSwipe)
.bind('swiperight', KronolithMobile.handleSwipe);
$(document).bind('pageshow', KronolithMobile.loadPage)
.bind('pagebeforechange', KronolithMobile.toPage)
.on("pageshow", "#eventview", function(event, ui) {
KronolithMobile.view = "event";
});
$('#kronolith-minical').on('click', 'td', function(e) {
KronolithMobile.selectMonthDay($(e.target).jqmData('date'));
});
// Load initial view.
KronolithMobile.loadPage();
}
};
$(KronolithMobile.onDocumentReady);
|