Convert browse javascript to a static js file
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 30 Jul 2010 16:02:16 +0000 (10:02 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 30 Jul 2010 17:43:27 +0000 (11:43 -0600)
turba/js/browse.js [new file with mode: 0644]
turba/lib/Turba.php
turba/lib/View/Browse.php
turba/search.php
turba/templates/browse/javascript.inc [deleted file]

diff --git a/turba/js/browse.js b/turba/js/browse.js
new file mode 100644 (file)
index 0000000..23f69c2
--- /dev/null
@@ -0,0 +1,92 @@
+/**
+ * browse.js
+ */
+
+var TurbaBrowse = {
+    // Defined externally: contact1, contact2, contact3, copy1, copy2,
+    //                     submit1, submit2, delete1
+};
+
+function AnySelected()
+{
+    for (i = 0; i < document.contacts.elements.length; i++) {
+        if (document.contacts.elements[i].checked) {
+            return true;
+        }
+    }
+    return false;
+}
+
+function Add(select)
+{
+    if (!AnySelected()) {
+        window.alert(TurbaBrowse.contact1);
+        return false;
+    }
+
+    key = select[select.selectedIndex].value;
+    if (key == '') {
+        window.alert(TurbaBrowse.contact2);
+        return false;
+    }
+
+    if (key.indexOf(':') == -1 || key.lastIndexOf(':') == key.length - 1) {
+        var newList = window.prompt(TurbaBrowse.contact3, '');
+        if (newList != null && newList != '') {
+            if (key.lastIndexOf(':') == key.length - 1) {
+                key = key.substr(0, key.length - 1);
+            }
+            document.contacts.targetAddressbook.value = key;
+            document.contacts.targetNew.value = 1;
+            document.contacts.targetList.value = newList;
+        } else {
+            return false;
+        }
+    } else {
+        document.contacts.targetList.value = key;
+    }
+
+    Submit('add');
+}
+
+function CopyMove(action, select)
+{
+    if (!AnySelected()) {
+        window.alert(TurbaBrowse.contact1);
+        return false;
+    }
+
+    key = select[select.selectedIndex].value;
+    if (key == '') {
+        window.alert(TurbaBrowse.copymove);
+        return false;
+    }
+
+    document.contacts.targetAddressbook.value = key;
+    Submit(action);
+}
+
+function Submit(action)
+{
+    if (AnySelected()) {
+        if (action != 'delete' || window.confirm(TurbaBrowse.submit)) {
+            document.contacts.actionID.value = action;
+            document.contacts.submit();
+        }
+    } else {
+        window.alert(TurbaBrowse.contact1);
+        return false;
+    }
+}
+
+function SelectAll()
+{
+    for (var i = 0; i < document.contacts.elements.length; i++) {
+        document.contacts.elements[i].checked = document.contacts.checkAll.checked;
+    }
+}
+
+function confirmDelete(name)
+{
+    return window.confirm(TurbaBrowse.confirmdelete.replace('%s', name));
+}
index 46d95b7..fc53c6b 100644 (file)
@@ -621,4 +621,27 @@ class Turba {
         return $menu;
     }
 
+    /**
+     * Add browse.js javascript to page.
+     */
+    public function addBrowseJs()
+    {
+        $js = array();
+        $js_text = array(
+            'confirmdelete' => _("Are you sure that you want to delete %s?"),
+            'contact1' => _("You must select at least one contact first."),
+            'contact2' => ("You must select a target contact list."),
+            'contact3' => _("Please name the new contact list:"),
+            'copymove' => _("You must select a target address book."),
+            'submit' => _("Are you sure that you want to delete the selected contacts?"),
+        );
+
+        foreach ($js_text as $key => $val) {
+            $js[] = 'TurbaBrowse.' . $key . ' = ' . Horde_Serialize::serialize($val, Horde_Serialize::JSON, $GLOBALS['registry']->getCharset());
+        }
+
+        Horde::addScriptFile('browse.js', 'turba');
+        Horde::addInlineScript($js);
+    }
+
 }
