From 60b84d42833ebc4b69f3283b9d15931357f04a86 Mon Sep 17 00:00:00 2001 From: Tarjei Huse Date: Wed, 14 Mar 2012 11:04:15 +0100 Subject: [PATCH] Add two changes: 1. Add a new reset() method so the files uploaded can be removed from the list when the upload is done. 2. Add a new option fireAtOnce (default false) which will submitt the form at once when a file is dropped into the uploadarea. --- Source/Form.Upload.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Source/Form.Upload.js b/Source/Form.Upload.js index 90ba3b0..3700b18 100644 --- a/Source/Form.Upload.js +++ b/Source/Form.Upload.js @@ -19,6 +19,7 @@ Form.Upload = new Class({ options: { dropMsg: 'Please drop your files here', + fireAtOnce: false, onComplete: function(){ // reload window.location.href = window.location.href; @@ -56,7 +57,11 @@ Form.Upload = new Class({ inputFiles = new Form.MultipleFileInput(input, list, drop, { onDragenter: drop.addClass.pass('hover', drop), onDragleave: drop.removeClass.pass('hover', drop), - onDrop: drop.removeClass.pass('hover', drop) + onDrop: function() { + drop.removeClass.pass('hover', drop); + if (self.options.fireAtOnce) + form.fireEvent("submit"); + } }), uploadReq = new Request.File({ @@ -76,17 +81,26 @@ Form.Upload = new Class({ inputname = input.get('name'); form.addEvent('submit', function(event){ - event.preventDefault(); + if (event) { + event.preventDefault(); + } inputFiles.getFiles().each(function(file){ uploadReq.append(inputname , file); }); uploadReq.send(); }); - + self.reset = function() { + var files = inputFiles.getFiles(); + for (var i = 0; i < files.length; i++) { + inputFiles.remove(files[i]); + } + }; }, legacyUpload: function(input){ + var rows = []; + var row = input.getParent('.formRow'); rowClone = row.clone(true, true), add = function(event){ @@ -106,6 +120,7 @@ Form.Upload = new Class({ if (label) label.set('for', inputID); newRow.inject(row, 'after'); + rows.push(newRow); }; new Element('a.addInputRow', { @@ -113,6 +128,13 @@ Form.Upload = new Class({ events: {click: add} }).inject(input, 'after'); + this.reset = function() { + for (var i = 0; i < rows.length; i++) { + rows[i].destroy(); + } + rows = []; + }; + }, isModern: function(){