1jQuery.fn.extend({ 2 selectbox: function(options) { 3 return this.each(function() { 4 new jQuery.SelectBox(this, options); 5 }); 6 } 7}); 8 9 10/* pawel maziarz: work around for ie logging */ 11if (!window.console) { 12 var console = { 13 log: function(msg) { 14 } 15 } 16} 17/* */ 18 19jQuery.SelectBox = function(selectobj, options) { 20 21 var opt = options || {}; 22 opt.inputClass = opt.inputClass || "selectbox"; 23 opt.containerClass = opt.containerClass || "selectbox-wrapper"; 24 opt.hoverClass = opt.hoverClass || "current"; 25 opt.currentClass = opt.selectedClass || "selected" 26 opt.debug = opt.debug || false; 27 28 var elm_id = selectobj.id; 29 var active = 0; 30 var inFocus = false; 31 var hasfocus = 0; 32 //jquery object for select element 33 var $select = $(selectobj); 34 // jquery container object 35 var $container = setupContainer(opt); 36 //jquery input object 37 var $input = setupInput(opt); 38 // hide select and append newly created elements 39 $select.hide().before($input).before($container); 40 41 42 init(); 43 44 $input 45 .click(function(){ 46 if (!inFocus) { 47 $container.toggle(); 48 } 49 }) 50 .focus(function(){ 51 if ($container.not(':visible')) { 52 inFocus = true; 53 $container.show(); 54 } 55 }) 56 .keydown(function(event) { 57 switch(event.keyCode) { 58 case 38: // up 59 event.preventDefault(); 60 moveSelect(-1); 61 break; 62 case 40: // down 63 event.preventDefault(); 64 moveSelect(1); 65 break; 66 //case 9: // tab 67 case 13: // return 68 event.preventDefault(); // seems not working in mac ! 69 $('li.'+opt.hoverClass).trigger('click'); 70 break; 71 case 27: //escape 72 hideMe(); 73 break; 74 } 75 }) 76 .blur(function() { 77 if ($container.is(':visible') && hasfocus > 0 ) { 78 if(opt.debug) console.log('container visible and has focus') 79 } else { 80 // Workaround for ie scroll - thanks to Bernd Matzner
81 if($.browser.msie || $.browser.safari){ // check for safari too - workaround for webkit 82 if(document.activeElement.getAttribute('id').indexOf('_container')==-1){ 83 hideMe(); 84 } else { 85 $input.focus(); 86 } 87 } else { 88 hideMe(); 89 } 90 } 91 }); 92 93 94 function hideMe() { 95 hasfocus = 0; 96 $container.hide(); 97 } 98 99 function init() { 100 $container.append(getSelectOptions($input.attr('id'))).hide(); 101 var width = $input.css('width'); 102 103 } 104 105 function setupContainer(options) { 106 var container = document.createElement("div"); 107 $container = $(container); 108 $container.attr('id', elm_id+'_container'); 109 $container.addClass(options.containerClass); 110 111 return $container; 112 } 113 114 function setupInput(options) { 115 var input = document.createElement("input"); 116 var $input = $(input); 117 $input.attr("id", elm_id+"_input"); 118 $input.attr("type", "text"); 119 $input.addClass(options.inputClass); 120 $input.attr("autocomplete", "off"); 121 $input.attr("readonly", "readonly"); 122 $input.attr("tabIndex", $select.attr("tabindex")); // "I" capital is important for ie 123 124 return $input; 125 } 126 127 function moveSelect(step) { 128 var lis = $("li", $container); 129 if (!lis || lis.length == 0) return false; 130 active += step; 131 //loop through list 132 if (active < 0) { 133 active = lis.size(); 134 } else if (active > lis.size()) { 135 active = 0; 136 } 137 scroll(lis, active); 138 lis.removeClass(opt.hoverClass); 139 140 $(lis[active]).addClass(opt.hoverClass); 141 } 142 143 function scroll(list, active) { 144 var el = $(list[active]).get(0); 145 var list = $container.get(0); 146 147 if (el.offsetTop + el.offsetHeight > list.scrollTop + list.clientHeight) { 148 list.scrollTop = el.offsetTop + el.offsetHeight - list.clientHeight; 149 } else if(el.offsetTop < list.scrollTop) { 150 list.scrollTop = el.offsetTop; 151 } 152 } 153 154 function setCurrent() { 155 var li = $("li."+opt.currentClass, $container).get(0); 156 var ar = (''+li.id).split('_'); 157 var el = ar[ar.length-1]; 158 $select.val(el); 159 $input.val($(li).html()); 160 return true; 161 } 162 163 // select value 164 function getCurrentSelected() { 165 return $select.val(); 166 } 167 168 // input value 169 function getCurrentValue() { 170 return $input.val(); 171 } 172 173 function getSelectOptions(parentid) { 174 var select_options = new Array(); 175 var ul = document.createElement('ul'); 176 $select.children('option').each(function() { 177 var li = document.createElement('li'); 178 li.setAttribute('id', parentid + '_' + $(this).val()); 179 li.innerHTML = $(this).html(); 180 if ($(this).is(':selected')) { 181 $input.val($(this).html()); 182 $(li).addClass(opt.currentClass); 183 } 184 ul.appendChild(li); 185 $(li) 186 .mouseover(function(event) { 187 hasfocus = 1; 188 if (opt.debug) console.log('over on : '+this.id); 189 jQuery(event.target, $container).addClass(opt.hoverClass); 190 }) 191 .mouseout(function(event) { 192 hasfocus = -1; 193 if (opt.debug) console.log('out on : '+this.id); 194 jQuery(event.target, $container).removeClass(opt.hoverClass); 195 }) 196 .click(function(event) { 197 var fl = $('li.'+opt.hoverClass, $container).get(0); 198 if (opt.debug) console.log('click on :'+this.id); 199 $('#' + elm_id + '_container' + ' li.'+opt.currentClass).removeClass(opt.currentClass); 200 $(this).addClass(opt.currentClass); 201 setCurrent(); 202 //$select.change(); 203 $select.get(0).blur(); 204 hideMe(); 205 }); 206 }); 207 return ul; 208 } 209 210 211 212};
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.