مستخدم:ASammour/search-azerty.js

من ويكيبيديا، الموسوعة الحرة

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

/*إضافة لإخفاء جملة هل تقصد عند حذف النص من مربع النص*/
$(".oo-ui-textInputWidget [type='search']").keyup(function(e) {
     if (e.keyCode == 8) {
         $('#didyoumeanr').empty();
     }
 });

 $("#searchInput").keyup(function(e) {
     if (e.keyCode == 8) {
         $('#didyoumean').empty();
     }
 });
 
 
/*إضافة زر إلى صندوق الأدوات ليُتيح تحويل الأحرف الإنجليزية إلى العربية*/
(function(mw, $, undefined) {
    var customizeBetaToolbar = function() {
        $('#wpTextbox1').wikiEditor('addToToolbar', {
            'section': 'main',
            'group': 'insert',
            'tools': {
                'en-ar': {
                    label: 'الإنجليزية إلى العربية',
                    type: 'button',
                    icon: '//upload.wikimedia.org/wikipedia/commons/d/d8/Toolbaricon_bold_E.png',
                    action: {
                        type: 'callback',
						/*عند الضغط على الزر سيقوم باستبدال المحدد بالنص العربي*/
                        execute: function(context) {
                        	/*عملية الاستبدال*/
                            $('#wpTextbox1').val(
                                $('#wpTextbox1').val().replace(getInputSelection($("#wpTextbox1")),
                                    replaceEnChars(getInputSelection($("#wpTextbox1")))));

                        }
                    }

               /*زر خاص بالتحويل من العربية إلى الإنجليزية*/
                },'ar-en': {
                    label: 'العربية إلى الإنجليزية',
                    type: 'button',
                    icon: '//upload.wikimedia.org/wikipedia/commons/8/81/Toolbaricon_bold_A-1.png',
                    action: {
                        type: 'callback',
						/*عند الضغط على الزر سيقوم باستبدال المحدد بالنص الإنجليزي*/
                        execute: function(context) {
                        	/*عملية الاستبدال*/
                            $('#wpTextbox1').val(
                                $('#wpTextbox1').val().replace(getInputSelection($("#wpTextbox1")),
                                    replaceArChars(getInputSelection($("#wpTextbox1")))));

                        }
                    }
                }
            }
        });
    };


    if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1) {
        mw.loader.using('user.options', function() {
            if (mw.user.options.get('usebetatoolbar')) {
                mw.loader.using('ext.wikiEditor', function() {
                    $(customizeBetaToolbar);
                });
            } else {
                $(customizeOrigToolbar);
            }
        });
    }
})(mediaWiki, jQuery);

/*نهاية الزر*/


/*النمط الخاص باللغة العربية*/
var arabic = /[\u0600-\u06FF]/;

/*افحص الحروف المدخلة في صندوق البحث عندما يتم إدخال أي حرف ، أو إزالته من الصندوق*/
$('#searchInput').keyup(updateCount);
$('#searchInput').keydown(updateCount);

$(".oo-ui-textInputWidget [type='search']").ready(results);
$(".oo-ui-textInputWidget [type='search']").keydown(results);
$(".oo-ui-textInputWidget [type='search']").keyup(results);


$("#searchInput").after('<div style = "color:blue;position:absolute; left:-1%;top: -80%;" id= "didyoumean"></div>');
$("#searchText").after('<div style = "color:blue;position:absolute; left:22%;top:15%;" id= "didyoumeanr"></div>');

/*الاستبدال في صفحة نتائج البحث*/
function results() {
	var cs  = "";
	if ($(".oo-ui-textInputWidget [type='search']").length>0){
		cs = $(".oo-ui-textInputWidget [type='search']").val().length;
	}
    var searchKey = $(".oo-ui-textInputWidget [type='search']").val();
    if (!(arabic.test(searchKey)) & cs > 4) {
        $("#didyoumeanr").html("هل تقصد: <u><a style = 'color:red;' id = 'suggestr'>" + replaceEnChars(searchKey) + "</a>        </u>");
        $('#suggestr').on('click', function() {
            var input = $(".oo-ui-textInputWidget [type='search']");
            input.val(replaceEnChars(searchKey));
            $("#didyoumeanr").empty();
        });
    }
}

