Exemple #1
0
function _l10n_inject_js()
{
    $ltr = doSlash(gTxt('l10n-ltr'));
    $rtl = doSlash(gTxt('l10n-rtl'));
    $toggle_title = doSlash(gTxt('l10n-toggle'));
    $langs = MLPLanguageHandler::get_installation_langs();
    $langs = MLPLanguageHandler::do_fleshout_dirs($langs);
    $langs = _l10n_php2js_array('langs', $langs);
    $fn = <<<end_js
var {$langs}
\tvar search_box   = null;
\tvar search_term  = null;
\tvar result_div   = null;
\tvar result_list  = null;
\tvar cresult_div  = null;
\tvar result_num   = null;
\tvar csearch_box  = null;
\tvar csearch_lang = null;
\tvar str_edit_div = null;
\tvar sbn_lang_sel = null;
\tvar last_req     = "";

\tvar\txml_manager = false;
\tif( window.XMLHttpRequest )
\t\t{
\t\txml_manager = new XMLHttpRequest();
\t\t}

function addLoadEvent(func)
\t{
\tvar oldonload = window.onload;
\tif (typeof window.onload != 'function')
\t\t{
\t\twindow.onload = func;
\t\t}
\telse
\t\t{
\t\twindow.onload = function()
\t\t\t{
\t\t\toldonload();
\t\t\tfunc();
\t\t\t}
\t\t}
\t}

function l10nSetCookie(name, value, days)
\t{
\tif (days)
\t\t{
\t\tvar date = new Date();
\t\tdate.setTime(date.getTime() + (days*24*60*60*1000));
\t\tvar expires = '; expires=' + date.toGMTString();
\t\t}
\telse
\t\t{
\t\tvar expires = '';
\t\t}

\tdocument.cookie = name + '=' + value + expires + '; path=/';
\t}

function l10nGetCookie(name)
\t{
\tvar nameEQ = name + '=';

\tvar ca = document.cookie.split(';');

\tfor (var i = 0; i < ca.length; i++)
\t\t{
\t\tvar c = ca[i];

\t\twhile (c.charAt(0)==' ')
\t\t\t{
\t\t\tc = c.substring(1, c.length);
\t\t\t}

\t\tif (c.indexOf(nameEQ) == 0)
\t\t\t{
\t\t\treturn c.substring(nameEQ.length, c.length);
\t\t\t}
\t\t}

\treturn null;
\t}

addLoadEvent( function(){l10n_js_init();} );
function l10n_js_init()
\t{
\tif (!document.getElementById)
\t\t{
\t\treturn false;
\t\t}

\tsearch_box   = document.getElementById('l10n_search_by_name');
\tresult_div   = document.getElementById('l10n_div_sbn_result_list');
\tresult_num   = document.getElementById('l10n_result_count');
\tcsearch_box  = document.getElementById('l10n_search_by_content');
\tcresult_div  = document.getElementById('l10n_sbc_result_list');
\tcsearch_lang = document.getElementById('sbc_lang_selection');
\tstr_edit_div = document.getElementById('l10n_div_string_edit');
\tsbn_lang_sel = document.getElementById('sbn_lang_selection');

\tif( search_box == null )
\t\treturn;

\taddEvent( search_box , 'keyup' , l10nRefineResultsEventHandler , false );

\tvar search_type = getCookie( 'l10n_string_search_by' );
\tif( search_type == null || search_type == '' || search_type == 'name' )
\t\t{
\t\tvar selection = getCookie( 'l10n_string_search_by_subtype' );
\t\tif( selection == null || selection == 'all' )
\t\t\t{
\t\t\tsbn_lang_sel.disabled = true;
\t\t\tselection = '';
\t\t\t}
\t\telse
\t\t\t{
\t\t\tsbn_lang_sel.disabled = false;
\t\t\tselection = sbn_lang_sel.value;
\t\t\t}
\t\tdo_name_search( selection );
\t\t}
\telse
\t\t{
\t\tresult_div.className="l10n_hidden";
\t\tcresult_div.className="l10n_visible";
\t\tdo_content_search();
\t\t}
\t}

function l10nRefineResultsEventHandler(event)
\t{
\tl10nRefineResults();
\t}

function l10nRefineResults()
\t{
\tvar result_list  = document.getElementById('l10n_sbn_result_list');
\tvar target = trim( search_box.value );
\ttarget = target.toLowerCase()
\tvar t_len = target.length;

\t//
\t// Iterate over all strings showing those that match, hiding those that don't...
\t//
\tvar item = result_list.firstChild;
\tvar visible = 0;
\twhile( item != null )
\t\t{
\t\tvar match_text = item.id;
\t\tvar tmp = match_text.substring(0,t_len)
\t\tvar match = (tmp == target);

\t\tif( match )
\t\t\t{
\t\t\titem.className = 'l10n_visible';
\t\t\t++visible;
\t\t\t}
\t\telse
\t\t\t{
\t\t\titem.className = 'l10n_hidden';
\t\t\t}

\t\titem = item.nextSibling;
\t\t}
\tvar result_num   = document.getElementById('l10n_result_count');
\tresult_num.innerHTML = visible;
\tl10nSetCookie( 'search_string_name_live' , target , 365 );
\t}

function trim(term)
\t{
\tvar len = term.length;
\tvar lenm1 = len - 1;

\twhile (term.substring(0,1) == ' ')
\t\t{
\t\tterm = term.substring(1, term.length);
\t\t}
\twhile (term.substring(term.length-1, term.length) == ' ')
\t\t{
\t\tterm = term.substring(0,term.length-1);
\t\t}
\treturn term;
\t}

function make_xml_req(req,req_receiver)
\t{
\tif( !xml_manager || (req_receiver == null) )
\t\treturn false;

\tif( (last_req != req) && (req != '') )
\t\t{
\t\tif( xml_manager && xml_manager.readyState < 4 )
\t\t\t{
\t\t\txml_manager.abort();
\t\t\t}
\t\tif( window.ActiveXObject )
\t\t\t{
\t\t\txml_manager = new ActiveXObject("Microsoft.XMLHTTP");
\t\t\t}

\t\txml_manager.onreadystatechange = req_receiver;
\t\txml_manager.open("GET", req);
\t\txml_manager.send(null);
\t\tlast_req = req;
\t\t}
\t}

function do_name_search( lang )
\t{
\tvar req = "?event=l10n&tab=snippets&step=l10n_search_for_names&l10n-sfn=" + lang;
\tmake_xml_req( req , ns_result_handler );
\t}
function ns_result_handler()
\t{
\tif (xml_manager.readyState == 4)
\t\t{
\t\tvar results = xml_manager.responseText;
\t\tresult_div.innerHTML = results;
\t\tl10nRefineResults();
\t\t}
\t}

function do_content_search()
\t{
\tvar search_term = encodeURI(csearch_box.value);
\tvar search_lang = csearch_lang.value;
\tvar query       = search_term + search_lang;

\tif( search_term != '' )
\t\t{
\t\tvar req = "?event=l10n&tab=snippets&step=l10_search_for_content&l10n-sfc=" + search_term + "&l10n-lang=" + search_lang;
\t\tmake_xml_req( req , cs_result_handler );
\t\tl10nSetCookie( 'search_string_content' , search_term , 365 );
\t\tl10nSetCookie( 'search_string_lang' , search_lang , 365 );
\t\t}
\t}

function cs_result_handler()
\t{
\tif (xml_manager.readyState == 4)
\t\t{
\t\tvar results = xml_manager.responseText;
\t\tcresult_div.innerHTML = results;
\t\t}
\t}

function do_string_edit(id)
\t{
\tvar req = "?event=l10n&tab=snippets&XMLHTTP=true&id=" + id;
\tmake_xml_req( req , string_edit_handler );
\t}


function string_edit_handler()
\t{
\tif (xml_manager.readyState == 4)
\t\t{
\t\tvar results = xml_manager.responseText;
\t\tstr_edit_div.innerHTML = results;
        window.scrollTo(0,128);
\t\t}
\t}

function update_search( id )
\t{
\tvar by_name = document.getElementById('l10n_div_s_by_n');
\tvar by_cont = document.getElementById('l10n_div_s_by_c');
\tif( id == 'sbn_radio_button' )
\t\t{
\t\tby_name.className="l10n_visible";
\t\tby_cont.className="l10n_hidden";
\t\tresult_div.className="l10n_visible";
\t\tcresult_div.className="l10n_hidden";
\t\tl10nSetCookie( 'l10n_string_search_by' , 'name' , 365 );
\t\tvar selection = l10nGetCookie( 'l10n_string_search_by_subtype' );
\t\tif( selection == null || selection == 'all' )
\t\t\tselection = '';
\t\telse
\t\t\tselection = sbn_lang_sel.value;
\t\tdo_name_search( selection );
\t\t}
\telse if ( id == 'sbc_radio_button' )
\t\t{
\t\tby_name.className="l10n_hidden";
\t\tby_cont.className="l10n_visible";
\t\tresult_div.className="l10n_hidden";
\t\tcresult_div.className="l10n_visible";
\t\tl10nSetCookie( 'l10n_string_search_by' , 'cont' , 365 );
\t\tdo_content_search();
\t\t}
\telse if( id == 'sbn_missing_radio_button' )
\t\t{
\t\tsbn_lang_sel.disabled = false;
\t\tvar selection = sbn_lang_sel.value;
\t\tl10nSetCookie( 'l10n_string_search_by_subtype' , 'missing' , 365 );
\t\tdo_name_search( selection );
\t\t}
\telse if( id == 'sbn_all_radio_button' )
\t\t{
\t\tsbn_lang_sel.disabled = true;
\t\tl10nSetCookie( 'l10n_string_search_by_subtype' , 'all' , 365 );
\t\tdo_name_search( '' );
\t\t}
\t}
function on_sbn_lang_change()
\t{
\tvar selection = sbn_lang_sel.value;
\tl10nSetCookie( 'search_string_name_lang' , selection , 365 );
\tdo_name_search( selection );
\t}

function toggleTextElements()
\t{
\ttoggleDirection('title');
\ttoggleDirection('body');
\ttoggleDirection('excerpt');
\t}

function togglePreview()
\t{
\ttoggleDirection('article-main');
\t}

function toggleDirection(id)
\t{
\tif (!document.getElementById)
\t\t{
\t\treturn false;
\t\t}

\tvar textarea = document.getElementById(id + '-data');
\tif( textarea == null )
\t\ttextarea = document.getElementById(id);
\tvar toggler  = document.getElementById(id + '-toggle');

\tif (textarea.style.direction == 'ltr')
\t\t{
\t\ttextarea.style.direction = 'rtl';
\t\tif( toggler != null )
\t\t\ttoggler.innerHTML = '{$rtl}';
\t\t}
\telse
\t\t{
\t\ttextarea.style.direction = 'ltr';
\t\tif( toggler != null )
\t\t\ttoggler.innerHTML = '{$ltr}';
\t\t}
\t}
function resetToggleDir( id , dir )
\t{
\tvar e = document.getElementById(id);
\tif( e == null )
\t\treturn;

\te.style.direction = dir;
\t}
function on_lang_selection_change()
\t{
\tvar selection = document.getElementById('l10n_lang_selector').value;
\tvar dir = langs[selection];

\tl10nSetCookie( 'rendition_lang_selection' , selection , 365 );

\tresetToggleDir( 'title', dir );
\tresetToggleDir( 'body', dir );
\tresetToggleDir( 'excerpt', dir );
\tresetToggleDir( 'article-main', dir );

\tvar toggler = document.getElementById('title-toggle');
\tif( toggler != null )
\t\ttoggler.innerHTML = '{$toggle_title}';

\tvar toggler  = document.getElementById('article-main-toggle');
\tif( toggler != null )
\t\ttoggler.innerHTML = '{$toggle_title}';
\t}

end_js;
    return $fn;
}