1/** 2 * ZURB FOUNDATION 5 MULTISELECT PLUGIN 3 * 4 * @author Andrea Mariani 5 * @mail [email protected] 6 * @twitter @andreamariani2k 7 * @web http://www.andreamariani.net 8 * 9 * ** THIS PLUGIN IS IN DEVELOPMENT ** * 10 */ 11 12(function($) { 13 14 15 16 //conflitto da risolvere: 17 //le prime due funzioni si danno noia 18 19 //todo: click out for close the dropdown 20 $(document).mouseup(function (e){ 21 var container = $(".zselect ul"); 22 if (!container.is(e.target) && container.has(e.target).length === 0) { 23 container.hide(); 24 //console.log(e.target); 25 } 26 else{ 27 //console.log(e.target); 28 //console.log('else'); 29 } 30 31 }); 32 33 34 //open dropdown onclick 35 $(document).on('click', '.zselect', function(e){ 36 var click = $(e.target).prop("tagName"); 37 //console.log(click); 38 if(click!=='LI' && click!=='INPUT'){ 39 $("li.zmsfilter input").val('').keyup(); //clean filter 40 $("ul",this).toggle(); 41 } 42 43 }); 44 45 46 //escape key for close all zselect 47 $(window).on('keydown', function(e){ 48 e = e || window.event; 49 if (e.keyCode === 27) { 50 $("li.zmsfilter input").val('').keyup(); //clean filter 51 $(".zselect ul").hide(); 52 } 53 }); 54 55 56 //click on label toggle input 57 $(document).on('click', 'li', function(e){ 58 if($(e.target).prop("tagName") !== "INPUT"){ 59 $("input:checkbox[disabled!='disabled']",this).prop('checked', function( i, val ) { return !val; }).trigger('change'); 60 } 61 62 }); 63 64 //select all and deselect all 65 $(document).on('click','.selectall,.deselectall', function(){ 66 var state = ($(this).hasClass('selectall'))?true:false; 67 $(this).parent().find("input:checkbox[disabled!='disabled']").prop('checked', state).change(); 68 }); 69 70 //optgroup 71 $(document).on('click','.optgroup', function(){ 72 $(this).parent().find(".optgroup_"+$(this).attr('data-optgroup')+" li input:checkbox[disabled!='disabled']").prop('checked', function( i, val ) { return !val; }).change(); 73 }); 74 75 76 //when resize window + init 77 function onResize(reflow){ 78 $.each( $(".zselect"), function(k,v){ 79 //if( $(v).find("ul").attr('style') !== undefined && reflow !== true ) return false; //break if already set 80 81 var w = $(v).outerWidth(); 82 83 $(v).find("ul").attr('style', 'width:'+w+'px!important;' ); 84 85 86 //var size = Math.max(Math.min(w / (1), parseFloat(20)), parseFloat(11)); 87 //console.log(size); 88 //$(v).find('ul li').css('font-size', size); 89 90 var w_li = $(v).find('ul li:eq(0)').width(); 91 //console.log(w_li); 92 93 }); 94 95 } 96 97 $( window ).resize(function() { 98 onResize(); 99 }); 100 101 102 function refreshPlaceholder(rel, placeholder, selectedText){ 103 var counter = selectedText || ['Selected ', 'of ']; 104 var checked=$("div#"+rel+" ul li input:checked").length; 105 var tot=$("div#"+rel+" ul li input:checkbox").length; 106 107 if(checked>0) { 108 $(".zselect#"+rel+" span.zmshead").text(counter[0]+" "+checked+" "+counter[1]+" "+tot); 109 } 110 else { 111 $(".zselect#"+rel+" span.zmshead").html( (placeholder===undefined) ? ' ' : placeholder ); 112 } 113 } 114 115 116 117 118var methods = { 119 init : function(options) { 120 121 var id,checked,disabled="",disabledClass=""; 122 var optgroup=[]; 123 var optgroup_size,optgroup_id = 0; 124 var optgroup_name = false;
125 126 $.each( $(this),function(k,v){ 127 128 id=Math.random().toString(36).substr(2, 9); 129 $(v).hide().attr('rel',id); 130 $(v).parent().append("<div id='"+id+"' class='zselect'><span class='zmshead'></span><ul></ul></div>"); 131 132 if(options.selectAll!==false){ 133 var sAllText="Select All"; 134 var desAllText="Deselect All"; 135 if(options.selectAllText!==undefined){ 136 sAllText=options.selectAllText[0]; 137 desAllText=options.selectAllText[1]; 138 } 139 140 $('#'+id+' ul').append("<li class='selectall'>"+sAllText+"</li>"); 141 $('#'+id+' ul').append("<li class='deselectall'>"+desAllText+"</li>"); 142 } 143 144 $.each(v, function(j,z){ 145 146 var appendTo; 147 if( $(z).parent().attr("label") !== undefined && optgroup.indexOf($(z).parent().attr("label"))===-1 ){ 148 optgroup_size = $(z).parent().find('option').size(); 149 optgroup_name = $(z).parent().attr("label"); 150 151 $('#'+id+' ul').append("<li class='optgroup' data-optgroup='"+optgroup_id+"'>"+$(z).parent().attr("label")+"</li>"); 152 $('#'+id+' ul').append($("<div>").addClass('optgroup_'+optgroup_id)); 153 optgroup.push($(z).parent().attr("label")); 154 } 155 //console.log( $(z).attr('value') + " " + $(z).text() ); 156 //console.log( $(z).attr('value') + " " + $(z).text() ); 157 //console.log(id); 158 //console.log( '#'+id+' ul' ); 159 checked = ( $(z).is('[data-selected]') ) ? "checked='checked'" : ""; 160 dataZ = ( $(z).data("z") !== undefined ) ? 'data-z="' + $(z).data("z") + '"' : ""; 161 162 if( $(z).is('[data-disabled]') ){ 163 disabled = "disabled='disabled'"; 164 disabledClass = "class='disabled'"; 165 } 166 else{ 167 disabled = disabledClass = ""; 168 } 169 170 if( optgroup_name === false ) appendTo = '#'+id+' ul'; 171 else appendTo = '#'+id+' ul div.optgroup_'+optgroup_id; 172 173 174 $(appendTo).append("<li "+disabledClass+"><input value='"+$(z).val()+"' type='checkbox' "+checked+" "+disabled+" "+dataZ+" /> "+$(z).text()+"</li>"); 175 176 if(optgroup_size === j+1){ 177 optgroup_size = 0; 178 optgroup_id ++; 179 optgroup_name = false; 180 } 181 182 183 }); 184 $('#'+id+' span.zmshead').html( (options.placeholder===undefined) ? ' ' : options.placeholder ); 185 }); 186 187 if(options.filter === true){ 188 //defaults 189 if (options.filterResult === undefined) options.filterResult = true; 190 if (options.filterResultText === undefined) options.filterResultText = 'showed'; 191 var fplaholder = (options.filterPlaceholder !== undefined) ? options.filterPlaceholder : "Filter..."; 192 193 194 var rel = this.attr('rel'); 195 $("div#"+rel+" ul").prepend('<li class="zmsfilter"><input type="text" placeholder="'+fplaholder+'" /></li>'); 196 197 198 199 if(options.filterResult === true) 200 $("div#"+rel+" ul").append('<li class="filterResult"></li>'); 201 202 $("div#"+rel+" ul li.zmsfilter input").keyup(function(){ 203 var value=$(this).val().toLowerCase(); 204 var show=0,tot=$("div#"+rel+" ul li input:checkbox").length; 205 $("div#"+rel+" ul li input:checkbox").filter( function(i,v) { 206 //console.log($(v).val()); 207 //console.log($(v).parent().text()); 208 if( $(v).val().toLowerCase().indexOf(value) === -1 && $(v).parent().text().toLowerCase().indexOf(value) === -1 ){//and text() check... 209 $(v).parent().hide(); 210 211 } 212 else { 213 $(v).parent().show(); 214 show++; 215 } 216 217 }); 218 219 if(options.filterResult === true) 220 $("div#"+rel+" ul li.filterResult").text(options.filterResultText + ' '+show+'/'+tot); 221 222 }); 223 }//end filter 224 225 226 if(options.live !== undefined){ 227 var rel = this.attr('rel'); 228 $(".zselect#"+rel).on('change','input:checkbox',function(e){ 229 $(options.live).val( methods.getValue($("select[rel='"+rel+"']")) ); 230 }); 231 }//end live 232 233 234 if(options.get !== undefined){ 235 var query = window.location.search.substring(1); 236 var vars = query.split("&"); 237 var need = false;
238 for (var i=0;i<vars.length;i++) { 239 var pair = vars[i].split("="); 240 if (pair[0] == options.get) { 241 242 need = pair[1].replace(',','%2C').split('%2C'); 243 244 } 245 } 246 247 if(need){ 248 var rel = this.attr('rel'); 249 for(var i=0; i<need.length; i++){ 250 //console.log(need[i]); 251 $(".zselect#"+rel+" ul li input:checkbox[value='"+need[i]+"']").trigger('click'); 252 253 } 254 refreshPlaceholder(rel,options.placeholder,options.selectedText); 255 } 256 } 257 258 //placeholder dopo click 259 $(".zselect#"+rel).on('change','input:checkbox',function(){ 260 refreshPlaceholder(rel,options.placeholder,options.selectedText); 261 }); 262 263 onResize(); 264 265 266 }, 267 268 269 270 271 getValue : function(selector) { 272 if(selector===undefined) selector = this; 273 var value = new Array(); 274 var rel = $(selector).attr('rel'); 275 $.each( $("div#"+rel+" ul li input"), function(k,v){ 276 if( $(v).val() !== undefined ){ 277 if( $(v).prop('checked') ) 278 value.push($(v).val()); 279 } 280 }); 281 return value; 282 }, 283 284 285 open : function( ) { 286 $("div#"+$(this).attr('rel')+" ul").show(); 287 }, 288 close : function( ) { 289 $("div#"+$(this).attr('rel')+" ul").hide(); 290 }, 291 292 293 disable : function (val,state){ //console.log(state); 294 if(val!==undefined){ 295 if(state) $("div#"+$(this).attr('rel')+" ul li input:checkbox[value='"+val+"']").attr('disabled','disabled'); 296 else $("div#"+$(this).attr('rel')+" ul li input:checkbox[value='"+val+"']").removeAttr('disabled'); 297 } 298 else{ 299 if(state) $("div#"+$(this).attr('rel')+" ul li input:checkbox").attr('disabled','disabled'); 300 else $("div#"+$(this).attr('rel')+" ul li input:checkbox").removeAttr('disabled'); 301 } 302 }, 303 set : function (val,checked){ 304 $("div#"+$(this).attr('rel')+" ul li input:checkbox[value='"+val+"']").prop('checked', checked).change(); 305 }, 306 uncheckall_inpage : function( ) { 307 $(".zselect ul li input:checkbox").prop('checked', false).change(); 308 }, 309 checkall_inpage : function( ) { 310 $(".zselect ul li input:checkbox").prop('checked', true).change(); 311 }, 312 checkall : function( ) { 313 $("div#"+$(this).attr('rel')+" ul li input:checkbox").prop('checked', true).change(); 314 }, 315 uncheckall : function( ) { 316 $("div#"+$(this).attr('rel')+" ul li input:checkbox").prop('checked', false).change(); 317 }, 318 destroy : function (val){ 319 $("div#"+$(this).attr('rel')+" ul li input:checkbox[value='"+val+"']").parent().remove(); 320 }, 321 reflow : function(){ 322 onResize(true); 323 }, 324 325 add : function(option, pos){ 326 var position = pos || 'append'; 327 var checked='', disabled='', disabledClass=''; 328 329 if(option.checked) checked=' checked="checked" '; 330 if(option.disabled) {disabled=' disabled="disabled" '; disabledClass=' class="disabled" ';} 331 332 if(position === 'append') $("div#"+$(this).attr('rel')+" ul").append("<li "+disabledClass+"><input value='"+option.value+"' type='checkbox' "+checked+" "+disabled+" /> "+option.text+"</li>"); 333 else { 334 if(position === 'prepend'){ 335 $("<li "+disabledClass+"><input value='"+option.value+"' type='checkbox' "+checked+" "+disabled+" /> "+option.text+"</li>").insertAfter($("div#"+$(this).attr('rel')+" ul li.deselectall")); 336 } 337 else{ 338 $("<li "+disabledClass+"><input value='"+option.value+"' type='checkbox' "+checked+" "+disabled+" /> "+option.text+"</li>").insertAfter($("div#"+$(this).attr('rel')+" ul li input[value='"+position.substring(1)+"']").closest('li')); 339 } 340 } 341 342 343 344 345 } 346 347 //,update : function( content ) { } 348 }; 349 350 351 352 353 354 355$.fn.zmultiselect = function(methodOrOptions) { 356 if ( methods[methodOrOptions] ) { 357 return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 )); 358 } else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) { 359 return methods.init.apply( this, arguments ); 360 } else { 361 $.error( 'Method ' + methodOrOptions + ' does not exist on zmultiselect' ); 362 } 363 }; 364 365 366 367})( jQuery );
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.