17 Commits

Author SHA1 Message Date
3fc6ff1c78 Performance improvements in other events 2020-07-21 16:35:00 +02:00
dac40e8d61 Improved scroll performance 2020-07-21 16:21:58 +02:00
6f965ce6c3 Remove media 2020-07-17 18:05:44 +02:00
988c7c15dd Update README.md 2020-07-17 18:04:01 +02:00
b5fe682764 Update README.md 2020-07-17 18:01:33 +02:00
732dcfca57 Update README.md 2020-07-17 18:01:17 +02:00
4b01ca94bb Fix dash 2020-07-17 17:50:20 +02:00
91338f9185 Update README.md 2020-07-17 16:29:34 +02:00
0284ebc9c4 Update README.md 2020-07-17 16:25:00 +02:00
3aa327bad6 Update README.md 2020-07-17 12:12:22 +02:00
a30f4023d2 Fix regex check 2020-07-17 12:12:02 +02:00
ae1c78da16 Add hover 2020-07-17 11:11:53 +02:00
37820b5bb4 Fix images 2020-07-16 11:30:47 +02:00
2c306485d4 Update README.md 2020-07-16 11:23:05 +02:00
605a4463b6 Update media 2020-07-16 11:15:38 +02:00
cd75682160 Delete example.png 2020-07-16 11:09:17 +02:00
facde1d9db Update README.md 2020-07-16 01:12:36 +02:00
5 changed files with 167 additions and 38 deletions

View File

