Author: nbelaevski
Date: 2008-02-14 21:56:13 -0500 (Thu, 14 Feb 2008)
New Revision: 6102
Modified:
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
Log:
in-place controls processing added
Modified:
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java 2008-02-15
00:59:43 UTC (rev 6101)
+++
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java 2008-02-15
02:56:13 UTC (rev 6102)
@@ -100,7 +100,7 @@
PrintWriter writer = httpResponse.getWriter();
writer
- .write("<html
id=\"_richfaces_file_upload_stopped\"><body><script>alert('stopped');</script></body></html>");
+ .write("<html
id=\"_richfaces_file_upload_stopped\"></html>");
writer.close();
}
} finally {
Modified:
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-02-15
00:59:43 UTC (rev 6101)
+++
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-02-15
02:56:13 UTC (rev 6102)
@@ -1,64 +1,30 @@
-LoadWatcher = Class.create();
-Object.extend(LoadWatcher.prototype, {
- initialize: function(iframe, success, failure) {
- this.iframe = iframe;
- this.success = success;
- this.failure = failure;
-
- this.loadObserver = function() {
- if (!this.stopped) {
- this.stop();
- this.success();
- }
- }.bind(this);
+FileUploadEntry = {};
+FileUploadEntry = Class.create();
- Event.observe(this.iframe, 'load', this.loadObserver);
-
- this.interval = setInterval(function() {
- if (!this.stopped) {
- var loaded = false;
- var error = null;
+FileUploadEntry.clearControlTemplate =
+ new E('a',
+ {
+ 'style':'text-decoration: none; color:black',
+ 'onclick': function (context) { return 'var entry =
FileUploadEntry.getComponent(this); entry.uploadObject.clear(entry); return false;';},
+ 'className':'upload_anc',
+ 'href':'#'
+ },
+ [
+ new T('Clear')
+ ]);
- try {
- if (this.iframe.contentWindow && this.iframe.contentWindow.document) {
- loaded = /complete/.test(this.iframe.contentWindow.document.readyState);
- }
- } catch (e) {
- error = e;
- }
-
- if (error) {
- this.stop();
- this.failure(error);
- } else if (loaded) {
- this.stop();
- this.success();
- }
- }
- }.bind(this), 50);
- },
-
- stop: function() {
- this.stopped = true;
-
- if (this.loadObserver) {
- Event.stopObserving(this.iframe, 'load', this.loadObserver);
- this.loadObserver = null;
- }
-
- if (this.interval) {
- clearInterval(this.interval);
- this.interval = null;
- }
-
- this.iframe = null;
- }
-});
+FileUploadEntry.stopControlTemplate =
+ new E('a',
+ {
+ 'style':'text-decoration: none; color:black',
+ 'onclick': function (context) { return
'FileUploadEntry.getComponent(this).uploadObject.stop(); return false;';},
+ 'className':'upload_anc',
+ 'href':'#'
+ },
+ [
+ new T('Stop')
+ ]);
-
-FileUploadEntry = {};
-FileUploadEntry = Class.create();
-
FileUploadEntry.template =
[
new E('table',
@@ -74,20 +40,18 @@
[
new ET(function (context) { return
Richfaces.evalMacro("fileName", context)})
]),
- new E('div',{'id':'progress'}),
- new
E('div',{'className':'upload_name_padding'},
- [
- new ET(function (context) { return
Richfaces.evalMacro("label", context)})])
- ]),
+ new E('div',{'id':'progress'}),
+ new E('div',{'className':'upload_name_padding'},
+ [
+ new ET(function (context) { return Richfaces.evalMacro("label",
context)})
+ ])
+ ]),
new E('td',{'style':'vertical-align: center;',
'className':'upload_table_td'},
[
- new E('div',{'className':'upload_font
upload_del'},
- [
- new E('a',{'style':'text-decoration: none;
color:black', 'onclick':function (context) { return 'return
$(\''+Richfaces.evalMacro("clientId",
context)+'\').component.confirm(\''+Richfaces.evalMacro("clientId",
context)+':cconfirm'+Richfaces.evalMacro("n",
context)+'\',\''+Richfaces.evalMacro("clientId",
context)+':clear'+Richfaces.evalMacro("n",
context)+'\');';}, 'id':function (context) { return
Richfaces.evalMacro("clientId",
context)+':clear'+Richfaces.evalMacro("n", context);},
'className':'upload_anc', 'href':'#'},
- [
- new T('Clear')
- ])
- ])
+ new E('div',{'className':'upload_font
upload_del'},
+ [
+ FileUploadEntry.clearControlTemplate
+ ])
]),
new E('td',{'className':'upload_table_td'},
[
@@ -108,25 +72,45 @@
FileUploadEntry.UPLOAD_TRANSFER_ERROR = "transfer_error";
FileUploadEntry.UPLOAD_SERVER_ERROR = "server_error";
+FileUploadEntry.LABELS = {};
+FileUploadEntry.LABELS[FileUploadEntry.INITIALIZED] = '';
+FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_IN_PROGRESS] = 'uploading';
+FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_CANCELED] = 'canceled';
+FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_SUCCESS] = 'done';
+FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_TRANSFER_ERROR] =
'transfer_error';
+FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_SERVER_ERROR] = 'server_error';
+FileUploadEntry.getComponent = function(elt) {
+ while (elt) {
+ var component = elt.component;
+ if (component) {
+ return component
+ } else {
+ elt = elt.parentNode;
+ }
+ }
+};
+
Object.extend(FileUploadEntry.prototype, {
element: null,
uploadObject: null,
- state: null,
+ state: FileUploadEntry.INITIALIZED,
initialize: function(element, uploadObject) {
this.element = element;
this.uploadObject = uploadObject;
- this.setState(FileUploadEntry.INITIALIZED);
-
+
var content = FileUploadEntry.template.invoke('getContent', {fileName:
$F(this.element)}).join('');
Element.insert(this.uploadObject.items, content);
this.entryElement =
this.uploadObject.items.childNodes[this.uploadObject.items.childNodes.length - 1];
+ this.entryElement.component = this;
+ this.statusLabel = this.entryElement.rows[0].cells[0].lastChild;
+ this.controlArea = this.entryElement.rows[0].cells[1].firstChild;
},
upload: function() {
@@ -136,12 +120,9 @@
stop: function() {
this.uploadObject.stopScript(this.uid);
- this.setState(FileUploadEntry.UPLOAD_CANCELED);
},
clear: function() {
- //todo review
- //this.uploadObject.remove(this);
this.element.parentNode.removeChild(this.element);
this.entryElement.parentNode.removeChild(this.entryElement);
},
@@ -149,12 +130,93 @@
setState: function(newState) {
var oldState = this.state;
this.state = newState;
+
+ Element.clearChildren(this.statusLabel);
+ Element.clearChildren(this.controlArea);
+
+ Element.insert(this.statusLabel, FileUploadEntry.LABELS[newState]);
+
+ if (newState == FileUploadEntry.UPLOAD_IN_PROGRESS) {
+ Element.insert(this.controlArea, FileUploadEntry.stopControlTemplate.getContent());
+ } else {
+ Element.insert(this.controlArea, FileUploadEntry.clearControlTemplate.getContent());
+ }
+
this.uploadObject.notifyStateChange(this, oldState);
}
});
+LoadWatcher = Class.create();
+Object.extend(LoadWatcher.prototype, {
+ initialize: function(iframe, callback) {
+ this.iframe = iframe;
+ this.callback = callback;
+
+ this.loadObserver = function() {
+ if (!this.stopped) {
+ this.stop();
+ this.onload();
+ }
+ }.bind(this);
+ Event.observe(this.iframe, 'load', this.loadObserver);
+
+ this.interval = setInterval(function() {
+ if (!this.stopped) {
+ var loaded = false;
+ var error = null;
+
+ try {
+ if (this.iframe.contentWindow && this.iframe.contentWindow.document) {
+ loaded = /complete/.test(this.iframe.contentWindow.document.readyState);
+ }
+ } catch (e) {
+ error = e;
+ }
+
+ if (error) {
+ this.stop();
+ this.onload();
+ } else if (loaded) {
+ this.stop();
+ this.onerror();
+ }
+ }
+ }.bind(this), 200);
+ },
+
+ onerror: function() {
+ this.callback(FileUploadEntry.UPLOAD_TRANSFER_ERROR);
+ },
+
+ onload: function() {
+ var iframeDocument = this.iframe.contentWindow.document;
+ var elt = iframeDocument.getElementById('_richfaces_file_upload_stopped');
+ if (elt) {
+ this.callback(FileUploadEntry.UPLOAD_CANCELED);
+ } else {
+ //TODO handle server internal errors, etc.
+ this.callback(FileUploadEntry.UPLOAD_SUCCESS);
+ }
+ },
+
+ stop: function() {
+ this.stopped = true;
+
+ if (this.loadObserver) {
+ Event.stopObserving(this.iframe, 'load', this.loadObserver);
+ this.loadObserver = null;
+ }
+
+ if (this.interval) {
+ clearInterval(this.interval);
+ this.interval = null;
+ }
+ }
+});
+
+
FileUpload1 = {};
FileUpload1 = Class.create();
@@ -245,16 +307,23 @@
this.activeEntry.stop();
}
},
-
- clear: function() {
- for (var i = 0; i < this.entries.length; i++) {
- var entry = this.entries[i];
- if (entry.state != FileUploadEntry.UPLOAD_IN_PROGRESS) {
- this.entries.splice(i--, 1);
- entry.clear();
+
+ clear: function(entry) {
+ if (entry) {
+ this.remove(entry);
+ entry.clear();
+ } else {
+ //this.entries.length should be evaluated every time!
+ for (var i = 0; i < this.entries.length; i++) {
+ var entry = this.entries[i];
+ if (entry.state != FileUploadEntry.UPLOAD_IN_PROGRESS) {
+ this.remove(entry);
+ i--;
+ entry.clear();
+ }
}
}
-
+
if (this.entries.length == 0) {
this.setupAutoUpload();
}
@@ -305,8 +374,6 @@
this.progressBar.setValue(0);
this.progressBar.enable();
- } else if (newState == FileUploadEntry.INITIALIZED) {
- //
}
},
@@ -347,16 +414,9 @@
if (!parentForm.onsubmit || parentForm.onsubmit()) {
var iframe = this.iframe;
- new LoadWatcher(iframe,
- function(){
- alert('ready');
- this.setState(FileUploadEntry.UPLOAD_SUCCESS);
- }.bind(entry),
-
- function(e) {
- alert('error! ' + e.message);
- }
- );
+ new LoadWatcher(iframe, function(newState) {
+ this.setState(newState);
+ }.bind(entry));
parentForm.submit();
}