Horde::addInlineJsVars() tweaks.
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 9 Nov 2010 05:26:51 +0000 (22:26 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 9 Nov 2010 05:46:14 +0000 (22:46 -0700)
Move optional parameters into single $opts parameter.
Add parameter to load definitions at top of stack.

12 files changed:
framework/Core/lib/Horde.php
framework/Core/lib/Horde/Script/Files.php
imp/lib/Ajax/Imple/ContactAutoCompleter.php
imp/lib/Ui/Compose.php
imp/lib/Views/Compose.php
imp/message-dimp.php
imp/search.php
imp/templates/dimp/javascript_defs_dimp.php
imp/templates/imp/javascript_defs.php
ingo/lib/Ingo.php
kronolith/lib/Kronolith.php
kronolith/templates/javascript_defs.php

index fea39c6..ab10487 100644 (file)
@@ -1789,17 +1789,31 @@ HTML;
     /**
      * Add inline javascript variable definitions to the output buffer.
      *
-     * @param array $data     Keys are the variable names, values are the data
-     *                        to JSON encode.  If the key begins with a '-', the
-     *                        data will be added to the output as-is.
-     * @param boolean $ret    If true, will return the list of variable
-     *                        definitions instead of outputting to page.
-     * @param string $onload  Wrap the definition in an onload handler?
-     *                        Either 'dom' (on dom:loaded), 'load'.
-     */
-    static public function addInlineJsVars($data, $ret = false, $onload = null)
+     * @param array $data  Keys are the variable names, values are the data
+     *                     to JSON encode.  If the key begins with a '-',
+     *                     the data will be added to the output as-is.
+     * @param array $opts  Options:
+     * <pre>
+     * onload - (string) Wrap the definition in an onload handler? Either
+     *          'dom' (on dom:loaded), 'load'.
+     *          DEFAULT: false
+     * ret_vars - (boolean) If true, will return the list of variable
+     *            definitions instead of outputting to page.
+     *            DEFAULT: false
+     * top - (boolean) Add definitions to top of stack?
+     *       DEFAULT: false
+     * </pre>
+     *
+     * @return array  Returns the variable list of 'ret_vars' option is true.
+     */
+    static public function addInlineJsVars($data, array $opts = array())
     {
         $out = array();
+        $opts = array_merge(array(
+            'onload' => null,
+            'ret_vars' => false,
+            'top' => false
+        ), $opts);
 
         foreach ($data as $key => $val) {
             if ($key[0] == '-') {
@@ -1811,11 +1825,11 @@ HTML;
             $out[] = $key . '=' . $val;
         }
 
-        if ($ret) {
+        if ($opts['ret_vars']) {
             return $out;
-        } else {
-            self::addInlineScript($out, $onload);
         }
+
+        self::addInlineScript($out, $opts['onload'], $opts['top']);
     }
 
     /**
index 9684fd8..311c97d 100644 (file)
@@ -111,7 +111,7 @@ class Horde_Script_Files
         if (($file == 'popup.js') && ($app == 'horde')) {
             Horde::addInlineJsVars(array(
                 'Horde.popup_block_text' => Horde_Core_Translation::t("A popup window could not be opened. Your browser may be blocking popups.")
-            ), 'dom');
+            ), array('onload' => 'dom'));
         }
 
         if ($file[0] == '/') {
index eb0bf34..0427d0e 100644 (file)
@@ -70,7 +70,7 @@ class IMP_Ajax_Imple_ContactAutoCompleter extends Horde_Core_Ajax_Imple_AutoComp
                     'if (!window.IMP) window.IMP = {}'
                 ), Horde::addInlineJsVars(array(
                     'IMP.ac_list' => $addrlist
-                ), true)));
+                ), array('ret_vars' => true))));
                 self::$_listOutput = true;
             }
 
index d9dd4dd..651eef9 100644 (file)
@@ -350,7 +350,7 @@ class IMP_Ui_Compose
 
         return Horde::addInlineJsVars(array(
             'IMP_Compose_Base.identities' => $identities
-        ), true);
+        ), array('ret_vars' => true));
     }
 
     /**
index 3919e01..3ba3169 100644 (file)
@@ -112,7 +112,7 @@ class IMP_Views_Compose
                 }
                 $result['js'] = array_merge($result['js'], Horde::addInlineJsVars(array(
                     'DIMP.conf_compose.flist' => $flist
-                ), true));
+                ), array('ret_vars' => true)));
             }
 
             $compose_link = Horde::getServiceLink('ajax', 'imp');
@@ -170,7 +170,7 @@ class IMP_Views_Compose
         } else {
             $result['js'] = array_merge($result['js'], Horde::addInlineJsVars(array(
                 '-DIMP.conf_compose.redirect' => 1
-            ), true));
+            ), array('ret_vars' => true)));
         }
 
         $t->set('bcc', $prefs->getValue('compose_bcc'));
index e9a0fea..aa27582 100644 (file)
@@ -67,7 +67,7 @@ foreach (array('from', 'to', 'cc', 'bcc', 'replyTo', 'log', 'uid', 'mailbox') as
         $js_vars['DimpFullmessage.' . $val] = $show_msg_result[$val];
     }
 }
-$js_out = Horde::addInlineJsVars($js_vars, true);
+$js_out = Horde::addInlineJsVars($js_vars, array('ret_vars' => true));
 
 /* Determine if compose mode is disabled. */
 $disable_compose = !IMP::canCompose();
index 3973283..2230634 100644 (file)
@@ -445,7 +445,7 @@ Horde::addInlineJsVars(array_merge($js_vars, array(
         'or' => _("OR"),
         'search_term' => _("Search Term:")
     )
-)), false, 'dom');
+)), array('onload' => 'dom'));
 
 if ($dimp_view) {
     if (!$vars->edit_query) {
index 53f6a68..f0bfec8 100644 (file)
@@ -240,6 +240,6 @@ if ($compose_page) {
     }
 }
 
-Horde::addInlineScript(array(
-    'var DIMP = ' . Horde_Serialize::serialize($code, Horde_Serialize::JSON, 'UTF-8')
-), null, true);
+Horde::addInlineJsVars(array(
+    'var DIMP' => $code
+), array('top' => true));
index 786c3b8..898e2db 100644 (file)
@@ -70,6 +70,6 @@ $code = array(
     )
 );
 
