/* ### jQuery Multiple File Upload Plugin ### By Diego A., http://www.fyneworks.com 02-April-2007: v1.0 First and final release. I don't see what else can be done to it... */ /* ### Default implementation ### The plugin will attach itself to file inputs with the class 'multi' when the page loads */ $(function(){ $.MultiFile(); }); // extend jQuery - $.MultiFile hook $.extend($, { MultiFile: function( limit /* Integer */ ){ return $("INPUT[@type='file']").MultiFile(limit); } }); // extend jQuery prototype $.extend($.fn, { // MultiFile function MultiFile: function( limit /* Integer */ ){ if(this._MultiFile){ return $(this); } this._MultiFile = true; // Bind to each element in current jQuery object return $(this).each(function(i){ // Remember our ancestors... var d = this; // debug??? d.debug = (d.className.indexOf('debug')>0); // debug??? d.max = (d.className.match(/\b((max|limit)\-[0-9]+)\b/i)); if(d.max){ d.max = new Number((d.max+'').match(/[0-9]+/)[0]); } // Default properties $.extend(d,{ n: 0, // How many elements are currently selected? I: 0, // How many elements altogether? k: d.name || d.id || 'MF_', // Instance Key? max: d.max || limit || -1, // Is there a limit? f: function(z){ return d.k+'_F_'+String(i)+'_'+String(z); } }); // Wrapper ID? d.w = d.k+'_MF_'+i; // Create wrapper to hold our file list var x = $(d); x.wrap('
'); // Bind a new element d.add = function( e ){ // Keep track of how many elements have been displayed d.n++; d.I++; // Add reference to this object e.selector = d; e.id = d.f(d.I); e.i = d.I; // If we've reached maximum number, disable input e if( d.max != -1 && d.n > d.max ){ e.disabled = true; }; // Remember most recent e d.current = e; /// now let's use jQuery e = $(e); // Triggered when a file is selected e.change(function(){ // Hide this element: display:none is evil! //this.style.display = 'block'; if(!d.debug){ this.style.position = 'absolute'; this.style.left = '-1000px'; }; // Create a new file input element var f = $(''); // Add it to the form $(this).parent().prepend(f); // Bind functionality d.add( f.get(0) ); // Update list d.list( this ); }); }; // Bind a new element // Add a new file to the list d.list = function( y ){ var t = $('#'+d.w), r = $(''), v = $(y).attr('value')+'', a = $(''+v.match(/[^\/\\]+$/gi)[0]+''), b = $('remove'); t.append(r); r.append('[',b,'] ',a);//.prepend(y.i+': '); b.click(function(){ d.n--; d.current.disabled = false; $('#'+d.f(y.i)).remove(); $(this).parent().remove(); return false; }); }; // Bind first file element if(!d.ft){ d.add(d); d.ft = true; } }); // each element } // MultiFile function }); // extend jQuery prototype