1/** 2 * @preserve SelectNav.js (v. 0.1) 3 * Converts your <ul>/<ol> navigation into a dropdown list for small screens 4 * https://github.com/lukaszfiszer/selectnav.js 5 */ 6 7window.selectnav = (function(){ 8 9"use strict"; 10 11 var selectnav = function(element,options){ 12 13 element = document.getElementById(element); 14 15 // return immediately if element doesn't exist 16 if( ! element){ 17 return; 18 } 19 20 // return immediately if element is not a list 21 if( ! islist(element) ){ 22 return; 23 } 24 25 // return immediately if no support for insertAdjacentHTML (Firefox 7 and under) 26 if( ! ('insertAdjacentHTML' in window.document.documentElement) ){ 27 return; 28 } 29 30 // add a js class to <html> tag 31 document.documentElement.className += " js"; 32 33 // retreive options and set defaults 34 var o = options || {}, 35 36 activeclass = o.activeclass || 'active', 37 autoselect = typeof(o.autoselect) === "boolean" ? o.autoselect : true, 38 nested = typeof(o.nested) === "boolean" ? o.nested : true, 39 indent = o.indent || "â", 40 label = o.label || "- Navigation -", 41 42 // helper variables 43 level = 0, 44 selected = " selected "; 45 46 // insert the freshly created dropdown navigation after the existing navigation 47 element.insertAdjacentHTML('afterend', parselist(element) ); 48 49 var nav = document.getElementById(id()); 50 51 // autoforward on click 52 if (nav.addEventListener) { 53 nav.addEventListener('change',goTo); 54 } 55 if (nav.attachEvent) { 56 nav.attachEvent('onchange', goTo); 57 } 58 59 return nav; 60 61 function goTo(e){ 62 63 // Crossbrowser issues - http://www.quirksmode.org/js/events_properties.html 64 var targ; 65 if (!e) e = window.event; 66 if (e.target) targ = e.target; 67 else if (e.srcElement) targ = e.srcElement; 68 if (targ.nodeType === 3) // defeat Safari bug 69 targ = targ.parentNode; 70 71 if(targ.value) window.location.href = targ.value; 72 } 73 74 function islist(list){ 75 var n = list.nodeName.toLowerCase(); 76 return (n === 'ul' || n === 'ol'); 77 } 78 79 function id(nextId){ 80 for(var j=1; document.getElementById('selectnav'+j);j++); 81 return (nextId) ? 'selectnav'+j : 'selectnav'+(j-1); 82 } 83 84 function parselist(list){ 85 86 // go one level down 87 level++; 88 89 var length = list.children.length, 90 html = '', 91 prefix = '', 92 k = level-1 93 ; 94 95 // return immediately if has no children 96 if (!length) { 97 return; 98 } 99 100 if(k) { 101 while(k--){ 102 prefix += indent; 103 } 104 prefix += " "; 105 } 106 107 for(var i=0; i < length; i++){ 108 109 var link = list.children[i].children[0]; 110 if(typeof(link) !== 'undefined'){ 111 var text = link.innerText || link.textContent; 112 var isselected = ''; 113 114 if(activeclass){ 115 isselected = link.className.search(activeclass) !== -1 || link.parentNode.className.search(activeclass) !== -1 ? selected : ''; 116 } 117 118 if(autoselect && !isselected){ 119 isselected = link.href === document.URL ? selected : ''; 120 } 121 122 html += '<option value="' + link.href + '" ' + isselected + '>' + prefix + text +'</option>'; 123 124 if(nested){ 125 var subElement = list.children[i].children[1]; 126 if( subElement && islist(subElement) ){ 127 html += parselist(subElement); 128 } 129 } 130 } 131 } 132 133 // adds label 134 if(level === 1 && label) { 135 html = '<option value="">' + label + '</option>' + html; 136 } 137 138 // add <select> tag to the top level of the list 139 if(level === 1) { 140 html = '<select class="selectnav" id="'+id(true)+'">' + html + '</select>'; 141 } 142 143 // go 1 level up 144 level--; 145 146 return html; 147 } 148 149 }; 150 151 return function (element,options) { 152 selectnav(element,options); 153 }; 154 155 156})(); 157// custom script for iconic one 158selectnav('menu-top', { 159 label: 'Menu', nested: true, indent: '-' 160 } 161);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.