Add AutoCompleter Ajax driver.
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 29 Oct 2009 01:27:45 +0000 (19:27 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 29 Oct 2009 02:25:58 +0000 (20:25 -0600)
framework/Ajax/lib/Horde/Ajax/Imple/AutoCompleter.php [new file with mode: 0644]
framework/Ajax/package.xml

diff --git a/framework/Ajax/lib/Horde/Ajax/Imple/AutoCompleter.php b/framework/Ajax/lib/Horde/Ajax/Imple/AutoCompleter.php
new file mode 100644 (file)
index 0000000..e1fb876
--- /dev/null
@@ -0,0 +1,114 @@
+<?php
+/**
+ * Attach an auto completer to a javascript element.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Michael Slusarz <slusarz@horde.org>
+ * @package Horde_Ajax
+ */
+abstract class Horde_Ajax_Imple_AutoCompleter extends Horde_Ajax_Imple_Base
+{
+    /**
+     * Constructor.
+     *
+     * @param array $params  Configuration parameters.
+     * <pre>
+     * 'triggerId' => (string) [optional] TODO
+     * </pre>
+     */
+    public function __construct($params)
+    {
+        if (empty($params['triggerId'])) {
+            $params['triggerId'] = $this->_randomid();
+        }
+
+        parent::__construct($params);
+    }
+
+    /**
+     * Attach the object to a javascript event.
+     */
+    public function attach()
+    {
+        $params = array(
+            '"' . $this->_params['triggerId'] . '"'
+        );
+
+        $config = $this->_attach(array(
+            'func_replace' => array(),
+            'tokens' => array(',', ';')
+        ));
+
+        if (isset($config['ajax'])) {
+            Horde::addScriptFile('autocomplete.js', 'horde');
+            Horde::addScriptFile('KeyNavList.js', 'horde');
+            Horde::addScriptFile('liquidmetal.js', 'horde');
+            $func = 'Ajax.Autocompleter';
+            $params[] = '"' . $this->_getUrl($config['ajax'], $GLOBALS['registry']->getApp(), array('input' => $this->_params['triggerId'])) . '"';
+        } elseif (isset($config['browser'])) {
+            Horde::addScriptFile('autocomplete.js', 'horde');
+            Horde::addScriptFile('KeyNavList.js', 'horde');
+            Horde::addScriptFile('liquidmetal.js', 'horde');
+            $func = 'Autocompleter.Local';
+            $params[] = $config['browser'];
+            $config['params'] = array_merge(array(
+                'partialSearch' => 1,
+                'fullSearch' => 1,
+                'score' => 1
+            ), $config['params']);
+        } elseif (isset($config['pretty'])) {
+            Horde::addScriptFile('prettyautocomplete.js', 'horde');
+            $func = 'PrettyAutocompleter';
+            $config['params'] = array_merge(array(
+                'boxClass' => 'hordeACBox kronolithLongField',
+                'trigger' => $this->_params['triggerId'],
+                'uri' => $this->_getUrl($config['pretty'], $GLOBALS['registry']->getApp()),
+                'URI_IMG_HORDE' => $GLOBALS['registry']->getImageDir('horde')
+            ), $config['params']);
+
+            if (!empty($this->_params['existing'])) {
+                $config['params']['existing'] = $this->_params['existing'];
+            }
+        } else {
+            return;
+        }
+
+        $js_params = Horde_Serialize::serialize($config['params'], Horde_Serialize::JSON);
+        foreach ($config['func_replace'] as $key => $val) {
+            $js_params = str_replace($key, $val, $js_params);
+        }
+
+        Horde::addScriptFile('effects.js', 'horde');
+
+        Horde::addInlineScript((isset($config['var']) ? $config['var'] . ' ' : '') . 'new ' . $func . '(' . implode(',', $params) . ',' . $js_params . ')', 'dom');
+    }
+
+    /**
+     * Attach the object to a javascript event.
+     *
+     * @return array  An array with the following elements:
+     * <pre>
+     * ONE of the following:
+     * 'ajax' - (string) Use 'Ajax.Autocompleter' class. Value is the AJAX
+     *          function name.
+     * 'browser' - (string) Use 'Autocompleter.Local' class. Value is the
+     *             javascript list of items to search.
+     * 'pretty' - (string) Use 'PrettyAutocompleter' class. Value is the AJAX
+     *            function name.
+     *
+     * Additional Options:
+     * 'func_replace' - (array) Replaces keys with values. Useful for adding
+     *                  functions to javascript parameters output.
+     * 'params' - (array) The list of javascript parameters to pass to the
+     *            autocomplete libraries.
+     * 'var' - (string) If set, the autocomplete object will be assigned to
+     *         this variable.
+     * </pre>
+     */
+    abstract protected function _attach($js_params);
+
+}
index 94bcf8b..c709220 100644 (file)
@@ -24,7 +24,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Initial release.
+ <notes>* Add AutoCompleter driver.
+ * Initial release.
  </notes>
  <contents>
   <dir name="/">
@@ -32,6 +33,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <dir name="Horde">
      <dir name="Ajax">
       <dir name="Imple">
+       <file name="AutoCompleter.php" role="php" />
        <file name="Base.php" role="php" />
        <file name="SpellChecker.php" role="php" />
       </dir> <!-- /lib/Horde/Ajax/Imple -->
@@ -67,6 +69,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
  </dependencies>
  <phprelease>
   <filelist>
+   <install name="lib/Horde/Ajax/Imple/AutoCompleter.php" as="Horde/Ajax/Imple/AutoCompleter.php" />
    <install name="lib/Horde/Ajax/Imple/Base.php" as="Horde/Ajax/Imple/Base.php" />
    <install name="lib/Horde/Ajax/Imple/SpellChecker.php" as="Horde/Ajax/Imple/SpellChecker.php" />
    <install name="lib/Horde/Ajax/Imple.php" as="Horde/Ajax/Imple.php" />