[Svn] r3928 - in trunk/plugins/enigma: . lib

trac at roundcube.net trac at roundcube.net
Mon Aug 30 20:18:21 CEST 2010


Author: alec
Date: 2010-08-30 13:18:21 -0500 (Mon, 30 Aug 2010)
New Revision: 3928

Modified:
   trunk/plugins/enigma/enigma.js
   trunk/plugins/enigma/lib/enigma_ui.php
Log:
- Keys list paging and searching


Modified: trunk/plugins/enigma/enigma.js
===================================================================
--- trunk/plugins/enigma/enigma.js	2010-08-30 18:00:39 UTC (rev 3927)
+++ trunk/plugins/enigma/enigma.js	2010-08-30 18:18:21 UTC (rev 3928)
@@ -17,12 +17,18 @@
                 rcmail.keys_list.addEventListener('select', function(o){ p.enigma_key_select(o); });
                 rcmail.keys_list.init();
                 rcmail.keys_list.focus();
+
+                rcmail.enigma_list();
+
+                rcmail.register_command('firstpage', function(props) {return rcmail.enigma_list_page('first'); });
+                rcmail.register_command('previouspage', function(props) {return rcmail.enigma_list_page('previous'); });
+                rcmail.register_command('nextpage', function(props) {return rcmail.enigma_list_page('next'); });
+                rcmail.register_command('lastpage', function(props) {return rcmail.enigma_list_page('last'); });
             }
 
             if (rcmail.env.action == 'edit-prefs') {
-                rcmail.enable_command('search', 'reset-search', true);
-                rcmail.addEventListener('beforesearch', 'enigma_search', rcmail);
-                rcmail.addEventListener('beforereset-search', 'enigma_search_reset', rcmail);
+                rcmail.register_command('search', function(props) {return rcmail.enigma_search(props); }, true);
+                rcmail.register_command('reset-search', function(props) {return rcmail.enigma_search_reset(props); }, true);
             }
             else if (rcmail.env.action == 'plugin.enigma') {
                 rcmail.register_command('plugin.enigma-import', function() { rcmail.enigma_import() }, true);
@@ -33,7 +39,7 @@
 }
 
 /*********************************************************/
-/*********       Enigma Settings  methods        *********/
+/*********    Enigma Settings/Keys/Certs UI      *********/
 /*********************************************************/
 
 // Display key(s) import form
@@ -83,28 +89,28 @@
     }
 };
 
+// Search keys/certs
 rcube_webmail.prototype.enigma_search = function(props)
 {
     if (!props && this.gui_objects.qsearchbox)
         props = this.gui_objects.qsearchbox.value;
 
-    if (props) {
+    if (props || this.env.search_request) {
+        var params = {'_a': 'keysearch', '_q': urlencode(props)};
 //        if (this.gui_objects.search_filter)
   //          addurl += '&_filter=' + this.gui_objects.search_filter.value;
         this.env.current_page = 1;
         this.set_busy(true, 'searching');
+
         this.enigma_loadframe();
-
-        // reload page (for keys list refresh)
-        this.http_post('plugin.enigma',
-            {'_a': 'keysearch', '_q': urlencode(props)},
-            true);
+        this.enigma_clear_list();
+        this.http_post('plugin.enigma', params, true);
     }
 
-    // skip default 'search' command action
     return false;
 }
 
+// Reset search filter and the list
 rcube_webmail.prototype.enigma_search_reset = function(props)
 {
     var s = this.env.search_request;
@@ -112,13 +118,79 @@
 
     if (s) {
         this.enigma_loadframe();
-        // reload page (for keys list refresh)
+        this.enigma_clear_list();
+
+        // refresh the list
+        this.enigma_list();
     }
 
-    // skip default 'reset-search' command action
     return false;
 }
 
+// Keys/certs listing
+rcube_webmail.prototype.enigma_list = function(page)
+{
+    var params = {'_a': 'keylist'};
+
+    this.env.current_page = page ? page : 1;
+    this.set_busy(true, 'loading');
+
+    if (this.env.search_request)
+        params._q = this.env.search_request;
+    if (page)
+        params._p = page;
+
+    this.enigma_clear_list();
+    this.http_post('plugin.enigma', params, true);
+}
+
+// Change list page
+rcube_webmail.prototype.enigma_list_page = function(page)
+{
+    if (page == 'next')
+        page = this.env.current_page + 1;
+    else if (page == 'last')
+        page = this.env.pagecount;
+    else if (page == 'prev' && this.env.current_page > 1)
+        page = this.env.current_page - 1;
+    else if (page == 'first' && this.env.current_page > 1)
+        page = 1;
+
+    this.enigma_list(page);
+}
+
+// Remove list rows
+rcube_webmail.prototype.enigma_clear_list = function()
+{
+    this.enigma_loadframe();
+    if (this.keys_list)
+        this.keys_list.clear(true);
+}
+
+// Adds a row to the list
+rcube_webmail.prototype.enigma_add_list_row = function(r)
+{
+    if (!this.gui_objects.keyslist || !this.keys_list)
+        return false;
+
+    var list = this.keys_list,
+        tbody = this.gui_objects.keyslist.tBodies[0],
+        rowcount = tbody.rows.length,
+        even = rowcount%2,
+        css_class = 'message'
+            + (even ? ' even' : ' odd'),
+        // for performance use DOM instead of jQuery here
+        row = document.createElement('tr'),
+        col = document.createElement('td');
+
+    row.id = 'rcmrow' + r.id;
+    row.className = css_class;
+
+    col.innerHTML = r.name;
+    row.appendChild(col);
+    list.insert_row(row);
+}
+
 /*********************************************************/
 /*********        Enigma Message methods         *********/
 /*********************************************************/
@@ -132,3 +204,4 @@
 
     return false;
 };
