Permalink
Browse files

ENHANCEMENT Allow Requirements::add_i18n_javascript() to return its f…

…iles more optimised inclusion, using it in LeftAndMain
  • Loading branch information...
1 parent fcc01a3 commit a633326c3e8c0ac438963db9bce276cbdb1dbffc @chillu chillu committed Jan 6, 2012
Showing with 51 additions and 28 deletions.
  1. +23 −17 admin/code/LeftAndMain.php
  2. +6 −0 admin/javascript/lang/en_US.js
  3. +22 −11 view/Requirements.php
View
@@ -222,6 +222,11 @@ function init() {
$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 @@ function init() {
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');
@@ -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', {
+ });
+}
View
@@ -228,12 +228,15 @@ static function include_in_response(SS_HTTPResponse $response) {
/**
* 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 @@ function include_in_response(SS_HTTPResponse $response) {
/**
* 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 @@ function combine_files($combinedFileName, $files) {
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 @@ function combine_files($combinedFileName, $files) {
}
}
}
-
$this->combine_files[$combinedFileName] = $files;
}

0 comments on commit a633326

Please sign in to comment.