From 8a48c691e1aca573ea14cea1dfe16c92ed128e59 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Mon, 8 Nov 2010 22:26:51 -0700 Subject: [PATCH] Horde::addInlineJsVars() tweaks. Move optional parameters into single $opts parameter. Add parameter to load definitions at top of stack. --- framework/Core/lib/Horde.php | 38 ++++++++++++++++++++--------- framework/Core/lib/Horde/Script/Files.php | 2 +- imp/lib/Ajax/Imple/ContactAutoCompleter.php | 2 +- imp/lib/Ui/Compose.php | 2 +- imp/lib/Views/Compose.php | 4 +-- imp/message-dimp.php | 2 +- imp/search.php | 2 +- imp/templates/dimp/javascript_defs_dimp.php | 6 ++--- imp/templates/imp/javascript_defs.php | 6 ++--- ingo/lib/Ingo.php | 4 +-- kronolith/lib/Kronolith.php | 4 ++- kronolith/templates/javascript_defs.php | 13 +++++----- 12 files changed, 50 insertions(+), 35 deletions(-) diff --git a/framework/Core/lib/Horde.php b/framework/Core/lib/Horde.php index fea39c6a7..ab1048758 100644 --- a/framework/Core/lib/Horde.php +++ b/framework/Core/lib/Horde.php @@ -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: + *
+     * 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
+     * 
+ * + * @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']); } /** diff --git a/framework/Core/lib/Horde/Script/Files.php b/framework/Core/lib/Horde/Script/Files.php index 9684fd86d..311c97d6b 100644 --- a/framework/Core/lib/Horde/Script/Files.php +++ b/framework/Core/lib/Horde/Script/Files.php @@ -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] == '/') { diff --git a/imp/lib/Ajax/Imple/ContactAutoCompleter.php b/imp/lib/Ajax/Imple/ContactAutoCompleter.php index eb0bf3426..0427d0eb3 100644 --- a/imp/lib/Ajax/Imple/ContactAutoCompleter.php +++ b/imp/lib/Ajax/Imple/ContactAutoCompleter.php @@ -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; } diff --git a/imp/lib/Ui/Compose.php b/imp/lib/Ui/Compose.php index d9dd4dd6a..651eef9bf 100644 --- a/imp/lib/Ui/Compose.php +++ b/imp/lib/Ui/Compose.php @@ -350,7 +350,7 @@ class IMP_Ui_Compose return Horde::addInlineJsVars(array( 'IMP_Compose_Base.identities' => $identities - ), true); + ), array('ret_vars' => true)); } /** diff --git a/imp/lib/Views/Compose.php b/imp/lib/Views/Compose.php index 3919e01b8..3ba31698a 100644 --- a/imp/lib/Views/Compose.php +++ b/imp/lib/Views/Compose.php @@ -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')); diff --git a/imp/message-dimp.php b/imp/message-dimp.php index e9a0fea1b..aa275820d 100644 --- a/imp/message-dimp.php +++ b/imp/message-dimp.php @@ -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(); diff --git a/imp/search.php b/imp/search.php index 397328376..2230634a7 100644 --- a/imp/search.php +++ b/imp/search.php @@ -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) { diff --git a/imp/templates/dimp/javascript_defs_dimp.php b/imp/templates/dimp/javascript_defs_dimp.php index 53f6a68ab..f0bfec84a 100644 --- a/imp/templates/dimp/javascript_defs_dimp.php +++ b/imp/templates/dimp/javascript_defs_dimp.php @@ -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)); diff --git a/imp/templates/imp/javascript_defs.php b/imp/templates/imp/javascript_defs.php index 786c3b8ae..898e2dbe3 100644 --- a/imp/templates/imp/javascript_defs.php +++ b/imp/templates/imp/javascript_defs.php @@ -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)); diff --git a/ingo/lib/Ingo.php b/ingo/lib/Ingo.php index ddf1ffb23..98b1e6480 100644 --- a/ingo/lib/Ingo.php +++ b/ingo/lib/Ingo.php @@ -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:") )); } } diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index 98befd538..65ea2b14f 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -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)); } /** diff --git a/kronolith/templates/javascript_defs.php b/kronolith/templates/javascript_defs.php index b20c19d11..02d4a03e2 100644 --- a/kronolith/templates/javascript_defs.php +++ b/kronolith/templates/javascript_defs.php @@ -21,10 +21,9 @@ $gettext = array( 'loading' => _("Loading ..."), ); -?> - +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)); -- 2.11.0