From 56160d4d49cf72475e4f0065703c20a93be9d36a Mon Sep 17 00:00:00 2001 From: Mohammad Bagher Ehtemam Date: Fri, 1 Mar 2013 00:53:37 -0800 Subject: [PATCH 1/4] Adding persianjs j --- jquery.persian.js | 104 +++++++++++++++++++++++++++++++++++++++++++ test/testJquery.html | 19 ++++++++ 2 files changed, 123 insertions(+) create mode 100644 jquery.persian.js create mode 100644 test/testJquery.html diff --git a/jquery.persian.js b/jquery.persian.js new file mode 100644 index 0000000..085277f --- /dev/null +++ b/jquery.persian.js @@ -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 = { + englishNumber:function() { + return this.each(function(){ + $value = $(this).val(); + $(this).val(_englishNumber($value)); + }); + + }, + toPersianChar:function(){ + return this.each(function(){ + $value = $(this).val(); + $(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' ); + } + + }; + +})( jQuery ); \ No newline at end of file diff --git a/test/testJquery.html b/test/testJquery.html new file mode 100644 index 0000000..c099842 --- /dev/null +++ b/test/testJquery.html @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + \ No newline at end of file From 02e87d428f2524d7a0b154d6d6df7591665844d6 Mon Sep 17 00:00:00 2001 From: Mohammad Bagher Ehtemam Date: Fri, 1 Mar 2013 00:53:52 -0800 Subject: [PATCH 2/4] Revert "Adding persianjs j" This reverts commit 56160d4d49cf72475e4f0065703c20a93be9d36a. --- jquery.persian.js | 104 ------------------------------------------- test/testJquery.html | 19 -------- 2 files changed, 123 deletions(-) delete mode 100644 jquery.persian.js delete mode 100644 test/testJquery.html diff --git a/jquery.persian.js b/jquery.persian.js deleted file mode 100644 index 085277f..0000000 --- a/jquery.persian.js +++ /dev/null @@ -1,104 +0,0 @@ -(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 = { - englishNumber:function() { - return this.each(function(){ - $value = $(this).val(); - $(this).val(_englishNumber($value)); - }); - - }, - toPersianChar:function(){ - return this.each(function(){ - $value = $(this).val(); - $(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' ); - } - - }; - -})( jQuery ); \ No newline at end of file diff --git a/test/testJquery.html b/test/testJquery.html deleted file mode 100644 index c099842..0000000 --- a/test/testJquery.html +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file From c0bc63a2c7ec6639c3736f0cdedf64d73af5aced Mon Sep 17 00:00:00 2001 From: Mohammad Bagher Ehtemam Date: Fri, 1 Mar 2013 00:55:14 -0800 Subject: [PATCH 3/4] Adding persianjs jQuery Plugin Adding persianjs jQuery Plugin --- jquery.persian.js | 104 +++++++++++++++++++++++++++++++++++++++++++ test/testJquery.html | 19 ++++++++ 2 files changed, 123 insertions(+) create mode 100644 jquery.persian.js create mode 100644 test/testJquery.html diff --git a/jquery.persian.js b/jquery.persian.js new file mode 100644 index 0000000..085277f --- /dev/null +++ b/jquery.persian.js @@ -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 = { + englishNumber:function() { + return this.each(function(){ + $value = $(this).val(); + $(this).val(_englishNumber($value)); + }); + + }, + toPersianChar:function(){ + return this.each(function(){ + $value = $(this).val(); + $(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' ); + } + + }; + +})( jQuery ); \ No newline at end of file diff --git a/test/testJquery.html b/test/testJquery.html new file mode 100644 index 0000000..c099842 --- /dev/null +++ b/test/testJquery.html @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + \ No newline at end of file From 1ba74c311b7c9178ca769f9c9d4ba1798823eb32 Mon Sep 17 00:00:00 2001 From: Mohammad Bagher Ehtemam Date: Fri, 1 Mar 2013 01:02:00 -0800 Subject: [PATCH 4/4] persianjs jquery plugin Updated with @salisa code --- jquery.persian.js | 265 +++++++++++++++++++++++++++++++++++-------- test/testJquery.html | 4 +- 2 files changed, 220 insertions(+), 49 deletions(-) diff --git a/jquery.persian.js b/jquery.persian.js index 085277f..1bc5b1d 100644 --- a/jquery.persian.js +++ b/jquery.persian.js @@ -1,4 +1,45 @@ -(function( $ ){ +/** +* PersianJs v0.1.0 +* https://github.com/usablica/persian.js +* MIT licensed +* +* Copyright (C) 2013 usabli.ca and other contributors +*/ +(function () { + + //Default config/variables + var VERSION = "0.1.0", + //Check for nodeJS + hasModule = (typeof module !== 'undefined' && module.exports); + + /** + * PersianJs main class + * + * @class PersianJs + */ + function PersianJs(str) { + this._str = str; + } + + /** + * Used for convert Arabic characters to Persian + * + * @method _toPersianChar + * @param {String} value + * @return {String} Returns Converted string + */ + function _toPersianChar(value) { + if (!value) { + return; + } + var arabicChars = ["ي", "ك", "‍", "دِ", "بِ", "زِ", "ذِ", "ِشِ", "ِسِ", "‌", "ى"], + persianChars = ["ی", "ک", "", "د", "ب", "ز", "ذ", "ش", "س", "", "ی"]; + + for (var i = 0, charsLen = arabicChars.length; i < charsLen; i++) { + value = value.replace(new RegExp(arabicChars[i], "g"), persianChars[i]); + } + return value; + } /** * Used for Change keyboard layout @@ -13,13 +54,13 @@ } 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 * @@ -33,13 +74,13 @@ } 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 @@ -52,53 +93,183 @@ } 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 = { - englishNumber:function() { - return this.each(function(){ - $value = $(this).val(); - $(this).val(_englishNumber($value)); - }); - - }, - toPersianChar:function(){ - return this.each(function(){ - $value = $(this).val(); - $(this).val(_toPersianChar($value)); - }); - }, - toPersianNumber:function(){ - return this.each(function(){ - $value = $(this).val(); - $(this).val(_toPersianNumber($value)); + + /** + * Used for fix Persian Charachters in URL + * https://fa.wikipedia.org/wiki/مدیاویکی:Gadget-Extra-Editbuttons-Functions.js + * + * @param {String} value + * @return {String} Returns fixed URL + * @api private + */ + function _fixURL(value) { + if (!value) { + return; + } + // Replace every %20 with _ to protect them from decodeURI + var old = ""; + while (old != value) { + old = value; + value = value.replace(/(http\S+?)\%20/g, '$1\u200c\u200c\u200c_\u200c\u200c\u200c'); + } + // Decode URIs + // NOTE: This would convert all %20's to _'s which could break some links + // but we will undo that later on + value = value.replace(/(http\S+)/g, function (s, p) { + return decodeURI(p); }); - }, - switchKey:function(){ - return this.each(function(){ - $value = $(this).val(); - $(this).val(_switchKey($value)); + // Revive all instances of %20 to make sure no links is broken + value = value.replace(/\u200c\u200c\u200c_\u200c\u200c\u200c/g, '%20'); + return value; + } + + /** + * jQuery plugin adapter for persian.js + * + * @author @salisa + * @param {?string} param + */ + var PJ = $.fn.persian = function(param) { + return this.each(function () { + + var $this = $(this), value, persianValue, persianChar, persianNumber, englishNumber; + + if (typeof param === 'string') { + value = PJ.getValue($this); + persianValue = PJ.getFa(value, param); + PJ.setValue($this, persianValue); + + // In the case of none param passing: $('element').persian(); + // apply three main methods: toPersianChar(), toPersianNumber(), englishNumber() + } else if (typeof param === 'undefined') { + value = PJ.getValue($this); + persianChar = PJ.getFa(value, 'toPersianChar'); + PJ.setValue($this, persianChar); + + persianNumber = PJ.getFa(persianChar, 'toPersianNumber'); + PJ.setValue($this, persianNumber); + + englishNumber = PJ.getFa(persianNumber, 'englishNumber'); + PJ.setValue($this, englishNumber); + } }); - } - -}; - $.fn.persianjs = function( method ) { + }; + + /** + * + * @param {$} $elm + * @returns {string} + */ + PJ.getValue = function ($elm) { + var result; + if ($elm.is('input, textarea')) { + result = $elm.val(); + } else { + result = $elm.text(); + } + return result; + }; + + /** + * + * @param {$} $elm + * @param {string} value + */ + PJ.setValue = function ($elm, value) { + if ($elm.is('input, textarea')) { + $elm.val(value); + } else { + $elm.text(value); + } + }; + + /** + * + * @param {string} value + * @param {string} param + * @returns {string} + */ + PJ.getFa = function (value, param) { + var PersianJs = persianJs(value), + persianValue = value; + + try { + persianValue = PersianJs[param].call(PersianJs); + } catch (e) { + console.error('Wrong param used! ->', param); + } + return persianValue; + }; + + var persianJs = function(inputStr) { + if (inputStr === "" || inputStr === null) { + return null; + } + return new PersianJs(inputStr); + }; - 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' ); - } - - }; - -})( jQuery ); \ No newline at end of file + /** + * Current PersianJs version + * + * @property version + * @type String + */ + persianJs.version = VERSION; + + //Prototype + persianJs.fn = PersianJs.prototype = { + clone: function () { + return persianJs(this); + }, + value: function () { + return this._str; + }, + toString: function () { + return this._str.toString(); + }, + set : function (value) { + this._str = String(value); + return this; + }, + toPersianChar: function() { + return _toPersianChar(this._str); + }, + toPersianNumber: function() { + return _toPersianNumber(this._str); + }, + fixURL: function() { + return _fixURL(this._str); + }, + englishNumber: function() { + return _englishNumber(this._str); + }, + switchKey: function() { + return _switchKey(this._str); + } + }; + + //Expose PersianJs + //CommonJS module is defined + if (hasModule) { + module.exports = persianJs; + } + //global ender:false + if (typeof ender === 'undefined') { + // here, `this` means `window` in the browser, or `global` on the server + // add `persianJs` as a global object via a string identifier, + // for Closure Compiler "advanced" mode + this['persianJs'] = persianJs; + } + //global define:false + if (typeof define === 'function' && define.amd) { + define('persianJs', [], function () { + return persianJs; + }); + } +})(); \ No newline at end of file diff --git a/test/testJquery.html b/test/testJquery.html index c099842..5c047d4 100644 --- a/test/testJquery.html +++ b/test/testJquery.html @@ -6,8 +6,8 @@