Skip to content

Commit

Permalink
Improved script performance
Browse files Browse the repository at this point in the history
  • Loading branch information
KittyGiraudel committed Mar 8, 2016
1 parent f8fcd1f commit 8a7bfa0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
29 changes: 17 additions & 12 deletions a11y-toggle.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
(function () {
var toggles = document.querySelectorAll('[data-a11y-toggle][aria-controls]');
var targetsList = [];

function prepareDOM (toggle, target) {
for (var i = 0; i < toggles.length; i += 1) {
var toggle = toggles[i];
targetsList.push('#' + toggle.getAttribute('aria-controls'));
toggle.hasAttribute('aria-expanded') || toggle.setAttribute('aria-expanded', false);
target.hasAttribute('aria-hidden') || target.setAttribute('aria-hidden', true);
}

function handleClick (toggle, target) {
var isExpanded = JSON.parse(toggle.getAttribute('aria-expanded'));
var targets = document.querySelectorAll(targetsList);
var targetsMap = {};

toggle.setAttribute('aria-expanded', !isExpanded);
target.setAttribute('aria-hidden', !!isExpanded);
for (var j = 0; j < targets.length; j += 1) {
var target = targets[j];
targetsMap[target.id] = target;
target.hasAttribute('aria-hidden') || target.setAttribute('aria-hidden', true);
}

for (var i = 0; i < toggles.length; i += 1) {
var toggle = toggles[i];
var target = document.getElementById(toggle.getAttribute('aria-controls'));
document.addEventListener('click', function (event) {
var toggle = event.target;
var target = targetsMap[toggle.getAttribute('aria-controls')];
var isExpanded = JSON.parse(toggle.getAttribute('aria-expanded'));

prepareDOM(toggle, target);
toggle.addEventListener('click', handleClick.bind(null, toggle, target), false);
}
toggle.setAttribute('aria-expanded', !isExpanded);
target.setAttribute('aria-hidden', isExpanded);
});
}());
2 changes: 1 addition & 1 deletion a11y-toggle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions example/main.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
(function () {
var toggles = document.querySelectorAll('[data-a11y-toggle][aria-controls]');
var targetsList = [];

function prepareDOM (toggle, target) {
for (var i = 0; i < toggles.length; i += 1) {
var toggle = toggles[i];
targetsList.push('#' + toggle.getAttribute('aria-controls'));
toggle.hasAttribute('aria-expanded') || toggle.setAttribute('aria-expanded', false);
target.hasAttribute('aria-hidden') || target.setAttribute('aria-hidden', true);
}

function handleClick (toggle, target) {
var isExpanded = JSON.parse(toggle.getAttribute('aria-expanded'));
var targets = document.querySelectorAll(targetsList);
var targetsMap = {};

toggle.setAttribute('aria-expanded', !isExpanded);
target.setAttribute('aria-hidden', !!isExpanded);
for (var j = 0; j < targets.length; j += 1) {
var target = targets[j];
targetsMap[target.id] = target;
target.hasAttribute('aria-hidden') || target.setAttribute('aria-hidden', true);
}

for (var i = 0; i < toggles.length; i += 1) {
var toggle = toggles[i];
var target = document.getElementById(toggle.getAttribute('aria-controls'));
document.addEventListener('click', function (event) {
var toggle = event.target;
var target = targetsMap[toggle.getAttribute('aria-controls')];
var isExpanded = JSON.parse(toggle.getAttribute('aria-expanded'));

prepareDOM(toggle, target);
toggle.addEventListener('click', handleClick.bind(null, toggle, target), false);
}
toggle.setAttribute('aria-expanded', !isExpanded);
target.setAttribute('aria-hidden', isExpanded);
});
}());

0 comments on commit 8a7bfa0

Please sign in to comment.