Modernize javascript.
authorJan Schneider <jan@horde.org>
Wed, 9 Jun 2010 21:44:17 +0000 (23:44 +0200)
committerJan Schneider <jan@horde.org>
Mon, 5 Jul 2010 10:34:43 +0000 (12:34 +0200)
turba/js/search.js [new file with mode: 0644]
turba/search.php
turba/templates/search/advanced.html.php [new file with mode: 0644]
turba/templates/search/advanced.inc [deleted file]
turba/templates/search/basic.inc
turba/templates/search/header.inc

diff --git a/turba/js/search.js b/turba/js/search.js
new file mode 100644 (file)
index 0000000..108a248
--- /dev/null
@@ -0,0 +1,40 @@
+/**
+ * Provides the javascript for the search.php script (basic view).
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file LICENSE for license information (ASL).  If you
+ * did not receive this file, see http://www.horde.org/licenses/asl.php.
+ *
+ * @author Jan Schneider <jan@horde.org>
+ */
+
+TurbaSearch = {
+
+    criteria: {},
+    shareSources: {},
+
+    updateCriteria: function()
+    {
+        if (!$('turbaSearchSource').options) {
+            return;
+        }
+
+        var source = $F('turbaSearchSource');
+
+        $('turbaSearchCriteria').update();
+        $H(this.criteria[source]).each(function(criterion) {
+            $('turbaSearchCriteria').insert(new Element('option', { value: criterion.key }).insert(criterion.value.escapeHTML()));
+        });
+
+        if ($('vbook-form')) {
+            if (this.shareSources[source] == true) {
+                $('vbook-form').show();
+            } else {
+                $('vbook-form').hide();
+            }
+        }
+    }
+}
+
+document.observe('dom:loaded', TurbaSearch.updateCriteria.bind(TurbaSearch));
index 8769e91..ba6f414 100644 (file)
@@ -188,6 +188,7 @@ if ($_SESSION['turba']['search_mode'] == 'basic') {
 Horde::addScriptFile('quickfinder.js', 'horde');
 Horde::addScriptFile('effects.js', 'horde');
 Horde::addScriptFile('redbox.js', 'horde');
+Horde::addScriptFile('search.js', 'turba');
 require TURBA_TEMPLATES . '/common-header.inc';
 require TURBA_TEMPLATES . '/menu.inc';
 require TURBA_TEMPLATES . '/search/header.inc';
diff --git a/turba/templates/search/advanced.html.php b/turba/templates/search/advanced.html.php
new file mode 100644 (file)
index 0000000..b173e33
--- /dev/null
@@ -0,0 +1,31 @@
+<table cellspacing="5" width="100%">
+ <tr>
+  <td>&nbsp;</td>
+  <td>
+   <input type="submit" class="button" name="search" value="<?php echo _("Search") ?>" />
+   <input type="reset" class="button" name="reset" value="<?php echo _("Reset to Defaults") ?>" />
+  </td>
+ </tr>
+ <?php if ($source_count > 1): ?>
+ <tr>
+  <td class="rightAlign"><strong><?php echo Horde::label('source', _("Address Book")) ?></strong></td>
+  <td class="leftAlign">
+   <select id="source" name="source" onchange="directory_search.submit()">
+    <?php echo $source_options ?>
+   </select>
+  </td>
+ </tr>
+<?php endif; ?>
+<?php
+foreach ($map as $name => $v) {
+    if (substr($name, 0, 2) != '__') {
+?>
+ <tr>
+  <td width="1%" class="nowrap rightAlign" ><strong><?php echo Horde::label($name, $GLOBALS['attributes'][$name]['label']) ?></strong></td>
+  <td class="leftAlign"><input type="text" size="30" id="<?php echo $name ?>" name="<?php echo $name ?>" value="<?php echo isset($criteria[$name]) ? htmlspecialchars($criteria[$name]) : '' ?>" /></td>
+ </tr>
+<?php
+    }
+}
+?>
+</table>
diff --git a/turba/templates/search/advanced.inc b/turba/templates/search/advanced.inc
deleted file mode 100644 (file)
index b173e33..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-<table cellspacing="5" width="100%">
- <tr>
-  <td>&nbsp;</td>
-  <td>
-   <input type="submit" class="button" name="search" value="<?php echo _("Search") ?>" />
-   <input type="reset" class="button" name="reset" value="<?php echo _("Reset to Defaults") ?>" />
-  </td>
- </tr>
- <?php if ($source_count > 1): ?>
- <tr>
-  <td class="rightAlign"><strong><?php echo Horde::label('source', _("Address Book")) ?></strong></td>
-  <td class="leftAlign">
-   <select id="source" name="source" onchange="directory_search.submit()">
-    <?php echo $source_options ?>
-   </select>
-  </td>
- </tr>
-<?php endif; ?>
-<?php
-foreach ($map as $name => $v) {
-    if (substr($name, 0, 2) != '__') {
-?>
- <tr>
-  <td width="1%" class="nowrap rightAlign" ><strong><?php echo Horde::label($name, $GLOBALS['attributes'][$name]['label']) ?></strong></td>
-  <td class="leftAlign"><input type="text" size="30" id="<?php echo $name ?>" name="<?php echo $name ?>" value="<?php echo isset($criteria[$name]) ? htmlspecialchars($criteria[$name]) : '' ?>" /></td>
- </tr>
-<?php
-    }
-}
-?>
-</table>
index f20cc85..f39e68c 100644 (file)
@@ -1,50 +1,17 @@
 <script type="text/javascript">
-<?php echo $js_criteria ?>
-function updateCriteria()
-{
-    var f = document.directory_search;
-    if (!f.source.options) {
-        return;
-    }
-
-    while (f.criteria.length > 0) {
-        f.criteria.options[f.criteria.length - 1] = null;
-    }
-
-    var index = f.source.selectedIndex;
-    if (!index) {
-        index = 0;
-    }
-
-    for (var i = 0; i < criteriaOptions.length; i++) {
-        if (criteriaOptions[i][0] == f.source.options[index].value) {
-            for (var j = 1; j < criteriaOptions[i].length; j++) {
-                f.criteria.options[f.criteria.length] = new Option(criteriaOptions[i][j][1], criteriaOptions[i][j][0]);
-            }
-            f.criteria.selectedIndex = 0;
-            if ($('vbook-form')) {
-                if (shareSources[criteriaOptions[i][0]] == true) {
-                    Element.show('vbook-form');
-                } else {
-                    Element.hide('vbook-form');
-                }
-            }
-            return;
-        }
-    }
-}
-Event.observe(window, 'load', updateCriteria);
+TurbaSearch.criteria = <?php echo json_encode($criteria) ?>;
+TurbaSearch.shareSources = <?php echo json_encode($shareSources) ?>;
 </script>
 
 <?php if ($source_count > 1): ?>
  <strong><?php echo Horde::label('source', _("From")) ?></strong>
- <select id="source" name="source" onchange="updateCriteria();">
+ <select id="turbaSearchSource" name="source" onchange="TurbaSearch.updateCriteria();">
   <?php echo $source_options ?>
  </select>
 <?php endif; ?>
 
  <strong><?php echo Horde::label('criteria', _("Find")) ?></strong>
- <select id="criteria" name="criteria">
+ <select id="turbaSearchCriteria" name="criteria">
   <?php echo $criteria_options ?>
  </select>
 
index b3a1a4e..7862d84 100644 (file)
@@ -6,20 +6,20 @@ $js_criteria = "var criteriaOptions = [];\n"
     . "var shareSources = [];\n";
 
 $source_count = 0;
+$criteria = $shareSources = array();
 foreach (Turba::getAddressBooks() as $key => $entry) {
-    $js_criteria .= "criteriaOptions[$source_count] = []\n"
-        . "criteriaOptions[$source_count][0] = '$key';\n";
+    $criteria[$key] = array();
 
     /* Build the criteria select widget. */
     $field_count = 1;
     foreach ($entry['search'] as $field) {
-        $js_criteria .= "criteriaOptions[$source_count][$field_count] = ['$field', '" . $GLOBALS['attributes'][$field]['label'] . "'];\n";
         if ($key == $source) {
             $selected = ($criteria == $field) ? ' selected="selected"' : '';
             $criteria_options .= "<option value=\"$field\"$selected>" .
                 htmlspecialchars($GLOBALS['attributes'][$field]['label']) . "</option>\n";
         }
         $field_count++;
+        $criteria[$key][$field] = $GLOBALS['attributes'][$field]['label'];
     }
 
     $selected = ($key == $source) ? ' selected="selected"' : '';
@@ -30,11 +30,7 @@ foreach (Turba::getAddressBooks() as $key => $entry) {
     $source_count++;
 
     /* Remember vbooks and sources that are using shares. */
-    if ($entry['type'] != 'vbook') {
-        $js_criteria .= "shareSources['$key'] = true;\n";
-    } else {
-        $js_criteria .= "shareSources['$key'] = false;\n";
-    }
+    $shareSources[$key] = $entry['type'] != 'vbook';
 }
 
 /* Build search mode tabs. */
@@ -50,5 +46,5 @@ echo $tabs->render($_SESSION['turba']['search_mode']);
 <form name="directory_search" action="search.php" method="get" onsubmit="RedBox.loading(); return true;">
 <?php echo Horde_Util::formInput() ?>
 <?php if ($source_count == 1): ?>
- <input type="hidden" name="source" value="<?php echo $unique_source ?>" />
+ <input type="hidden" id="turbaSearchSource" name="source" value="<?php echo $unique_source ?>" />
 <?php endif; ?>