Files
wyczarujprezent.pl/modules/masseditproduct/views/js/jquery.liTranslit.js
2024-10-28 22:14:22 +01:00

269 lines
7.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* jQuery liTranslit v 1.4
* http://masscode.ru/index.php/k2/item/28-litranslit
*
* Copyright 2013, Linnik Yura
* Free to use
*
* Last Update 25.10.2014
*/
(function ($) {
var methods = {
init: function (options) {
var o = {
eventType:'keyup blur copy paste cut start',
elAlias: $(this), //Элемент, в который будет записываться результат транслитерации или false
reg:'', //'" "="-","ж"="zzzz"' or false or ''
translated: function (el, text, eventType) {},
caseType: 'lower', // lower(default), upper, inherit - регистр выходных данных
status:true,
string:'' //используется для транслита строковой переменной
};
if (options) {
$.extend(o, options);
}
var general = $(this);
if(!general.length){
general = $('<div>').text(o.string);
}
return general.each(function(){
var
elName = $(this),
elAlias = o.elAlias.length ? o.elAlias.css({wordWrap:'break-word'}) : general.css({wordWrap:'break-word'}),
nameVal;
elName.data({
status:o.status
})
var inser_trans = function(result,e) {
if(o.caseType == 'upper'){
result = result.toUpperCase();
}
if(o.caseType == 'lower'){
result = result.toLowerCase();
}
if(elName.data('status') && o.elAlias){
if (elAlias.prop("value") !== undefined) {
elAlias.val(result);
}else{
elAlias.html(result);
}
}
if(result != ''){
if (o.translated !== undefined) {
var type;
if(e == undefined){
type = 'no event';
}else{
type = e.type;
}
o.translated(elName, result, type);
}
}
}
var customReg = function(str){
customArr = o.reg.split(',');
for(var i=0;i<customArr.length; i++){
var customItem = customArr[i].split('=');
var regi = customItem[0].replace(/"/g,'');
var newstr = customItem[1].replace(/"/g,'');
var re = new RegExp(regi,"ig");
str = str.replace(re,newstr)
}
return str
}
var tr = function(el,e){
if (el.prop("value") !== undefined) {
nameVal = el.val();
}else{
nameVal = el.text();
}
if(o.reg && o.reg != ''){
nameVal = customReg(nameVal)
}
inser_trans(get_trans(nameVal),e);
};
elName.on(o.eventType,function (e) {
var el = $(this);
setTimeout(function(){
tr(el,e);
},50)
});
tr(elName);
function get_trans() {
en_to_ru = {
'а': 'a',
'б': 'b',
'в': 'v',
'г': 'g',
'д': 'd',
'е': 'e',
'ё': 'jo',
'ж': 'zh',
'з': 'z',
'и': 'i',
'й': 'j',
'к': 'k',
'л': 'l',
'м': 'm',
'н': 'n',
'о': 'o',
'п': 'p',
'р': 'r',
'с': 's',
'т': 't',
'у': 'u',
'ф': 'f',
'х': 'h',
'ц': 'c',
'ч': 'ch',
'ш': 'sh',
'щ': 'sch',
'ъ': '',
'ы': 'y',
'ь': '',
'э': 'e',
'ю': 'ju',
'я': 'ja',
' ': '_',
'і': 'i',
'ї': 'i',
'є': 'e',
'А': 'a',
'Б': 'b',
'В': 'v',
'Г': 'g',
'Д': 'd',
'Е': 'e',
'Ё': 'jo',
'Ж': 'zh',
'З': 'z',
'И': 'i',
'Й': 'j',
'К': 'k',
'Л': 'l',
'М': 'm',
'Н': 'n',
'О': 'o',
'П': 'p',
'Р': 'r',
'С': 's',
'Т': 't',
'У': 'u',
'Ф': 'f',
'Х': 'h',
'Ц': 'c',
'Ч': 'ch',
'Ш': 'sh',
'Щ': 'sch',
'Ъ': '',
'Ы': 'y',
'Ь': '',
'Э': 'e',
'Ю': 'ju',
'Я': 'ja',
' ': '_',
'І': 'i',
'Ї': 'i',
'Є': 'e',
'ą': 'a',
'ć': 'c',
'ę': 'e',
'ń': 'n',
'ó': 'o',
'ś': 's',
'ź': 'z',
'Ą': 'a',
'Ć': 'c',
'Ę': 'e',
'Ł': 'l',
'Ń': 'n',
'Ó': 'o',
'Ś': 's',
'Ź': 'z',
'â': 'a',
'Â': 'a',
'à': 'a',
'À': 'a',
'ç': 'c',
'Ç': 'c',
'É': 'e',
'é': 'e',
'Ê': 'e',
'È': 'e',
'è': 'e',
'Ë': 'e',
'Î': 'i',
'î': 'i',
'Ï': 'i',
'ï': 'i',
'Ô': 'o',
'ô': 'o',
'Û': 'u',
'û': 'u',
'Ù': 'u',
'ù': 'u',
'Ü': 'u',
'ü': 'u',
'Ÿ': 'y',
'ÿ': 'y'
};
nameVal = trim(nameVal);
nameVal = nameVal.split("");
var trans = new String();
for (i = 0; i < nameVal.length; i++) {
for (key in en_to_ru) {
val = en_to_ru[key];
if (key == nameVal[i]) {
trans += val;
break
}else if (key == "Є") {
trans += nameVal[i]
};
};
};
return trans;
}
function trim(string) {
//Удаляем пробел в начале строки и ненужные символы
string = string.replace(/(^\s+)|'|"|<|>|\!|\||@|#|$|%|^|\^|\$|\\|\/|&|\*|\(\)|\|\/|;|\+|№|,|\?|:|\[|\]/g, "");
return string;
};
})
},
disable: function () {
$(this).data({
status:false
})
},
enable: function () {
$(this).data({
status:true
})
}
};
$.fn.liTranslit = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Метод ' + method + ' в jQuery.liTranslit не существует');
}
};
})(jQuery);