@ -1,12 +1,28 @@
# FFNM (Front-End for the next master) # FFNM (Front-End for the next master)
![example](media/example.png) ``` html
<button i-click="class:add('open', 'article')">open</button>
Simple utility to **avoid writing Javascript** when working with **classes**. It only takes up **1,7 Kb**! <article></article>
```
👇 🖱 **Click!** 👇
``` html
<button i-click="class:add('open', 'article')">open</button>
<article class="open"></article>
```
Simple utility to **avoid writing Javascript** when working with **classes**. It only takes up **2.5Kb**!
- Simplifies the **click**.
- Simplifies the **scroll**.
- Simplifies **hover**.
## DEMOS ## DEMOS
[Click](https://codepen.io/androsfenollosa/pen/dyGdRVE) [Click](https://codepen.io/androsfenollosa/pen/dyGdRVE) | [Scroll](https://codepen.io/androsfenollosa/pen/xxZQxNV)
## Documentation ## Documentation
@ -22,7 +38,7 @@ Simple utility to **avoid writing Javascript** when working with **classes**. It
Add to your `<head>` the following tag. Add to your `<head>` the following tag.
```html ```html
<script src="https://cdn.jsdelivr.net/gh/tanrax/FFNM@v1.0.0/dist/ffnm.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/tanrax/FFNM@v1.2.3/dist/ffnm.min.js"></script>
``` ```
### Examples ### Examples
@ -96,27 +112,34 @@ Add to your `<head>` the following tag.
<nav i-scroll-down="class:remove('show')"></nav> <nav i-scroll-down="class:remove('show')"></nav>
``` ```
#### Hover
#### Classes without events ##### Add the class `show` to `div` when hover.
##### Add the class `show` to the `#nav` selector ```html
<div i-hover="class:add('show')"></div>
```javascript
class:add('show', '#nav')
``` ```
##### Remove the `show` class from the `#nav` selector ##### Remove the `show` class from `div` when hover.
```javascript ```html
class:remove('show', '#nav') <div i-hover="class:remove('show')"></div>
``` ```
##### Switch the class `show` to the `#nav` selector ##### Toggle the `show` class from `div` when hover.
```javascript ```html
class:toggle('show', '#nav') <div i-hover="class:toggle('show')"></div>
``` ```
##### Add the class `show` to `#button` when `div` hover.
```html
<div i-hover="class:toggle('show', '#button')"></div>
<button id="button">Hi</div>
```
--- ---
## Spanish/Español ## Spanish/Español
@ -126,7 +149,7 @@ class:toggle('show', '#nav')
Añade a tu `<head>` la siguiente etiqueta. Añade a tu `<head>` la siguiente etiqueta.
```html ```html
<script src="https://cdn.jsdelivr.net/gh/tanrax/FFNM@v1.0.0/ffnm.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/tanrax/FFNM@v1.2.3/dist/ffnm.min.js"></script>
``` ```
### Ejemplos de uso ### Ejemplos de uso
@ -200,26 +223,33 @@ Añade a tu `<head>` la siguiente etiqueta.
<nav i-scroll-down="class:remove('show')"></nav> <nav i-scroll-down="class:remove('show')"></nav>
``` ```
#### Hover
#### Clases sin eventos ##### Añade la clase `show` al `div` cuando sea hover.
##### Añadir la clase `show` al selector `#menu` ```html
<div i-hover="class:add('show')"></div>
```javascript
class:add('show', '#menu')
``` ```
##### Quitar la clase `show` al selector `#menu` ##### Quita la clase `show` al `div` cuando sea hover.
```javascript ```html
class:remove('show', '#menu') <div i-hover="class:remove('show')"></div>
``` ```
##### Intercambiar la clase `show` al selector `#menu` ##### Intercambia la clase `show` al `div` cuando sea hover.
```javascript ```html
class:toggle('show', '#menu') <div i-hover="class:toggle('show')"></div>
``` ```
##### Añade la clase `show` a `#button` cuando `div` sea hover.
```html
<div i-hover="class:toggle('show', '#button')"></div>
<button id="button">Hi</div>
```
--- ---
## Development ## Development

2
dist/ffnm.min.js vendored
View File

@ -1,2 +1,2 @@
document.addEventListener("DOMContentLoaded",()=>{const e={class:["add","remove","toggle"]};let c=[],t=void 0;function l(e,c){let t=e.getAttribute(c),l=RegExp("^(\\w+):").exec(t)[1],s=RegExp(":(\\w+)\\(").exec(t)[1],a=RegExp("\\('(\\w+)',?").exec(t)[1],o=RegExp(", *'([#,.,a-zA-Z]\\w*)'\\)");return{functionParent:l,functionChild:s,value:a,target:null!==o.exec(t)?o.exec(t)[1]:void 0}}c=[],["i-click","i-scroll-up","i-scroll-down"].map(t=>[...document.querySelectorAll(`[${t}]`)].map(l=>{let s=Object.keys(e).map(c=>e[c].map(e=>RegExp(`^${c}:${e}\\('\\w+'(, *'[#,.,a-zA-Z]\\w*')?\\)$`).test(l.getAttribute(t))).some(e=>e)).every(e=>e);return s?c.push(l):(console.error("FFNM: Bad syntax"),console.error(l)),s}).every(e=>e)).every(e=>e),[...document.querySelectorAll("[i-click]")].forEach(e=>{let c=l(e,"i-click");switch(c.functionParent){case"class":[...document.querySelectorAll(c.target)].concat(e).forEach(t=>{e.addEventListener("click",()=>{switch(c.functionChild){case"add":t.classList.add(c.value);break;case"remove":t.classList.remove(c.value);break;case"toggle":t.classList.toggle(c.value)}})})}}),window.addEventListener("scroll",()=>{let e=window.pageYOffset||document.documentElement.scrollTop;[...document.querySelectorAll("[i-scroll-down]")].forEach(c=>{if(e>t){let e=l(c,"i-scroll-down");switch(e.functionParent){case"class":switch(e.functionChild){case"add":c.classList.add(e.value);break;case"remove":c.classList.remove(e.value)}}}}),[...document.querySelectorAll("[i-scroll-up]")].forEach(c=>{if(e<=t){let e=l(c,"i-scroll-up");switch(e.functionParent){case"class":switch(e.functionChild){case"add":c.classList.add(e.value);break;case"remove":c.classList.remove(e.value)}}}}),t=e<=0?0:e},!1)}); document.addEventListener("DOMContentLoaded",()=>{const e={class:["add","remove","toggle"]};let t=[],s=void 0;function a(e,t){let s=e.getAttribute(t),a=RegExp("^(\\w+):").exec(s)[1],c=RegExp(":(\\w+)\\(").exec(s)[1],o=RegExp("\\('(\\w[_-\\w0-9]+)',?").exec(s)[1],l=RegExp(", ?'([.#i\\w][_-\\w0-9]+)'\\)");return{functionParent:a,functionChild:c,value:o,target:null!==l.exec(s)?l.exec(s)[1]:void 0}}function c(e){return[...t].filter(t=>t.hasAttribute(e))}t=[],["i-click","i-scroll-up","i-scroll-down","i-hover"].map(s=>[...document.querySelectorAll(`[${s}]`)].map(a=>{let c=Object.keys(e).map(t=>e[t].map(e=>RegExp(`^${t}:${e}\\('.+'(, ?'[.#i\\w][_-\\w0-9]+')?\\)$`).test(a.getAttribute(s))).some(e=>e)).every(e=>e);return c?t.push(a):(console.error("FFNM: Bad syntax"),console.error(a)),c}).every(e=>e)).every(e=>e),c("i-click").forEach(e=>{let t=a(e,"i-click");switch(t.functionParent){case"class":[...document.querySelectorAll(t.target)].concat(void 0===t.target?e:void 0).forEach(s=>{void 0!==s&&e.addEventListener("click",()=>{switch(t.functionChild){case"add":s.classList.add(t.value);break;case"remove":s.classList.remove(t.value);break;case"toggle":s.classList.toggle(t.value)}})})}}),function(){let e=c("i-scroll-down"),t=c("i-scroll-up");window.addEventListener("scroll",()=>{let c=window.pageYOffset||document.documentElement.scrollTop;e.forEach(e=>{if(c>s){let t=a(e,"i-scroll-down");switch(t.functionParent){case"class":switch(t.functionChild){case"add":e.classList.add(t.value);break;case"remove":e.classList.remove(t.value)}}}}),t.forEach(e=>{if(c<=s){let t=a(e,"i-scroll-up");switch(t.functionParent){case"class":switch(t.functionChild){case"add":e.classList.add(t.value);break;case"remove":e.classList.remove(t.value)}}}}),s=c<=0?0:c},!1)}(),c("i-hover").forEach(e=>{let t=a(e,"i-hover");switch(t.functionParent){case"class":[...document.querySelectorAll(t.target)].concat(void 0===t.target?e:void 0).forEach(s=>{if(void 0!==s)switch(t.functionChild){case"add":e.addEventListener("mouseenter",()=>{s.classList.add(t.value)}),e.addEventListener("mouseout",()=>{s.classList.remove(t.value)});break;case"remove":e.addEventListener("mouseenter",()=>{s.classList.remove(t.value)}),e.addEventListener("mouseout",()=>{s.classList.add(t.value)});break;case"toggle":e.addEventListener("mouseenter",()=>{s.classList.toggle(t.value)}),e.addEventListener("mouseout",()=>{s.classList.toggle(t.value)})}})}})});
//# sourceMappingURL=ffnm.min.js.map //# sourceMappingURL=ffnm.min.js.map

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

View File

@ -3,7 +3,7 @@ document.addEventListener('DOMContentLoaded', () => {
//=== //===
// VARIABLES // VARIABLES
//=== //===
const EVENTS = ['i-click', 'i-scroll-up', 'i-scroll-down']; const EVENTS = ['i-click', 'i-scroll-up', 'i-scroll-down', 'i-hover'];
const FUNCTION_TREE = { const FUNCTION_TREE = {
'class': ['add', 'remove', 'toggle'] 'class': ['add', 'remove', 'toggle']
}; };
@ -28,7 +28,7 @@ document.addEventListener('DOMContentLoaded', () => {
let checked = Object.keys(FUNCTION_TREE).map((key) => { let checked = Object.keys(FUNCTION_TREE).map((key) => {
// Check METHODS FUNCTION_TREE // Check METHODS FUNCTION_TREE
return FUNCTION_TREE[key].map((method) => { return FUNCTION_TREE[key].map((method) => {
return RegExp(`^${key}:${method}\\('\\w+'(, *'[#,.,a-zA-Z]\\w*')?\\)$`).test(element.getAttribute(event)); return RegExp(`^${key}:${method}\\('.+'(, ?'[.#i\\w][_-\\w0-9]+')?\\)$`).test(element.getAttribute(event));
}).some(method => method); }).some(method => method);
}).every(key => key); }).every(key => key);
if(checked) { if(checked) {
@ -52,25 +52,36 @@ document.addEventListener('DOMContentLoaded', () => {
*/ */
function splitParams(element, attribute) { function splitParams(element, attribute) {
let params = element.getAttribute(attribute); let params = element.getAttribute(attribute);
let functionParent = RegExp(`^(\\w+):`).exec(params)[1]; let functionParent = RegExp("^(\\w+):").exec(params)[1];
let functionChild = RegExp(`:(\\w+)\\(`).exec(params)[1]; let functionChild = RegExp(":(\\w+)\\(").exec(params)[1];
let value = RegExp(`\\(\'(\\w+)\',?`).exec(params)[1]; let value = RegExp("\\(\'(\\w[_-\\w0-9]+)\',?").exec(params)[1];
let resultTarget = RegExp(`, *\'([#,.,a-zA-Z]\\w*)\'\\)`); let resultTarget = RegExp(", ?\'([.#i\\w][_-\\w0-9]+)\'\\)");
let target = resultTarget.exec(params) !== null ? resultTarget.exec(params)[1] : undefined; let target = resultTarget.exec(params) !== null ? resultTarget.exec(params)[1] : undefined;
return {'functionParent': functionParent, 'functionChild': functionChild, 'value': value, 'target': target}; return {'functionParent': functionParent, 'functionChild': functionChild, 'value': value, 'target': target};
} }
/**
* Method that returns all validated elements filtered by an event
* return Array
*/
function getElementsValidatesByEvent(nameEvent) {
return [...elementsValidates].filter(element => {
return element.hasAttribute(nameEvent);
});
}
/** /**
* Method add events i-click * Method add events i-click
* return void * return void
*/ */
function addEventClick() { function addEventClick() {
let eventClick = 'i-click'; let eventClick = 'i-click';
return [...document.querySelectorAll(`[${eventClick}]`)].forEach((element) => { return getElementsValidatesByEvent(eventClick).forEach((element) => {
let params = splitParams(element, eventClick); let params = splitParams(element, eventClick);
switch(params.functionParent) { switch(params.functionParent) {
case 'class': case 'class':
[...document.querySelectorAll(params.target)].concat(element).forEach((item) => { [...document.querySelectorAll(params.target)].concat(params.target === undefined ? element : undefined).forEach((item) => {
if (item !== undefined) {
element.addEventListener('click', () => { element.addEventListener('click', () => {
switch(params.functionChild) { switch(params.functionChild) {
case 'add': case 'add':
@ -84,6 +95,7 @@ document.addEventListener('DOMContentLoaded', () => {
break; break;
} }
}); });
}
}); });
break; break;
} }
@ -97,10 +109,12 @@ document.addEventListener('DOMContentLoaded', () => {
function addEventScroll() { function addEventScroll() {
let eventScrollDown = 'i-scroll-down'; let eventScrollDown = 'i-scroll-down';
let eventScrollUp = 'i-scroll-up'; let eventScrollUp = 'i-scroll-up';
let elementsDown = getElementsValidatesByEvent(eventScrollDown);
let elementsUp = getElementsValidatesByEvent(eventScrollUp);
window.addEventListener("scroll", () => { window.addEventListener("scroll", () => {
let posScroll = window.pageYOffset || document.documentElement.scrollTop; let posScroll = window.pageYOffset || document.documentElement.scrollTop;
// Scroll down // Scroll down
[...document.querySelectorAll(`[${eventScrollDown}]`)].forEach((element) => { elementsDown.forEach((element) => {
if (posScroll > lastScrollTop) { if (posScroll > lastScrollTop) {
let params = splitParams(element, eventScrollDown); let params = splitParams(element, eventScrollDown);
switch(params.functionParent) { switch(params.functionParent) {
@ -118,7 +132,7 @@ document.addEventListener('DOMContentLoaded', () => {
} }
}); });
// Scroll up // Scroll up
[...document.querySelectorAll(`[${eventScrollUp}]`)].forEach((element) => { elementsUp.forEach((element) => {
if (posScroll <= lastScrollTop) { if (posScroll <= lastScrollTop) {
let params = splitParams(element, eventScrollUp); let params = splitParams(element, eventScrollUp);
switch(params.functionParent) { switch(params.functionParent) {
@ -139,10 +153,64 @@ document.addEventListener('DOMContentLoaded', () => {
}, false); }, false);
} }
/**
* Method add events i-hover
* return void
*/
function addEventHover() {
let eventHover = 'i-hover';
return getElementsValidatesByEvent(eventHover).forEach((element) => {
let params = splitParams(element, eventHover);
switch(params.functionParent) {
case 'class':
[...document.querySelectorAll(params.target)].concat(params.target === undefined ? element : undefined).forEach((item) => {
if (item !== undefined) {
// Enter
switch(params.functionChild) {
case 'add':
// Enter
element.addEventListener('mouseenter', () => {
item.classList.add(params.value);
});
// Out
element.addEventListener('mouseout', () => {
item.classList.remove(params.value);
});
break;
case 'remove':
// Enter
element.addEventListener('mouseenter', () => {
item.classList.remove(params.value);
});
// Out
element.addEventListener('mouseout', () => {
item.classList.add(params.value);
});
break;
case 'toggle':
// Enter
element.addEventListener('mouseenter', () => {
item.classList.toggle(params.value);
});
// Out
element.addEventListener('mouseout', () => {
item.classList.toggle(params.value);
});
break;
}
}
});
break;
}
});
}
//=== //===
// INIT // INIT
//=== //===
validateSyntax(); validateSyntax();
addEventClick(); addEventClick();
addEventScroll(); addEventScroll();
addEventHover();
}); });

31
test/i-hover.html Normal file
View File

@ -0,0 +1,31 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Test i-hover</title>
<script src="../dist/ffnm.min.js"></script>
<style>
.uppercase {
text-transform: uppercase;
}
</style>
</head>
<body>
<p i-hover="class:add('uppercase', '.text__1--hover')">Place the cursor over this text</p>
<p class="text__1--hover">
Quisque id diam vel quam elementum pulvinar etiam non quam?
</p>
<p class="text__1--hover">
Ac, feugiat sed lectus vestibulum mattis ullamcorper velit sed ullamcorper.
</p>
<p class="text__1--hover" i-hover="class:add('uppercase')">
Nunc aliquet bibendum enim, facilisis gravida neque convallis a cras.
</p>
<p class="text__1--hover">
Orci phasellus egestas tellus rutrum tellus pellentesque eu tincidunt tortor.
</p>
<p class="text__1--hover">
Volutpat consequat, mauris nunc congue nisi, vitae suscipit tellus mauris.
</p>
</body>
</html>