Skip to content

Commit

Permalink
ENHANCEMENT Allow Requirements::add_i18n_javascript() to return its f…
Browse files Browse the repository at this point in the history
…iles more optimised inclusion, using it in LeftAndMain
  • Loading branch information
chillu committed Jan 6, 2012
1 parent fcc01a3 commit a633326
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
40 changes: 23 additions & 17 deletions admin/code/LeftAndMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions admin/javascript/lang/en_US.js
Original file line number Diff line number Diff line change
@@ -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', {
});
}
33 changes: 22 additions & 11 deletions view/Requirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -887,7 +899,6 @@ function combine_files($combinedFileName, $files) {
}
}
}

$this->combine_files[$combinedFileName] = $files;
}

Expand Down

0 comments on commit a633326

Please sign in to comment.