Build calendar list on the client side.
authorJan Schneider <jan@horde.org>
Thu, 5 Mar 2009 00:42:50 +0000 (01:42 +0100)
committerJan Schneider <jan@horde.org>
Thu, 5 Mar 2009 00:42:50 +0000 (01:42 +0100)
Retrieve each calendar's events seperately.

kronolith/ajax.php
kronolith/js/src/kronolith.js
kronolith/lib/Driver/Holidays.php
kronolith/lib/Driver/Horde.php
kronolith/lib/Driver/Ical.php
kronolith/lib/Driver/Kolab.php
kronolith/lib/Driver/Sql.php
kronolith/lib/Kronolith.php
kronolith/templates/index/index.inc
kronolith/templates/panel.inc

index af2b060..078d2d3 100644 (file)
@@ -56,17 +56,19 @@ switch ($action) {
 case 'ListEvents':
     $start = new Horde_Date(Util::getFormData('start'));
     $end   = new Horde_Date(Util::getFormData('end'));
-    $dates = Kronolith::listEvents($start, $end);
-    if (is_a($dates, 'PEAR_Error')) {
-        $notification->push($dates, 'horde.error');
+    $cal   = Util::getFormData('cal');
+    list($driver, $calendar) = explode('|', $cal);
+    $kronolith_driver = Kronolith::getDriver($driver, $calendar);
+    $events = $kronolith_driver->listEvents($start, $end, true, false, true);
+    if (is_a($events, 'PEAR_Error')) {
+        $notification->push($events, 'horde.error');
         $result = false;
     } else {
         $result = new stdClass;
+        $result->cal = $cal;
         $result->sig = $start->dateString() . $end->dateString();
-        foreach ($dates as $date => $events) {
-            foreach ($events as $id => $event) {
-                $result->events[$date][$id] = $event->toJSON();
-            }
+        if (count($events)) {
+            $result->events = $events;
         }
     }
     break;
index cee2fd7..e2884d9 100644 (file)
@@ -17,12 +17,13 @@ var frames = { horde_main: true },
 KronolithCore = {
     // Vars used and defaulting to null/false:
     //   DMenu, alertrequest, inAjaxCallback, is_logout, onDoActionComplete,
-    //   eventForm, eventsLoading
+    //   eventForm
 
     view: '',
     calendars: [],
     ecache: {},
     efifo: {},
+    eventsLoading: $H(),
     date: new Date(),
 
     doActionOpts: {
@@ -468,11 +469,78 @@ KronolithCore = {
     },
 
     /**
+     * Rebuilds the list of calendars.
      */
-    _loadEvents: function(firstDay, lastDay, callback, calendar)
+    updateCalendarList: function()
     {
-        this.eventsLoading = firstDay.dateString() + lastDay.dateString();
-        this.doAction('ListEvents', { start: firstDay.dateString(), end: lastDay.dateString() }, callback);
+        var internal = $H(Kronolith.conf.calendars.internal),
+            remote = $H(Kronolith.conf.calendars.remote),
+            my = 0, shared = 0, div;
+
+        internal.each(function(cal) {
+            if (cal.value.owner) {
+                my++;
+                div = $('kronolithMyCalendars');
+            } else {
+                shared++;
+                div = $('kronolithSharedCalendars');
+            }
+            div.appendChild(new Element('DIV', { 'class': cal.value.show ? 'kronolithCalOn' : 'kronolithCalOff' }).setStyle({ backgroundColor: cal.value.bg, color: cal.value.fg }).update(cal.value.name));
+        });
+        if (my) {
+            $('kronolithMyCalendars').show();
+        } else {
+            $('kronolithMyCalendars').hide();
+        }
+        if (shared) {
+            $('kronolithSharedCalendars').show();
+        } else {
+            $('kronolithSharedCalendars').hide();
+        }
+
+        remote.each(function(cal) {
+            $('kronolithRemoteCalendars').appendChild(new Element('DIV', { 'class': cal.value.show ? 'kronolithCalOn' : 'kronolithCalOff' }).setStyle({ backgroundColor: cal.value.bg, color: cal.value.fg }).update(cal.value.name));
+        });
+        if (remote.size()) {
+            $('kronolithRemoteCalendars').show();
+        } else {
+            $('kronolithRemoteCalendars').hide();
+        }
+    },
+
+    /**
+     */
+    _loadEvents: function(firstDay, lastDay, callback, calendars)
+    {
+        var start = firstDay.dateString(), end = lastDay.dateString(),
+            driver, calendar;
+        if (typeof calendars == 'undefined') {
+            calendars = Kronolith.conf.calendars;
+        }
+        calendars = $H(calendars);
+        calendars.each(function(type) {
+            switch (type.key) {
+            case 'internal':
+                driver = '';
+                break;
+            case 'external':
+                driver = 'Horde';
+                break;
+            case 'remote':
+                driver = 'Ical';
+                break;
+            case 'holiday':
+                driver = 'Holiday';
+                break;
+            }
+            $H(type.value).each(function(cal) {
+                if (cal.value.show) {
+                    calendar = driver + '|' + cal.key;
+                    this.eventsLoading[calendar] = start + end;
+                    this.doAction('ListEvents', { start: start, end: end, cal: calendar }, callback);
+                }
+            }, this);
+        }, this);
     },
 
     /**
@@ -485,7 +553,7 @@ KronolithCore = {
         var div;
 
         // Check if this is the still the result of the most current request.
-        if (r.response.sig != this.eventsLoading) {
+        if (r.response.sig != this.eventsLoading[r.response.cal]) {
             return;
         }
 
@@ -733,12 +801,14 @@ KronolithCore = {
     /* Onload function. */
     onDomLoad: function()
     {
-        KronolithCore.init();
+        this.init();
 
         if (Horde.dhtmlHistory.initialize()) {
             Horde.dhtmlHistory.addListener(this.go.bind(this));
         }
 
+        this.updateCalendarList();
+
         /* Initialize the starting page if necessary. addListener() will have
          * already fired if there is a current location so only do a go()
          * call if there is no current location. */
index 17db2db..ed3b51c 100644 (file)
@@ -32,11 +32,14 @@ class Kronolith_Driver_Holidays extends Kronolith_Driver
      *                                   $startDate - $endDate range.
      * @param boolean $hasAlarm          Only return events with alarms? Has no
      *                                   effect in this driver.
+     * @param boolean $json              Store the results of the events'
+     *                                   toJSON() method?
      *
      * @return array  Events in the given time range.
      */
     public function listEvents($startDate = null, $endDate = null,
-                               $showRecurrence = false, $hasAlarm = false)
+                               $showRecurrence = false, $hasAlarm = false,
+                               $json = false)
     {
         if (!class_exists('Date_Holidays')) {
             Horde::logMessage('Support for Date_Holidays has been enabled but the package seems to be missing.',
@@ -68,7 +71,7 @@ class Kronolith_Driver_Holidays extends Kronolith_Driver
             $events = $this->_getEvents($dh, $startDate, $endDate);
             foreach ($events as $event) {
                 Kronolith::addEvents($results, $event, $startDate, $endDate,
-                                     $showRecurrence);
+                                     $showRecurrence, $json);
             }
         }
 
index f6b49bb..c89781d 100644 (file)
@@ -44,11 +44,14 @@ class Kronolith_Driver_Horde extends Kronolith_Driver
      *                                   recurring events once inside the
      *                                   $startDate - $endDate range.
      * @param boolean $hasAlarm          Only return events with alarms?
+     * @param boolean $json              Store the results of the events'
+     *                                   toJSON() method?
      *
      * @return array  Events in the given time range.
      */
     public function listEvents($startDate = null, $endDate = null,
-                               $showRecurrence = false, $hasAlarm = false)
+                               $showRecurrence = false, $hasAlarm = false,
+                               $json = false)
     {
         list($this->api, $category) = explode('/', $this->_calendar, 2);
         if (!$this->_params['registry']->hasMethod($this->api . '/listTimeObjects')) {
@@ -86,7 +89,7 @@ class Kronolith_Driver_Horde extends Kronolith_Driver
             }
 
             Kronolith::addEvents($results, $event, $startDate,
-                                 $endDate, $showRecurrence);
+                                 $endDate, $showRecurrence, $json);
         }
 
         return $results;
index 956923b..cc42b6f 100644 (file)
@@ -46,11 +46,14 @@ class Kronolith_Driver_Ical extends Kronolith_Driver
      *                                   recurring events once inside the
      *                                   $startDate - $endDate range.
      * @param boolean $hasAlarm          Only return events with alarms?
+     * @param boolean $json              Store the results of the events'
+     *                                   toJSON() method?
      *
      * @return array  Events in the given time range.
      */
     public function listEvents($startDate = null, $endDate = null,
-                               $showRecurrence = false, $hasAlarm = false)
+                               $showRecurrence = false, $hasAlarm = false,
+                               $json = false)
     {
         $data = $this->_getRemoteCalendar();
         if (is_a($data, 'PEAR_Error')) {
@@ -118,7 +121,7 @@ class Kronolith_Driver_Ical extends Kronolith_Driver
                 $timestamp = $exceptions[$event->getUID()][$event->getSequence()];
                 $events[$key]->recurrence->addException(date('Y', $timestamp), date('m', $timestamp), date('d', $timestamp));
                 Kronolith::addEvents($results, $event, $startDate, $endDate,
-                                     $showRecurrence);
+                                     $showRecurrence, $json);
             }
         }
 
index ad7cb43..4f931b6 100644 (file)
@@ -195,11 +195,14 @@ class Kronolith_Driver_Kolab extends Kronolith_Driver
      *                                   recurring events once inside the
      *                                   $startDate - $endDate range.
      * @param boolean $hasAlarm          Only return events with alarms?
+     * @param boolean $json              Store the results of the events'
+     *                                   toJSON() method?
      *
      * @return array  Events in the given time range.
      */
     public function listEvents($startDate = null, $endDate = null,
-                               $showRecurrence = false, $hasAlarm = false)
+                               $showRecurrence = false, $hasAlarm = false,
+                               $json = false)
     {
         $result = $this->synchronize();
         if (is_a($result, 'PEAR_Error')) {
@@ -245,7 +248,7 @@ class Kronolith_Driver_Kolab extends Kronolith_Driver
             }
 
             Kronolith::addEvents($events, $event, $startDate, $endDate,
-                                 $showRecurrence);
+                                 $showRecurrence, $json);
         }
 
         return $events;