+

Modified: trunk/plugins/enigma/lib/enigma_ui.php
===================================================================
--- trunk/plugins/enigma/lib/enigma_ui.php	2010-08-30 18:00:39 UTC (rev 3927)
+++ trunk/plugins/enigma/lib/enigma_ui.php	2010-08-30 18:18:21 UTC (rev 3928)
@@ -27,7 +27,6 @@
     private $enigma;
     private $home;
     private $css_added;
-    private $listsize;
     private $data;
 
 
@@ -50,7 +49,7 @@
 
         // Enigma actions
         if ($this->rc->action == 'plugin.enigma') {
-            $action = get_input_value('_a', RCUBE_INPUT_GET);
+            $action = get_input_value('_a', RCUBE_INPUT_GPC);
 
             switch ($action) {
                 case 'keyedit':
@@ -60,8 +59,10 @@
                     $this->key_import();
                     break;
                 case 'keysearch':
-                    $this->key_search();
+                case 'keylist':
+                    $this->key_list();
                     break;
+                case 'keyinfo':
                 default:
                     $this->key_info();
             }
@@ -70,9 +71,9 @@
         else { // if ($this->rc->action == 'edit-prefs') {
             if ($section == 'enigmacerts') {
                 $this->rc->output->add_handlers(array(
-                    'keyslist' => array($this, 'certs_list'),
-                    'keyframe' => array($this, 'cert_frame'),
-                    'countdisplay' => array($this, 'certs_rowcount'),
+                    'keyslist' => array($this, 'tpl_certs_list'),
+                    'keyframe' => array($this, 'tpl_cert_frame'),
+                    'countdisplay' => array($this, 'tpl_certs_rowcount'),
                     'searchform' => array($this->rc->output, 'search_form'),
                 ));
                 $this->rc->output->set_pagetitle($this->enigma->gettext('enigmacerts'));
@@ -80,9 +81,9 @@
             }
             else {
                 $this->rc->output->add_handlers(array(
-                    'keyslist' => array($this, 'keys_list'),
-                    'keyframe' => array($this, 'key_frame'),
-                    'countdisplay' => array($this, 'keys_rowcount'),
+                    'keyslist' => array($this, 'tpl_keys_list'),
+                    'keyframe' => array($this, 'tpl_key_frame'),
+                    'countdisplay' => array($this, 'tpl_keys_rowcount'),
                     'searchform' => array($this->rc->output, 'search_form'),
                 ));
                 $this->rc->output->set_pagetitle($this->enigma->gettext('enigmakeys'));
@@ -114,12 +115,12 @@
      *
      * @return string HTML output
      */
-    function key_frame($attrib)
+    function tpl_key_frame($attrib)
     {
         if (!$attrib['id']) {
             $attrib['id'] = 'rcmkeysframe';
         }
-    
+
         $attrib['name'] = $attrib['id'];
 
         $this->rc->output->set_env('contentframe', $attrib['name']);
@@ -136,10 +137,8 @@
      *
      * @return string HTML content
      */
-    function keys_list($attrib)
+    function tpl_keys_list($attrib)
     {
-        $this->enigma->load_engine();
-
         // add id to message list table if not specified
         if (!strlen($attrib['id'])) {
             $attrib['id'] = 'rcmenigmakeyslist';
@@ -147,42 +146,72 @@
 
         // define list of cols to be displayed
         $a_show_cols = array('name');
-        $result = array();
 
-        // Get the list
-        $list = $this->enigma->engine->list_keys();
-
-        if (is_array($list)) {
-            // Sort the list by key (user) name
-            usort($list, array('enigma_key', 'cmp'));
-
-            foreach($list as $idx => $key) {
-                $result[] = array('name' => $key->name, 'id' => $key->id);
-                unset($list[$idx]);
-            }
-        }
-
         // create XHTML table
-        $out = rcube_table_output($attrib, $result, $a_show_cols, 'id');
+        $out = rcube_table_output($attrib, array(), $a_show_cols, 'id');
 
-        if ($list && ($list instanceof enigma_error))
-            $this->rc->output->show_message('enigma.keylisterror', 'error');
-        else if (empty($result))
-            $this->rc->output->show_message('enigma.nokeysfound', 'notice');
-        else
-            $this->listsize = count($result);
-
         // set client env
         $this->rc->output->add_gui_object('keyslist', $attrib['id']);
         $this->rc->output->include_script('list.js');
-  
+
         // add some labels to client
         $this->rc->output->add_label('enigma.keyconfirmdelete');
-  
+
         return $out;
     }
 
+    /**
+     * Key listing (and searching) request handler
+     */
+    private function key_list()
+    {
+        $this->enigma->load_engine();
 
+        $pagesize = $this->rc->config->get('pagesize', 100);
+        $page     = max(intval(get_input_value('_p', RCUBE_INPUT_GPC)), 1);
+        $search   = get_input_value('_q', RCUBE_INPUT_GPC);
+
+        // define list of cols to be displayed
+        $a_show_cols = array('name');
+        $result = array();
+
+        // Get the list
+        $list = $this->enigma->engine->list_keys($search);
+
+        if ($list && ($list instanceof enigma_error))
+            $this->rc->output->show_message('enigma.keylisterror', 'error');
+        else if (empty($list))
+            $this->rc->output->show_message('enigma.nokeysfound', 'notice');
+        else {
+            if (is_array($list)) {
+                // Save the size
+                $listsize = count($list);
+
+                // Sort the list by key (user) name
+                usort($list, array('enigma_key', 'cmp'));
+
+                // Slice current page
+                $list = array_slice($list, ($page - 1) * $pagesize, $pagesize);
+
+                $size = count($list);
+
+                // Add rows
+                foreach($list as $idx => $key) {
+                    $this->rc->output->command('enigma_add_list_row',
+                        array('name' => Q($key->name), 'id' => $key->id));
+                }
+            }
+        }
+
+        $this->rc->output->set_env('search_request', $search);
+        $this->rc->output->set_env('pagecount', ceil($listsize/$pagesize));
+        $this->rc->output->set_env('current_page', $page);
+        $this->rc->output->command('set_rowcount',
+            $this->get_rowcount_text($listsize, $size, $page));
+
+        $this->rc->output->send();
+    }
+
     /**
      * Template object for list records counter.
      *
@@ -190,7 +219,7 @@
      *
      * @return string HTML output
      */
-    function keys_rowcount($attrib)
+    function tpl_keys_rowcount($attrib)
     {
         if (!$attrib['id'])
             $attrib['id'] = 'rcmcountdisplay';
@@ -203,22 +232,21 @@
     /**
      * Returns text representation of list records counter
      */
-    private function get_rowcount_text()
+    private function get_rowcount_text($all, $curr_count, $page)
     {
-        $page_size = $this->rc->config->get('pagesize', 100);
-        $count = $this->listsize;
-        $first = 0;
+        $pagesize = $this->rc->config->get('pagesize', 100);
+        $first = ($page - 1) * $pagesize;
 
-        if (!$count)
+        if (!$curr_count)
             $out = $this->enigma->gettext('nokeysfound');
         else
             $out = $this->enigma->gettext(array(
                 'name' => 'keysfromto',
                 'vars' => array(
                     'from'  => $first + 1,
-                    'to'    => min($count, $page_size),
-                    'count' => $count)
-        ));
+                    'to'    => $first + $curr_count,
+                    'count' => $all)
+            ));
 
         return $out;
     }
@@ -242,8 +270,8 @@
         }
 
         $this->rc->output->add_handlers(array(
-            'keyname' => array($this, 'key_name'),
-            'keydata' => array($this, 'key_data'),
+            'keyname' => array($this, 'tpl_key_name'),
+            'keydata' => array($this, 'tpl_key_data'),
         ));
 
         $this->rc->output->set_pagetitle($this->enigma->gettext('keyinfo'));
@@ -253,7 +281,7 @@
     /**
      * Template object for key name
      */
-    function key_name($attrib)
+    function tpl_key_name($attrib)
     {
         return Q($this->data->name);
     }
@@ -261,7 +289,7 @@
     /**
      * Template object for key information page content
      */
-    function key_data($attrib)
+    function tpl_key_data($attrib)
     {
         $out = '';
         $table = new html_table(array('cols' => 2)); 
@@ -340,7 +368,7 @@
         }
 
         $this->rc->output->add_handlers(array(
-            'importform' => array($this, 'key_import_form'),
+            'importform' => array($this, 'tpl_key_import_form'),
         ));
 
         $this->rc->output->set_pagetitle($this->enigma->gettext('keyimport'));
@@ -350,7 +378,7 @@
     /**
      * Template object for key import (upload) form
      */
-    function key_import_form($attrib)
+    function tpl_key_import_form($attrib)
     {
         $attrib += array('id' => 'rcmKeyImportForm');
   

_______________________________________________
http://lists.roundcube.net/mailman/l