Changeset 372 for trunk


Ignore:
Timestamp:
01/30/2010 01:17:04 AM (2 years ago)
Author:
ofer
Message:

Fix for progressbar, use jQuery caller to compress script further

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/plugin/transposh/js/transposh.js

    r370 r372  
    1818/*global Date, Math, Microsoft, alert, clearTimeout, document, google, jQuery, setTimeout, t_jp, window */ 
    1919// fetch translation from google translate... 
    20 (function () { // closure 
     20// We first try to avoid conflict with other frameworks 
     21jQuery.noConflict(); 
     22 
     23(function ($) { // closure 
    2124    var langLoaded, loadLang, getMSN, 
    2225    // number of phrases that might be translated 
     
    3942    function fix_page(token, translation) { 
    4043        // Todo - Probably not needed, but in case we get bad stuff 
    41         if (jQuery.trim(translation).length === 0) { 
     44        if ($.trim(translation).length === 0) { 
    4245            return; 
    4346        } 
     
    4750        // edit script will fix this 
    4851        var fix_image = function () { // handle the image changes 
    49             var img_segment_id = jQuery(this).attr('id').substr(jQuery(this).attr('id').lastIndexOf('_') + 1), 
    50             img = jQuery("#" + t_jp_prefix + "img_" + img_segment_id); 
    51             jQuery("#" + t_jp_prefix + img_segment_id).attr('source', 1); // source is 1 
     52            var img_segment_id = $(this).attr('id').substr($(this).attr('id').lastIndexOf('_') + 1), 
     53            img = $("#" + t_jp_prefix + "img_" + img_segment_id); 
     54            $("#" + t_jp_prefix + img_segment_id).attr('source', 1); // source is 1 
    5255            img.removeClass('tr-icon-yellow').removeClass('tr-icon-green').addClass('tr-icon-yellow'); 
    5356        }; 
    5457 
    5558        // rewrite text for all matching items at once 
    56         jQuery("*[token='" + token + "'][hidden!='y']") 
     59        $("*[token='" + token + "'][hidden!='y']") 
    5760        .html(translation) 
    5861        .each(fix_image); 
    5962 
    6063        // TODO - FIX hidden elements too (need to update father's title) 
    61         jQuery("*[token='" + token + "'][hidden='y']") 
     64        $("*[token='" + token + "'][hidden='y']") 
    6265        .attr('trans', translation) 
    6366        .each(fix_image); 
    6467    } 
    6568 
     69    // function to move the progress bars (if needed) 
     70    function make_progress(id, value) { 
     71        if (t_jp.progress) { 
     72            $('#' + id).progressbar('value', value); 
     73        } 
     74    } 
     75     
    6676    // we have four params, here two are implicit (source =1 auto translate, lang = target language) 
    6777    function ajax_translate(token, translation) { 
     
    8696                // We are pre-accounting the progress bar here - which is not very nice 
    8797                //if (source > 0) { 
    88                 done_posted += jQuery("*[token='" + tokens[i] + "']").size(); 
     98                done_posted += $("*[token='" + tokens[i] + "']").size(); 
    8999            //} 
    90100            } 
    91             jQuery.ajax({ 
     101            $.ajax({ 
    92102                type: "POST", 
    93103                url: t_jp.post_url, 
     
    95105                success: function () { 
    96106                    // Success now only updates the save progress bar (green) 
    97                     jQuery('#' + progressbar_posted_id).progressbar('value', done_posted / possibly_translateable * 100); 
     107                    make_progress(progressbar_posted_id, done_posted / possibly_translateable * 100); 
     108                    //$('#' + progressbar_posted_id).progressbar('value', done_posted / possibly_translateable * 100); 
    98109                } 
    99110            // we removed the error function, as there is no alert for automated thing, this will silently fail 
     
    110121    function create_progress_bar() { 
    111122        // progress bar is for alteast 5 items 
    112         jQuery("#" + t_jp_prefix + "credit").css({ 
     123        $("#" + t_jp_prefix + "credit").css({ 
    113124            'overflow': 'auto' 
    114125        }).append('<div style="float: left;width: 90%;height: 10px" id="' + progressbar_id + '"/><div style="margin-bottom:10px;float:left;width: 90%;height: 10px" id="' + progressbar_posted_id + '"/>'); 
    115         jQuery('#' + progressbar_id).progressbar({ 
     126        $('#' + progressbar_id).progressbar({ 
    116127            value: 0 
    117128        }); 
    118         jQuery('#' + progressbar_posted_id).progressbar({ 
     129        $('#' + progressbar_posted_id).progressbar({ 
    119130            value: 0 
    120131        }); 
    121132        // color the "save" bar 
    122         jQuery('#' + progressbar_posted_id + " > div").css({ 
     133        $('#' + progressbar_posted_id + " > div").css({ 
    123134            'background': '#28F828', 
    124135            'border' : "#08A908 1px solid" 
     
    130141        // auto_translated_previously... 
    131142        var auto_translated_phrases = [], binglang = t_jp.lang; 
    132         jQuery("." + t_jp_prefix + '[source=""]').each(function (i) { 
     143        $("." + t_jp_prefix + '[source=""]').each(function (i) { 
    133144            // not needed! 
    134             //var translated_id = jQuery(this).attr('id'), 
    135             var token = jQuery(this).attr('token'), 
     145            //var translated_id = $(this).attr('id'), 
     146            var token = $(this).attr('token'), 
    136147            //alert(translated_id); 
    137148            // we only have orig if we have some translation,? 
    138             to_trans = jQuery(this).attr('orig'); 
     149            to_trans = $(this).attr('orig'); 
    139150            if (to_trans === undefined) { 
    140                 to_trans = jQuery(this).html(); 
     151                to_trans = $(this).html(); 
    141152            } 
    142153            if (auto_translated_phrases[to_trans] !== 1) { 
     
    151162                    try { 
    152163                        Microsoft.Translator.translate(to_trans, "", binglang, function (translation) { 
    153                             ajax_translate(token, jQuery("<div>" + translation + "</div>").text()); 
    154                             jQuery('#' + progressbar_id).progressbar('value', (possibly_translateable - jQuery("." + t_jp_prefix + '[source=""]').size()) / possibly_translateable * 100); 
     164                            ajax_translate(token, $("<div>" + translation + "</div>").text()); 
     165                            make_progress(progressbar_id, (possibly_translateable - $("." + t_jp_prefix + '[source=""]').size()) / possibly_translateable * 100); 
     166                            //$('#' + progressbar_id).progressbar('value', (possibly_translateable - $("." + t_jp_prefix + '[source=""]').size()) / possibly_translateable * 100); 
    155167                        }); 
    156168                    } 
     
    167179                            //var segment_id = translated_id.substr(translated_id.lastIndexOf('_') + 1); 
    168180                            // No longer need because now included in the ajax translate 
    169                             //fix_page(jQuery("<div>" + result.translation + "</div>").text(), 1, segment_id); 
     181                            //fix_page($("<div>" + result.translation + "</div>").text(), 1, segment_id); 
    170182                            // ???? 
    171                             //to_trans = jQuery(this).attr('orig'); 
    172                             ajax_translate(token, jQuery("<div>" + result.translation + "</div>").text()); 
     183                            //to_trans = $(this).attr('orig'); 
     184                            ajax_translate(token, $("<div>" + result.translation + "</div>").text()); 
    173185                            // update the regular progress bar 
    174                             // done = possibly_translateable - jQuery("." + t_jp_prefix + '[source=""]').size(); 
    175                             jQuery('#' + progressbar_id).progressbar('value', (possibly_translateable - jQuery("." + t_jp_prefix + '[source=""]').size()) / possibly_translateable * 100); 
     186                            // done = possibly_translateable - $("." + t_jp_prefix + '[source=""]').size(); 
     187                            make_progress(progressbar_id, (possibly_translateable - $("." + t_jp_prefix + '[source=""]').size()) / possibly_translateable * 100); 
     188                            //$('#' + progressbar_id).progressbar('value', (possibly_translateable - $("." + t_jp_prefix + '[source=""]').size()) / possibly_translateable * 100); 
    176189                        } 
    177190                    }); 
     
    181194    } 
    182195 
    183     // We first try to avoid conflict with other frameworks 
    184     jQuery.noConflict(); 
    185  
    186196    loadLang = function () { 
    187197        google.load("language", "1", { 
     
    190200    }; 
    191201 
    192     jQuery(document).ready( 
     202    $(document).ready( 
    193203        function () { 
    194204            // this is the set_default_language function 
    195205            // attach a function to the set_default_language link if its there 
    196             jQuery('#' + t_jp_prefix + 'setdeflang').click(function () { 
    197                 jQuery.get(t_jp.post_url + "?tr_cookie=" + Math.random()); 
    198                 jQuery(this).hide("slow"); 
     206            $('#' + t_jp_prefix + 'setdeflang').click(function () { 
     207                $.get(t_jp.post_url + "?tr_cookie=" + Math.random()); 
     208                $(this).hide("slow"); 
    199209                return false; 
    200210            }); 
     
    204214            //var translationstats, possibly_translateable, now; 
    205215            // now lets check if auto translate is needed 
    206             //translationstats = jQuery("meta[name=translation-stats]").attr("content"); 
     216            //translationstats = $("meta[name=translation-stats]").attr("content"); 
    207217            // Logic borrowed from jquery and http://json.org/json2.js - Didn't see the reason for that, if someone can modify the html, he can probably do any script he wants too... 
    208218            /*if (/^[\],:{}\s]*$/.test(translationstats.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@") 
     
    222232                                }*/ 
    223233 
    224             //            var translationstats = window["eval"]("(" + jQuery("meta[name=translation-stats]").attr("content") + ")"), possibly_translateable, now; 
     234            //            var translationstats = window["eval"]("(" + $("meta[name=translation-stats]").attr("content") + ")"), possibly_translateable, now; 
    225235            //if (translationstats !== undefined) { 
    226236            //possibly_translateable = (translationstats.total_phrases - translationstats.translated_phrases - (translationstats.meta_phrases - translationstats.meta_translated_phrases)); 
    227             possibly_translateable = jQuery("." + t_jp_prefix + '[source=""]').size(); 
     237            possibly_translateable = $("." + t_jp_prefix + '[source=""]').size(); 
    228238 
    229239            now = new Date(); 
    230240            // we make sure script sub loaded are cached 
    231             jQuery.ajaxSetup({ 
     241            $.ajaxSetup({ 
    232242                cache: true 
    233243            }); 
     
    239249                    if (t_jp.progress) { 
    240250                        var loaduiandtranslate = function () { 
    241                             jQuery.xLazyLoader({ 
     251                            $.xLazyLoader({ 
    242252                                js: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js', 
    243253                                css: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css', 
     
    248258                            }); 
    249259                        }; 
    250                         if (typeof jQuery.xLazyLoader === 'function') { 
     260                        if (typeof $.xLazyLoader === 'function') { 
    251261                            loaduiandtranslate(); 
    252262                        } else { 
    253                             jQuery.getScript(t_jp.plugin_url + '/js/lazy.js', loaduiandtranslate); 
     263                            $.getScript(t_jp.plugin_url + '/js/lazy.js', loaduiandtranslate); 
    254264                        } 
    255265                    } else { 
     
    262272                    source = 2; 
    263273                    getMSN = function () { 
    264                         jQuery.getScript('http://api.microsofttranslator.com/V1/Ajax.svc/Embed?appId=' + t_jp.msnkey, langLoaded); 
     274                        $.getScript('http://api.microsofttranslator.com/V1/Ajax.svc/Embed?appId=' + t_jp.msnkey, langLoaded); 
    265275                    }; 
    266276                    // don't know why, but that's how it works 
    267277                    if (t_jp.edit && t_jp.progress) { 
    268                         jQuery.getScript(t_jp.plugin_url + '/js/lazy.js', getMSN); 
     278                        $.getScript(t_jp.plugin_url + '/js/lazy.js', getMSN); 
    269279                    } else { 
    270280                        getMSN(); 
    271281                    } 
    272282                } else { 
    273                     jQuery.getScript('http://www.google.com/jsapi', loadLang); 
     283                    $.getScript('http://www.google.com/jsapi', loadLang); 
    274284                } 
    275285            } 
     
    278288            // this is the part when we have editor support 
    279289            if (t_jp.edit) { 
    280                 jQuery.getScript(t_jp.plugin_url + '/js/transposhedit.js'); 
    281             } 
    282         }); 
    283 }()); // end of closure 
     290                $.getScript(t_jp.plugin_url + '/js/transposhedit.js'); 
     291            } 
     292        }); 
     293}(jQuery)); // end of closure 
Note: See TracChangeset for help on using the changeset viewer.