Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
v1.3
Browse files Browse the repository at this point in the history
Resilves: #3 add support for Hebrew language
delete jquery
  • Loading branch information
miladd3 committed Oct 25, 2019
1 parent 0a738d5 commit a20a347
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 76 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ add .dir-auto class to input that you want to automate direction on it
```html
<textarea class="dir-auto" ></textarea>
```
## 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

Expand Down
45 changes: 24 additions & 21 deletions automatic-direction.js
Original file line number Diff line number Diff line change
@@ -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;
});
});
});
45 changes: 24 additions & 21 deletions docs/automatic-direction.js
Original file line number Diff line number Diff line change
@@ -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;
});
});
});
3 changes: 2 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<h1>javascript Automatic direction input</h1>
<h2>By <a href="http://dehghan.net">Milad Dehghan</a></h2>
<p>
This is based on <strong>character count</strong><br> If arabic(or persian) characters are more than Latin characters it will be RTL .
This is based on <strong>character count</strong><br> If Arabic(or Persian and Hebrew) characters are more than Latin characters it will be RTL .
<br>
<small>Read how to use in <a href="https://github.com/miladd3/automatic-direction" title="github">github</a></small>
</p>
Expand All @@ -39,6 +39,7 @@ <h2>By <a href="http://dehghan.net">Milad Dehghan</a></h2>
<ul class="detail">
<li>Arabic(or persian) characters: <span class="count-f"></span></li>
<li>Latin Characters: <span class="count-e"></span></li>
<li>Hebrew Characters: <span class="count-h"></span></li>
<li>All Characters: <span class="count-all"></span></li>
</ul>

Expand Down
30 changes: 0 additions & 30 deletions jquery.automatic-direction.js

This file was deleted.

0 comments on commit a20a347

Please sign in to comment.