/* * OptionList 1.1 - jQuery Plugin * http://www.smartango.com/blog/jquery-optionlist-plugin * Copyright (c) 2009 Daniele Cruciani * Dual licensed under the MIT and GPL licenses */ /* option with default is: method: 'install', mode:'multiple', // or 'single' express: 1, // exclude present target: '#optionlist', input:'#iptvalues', items:{}, optional: $("").attr("type","text"), autocompletedata: '', alertfun: function(msg){alert(msg);}, preset:[] */ ;(function($){ $.optionlist={msg : { nocomma:"Non inserire virgole", nonumber:"Il valore non puo' essere un numero", express:"Elemento gia' presente nella lista" }}; optionlist = function(o) { this.avail = o.avail; this.o = o; this.start = function(){ if (typeof o.preset == 'object' && o.preset instanceof Array) { for(var i=0;i").addClass("optionlist_item") .append( $("").html(this.o.items[id]) //this.o.items[id] ) .append( $("").attr('title','rimuovi '+ this.o.items[id]) .addClass("optionlist_rmitm").html("x") .attr("id","optionlist_rmitm_"+id) .click(function() { var myid = this.id.replace("optionlist_rmitm_",""); ol.remove(myid); return false; }) ) .hover(function() { // in var pos = $(this).position(); var width = $(this).children("span").width(); $(this).children(".optionlist_rmitm").css('top',pos.top) .css('left',pos.left+width-5).show(); }, function() { // out $(this).children(".optionlist_rmitm").hide(); }); $(this.o.target).append(item); } var clear = $("
").css("clear","both"); $(this.o.target).append(clear); } else { // mode == single $(this.o.input).val(idlist); if (this.o.targetmode == 'val') { $(this.o.target).val(this.o.items[idlist]); } else { $(this.o.target).html(this.o.items[idlist]); } } } else { $(this.o.input).val(""); $(this.o.target).html(''); } }; this.add=function(id){ if (this.o.mode != 'single') { var funcz = $(this.o.target).data("idlist"); var funcarr = []; if(funcz)funcarr = funcz.split(","); funcarr.push(id); funcz = funcarr.join(","); $(this.o.target).data("idlist",funcz); $(this.o.input).val(funcz); } else { // mode single //var funcz = $(this.o.target).data("idlist"); $(this.o.target).data("idlist",id.toString()); $(this.o.input).val(id); } this.update() this.update_avail(); }; this.remove=function(id) { var funcz = $(this.o.target).data("idlist"); var nfuncz = funcz.replace(id,"") .replace(",,",",") .replace(/^,/,"").replace(/,$/,""); $(this.o.target).data("idlist",nfuncz); $(this.o.input).val(nfuncz); this.update(); this.update_avail(); }; this.empty=function() { $(this.o.target).data("idlist",""); $(this.o.input).val(''); } this.update_avail=function() { var idlist = $(this.o.target).data("idlist"); idlist = ","+idlist+","; var ol = this; this.avail.html(""); $.each(this.o.items,function(i,v) { var match = ","+i+","; if (!idlist.match(match)) { var ael = $("").html(v).click(function(){ ol.add(i); ol.o.avail.hide(); $("#optionlist_overlay").remove(); }).addClass("optionlist_additem"); ol.avail.append(ael); ol.avail.append($("
")); } }); if(this.o.optional) { var ol = this; ol.o.optional.val(''); opt = ol.o.optional; act = $("