diff --git a/README.md b/README.md index 3d0e57e..3ebf913 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,6 @@ add .dir-auto class to input that you want to automate direction on it ```html ``` -## JQuery -This Library is **Plain Javascript** with no need of JQuery. -but if you need jquery version of this library for any reason there is a **jquery.automatic-direction.js** with the same functionality with jquery selectors and functions. ## Licence and Credits diff --git a/automatic-direction.js b/automatic-direction.js index 2a212f7..3e9d92f 100644 --- a/automatic-direction.js +++ b/automatic-direction.js @@ -1,32 +1,35 @@ /*! - * automatic Direction v1.2 + * automatic Direction v1.3 * By Milad Dehghan (http://dehghan.net) * github: https://github.com/miladd3/automatic-direction */ window.addEventListener('load', function() { + var elements = [].slice.call(document.querySelectorAll('.dir-auto')); - var elements = [].slice.call(document.querySelectorAll('.dir-auto')); + elements.forEach(function(el) { + var farsiCountNode = el.parentNode.querySelector('.count-f'), + latinCountNode = el.parentNode.querySelector('.count-e'), + hebrewCountNode = el.parentNode.querySelector('.count-h'), + countNode = el.parentNode.querySelector('.count-all'); - elements.forEach(function(el) { - var farsiCountNode = el.parentNode.querySelector('.count-f'), - latinCountNode = el.parentNode.querySelector('.count-e'), - countNode = el.parentNode.querySelector('.count-all'); - - el.addEventListener('keyup', function(e) { - var value = e.target.value, - farsiChars = value.match(/[\u0600-\u06FF]/g), - spaceChars = value.match(/\s/g), - count = value.length, - farsiCount = farsiChars ? farsiChars.length : 0, + el.addEventListener('keyup', function(e) { + var value = e.target.value, + farsiChars = value.match(/[\u0600-\u06FF]/g), + hebrewChars = value.match(/[\u0590-\u05FF]/g), + spaceChars = value.match(/\s/g), + count = value.length, + farsiCount = farsiChars ? farsiChars.length : 0, + hebrewCount = hebrewChars ? hebrewChars.length : 0, spaceCount = spaceChars ? spaceChars.length : 0, - latinCount = count - farsiCount - spaceCount; - - e.target.setAttribute('dir', (farsiCount > latinCount) ? 'rtl' : 'ltr'); + rtlCount = farsiCount + hebrewCount; + latinCount = count - farsiCount - spaceCount - hebrewCount; - if(farsiCountNode) farsiCountNode.innerHTML = farsiCount; - if(latinCountNode) latinCountNode.innerHTML = latinCount; - if(countNode) countNode.innerHTML = count; - }); - }); + e.target.setAttribute('dir', rtlCount > latinCount ? 'rtl' : 'ltr'); + if (farsiCountNode) farsiCountNode.innerHTML = farsiCount; + if (latinCountNode) latinCountNode.innerHTML = latinCount; + if (hebrewCountNode) hebrewCountNode.innerHTML = hebrewCount; + if (countNode) countNode.innerHTML = count; + }); + }); }); diff --git a/docs/automatic-direction.js b/docs/automatic-direction.js index 2a212f7..3e9d92f 100644 --- a/docs/automatic-direction.js +++ b/docs/automatic-direction.js @@ -1,32 +1,35 @@ /*! - * automatic Direction v1.2 + * automatic Direction v1.3 * By Milad Dehghan (http://dehghan.net) * github: https://github.com/miladd3/automatic-direction */ window.addEventListener('load', function() { + var elements = [].slice.call(document.querySelectorAll('.dir-auto')); - var elements = [].slice.call(document.querySelectorAll('.dir-auto')); + elements.forEach(function(el) { + var farsiCountNode = el.parentNode.querySelector('.count-f'), + latinCountNode = el.parentNode.querySelector('.count-e'), + hebrewCountNode = el.parentNode.querySelector('.count-h'), + countNode = el.parentNode.querySelector('.count-all'); - elements.forEach(function(el) { - var farsiCountNode = el.parentNode.querySelector('.count-f'), - latinCountNode = el.parentNode.querySelector('.count-e'), - countNode = el.parentNode.querySelector('.count-all'); - - el.addEventListener('keyup', function(e) { - var value = e.target.value, - farsiChars = value.match(/[\u0600-\u06FF]/g), - spaceChars = value.match(/\s/g), - count = value.length, - farsiCount = farsiChars ? farsiChars.length : 0, + el.addEventListener('keyup', function(e) { + var value = e.target.value, + farsiChars = value.match(/[\u0600-\u06FF]/g), + hebrewChars = value.match(/[\u0590-\u05FF]/g), + spaceChars = value.match(/\s/g), + count = value.length, + farsiCount = farsiChars ? farsiChars.length : 0, + hebrewCount = hebrewChars ? hebrewChars.length : 0, spaceCount = spaceChars ? spaceChars.length : 0, - latinCount = count - farsiCount - spaceCount; - - e.target.setAttribute('dir', (farsiCount > latinCount) ? 'rtl' : 'ltr'); + rtlCount = farsiCount + hebrewCount; + latinCount = count - farsiCount - spaceCount - hebrewCount; - if(farsiCountNode) farsiCountNode.innerHTML = farsiCount; - if(latinCountNode) latinCountNode.innerHTML = latinCount; - if(countNode) countNode.innerHTML = count; - }); - }); + e.target.setAttribute('dir', rtlCount > latinCount ? 'rtl' : 'ltr'); + if (farsiCountNode) farsiCountNode.innerHTML = farsiCount; + if (latinCountNode) latinCountNode.innerHTML = latinCount; + if (hebrewCountNode) hebrewCountNode.innerHTML = hebrewCount; + if (countNode) countNode.innerHTML = count; + }); + }); }); diff --git a/docs/index.html b/docs/index.html index 28fb9da..3eb8850 100644 --- a/docs/index.html +++ b/docs/index.html @@ -28,7 +28,7 @@

javascript Automatic direction input

By Milad Dehghan

- This is based on character count
If arabic(or persian) characters are more than Latin characters it will be RTL . + This is based on character count
If Arabic(or Persian and Hebrew) characters are more than Latin characters it will be RTL .
Read how to use in github

@@ -39,6 +39,7 @@

By Milad Dehghan

diff --git a/jquery.automatic-direction.js b/jquery.automatic-direction.js deleted file mode 100644 index 2c50fe1..0000000 --- a/jquery.automatic-direction.js +++ /dev/null @@ -1,30 +0,0 @@ -/*! - * automatic Direction v1.2 - * By Milad Dehghan (http://dehghan.net) - * github: https://github.com/miladd3/automatic-direction - */ -$(function() { - - $(".dir-auto").keyup(function(){ - - var textareavalue = $(this).val(); //Getting input value - var arabic = /[\u0600-\u06FF]/g; //setting arabic unicode - var match = textareavalue.match(arabic); - var spacesMatch = textareavalue.match(new RegExp(" ", 'g')); - allcount = textareavalue.length; - farsicount = match ? match.length : 0; - spacesCount = spacesMatch ? spacesMatch.length : 0; - Englishcount = allcount - farsicount - spacesCount; - - if (farsicount > Englishcount) { - $(this).attr('dir','rtl'); - }else { - $(this).attr('dir','ltr'); - } - - //Showing character count on front-end - $(this).parent().find('.count-f').text(farsicount); - $(this).parent().find('.count-e').text(Englishcount); - $(this).parent().find('.count-all').text(allcount); - }); -}); \ No newline at end of file