MediaWiki:Gadget-Ae-to-Aelig.js

Сæрибар энциклопеди Википедийы æрмæг.

Бамбæр æй:
after saving, you have to bypass your browser's cache to see the changes. Mozilla/Safari/Konqueror: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera: press F5.

/*
Ацы гаджет а+е тагъд фыстæй æ-мæ кæны. Нырмæ фæлтæринаг у.

*/
var wgAe = {};
wgAe.init = function() {
	wgAe.APressed = false;
	jQuery.fn.setCursorPosition = function(position){
	    if(this.lengh == 0) return this;
	    return $(this).setSelection(position, position);
	};
	jQuery.fn.getCursorPosition = function(){
	    if(this.lengh == 0) return -1;
	    return $(this).getSelectionStart();
	};
	jQuery.fn.getSelectionStart = function(){
	    if(this.lengh == 0) return -1;
	    input = this[0];

	    var pos = input.value.length;

	    if (input.createTextRange) {
	        var r = document.selection.createRange().duplicate();
	        r.moveEnd('character', input.value.length);
	        if (r.text == '')
	        pos = input.value.length;
	        pos = input.value.lastIndexOf(r.text);
	    } else if(typeof(input.selectionStart)!="undefined")
	    pos = input.selectionStart;

	    return pos;
	};
	jQuery.fn.setSelection = function(selectionStart, selectionEnd) {
	    if(this.lengh == 0) return this;
	    input = this[0];

	    if (input.createTextRange) {
	        var range = input.createTextRange();
	        range.collapse(true);
	        range.moveEnd('character', selectionEnd);
	        range.moveStart('character', selectionStart);
	        range.select();
	    } else if (input.setSelectionRange) {
	        input.focus();
	        input.setSelectionRange(selectionStart, selectionEnd);
	    }

	    return this;
	};
	wgAe.timeFlag = 0;
	$("input:text, textarea").keydown(
			function(e) {
				var code = (e.keyCode ? e.keyCode : e.which);
				if (code == 70) {
					wgAe.timeFlag = $.now();
					wgAe.APressed = true;
				} else if (wgAe.APressed && $.now() - wgAe.timeFlag < 500) {
					if (code == 84) {
						var $target=$(e.target);
						var caps = false;
						var caretPosition = $target.getSelectionStart();
						var val = $target.val();
						var enteredKey = val.slice(caretPosition - 1,
								caretPosition);
						enteredKey.toUpperCase() === enteredKey ? caps = true
								: caps = false;
						var pre = val.slice(0, caretPosition - 1);
						var ins = caps ? 'Æ' : 'æ';
						val = pre + ins
								+ val.slice(caretPosition, val.length);
						$target.val(val);
						$target.setSelection(caretPosition,caretPosition);
						wgAe.APressed = false;
						e.stopPropagation();
						return false;
					}
					wgAe.APressed = false;
				}
			});
};
$(document).ready(wgAe.init);