index f1b60d6..bbb4879 100644 (file)
@@ -225,11 +225,14 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
      *                                   recurring events once inside the
      *                                   $startDate - $endDate range.
      * @param boolean $hasAlarm          Only return events with alarms?
+     * @param boolean $json              Store the results of the events'
+     *                                   toJSON() method?
      *
      * @return array  Events in the given time range.
      */
     public function listEvents($startDate = null, $endDate = null,
-                               $showRecurrence = false, $hasAlarm = false)
+                               $showRecurrence = false, $hasAlarm = false,
+                               $json = false)
     {
         if (is_null($startDate)) {
             $startDate = new Horde_Date(array('mday' => 1,
@@ -257,7 +260,7 @@ class Kronolith_Driver_Sql extends Kronolith_Driver
         $results = array();
         foreach ($events as $id) {
             Kronolith::addEvents($results, $this->getEvent($id), $startDate,
-                                 $endDate, $showRecurrence);
+                                 $endDate, $showRecurrence, $json);
         }
 
         return $results;
index 35b1810..3c4506b 100644 (file)
@@ -146,6 +146,32 @@ class Kronolith
             // Turn debugging on?
             'debug' => !empty($conf['js']['debug']),
         );
+        foreach ($GLOBALS['all_calendars'] as $id => $calendar) {
+            $owner = $calendar->get('owner') == Auth::getAuth();
+            $code['conf']['calendars']['internal'][$id] = array(
+                'name' => ($owner ? '' : '[' . Auth::removeHook($calendar->get('owner')) . '] ')
+                    . $calendar->get('name'),
+                'owner' => $owner,
+                'fg' => Kronolith::foregroundColor($calendar),
+                'bg' => Kronolith::backgroundColor($calendar),
+                'show' => in_array($id, $GLOBALS['display_calendars']));
+        }
+        foreach ($GLOBALS['all_external_calendars'] as $api => $categories) {
+            foreach ($categories as $id => $name) {
+                $calendar = $api . '/' . $id;
+                $code['conf']['calendars']['external'][$calendar] = array(
+                    'name' => $name,
+                    'api' => $GLOBALS['registry']->get('name', $GLOBALS['registry']->hasInterface($api)),
+                    'show' => in_array($calendar, $GLOBALS['display_external_calendars']));
+            }
+        }
+        foreach ($GLOBALS['all_remote_calendars'] as $calendar) {
+            $code['conf']['calendars']['remote'][$calendar['url']] = array(
+                'name' => $calendar['name'],
+                'fg' => Kronolith::foregroundColor($calendar),
+                'bg' => Kronolith::backgroundColor($calendar),
+                'show' => in_array($calendar['url'], $GLOBALS['display_remote_calendars']));
+        }
 
         /* Gettext strings used in core javascript files. */
         $code['text'] = array_map('addslashes', array(
@@ -637,7 +663,7 @@ class Kronolith
      * @access private
      */
     public static function addEvents(&$results, &$event, $startDate, $endDate,
-                                     $showRecurrence)
+                                     $showRecurrence, $json)
     {
         if ($event->recurs() && $showRecurrence) {
             /* Recurring Event. */
@@ -679,7 +705,7 @@ class Kronolith
                 if (!$event->recurrence->hasException($event->start->year,
                                                       $event->start->month,
                                                       $event->start->mday)) {
-                    Kronolith::addCoverDates($results, $event, $event->start, $event->end);
+                    Kronolith::addCoverDates($results, $event, $event->start, $event->end, $json);
                 }
 
                 /* Start searching for recurrences from the day after it
@@ -699,7 +725,7 @@ class Kronolith
                     $nextEnd->mday  += $diff[2];
                     $nextEnd->hour  += $diff[3];
                     $nextEnd->min   += $diff[4];
-                    Kronolith::addCoverDates($results, $event, $next, $nextEnd);
+                    Kronolith::addCoverDates($results, $event, $next, $nextEnd, $json);
                 }
                 $next = $event->recurrence->nextRecurrence(
                     array('year' => $next->year,
@@ -781,7 +807,7 @@ class Kronolith
                             'month' => $eventEnd->month, 'mday' => $eventEnd->mday, 'year' => $eventEnd->year));
                     }
 
-                    $results[$loopDate->dateString()][$addEvent->getId()] = $addEvent;
+                    $results[$loopDate->dateString()][$addEvent->getId()] = $json ? $addEvent->toJSON() : $addEvent;
                 }
 
                 $loopDate = new Horde_Date(
@@ -801,9 +827,11 @@ class Kronolith
      * @param Horde_Date $eventStart  The event's start at the actual
      *                                recurrence.
      * @param Horde_Date $eventEnd    The event's end at the actual recurrence.
+     * @param boolean $json           Store the results of the events' toJSON()
+     *                                method?
      */
     public static function addCoverDates(&$results, $event, $eventStart,
-                                         $eventEnd)
+                                         $eventEnd, $json)
     {
         $i = $eventStart->mday;
         $loopDate = new Horde_Date(array('month' => $eventStart->month,
@@ -815,7 +843,7 @@ class Kronolith
                 $addEvent = clone $event;
                 $addEvent->start = $eventStart;
                 $addEvent->end = $eventEnd;
-                $results[$loopDate->dateString()][$addEvent->getId()] = $addEvent;
+                $results[$loopDate->dateString()][$addEvent->getId()] = $json ? $addEvent->toJSON() : $addEvent;
             }
             $loopDate = new Horde_Date(
                 array('month' => $eventStart->month,
@@ -965,12 +993,13 @@ class Kronolith
         /* Make sure all the remote calendars still exist. */
         $_temp = $GLOBALS['display_remote_calendars'];
         $GLOBALS['display_remote_calendars'] = array();
-        $_all = @unserialize($GLOBALS['prefs']->getValue('remote_cals'));
-        if (is_array($_all)) {
-            foreach ($_all as $id) {
-                if (in_array($id['url'], $_temp)) {
-                    $GLOBALS['display_remote_calendars'][] = $id['url'];
-                }
+        $GLOBALS['all_remote_calendars'] = @unserialize($GLOBALS['prefs']->getValue('remote_cals'));
+        if (!is_array($GLOBALS['all_remote_calendars'])) {
+            $GLOBALS['all_remote_calendars'] = array();
+        }
+        foreach ($GLOBALS['all_remote_calendars'] as $id) {
+            if (in_array($id['url'], $_temp)) {
+                $GLOBALS['display_remote_calendars'][] = $id['url'];
             }
         }
         $GLOBALS['prefs']->setValue('display_remote_cals', serialize($GLOBALS['display_remote_calendars']));
index fe8d937..54a2535 100644 (file)
     <?php echo _("My Calendars") ?>
   </h3>
 
-  <?php if (count($my_calendars)): ?>
-  <div class="kronolithCalendars">
-    <?php foreach ($my_calendars as $id => $cal): ?>
-    <div class="<?php echo (in_array($id, $display_calendars)) ? 'kronolithCalOn' : 'kronolithCalOff' ?> full" style="background-color:<?php echo Kronolith::backgroundColor($cal) ?>;color:<?php echo Kronolith::foregroundColor($cal) ?>"><?php echo htmlspecialchars($cal->get('name')) ?></div>
-    <?php endforeach; ?>
+  <div id="kronolithMyCalendars" class="kronolithCalendars" style="display:none">
   </div>
-  <?php endif; ?>
 
   <h3>
     <a href="#" class="kronolithAdd">+</a>
     <?php echo _("Shared Calendars") ?>
   </h3>
 
-  <?php if (count($shared_calendars)): ?>
-  <div class="kronolithCalendars">
-    <?php foreach ($shared_calendars as $id => $cal): ?>
-    <div class="<?php echo (in_array($id, $display_calendars)) ? 'kronolithCalOn' : 'kronolithCalOff' ?> full" style="background-color:<?php echo Kronolith::backgroundColor($cal) ?>;color:<?php echo Kronolith::foregroundColor($cal) ?>"><?php echo htmlspecialchars($cal->get('name')) ?></div>
-    <?php endforeach; ?>
+  <div id="kronolithSharedCalendars" class="kronolithCalendars" style="display:none">
   </div>
-  <?php endif; ?>
 
   <h3>
     <a href="#" class="kronolithAdd">+</a>
     <?php echo _("Remote Calendars") ?>
   </h3>
 
-  <?php if (count($remote_calendars)): ?>
-  <div class="kronolithCalendars">
-    <?php foreach ($remote_calendars as $id => $cal): ?>
-    <div class="<?php echo (in_array($id, $display_calendars)) ? 'kronolithCalOn' : 'kronolithCalOff' ?> full" style="background-color:<?php echo Kronolith::backgroundColor($cal) ?>;color:<?php echo Kronolith::foregroundColor($cal) ?>"><?php echo htmlspecialchars($cal['name']) ?></div>
-    <?php endforeach; ?>
+  <div id="kronolithRemoteCalendars" class="kronolithCalendars" style="display:none">
   </div>
-  <?php endif; ?>
 
   <div id="kronolithMinical">
     <table cellspacing="1" cellpadding="0" border="0">
index 55032bb..1748651 100644 (file)
@@ -1,10 +1,10 @@
 <script type="text/javascript">
-    function toggleTags(domid)
-    {
-        $('tag-show_' + domid).toggle();
-        $('tag-hide_' + domid).toggle();
-        $('tagnode_' + domid).toggle();
-    }
+function toggleTags(domid)
+{
+    $('tag-show_' + domid).toggle();
+    $('tag-hide_' + domid).toggle();
+    $('tagnode_' + domid).toggle();
+}
 </script>
 <?php
 Horde::addScriptFile('QuickFinder.js', 'horde', true);
@@ -13,7 +13,6 @@ Horde::addScriptFile('calendar-panel.js', 'kronolith', true);
 
 $info_img = Horde::img('info_icon.png', _("Calendar Information"), array('class' => 'calendar-info'), $registry->getImageDir('horde'));
 
-$remote_calendars = @unserialize($prefs->getValue('remote_cals'));
 $current_user = Auth::getAuth();
 $my_calendars = array();
 $shared_calendars = array();
@@ -110,10 +109,10 @@ $tagger = Kronolith::getTagger();
  </ul>
 <?php endif; ?>
 
-<?php if (count($remote_calendars)): ?>
+<?php if (count($GLOBALS['all_remote_calendars'])): ?>
  <h4><?php echo _("Remote Calendars:") ?></h4>
  <ul id="remotecalendars">
-<?php foreach ($remote_calendars as $id => $cal): ?>
+<?php foreach ($GLOBALS['all_remote_calendars'] as $id => $cal): ?>
   <li<?php echo Kronolith::getCSSColors($cal) ?>><label><input type="checkbox" class="checkbox" name="display_cal[]" value="remote_<?php echo htmlspecialchars($cal['url']) ?>"<?php echo (in_array($cal['url'], $display_remote_calendars) ? ' checked="checked"' : '') . ' /> ' . htmlspecialchars($cal['name']) ?></label> <?php echo $info_img ?></li>
 <?php endforeach; ?>
  </ul>