-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c05d01c
commit 1a4e298
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |