Skip to content

Commit

Permalink
Add on click plugin:
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradLinkowski committed Jul 23, 2020
1 parent c05d01c commit 1a4e298
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import print from './plugins/print/print.js';
import marquee from './plugins/marquee.js';
import transitionWipe from './plugins/transitionWipe.js';
import dataUtils from './plugins/dataUtils.js';
import onClick from './plugins/onClick.js';

// Add built-in plugins
Checkboxland.extend(print);
Checkboxland.extend(marquee);
Checkboxland.extend(transitionWipe);
Checkboxland.extend(dataUtils);
Checkboxland.extend(onClick);

export { Checkboxland }
52 changes: 52 additions & 0 deletions src/plugins/onClick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
let handleFun = null;
let displayEl = null;

function onClick(callback) {
displayEl = this.displayEl;
handleFun = handleEvent.bind(this, callback);
displayEl.addEventListener('click', handleFun);
}

function handleEvent(callback, event) {
const coords = findCoodrs(displayEl, event.target);
if (!coords) {
return;
}

const result = {
x: coords.x,
y: coords.y,
checkbox: event.target,
}

if (typeof callback == 'function') {
callback(result);
} else if ('handleEvent' in callback && typeof callback.handleEvent == 'function') {
callback.handleEvent(result);
} else {
throw new TypeError('Callback should be a function or an EventListener object');
}
}

function findCoodrs(root, target) {
for (let y = 0; y < root.children.length; y += 1) {
const div = root.children[y];
for (let x = 0; x < div.children.length; x += 1) {
const checkbox = div.children[x];
if (checkbox === target) {
return { x, y };
}
}
}
return null;
}

function cleanUp() {
displayEl.removeEventListener('click', handleFun);
}

export default {
name: 'onClick',
exec: onClick,
cleanUp: cleanUp,
}

0 comments on commit 1a4e298

Please sign in to comment.