JS localization from mdawaffe. fixes #3911

git-svn-id: http://svn.automattic.com/wordpress/trunk@4968 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-03-06 00:39:46 +00:00
parent 4df232e71e
commit 682fcc5d4a
2 changed files with 91 additions and 8 deletions

View File

@ -1,7 +1,24 @@
<?php @require_once('../../wp-config.php'); cache_javascript_headers(); ?>
addLoadEvent(function(){theList=new listMan();});
function deleteSomething(what,id,message,obj){if(!obj)obj=theList;if(!message)message="<?php printf(js_escape(__('Are you sure you want to delete this %s?')),"'+what+'"); ?>";if(confirm(message))return obj.ajaxDelete(what,id);else return false;}
function dimSomething(what,id,dimClass,obj){if(!obj)obj=theList;return obj.ajaxDimmer(what,id,dimClass);}
addLoadEvent( function() {
if ( 'undefined' != typeof listManL10n )
Object.extend(listMan.prototype, listManL10n);
theList = new listMan();
} );
function deleteSomething( what, id, message, obj ) {
if ( !obj )
obj=theList;
if ( !message )
message = obj.delText.replace(/%/g, what);
if( confirm(message) )
return obj.ajaxDelete( what, id );
else return false;
}
function dimSomething( what, id, dimClass, obj ) {
if ( !obj )
obj = theList;
return obj.ajaxDimmer(what,id,dimClass);
}
var listMan = Class.create();
Object.extend(listMan.prototype, {
@ -19,6 +36,9 @@ Object.extend(listMan.prototype, {
dataStore: null,
formStore: null,
jumpText: 'Jump to new item',
delText: 'Are you sure you want to delete this %s?',
initialize: function(theListId) {
this.theList = $(theListId ? theListId : 'the-list');
if ( !this.theList )
@ -53,7 +73,7 @@ Object.extend(listMan.prototype, {
if ( m )
showLinkMessage += m;
else
showLinkMessage += "<a href='#" + what + '-' + id + "'><?php echo js_escape(__('Jump to new item')); ?>";
showLinkMessage += "<a href='#" + what + '-' + id + "'>" + tempObj.jumpText + "</a>";
});
if ( tempObj.showLink && showLinkMessage )
Element.update(ajaxAdd.myResponseElement,"<div id='jumplink' class='updated fade'><p>" + showLinkMessage + "</p></div>");
@ -127,7 +147,7 @@ Object.extend(listMan.prototype, {
Element.cleanWhitespace(this.theList);
var id = this.topAdder ? this.theList.firstChild.id : this.theList.lastChild.id;
if ( this.alt )
if ( this.theList.childNodes.length % 2 )
if ( ( this.theList.childNodes.length + this.altOffset ) % 2 )
Element.addClassName($(id),this.alt);
Fat.fade_element(id);
},

View File

@ -21,7 +21,11 @@ class WP_Scripts {
$this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.0-0');
$this->add( 'autosave', '/wp-includes/js/autosave-js.php', array('prototype', 'sack'), '20070116');
$this->add( 'wp-ajax', '/wp-includes/js/wp-ajax-js.php', array('prototype'), '20070118');
$this->add( 'listman', '/wp-includes/js/list-manipulation-js.php', array('wp-ajax', 'fat'), '20070118');
$this->add( 'listman', '/wp-includes/js/list-manipulation.js', array('wp-ajax', 'fat'), '20070305');
$this->localize( 'listman', 'listManL10n', array(
'jumpText' => __('Jump to new item'),
'delText' => __('Are you sure you want to delete this %s?')
) );
$this->add( 'scriptaculous-root', '/wp-includes/js/scriptaculous/wp-scriptaculous.js', array('prototype'), '1.7.0');
$this->add( 'scriptaculous-builder', '/wp-includes/js/scriptaculous/builder.js', array('scriptaculous-root'), '1.7.0');
$this->add( 'scriptaculous-dragdrop', '/wp-includes/js/scriptaculous/dragdrop.js', array('scriptaculous-builder', 'scriptaculous-effects'), '1.7.0');
@ -83,12 +87,31 @@ class WP_Scripts {
$src = add_query_arg('ver', $ver, $src);
$src = apply_filters( 'script_loader_src', $src );
echo "<script type='text/javascript' src='$src'></script>\n";
$this->print_scripts_l10n( $handle );
}
$this->printed[] = $handle;
}
}
}
function print_scripts_l10n( $handle ) {
if ( empty($this->scripts[$handle]->l10n_object) || empty($this->scripts[$handle]->l10n) || !is_array($this->scripts[$handle]->l10n) )
return;
$object_name = $this->scripts[$handle]->l10n_object;
echo "<script type='text/javascript'>\n";
echo "/* <![CDATA[ */\n";
echo "\t$object_name = {\n";
$eol = '';
foreach ( $this->scripts[$handle]->l10n as $var => $val ) {
echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
$eol = ",\n";
}
echo "\n\t}\n";
echo "/* ]]> */\n";
echo "</script>\n";
}
/**
* Determines dependencies of scripts
@ -140,6 +163,22 @@ class WP_Scripts {
return true;
}
/**
* Localizes a script
*
* Localizes only if script has already been added
*
* @param string handle Script name
* @param string object_name Name of JS object to hold l10n info
* @param array l10n Array of JS var name => localized string
* @return bool Successful localization
*/
function localize( $handle, $object_name, $l10n ) {
if ( !isset($this->scripts[$handle]) )
return false;
return $this->scripts[$handle]->localize( $object_name, $l10n );
}
function remove( $handles ) {
foreach ( (array) $handles as $handle )
unset($this->scripts[$handle]);
@ -182,7 +221,8 @@ class _WP_Script {
var $src;
var $deps = array();
var $ver = false;
var $args = false;
var $l10n_object = '';
var $l10n = array();
function _WP_Script() {
@list($this->handle, $this->src, $this->deps, $this->ver) = func_get_args();
@ -191,6 +231,14 @@ class _WP_Script {
if ( !$this->ver )
$this->ver = false;
}
function localize( $object_name, $l10n ) {
if ( !$object_name || !is_array($l10n) )
return false;
$this->l10n_object = $object_name;
$this->l10n = $l10n;
return true;
}
}
/**
@ -227,6 +275,21 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false ) {
$wp_scripts->add( $handle, $src, $deps, $ver );
}
/**
* Localizes a script
*
* Localizes only if script has already been added
*
* @see WP_Script::localize()
*/
function wp_localize_script( $handle, $object_name, $l10n ) {
global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )
return false;
return $wp_scripts->localize( $handle, $object_name, $l10n );
}
function wp_deregister_script( $handle ) {
global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )