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