index a82af2c..3bf8c33 100644 (file)
@@ -192,7 +192,7 @@ class Turba_View_Browse {
                         if (!is_array($targetDriver->map[$info_key]) ||
                             isset($targetDriver->map[$info_key]['attribute'])) {
                             $objectValue = $object->getValue($info_key);
-                            
+
                             // Get 'data' value if object type is image, the
                             // direct value in other case.
                             $objAttributes[$info_key] = isset($GLOBALS['attributes'][$info_key]) && $GLOBALS['attributes'][$info_key]['type'] == 'image' ? $objectValue['load']['data'] : $objectValue;
@@ -338,7 +338,7 @@ class Turba_View_Browse {
 
         $templates = array();
         if (isset($driver)) {
-            $templates[] = '/browse/javascript.inc';
+            Turba::addBrowseJs();
 
             // Read the columns to display from the preferences.
             $sources = Turba::getColumns();
index b1c65c7..8bbb8c8 100644 (file)
@@ -269,6 +269,10 @@ Horde::addScriptFile('quickfinder.js', 'horde');
 Horde::addScriptFile('effects.js', 'horde');
 Horde::addScriptFile('redbox.js', 'horde');
 Horde::addScriptFile('search.js', 'turba');
+if (isset($view) && is_object($view)) {
+    Turba::addBrowseJs();
+}
+
 require TURBA_TEMPLATES . '/common-header.inc';
 require TURBA_TEMPLATES . '/menu.inc';
 echo $tabs->render($_SESSION['turba']['search_mode']);
@@ -278,7 +282,6 @@ if ($_SESSION['turba']['search_mode'] != 'duplicate') {
     echo $vbookView->render('vbook');
 }
 if (isset($view) && is_object($view)) {
-    require TURBA_TEMPLATES . '/browse/javascript.inc';
     require TURBA_TEMPLATES . '/browse/header.inc';
     $view->display();
 }
diff --git a/turba/templates/browse/javascript.inc b/turba/templates/browse/javascript.inc
deleted file mode 100644 (file)
index 9f536ac..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-<script type="text/javascript">
-
-function AnySelected()
-{
-    for (i = 0; i < document.contacts.elements.length; i++) {
-        if (document.contacts.elements[i].checked) {
-            return true;
-        }
-    }
-    return false;
-}
-
-function Add(select)
-{
-    if (!AnySelected()) {
-        window.alert('<?php echo addslashes(_("You must select at least one contact first.")) ?>');
-        return false;
-    }
-
-    key = select[select.selectedIndex].value;
-    if (key == '') {
-        alert('<?php echo addslashes(_("You must select a target contact list.")) ?>');
-        return false;
-    }
-
-    if (key.indexOf(':') == -1 || key.lastIndexOf(':') == key.length - 1) {
-        var newList = window.prompt('<?php echo addslashes(_("Please name the new contact list:")) ?>\n', '');
-        if (newList != null && newList != '') {
-            if (key.lastIndexOf(':') == key.length - 1) {
-                key = key.substr(0, key.length - 1);
-            }
-            document.contacts.targetAddressbook.value = key;
-            document.contacts.targetNew.value = 1;
-            document.contacts.targetList.value = newList;
-        } else {
-            return false;
-        }
-    } else {
-        document.contacts.targetList.value = key;
-    }
-
-    Submit('add');
-}
-
-function CopyMove(action, select)
-{
-    if (!AnySelected()) {
-        window.alert('<?php echo addslashes(_("You must select at least one contact first.")) ?>');
-        return false;
-    }
-
-    key = select[select.selectedIndex].value;
-    if (key == '') {
-        alert('<?php echo addslashes(_("You must select a target address book.")) ?>');
-        return false;
-    }
-
-    document.contacts.targetAddressbook.value = key;
-    Submit(action);
-}
-
-function Submit(action)
-{
-    if (AnySelected()) {
-        if (action != 'delete' ||
-            confirm('<?php echo addslashes(_("Are you sure that you want to delete the selected contacts?")) ?>')) {
-            document.contacts.actionID.value = action;
-            document.contacts.submit();
-        }
-    } else {
-        window.alert('<?php echo addslashes(_("You must select at least one contact first.")) ?>');
-        return false;
-    }
-}
-
-function SelectAll()
-{
-    for (var i = 0; i < document.contacts.elements.length; i++) {
-        document.contacts.elements[i].checked = document.contacts.checkAll.checked;
-    }
-}
-
-function confirmDelete(name)
-{
-    return confirm('<?php echo addslashes(_("Are you sure that you want to delete %s?")) ?>'.replace('%s', name));
-}
-
-</script>