This commit is contained in:
Andros Fenollosa 2020-07-17 17:50:20 +02:00
parent 91338f9185
commit 4b01ca94bb
4 changed files with 47 additions and 43 deletions

View File

@ -34,7 +34,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.2.1/dist/ffnm.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/tanrax/FFNM@v1.2.2/dist/ffnm.min.js"></script>
``` ```
### Examples ### Examples
@ -145,7 +145,7 @@ Add to your `<head>` the following tag.
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.2.1/dist/ffnm.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/tanrax/FFNM@v1.2.2/dist/ffnm.min.js"></script>
``` ```
### Ejemplos de uso ### Ejemplos de uso

2
dist/ffnm.min.js vendored
View File

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

View File

@ -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}\\('.+'(, *'[.#i\\w][_-\\w0-9]+')?\\)$`).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,10 +52,10 @@ 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};
} }
@ -70,7 +70,8 @@ document.addEventListener('DOMContentLoaded', () => {
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 +85,7 @@ document.addEventListener('DOMContentLoaded', () => {
break; break;
} }
}); });
}
}); });
break; break;
} }
@ -150,39 +152,41 @@ document.addEventListener('DOMContentLoaded', () => {
let params = splitParams(element, eventHover); let params = splitParams(element, eventHover);
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) => {
// Enter if (item !== undefined) {
switch(params.functionChild) { // Enter
case 'add': switch(params.functionChild) {
// Enter case 'add':
element.addEventListener('mouseenter', () => { // Enter
item.classList.add(params.value); element.addEventListener('mouseenter', () => {
}); item.classList.add(params.value);
// Out });
element.addEventListener('mouseout', () => { // Out
item.classList.remove(params.value); element.addEventListener('mouseout', () => {
}); item.classList.remove(params.value);
break; });
case 'remove': break;
// Enter case 'remove':
element.addEventListener('mouseenter', () => { // Enter
item.classList.remove(params.value); element.addEventListener('mouseenter', () => {
}); item.classList.remove(params.value);
// Out });
element.addEventListener('mouseout', () => { // Out
item.classList.add(params.value); element.addEventListener('mouseout', () => {
}); item.classList.add(params.value);
break; });
case 'toggle': break;
// Enter case 'toggle':
element.addEventListener('mouseenter', () => { // Enter
item.classList.toggle(params.value); element.addEventListener('mouseenter', () => {
}); item.classList.toggle(params.value);
// Out });
element.addEventListener('mouseout', () => { // Out
item.classList.toggle(params.value); element.addEventListener('mouseout', () => {
}); item.classList.toggle(params.value);
break; });
break;
}
} }
}); });
break; break;

View File

@ -11,7 +11,7 @@
</style> </style>
</head> </head>
<body> <body>
<p i-hover="class:add('uppercase', '.text__-hover')">Place the cursor over this text</p> <p i-hover="class:add('uppercase', '.text__1--hover')">Place the cursor over this text</p>
<p class="text__1--hover"> <p class="text__1--hover">
Quisque id diam vel quam elementum pulvinar etiam non quam? Quisque id diam vel quam elementum pulvinar etiam non quam?
</p> </p>