/*جلب النص المحدد*/
function getInputSelection(elem) {
    if (typeof elem != "undefined") {
        s = elem[0].selectionStart;
        e = elem[0].selectionEnd;
        return elem.val().substring(s, e);
    } else {
        return '';
    }
}

/*الاستبدال في صندوق البحث العادي*/
function updateCount() {
	var cs  = "";
	var searchKey = "";
	if ($(this).length > 0){
		cs = $(this).val().length;
    	searchKey = $(this).val();
    }
    if (!(arabic.test(searchKey)) & cs > 4) {
        $("#didyoumean").html("هل تقصد: <u><a style = 'color:red;' id = 'suggest'>" + replaceEnChars(searchKey) + "</a>        </u>");
        $('#suggest').on('click', function() {
            var input = $("#searchInput");
            input.val(replaceEnChars(searchKey));
            $("#didyoumean").empty();
        });
    }
}


/*هنا يتم الاستبدال*/
function replaceEnChars(text) {
    text = text.replace(/a/g, 'ض');
    text = text.replace(/A/g, 'َ');
    text = text.replace(/z/g, 'ص');
    text = text.replace(/Z/g, 'ً');
    text = text.replace(/e/g, 'ث');
    text = text.replace(/E/g, 'ُ');
    text = text.replace(/r/g, "ق");
    text = text.replace(/R/g, "ٌ");
    text = text.replace(/t/g, "ف");
    text = text.replace(/T/g, "لإ");
    text = text.replace(/y/g, "غ");
    text = text.replace(/Y/g, "إ");
    text = text.replace(/u/g, "ع");
    text = text.replace(/U/g, "‘");
    text = text.replace(/i/g, "ه");
    text = text.replace(/I/g, "÷");
    text = text.replace(/o/g, "خ");
    text = text.replace(/O/g, "×");
    text = text.replace(/p/g, "ح");
    text = text.replace(/P/g, "؛");

    text = text.replace(/q/g, "ش");
    text = text.replace(/Q/g, "ِ");
    text = text.replace(/s/g, "س");
    text = text.replace(/S/g, "ٍ");
    text = text.replace(/d/g, "ي");
    text = text.replace(/D/g, "]");
    text = text.replace(/f/g, "ب");
    text = text.replace(/F/g, "[");
    text = text.replace(/g/g, "ل");
    text = text.replace(/G/g, "لأ");
    text = text.replace(/h/g, "ا");
    text = text.replace(/H/g, "أ");
    text = text.replace(/j/g, "ت");
    text = text.replace(/J/g, "ـ");
    text = text.replace(/k/g, "ن");
    text = text.replace(/K/g, "،");
    text = text.replace(/l/g, "م");
    text = text.replace(/L/g, "/");
    text = text.replace(/m/g, "ك");
    text = text.replace(/M/g, ":");
    text = text.replace(/\%/g, "\"");
    text = text.replace(/ù/g, "ط");
   
   
    text = text.replace(/w/g, "ئ");
    text = text.replace(/W/g, "~");
    text = text.replace(/x/g, "ء");
    text = text.replace(/X/g, "ْ");
    text = text.replace(/c/g, "ؤ");
    text = text.replace(/C/g, "}");
    text = text.replace(/v/g, "ر");
    text = text.replace(/V/g, "{");
    text = text.replace(/b/g, "لا");
    text = text.replace(/B/g, "لآ");
    text = text.replace(/n/g, "ى");
    text = text.replace(/N/g, "آ");
    text = text.replace(/\,/g, "ة");
    text = text.replace(/\?/g, "’");
    text = text.replace(/\./g, "'");
    text = text.replace(/\;/g, "و");
    
    text = text.replace(/\//g, ".");
    text = text.replace(/\:/g, "ز");

    text = text.replace(/\§/gi, "؟");
    text = text.replace(/\!/gi, "ظ");
    
    text = text.replace(/\µ/gi, "|");

    text = text.replace(/\*/gi, 'ذ');
    text = text.replace(/\£/gi, '<');
    text = text.replace(/\$/gi, 'د');
    text = text.replace(/\^/gi, 'ج');
    text = text.replace(/\"/gi, '>');
	text = text.replace(/\ّ/gi, 'µ');

    return text;
}


function replaceArChars(string) {
	string = string.replace (/ض/g,"a");
	string = string.replace (/َ/g,"A");
	string = string.replace (/ص/g,"z");
	string = string.replace (/ً/g,"Z");
	string = string.replace (/ث/g,"e");
	string = string.replace (/ُ/g,"E");
	string = string.replace (/ق/g,"r");
	string = string.replace (/ٌ/g,"R");
	string = string.replace (/ف/g,"t");
	string = string.replace (/لإ/g,"T");
	string = string.replace (/غ/g,"y");
	string = string.replace (/إ/g,"Y");
	string = string.replace (/ع/g,"u");
	string = string.replace (/‘/g,"U");
	string = string.replace (/ه/g,"i");
	string = string.replace (/÷/g,"I");
	string = string.replace (/خ/g,"o");
	string = string.replace (/×/g,"O");
	string = string.replace (/ح/g,"p");
	string = string.replace (/؛/g,"P");
	string = string.replace (/ش/g,"q");
	string = string.replace (/ِ/g,"Q");
	string = string.replace (/س/g,"s");
	string = string.replace (/ٍ/g,"S");
	string = string.replace (/ي/g,"d");
	string = string.replace (/]/g,"D");
	string = string.replace (/ب/g,"f");
	string = string.replace (/\[/g,"F");
	string = string.replace (/ل/g,"g");
	string = string.replace (/لأ/g,"G");
	string = string.replace (/ا/g,"h");
	string = string.replace (/أ/g,"H");
	string = string.replace (/ت/g,"j");
	string = string.replace (/ـ/g,"J");
	string = string.replace (/ن/g,"k");
	string = string.replace (/،/g,"K");
	string = string.replace (/م/g,"l");
	string = string.replace (/\//g,"L");
	string = string.replace (/ك/g,"m");
	string = string.replace (/:/g,"M");
	string = string.replace (/\"/g,"\%");
	string = string.replace (/ط/g,"ù");
	string = string.replace (/ئ/g,"w");
	string = string.replace (/~/g,"W");
	string = string.replace (/ء/g,"x");
	string = string.replace (/ْ/g,"X");
	string = string.replace (/ؤ/g,"c");
	string = string.replace (/}/g,"C");
	string = string.replace (/ر/g,"v");
	string = string.replace (/{/g,"V");
	string = string.replace (/لا/g,"b");
	string = string.replace (/لآ/g,"B");
	string = string.replace (/ى/g,"n");
	string = string.replace (/آ/g,"N");
	string = string.replace (/’/g,"\?");
	string = string.replace (/'/g,"\.");
	string = string.replace (/و/g,"\;");
	string = string.replace (/./g,"//");
	string = string.replace (/ز/g,"\:");
	string = string.replace (/؟/g,"§");
	string = string.replace (/ظ/g,"\!");
	string = string.replace (/|/g,"µ");
	string = string.replace (/ذ/g,"*");

    string = string.replace(/\</gi, '£');
    string = string.replace(/د/gi, '$');
    string = string.replace(/ج/gi, '^');
    string = string.replace(/\>/gi, '"');
    string = string.replace(/\µ/gi, 'ّ')
    return string;
}