// -------------------------------------------------- // hashtabber v2.2 by smutnyleszek@gmail.com // http://hashtabber.smutnyleszek.com // license cc0 1.0 // -------------------------------------------------- // -------------------------------------------------- // class managing helper functions // -------------------------------------------------- function hasclass(el, cl) { return el.classname && new regexp('(\\s|^)' + cl + '(\\s|$)').test(el.classname); } function addclass(el, cl) { if (!hasclass(el, cl)) { el.classname += ' ' + cl; } } function removeclass(el, cl) { var reg = new regexp('(\\s|^)' + cl + '(\\s|$)'); el.classname = el.classname.replace(reg, ' ').replace(/(^\s*)|(\s*$)/g, ''); } function toggleclass(el, cl) { if (hasclass(el, cl)) { removeclass(el, cl); } else { addclass(el, cl); } } // -------------------------------------------------- // main hashtabber creator function // -------------------------------------------------- function hashtabber(customoptions) { this.options = { classactive: 'active', classdata: 'hashtabber-data', classnav: 'hashtabber-nav', datadefault: 'data-hashtabber-default', dataid: 'data-hashtabber-id', datapair: 'data-hashtabber-pair' }; // check if there are any custom options if (customoptions) { // go through all the options and set new values if provided if (customoptions.classactive) { this.options.classactive = customoptions.classactive; } if (customoptions.classdata) { this.options.classdata = customoptions.classdata; } if (customoptions.classnav) { this.options.classnav = customoptions.classnav; } if (customoptions.datadefault) { this.options.datadefault = customoptions.datadefault; } if (customoptions.dataid) { this.options.dataid = customoptions.dataid; } if (customoptions.datapair) { this.options.datapair = customoptions.datapair; } } this.helpers = { hashprober: function () { // get hash from window location and remove # character var hash = string(window.location.hash.replace('#', '')); var outcome = false; // if hash not empty // split it to array of parameters by "=" and "&" if (hash.length !== 0) { outcome = hash.split(/\=|&/); } return outcome; }, idsgiver: function (options) { var a, b, c, d; var tabberid; var tabberdefault; var tabname; var navlist; var navlichildren; var datalist; var tabbers = document.queryselectorall('.' + options.classnav); // loop through every instance of hashtabber (nav) for (a = 0; a < tabbers.length; a += 1) { // get current tabber id and default tab tabberid = tabbers[a].getattribute(options.dataid); tabberdefault = '0'; tabname = ''; // check if -default html data is set if (tabbers[a].getattribute(options.datadefault)) { tabberdefault = tabbers[a].getattribute(options.datadefault); } // loop through every nav element of current tabber navlist = tabbers[a].queryselectorall('.' + options.classnav + '>li'); for (b = 0; b < navlist.length; b += 1) { // set current item name to loop index // or custom -pair name if given tabname = string(b); if (navlist[b].getattribute(options.datapair)) { tabname = navlist[b].getattribute(options.datapair); } // loop through all child nodes of li // check for the first a node and add #link to it navlichildren = navlist[b].childnodes; for (c = 0; c < navlichildren.length; c += 1) { if (navlichildren[c].nodename === 'a') { navlichildren[c].setattribute('href', '#' + tabberid + '=' + tabname); } } // set default to active if (tabname === tabberdefault) { addclass(navlist[b], options.classactive); } } // find corresponding data element and lopp through its children datalist = document.queryselectorall('.' + options.classdata + '[' + options.dataid + '="' + tabberid + '"]' + '>li'); for (d = 0; d < datalist.length; d += 1) { // set current item name to loop index // or custom -pair name if given tabname = string(d); if (datalist[d].getattribute(options.datapair)) { tabname = datalist[d].getattribute(options.datapair); } // set default to active if (tabname === tabberdefault) { addclass(datalist[d], options.classactive); } } } return true; }, tabswiper: function (options, hasharray) { var a, b, c; var parameter; var value; var tabname; var tabbernavlist; var tabberdatalist; // loop through all hash parameters (every second item) for (a = 0; a < hasharray.length; a += 2) { // set the parameter and value parameter = hasharray[a]; value = hasharray[a + 1]; tabname = ''; // check if hashlink exists in nav element if (document.queryselectorall('.' + options.classnav + ' a[href*="#' + parameter + '=' + value + '"]').length > 0) { // get the current tabber nav and data lists tabbernavlist = document.queryselectorall('.' + options.classnav + '[' + options.dataid + '="' + parameter + '"]' + '>li'); tabberdatalist = document.queryselectorall('.' + options.classdata + '[' + options.dataid + '="' + parameter + '"]' + '>li'); // clear active class of all nav elements and give it to the target one for (b = 0; b < tabbernavlist.length; b += 1) { // set current item name to loop index // or custom -pair name if given tabname = string(b); if (tabbernavlist[b].getattribute(options.datapair)) { tabname = tabbernavlist[b].getattribute(options.datapair); } // change classes if (tabname === value) { addclass(tabbernavlist[b], options.classactive); } else { removeclass(tabbernavlist[b], options.classactive); } } // clear active class of all data elements and give it to the target one for (c = 0; c < tabberdatalist.length; c += 1) { // set current item name to loop index // or custom -pair name if given tabname = string(c); if (tabberdatalist[c].getattribute(options.datapair)) { tabname = tabberdatalist[c].getattribute(options.datapair); } // change classes if (tabname === value) { addclass(tabberdatalist[c], options.classactive); } else { removeclass(tabberdatalist[c], options.classactive); } } } } return true; } }; this.run = function () { var that = this; // create ids for every link element in hashtabber navigation that.helpers.idsgiver(that.options); // run for the first time on page load that.helpers.tabswiper(that.options, that.helpers.hashprober()); // add listener to hash change window.onhashchange = function () { that.helpers.tabswiper(that.options, that.helpers.hashprober()); }; return true; }; return true; }