From 534d6a43dfcba0f932158b830d386a77849db164 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Thu, 18 Nov 2010 00:24:02 -0500 Subject: [PATCH] initial attempt at event cache --- kronolith/js/mobile.js | 179 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 119 insertions(+), 60 deletions(-) diff --git a/kronolith/js/mobile.js b/kronolith/js/mobile.js index 9d5fb7c4e..4885074cd 100644 --- a/kronolith/js/mobile.js +++ b/kronolith/js/mobile.js @@ -16,25 +16,21 @@ /** * List of calendars we are displaying - **/ + */ calendars: [], /** * List of calendars that are currently loaded for the current view - **/ + */ loadedCalendars: [], /** - * List of events being displayed on the day view - **/ - events: [], - - /** - * Event cache. For now, only used for month view. + * Event cache */ ecache: {}, cacheStart: null, cacheEnd: null, + /** * The currently displayed view **/ @@ -53,41 +49,76 @@ }, /** - * Load all events between start and end time. Returns short version of - * event info. + * Load all events between start and end time. * * @param Date start * @param Date end + * @param string view The view we are loading for (month, day) */ - loadEvents: function(start, end, view) + loadEvents: function(firstDay, lastDay, view) { + // Clear out the loaded cal cache KronolithMobile.loadedCalendars = []; - KronolithMobile.events = []; - start = start.toString('yyyyMMdd'); - end = end.toString('yyyyMMdd'); - for (cal in KronolithMobile.calendars) { - cal = KronolithMobile.calendars[cal]; + + $.each(KronolithMobile.calendars, function(key, cal) { + var startDay = firstDay.clone() , endDay = lastDay.clone(), + cals = KronolithMobile.ecache[cal[0]]; + if (typeof cals != 'undefined' && + typeof cals[cal[1]] != 'undefined') { + + cals = cals[cal[1]]; + c = cals[startDay.dateString()]; + while (typeof c != 'undefined' && startDay.isBefore(endDay)) { + KronolithMobile.loadedCalendars.push(cal.join('|')); + if (KronolithMobile.view != 'month') { + KronolithMobile.insertEvents([startDay, startDay], view, cal.join('|')); + } + startDay.add(1).day(); + c = cals[startDay.dateString()]; + } + + c = cals[endDay.dateString()]; + while (typeof c != 'undefined' && !startDay.isAfter(endDay)) { + KronolithMobile.loadedCalendars.push(cal); + if (KronolithMobile.view != 'month') { + KronolithMobile.insertEvents([endDay, endDay], view, cal.join('|')); + } + endDay.add(-1).day(); + c = cals[endDay.dateString()]; + } + if (startDay.compareTo(endDay) > 0) { + return; + } + } + + var start = startDay.dateString(), end = endDay.dateString(); KronolithMobile.doAction('listEvents', { 'start': start, 'end': end, - 'cal': cal[0] + '|' + cal[1], + 'cal': cal.join('|'), 'view': view, 'sig': start + end + (Math.random() + '').slice(2) }, KronolithMobile.loadEventsCallback ); - } + }); }, /** - * Used as sorting funtion to Array.sort for sorting events by start time + * Sort a collection of events as returned from the ecache */ sortEvents: function(events) { - return events.sort(function(a, b) { - sortA = a.e.s; - sortB = b.e.s; + var e = []; + + // Need a native array to sort. + $.each(events, function(id, event) { + e.push(event); + }); + return e.sort(function(a, b) { + sortA = a.sort; + sortB = b.sort; return (sortA < sortB) ? -1 : (sortA > sortB) ? 1 : 0; }); }, @@ -105,46 +136,59 @@ { var start = KronolithMobile.parseDate(data.response.sig.substr(0, 8)), end = KronolithMobile.parseDate(data.response.sig.substr(8, 8)), - dates = [start, end], list, events; + dates = [start, end], view = data.response.view, list, events; data = data.response; KronolithMobile.loadedCalendars.push(data.cal); + KronolithMobile.storeCache(data.events, data.cal, dates, true); + KronolithMobile.insertEvents(dates, view, data.cal); + }, - if (KronolithMobile.view == 'day') { - if (data.events) { - $.each(data.events, function(datestring, events) { - $.each(events, function(index, event) { - KronolithMobile.events.push({ 'e': event, 'id': index, 'cal': data.cal }); - }); - }); - } + //@TODO rename to renderView? + insertEvents: function(dates, view, cal) + { + var date, events, list; + switch (view) { + case 'day': + // Make sure all calendars are loaded before rendering the view. + // @TODO: Implement LIFO queue as in kronolith.js + if (KronolithMobile.loadedCalendars.length != KronolithMobile.calendars.length + || KronolithMobile.view != view) { - if (KronolithMobile.loadedCalendars.length == KronolithMobile.calendars.length) { - events = KronolithMobile.sortEvents(KronolithMobile.events); + KronolithMobile.timeoutId = window.setTimeout(function() {KronolithMobile.insertEvents(dates, view);}, 0); + return; + } + + if (KronolithMobile.timeoutId) { + window.clearTimeout(KronolithMobile.timeoutId); + KronolithMobile.timoutId = false; + } + date = dates[0].clone(); + events = KronolithMobile.getCacheForDate(date.dateString()); + + events = KronolithMobile.sortEvents(events); list = $('