From 3ac332f523ea9ee10506fcd9de15c9a8c47425c5 Mon Sep 17 00:00:00 2001 From: ewan-m Date: Wed, 11 Dec 2019 12:10:04 +0000 Subject: [PATCH 1/3] Remove unnecessary switch statement --- index.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/index.js b/index.js index 8f6169e..59d87d3 100644 --- a/index.js +++ b/index.js @@ -188,19 +188,7 @@ function makeChoice(whichOption) { } function setState(newState) { - switch (newState) { - case 'initial': - csState = 'initial' - break - case 'opened': - csState = 'opened' - break - case 'filtered': - csState = 'filtered' - break - case 'closed': - csState = 'closed' - } + csState = newState; // console.log({csState}) } From a971b94687aac693ff41c7f3dbefeb220bcb9d96 Mon Sep 17 00:00:00 2001 From: ewan-m Date: Wed, 11 Dec 2019 12:31:01 +0000 Subject: [PATCH 2/3] Replace e.target.closest with IE compatible bespoke function --- index.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 59d87d3..0f68802 100644 --- a/index.js +++ b/index.js @@ -83,7 +83,7 @@ csSelector.addEventListener('keyup', function(e) { }) document.addEventListener('click', function(e) { - if (!e.target.closest('#myCustomSelect')) { + if (!aParentElementHasId(e, '#myCustomSelect', 20)) { // click outside of the custom group toggleList('Shut') setState('initial') @@ -93,6 +93,23 @@ document.addEventListener('click', function(e) { // FUNCTIONS // ///////////////////////////////// +function aParentElementHasId(event, id, maxDepth) { + let currentElement = event.srcElement + let count = 0 + + while (currentElement !== undefined) { + count++ + if (currentElement.id === id) { + return true + } + if (maxDepth && count > maxDepth) { + return false + } + currentElement = currentElement.parentElement + } + return false +} + function toggleList(whichWay) { if (whichWay === 'Open') { csList.classList.remove('hidden-all') @@ -104,8 +121,7 @@ function toggleList(whichWay) { } function findFocus() { - const focusPoint = document.activeElement - return focusPoint + return document.activeElement } function moveFocus(fromHere, toThere) { @@ -188,8 +204,7 @@ function makeChoice(whichOption) { } function setState(newState) { - csState = newState; - // console.log({csState}) + csState = newState } function doKeyAction(whichKey) { From 12057015e47998924f74b4d0eb4c557a6e8c03a8 Mon Sep 17 00:00:00 2001 From: ewan-m Date: Wed, 11 Dec 2019 12:35:44 +0000 Subject: [PATCH 3/3] Remove # for id selector --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0f68802..16df951 100644 --- a/index.js +++ b/index.js @@ -83,7 +83,7 @@ csSelector.addEventListener('keyup', function(e) { }) document.addEventListener('click', function(e) { - if (!aParentElementHasId(e, '#myCustomSelect', 20)) { + if (!aParentElementHasId(e, 'myCustomSelect', 20)) { // click outside of the custom group toggleList('Shut') setState('initial') @@ -97,7 +97,7 @@ function aParentElementHasId(event, id, maxDepth) { let currentElement = event.srcElement let count = 0 - while (currentElement !== undefined) { + while (currentElement !== undefined && currentElement !== null) { count++ if (currentElement.id === id) { return true