-Horde::addInlineScript(array(
-    'var IMP = ' . Horde_Serialize::serialize($code, Horde_Serialize::JSON, 'UTF-8')
-), null, true);
+Horde::addInlineJsVars(array(
+    'var IMP' => $code
+), array('top' => true));
index ddf1ffb..98b1e64 100644 (file)
@@ -460,8 +460,8 @@ class Ingo
     {
         if ($GLOBALS['registry']->hasMethod('mail/createFolder')) {
             Horde::addScriptFile('new_folder.js', 'ingo');
-            Horde::addInlineScript(array(
-                'IngoNewFolder.folderprompt = ' . Horde_Serialize::serialize(_("Please enter the name of the new folder:"), Horde_Serialize::JSON)
+            Horde::addInlineJsVars(array(
+                'IngoNewFolder.folderprompt' => _("Please enter the name of the new folder:")
             ));
         }
     }
index 98befd5..65ea2b1 100644 (file)
@@ -335,7 +335,9 @@ class Kronolith
         // Maps
         $code['conf']['maps'] = $GLOBALS['conf']['maps'];
 
-        return array('var Kronolith = ' . Horde_Serialize::serialize($code, Horde_Serialize::JSON) . ';');
+        return Horde::addInlineJsVars(array(
+            'var Kronolith' => $code
+        ), array('ret_vars' => true));
     }
 
     /**
index b20c19d..02d4a03 100644 (file)
@@ -21,10 +21,9 @@ $gettext = array(
     'loading' => _("Loading ..."),
 );
 
-?>
-<script type="text/javascript">//<![CDATA[
-var KronolithDate = new Date(<?php printf('%d, %d, %d', $currentDate->year, $currentDate->month - 1, $currentDate->mday) ?>);
-var KronolithText = <?php echo Horde_Serialize::serialize($gettext, Horde_Serialize::JSON) ?>;
-var KronolithVar = <?php echo Horde_Serialize::serialize($var, Horde_Serialize::JSON) ?>;
-var KronolithView = '<?php if (isset($view) && is_object($view)) echo $view->getName() ?>';
-//]]></script>
+Horde::addInlineJsVars(array(
+    '-var KronolithDate' => 'new Date(' . sprintf('%d, %d, %d', $currentDate->year, $currentDate->month - 1, $currentDate->mday) . ')',
+    'var KronolithText' => $gettext,
+    'var KronolithVar' => $var,
+    'var KronolithView' => (isset($view) && is_object($view)) ? $view->getName() : ''
+), array('top' => true));