Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

persianjs jQuery Plugin #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions jquery.persian.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
(function( $ ){

/**
* Used for Change keyboard layout
*
* @method _switchKey
* @param {String} value
* @return {String} Returns Converted char
*/
function _switchKey(value) {
if (!value) {
return;
}
var persianChar = [ "ض", "ص", "ث", "ق", "ف", "غ", "ع", "ه", "خ", "ح", "ج", "چ", "ش", "س", "ی", "ب", "ل", "ا", "ت", "ن", "م", "ک", "گ", "ظ", "ط", "ز", "ر", "ذ", "د", "پ", "و","؟" ],
englishChar = [ "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "z", "x", "c", "v", "b", "n", "m", ",","?" ];

for (var i = 0, charsLen = persianChar.length; i < charsLen; i++) {
value = value.replace(new RegExp(persianChar[i], "g"), englishChar[i]);
}
return value;
}

/**
* Used for convert Arabic numbers to Persian
*
* @method _toPersianNumber
* @param {String} value
* @return {String} Returns Converted numbers
*/
function _toPersianNumber(value) {
if (!value) {
return;
}
var arabicNumbers = ["١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩", "٠"],
persianNumbers = ["۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "۰"];

for (var i = 0, numbersLen = arabicNumbers.length; i < numbersLen; i++) {
value = value.replace(new RegExp(arabicNumbers[i], "g"), persianNumbers[i]);
}
return value;
}

/**
* Used for convert English numbers to Persian
* @method _englishNumber
* @param {String} value
* @return {String} Returns Converted numbers
*/
function _englishNumber(value) {
if (!value) {
return;
}
var englishNumbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
persianNumbers = ["۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "۰"];

for (var i = 0, numbersLen = englishNumbers.length; i < numbersLen; i++) {
value = value.replace(new RegExp(englishNumbers[i], "g"), persianNumbers[i]);
}
return value;
}



var methods = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every change in core of persian.js need a method update, it's better to have such a dynamic binding in core method calls.
let see this Gist: https://gist.github.com/salisa/5063522

englishNumber:function() {
return this.each(function(){
$value = $(this).val();
$(this).val(_englishNumber($value));
});

},
toPersianChar:function(){
return this.each(function(){
$value = $(this).val();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must mention about the state of none input element like

 <div id="someEnglishNumbers">123</div> 

elements! :)

$(this).val(_toPersianChar($value));
});
},
toPersianNumber:function(){
return this.each(function(){
$value = $(this).val();
$(this).val(_toPersianNumber($value));
});
},
switchKey:function(){
return this.each(function(){
$value = $(this).val();
$(this).val(_switchKey($value));
});
}

};
$.fn.persianjs = 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 ' + method + ' does not exist on jQuery.tooltip' );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jQuery Tooltip!?

}

};

})( jQuery );
19 changes: 19 additions & 0 deletions test/testJquery.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="../jquery.persian.js" type="text/javascript"></script>
<script>
$(function(){
$("#txtOne,#txtTwo").persianjs("englishNumber");
$("#txtThree").persianjs("toPersianNumber");
});
</script>
</head>
<body>
<input type="text" value="1234567890" id="txtOne"/>
<input type="text" value="1234567890" id="txtTwo"/>
<input type="text" value="٥" id="txtThree"/>
</body>
</html>