Build mobile apps with simple HTML, CSS, and JS components. http://goratchet.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 KiB

/* ========================================================================
* Ratchet: segmented-controllers.js v2.0.2
* http://goratchet.com/components#segmentedControls
* ========================================================================
10 years ago
* Copyright 2015 Connor Sears
* Licensed under MIT (https://github.com/twbs/ratchet/blob/master/LICENSE)
* ======================================================================== */
12 years ago
!(function () {
'use strict';
12 years ago
var getTarget = function (target) {
var i;
var segmentedControls = document.querySelectorAll('.segmented-control .control-item');
12 years ago
for (; target && target !== document; target = target.parentNode) {
for (i = segmentedControls.length; i--;) {
if (segmentedControls[i] === target) {
return target;
}
}
12 years ago
}
};
window.addEventListener('touchend', function (e) {
12 years ago
var activeTab;
var activeBodies;
12 years ago
var targetBody;
var targetTab = getTarget(e.target);
var className = 'active';
12 years ago
var classSelector = '.' + className;
if (!targetTab) {
return;
}
12 years ago
activeTab = targetTab.parentNode.querySelector(classSelector);
if (activeTab) {
activeTab.classList.remove(className);
}
12 years ago
targetTab.classList.add(className);
if (!targetTab.hash) {
return;
}
12 years ago
targetBody = document.querySelector(targetTab.hash);
12 years ago
if (!targetBody) {
return;
}
12 years ago
activeBodies = targetBody.parentNode.querySelectorAll(classSelector);
12 years ago
for (var i = 0; i < activeBodies.length; i++) {
activeBodies[i].classList.remove(className);
}
12 years ago
targetBody.classList.add(className);
12 years ago
});
window.addEventListener('click', function (e) {
if (getTarget(e.target)) {
e.preventDefault();
}
});
}());