From a633326c3e8c0ac438963db9bce276cbdb1dbffc Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 6 Jan 2012 12:00:04 +0100 Subject: [PATCH] ENHANCEMENT Allow Requirements::add_i18n_javascript() to return its files more optimised inclusion, using it in LeftAndMain --- admin/code/LeftAndMain.php | 40 +++++++++++++++++++--------------- admin/javascript/lang/en_US.js | 6 +++++ view/Requirements.php | 33 ++++++++++++++++++---------- 3 files changed, 51 insertions(+), 28 deletions(-) create mode 100644 admin/javascript/lang/en_US.js diff --git a/admin/code/LeftAndMain.php b/admin/code/LeftAndMain.php index a29d91b80..d656c13ff 100644 --- a/admin/code/LeftAndMain.php +++ b/admin/code/LeftAndMain.php @@ -222,6 +222,11 @@ class LeftAndMain extends Controller { $htmlEditorConfig->setOption('content_css', implode(',', $cssFiles)); } + // Using uncompressed files as they'll be processed by JSMin in the Requirements class. + // Not as effective as other compressors or pre-compressed+finetuned files, + // but overall the unified minification into a single file brings more performance benefits + // than a couple of saved bytes (after gzip) in individual files. + // We also re-compress already compressed files through JSMin as this causes weird runtime bugs. Requirements::combine_files( 'lib.js', array( @@ -261,28 +266,29 @@ class LeftAndMain extends Controller { CMS_DIR . '/javascript/ThumbnailStripField.js', ) ); - + HTMLEditorField::include_js(); Requirements::combine_files( 'leftandmain.js', - array( - SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.js', - SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Panel.js', - SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Tree.js', - SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Ping.js', - SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Content.js', - SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.EditForm.js', - SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Menu.js', - SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.AddForm.js', - SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Preview.js', - SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.BatchActions.js', - ) + array_unique(array_merge( + array( + SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.js', + SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Panel.js', + SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Tree.js', + SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Ping.js', + SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Content.js', + SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.EditForm.js', + SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Menu.js', + SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.AddForm.js', + SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Preview.js', + SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.BatchActions.js', + ), + Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang', true, true), + Requirements::add_i18n_javascript(SAPPHIRE_ADMIN_DIR . '/javascript/lang', true, true) + )) ); - - Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang'); - Requirements::add_i18n_javascript(SAPPHIRE_ADMIN_DIR . '/javascript/lang'); - + Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery-ui.css'); Requirements::css(SAPPHIRE_ADMIN_DIR .'/thirdparty/chosen/chosen/chosen.css'); Requirements::css(THIRDPARTY_DIR . '/jstree/themes/apple/style.css'); diff --git a/admin/javascript/lang/en_US.js b/admin/javascript/lang/en_US.js new file mode 100644 index 000000000..be328c166 --- /dev/null +++ b/admin/javascript/lang/en_US.js @@ -0,0 +1,6 @@ +if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') { + if(typeof(console) != 'undefined') console.error('Class ss.i18n not defined'); +} else { + ss.i18n.addDictionary('en_US', { + }); +} diff --git a/view/Requirements.php b/view/Requirements.php index 30f889bc1..7703f7f4a 100644 --- a/view/Requirements.php +++ b/view/Requirements.php @@ -228,12 +228,15 @@ class Requirements { /** * Add i18n files from the given javascript directory. - * @param $langDir The javascript lang directory, relative to the site root, e.g., 'sapphire/javascript/lang' + * + * @param String + * @param Boolean + * @param Boolean * * See {@link Requirements_Backend::add_i18n_javascript()} for more information. */ - public static function add_i18n_javascript($langDir) { - return self::backend()->add_i18n_javascript($langDir); + public static function add_i18n_javascript($langDir, $return = false, $langOnly = false) { + return self::backend()->add_i18n_javascript($langDir, $return, $langOnly); } /** @@ -739,23 +742,33 @@ class Requirements_Backend { /** * Add i18n files from the given javascript directory. Sapphire expects that the given directory * will contain a number of java script files named by language: en_US.js, de_DE.js, etc. - * @param $langDir The javascript lang directory, relative to the site root, e.g., 'sapphire/javascript/lang' + * + * @param String The javascript lang directory, relative to the site root, e.g., 'sapphire/javascript/lang' + * @param Boolean Return all relative file paths rather than including them in requirements + * @param Boolean Only include language files, not the base libraries */ - public function add_i18n_javascript($langDir) { + public function add_i18n_javascript($langDir, $return = false, $langOnly = false) { + $files = array(); if(i18n::get_js_i18n()) { // Include i18n.js even if no languages are found. The fact that // add_i18n_javascript() was called indicates that the methods in // here are needed. - $this->javascript(SAPPHIRE_DIR . '/javascript/i18n.js'); + if(!$langOnly) $files[] = SAPPHIRE_DIR . '/javascript/i18n.js'; if(substr($langDir,-1) != '/') $langDir .= '/'; - $this->javascript($langDir . i18n::default_locale() . '.js'); - $this->javascript($langDir . i18n::get_locale() . '.js'); + $files[] = $langDir . i18n::default_locale() . '.js'; + $files[] = $langDir . i18n::get_locale() . '.js'; // Stub i18n implementation for when i18n is disabled. } else { - $this->javascript[SAPPHIRE_DIR . '/javascript/i18nx.js'] = true; + if(!$langOnly) $files[] = SAPPHIRE_DIR . '/javascript/i18nx.js'; + } + + if($return) { + return $files; + } else { + foreach($files as $file) $this->javascript($file); } } @@ -848,7 +861,6 @@ class Requirements_Backend { return false; } } - foreach($files as $index=>$file) { if(is_array($file)) { // Either associative array path=>path type=>type or numeric 0=>path 1=>type @@ -887,7 +899,6 @@ class Requirements_Backend { } } } - $this->combine_files[$combinedFileName] = $files; }