- finishing off ui changes to file upload widget based on linton's updated mockups.

- fixing various upload related bugs (resize issue, not handling file exists exception, not attaching uploaded files to workflow, loosing some of the uploaded files, and probably some more).

- reusing upload_helper.js code from xforms.js.  added facility for having the javascript return page return an error and file type image to support xform - doesn't effect other usages (create form).

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4800 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ariel Backenroth
2007-01-12 01:03:35 +00:00
parent e2915b245a
commit 7e6ad08ad8
7 changed files with 264 additions and 183 deletions

View File

@@ -350,6 +350,12 @@ public class CreateWebContentWizard extends BaseContentWizard
{
srcPaths.add(AVMConstants.getCorrespondingPath(rendition.getPath(), sandboxName));
}
for (NodeRef uploadedFile : uploadedFiles)
{
final String uploadPath = AVMNodeConverter.ToAVMVersionPath(uploadedFile).getSecond();
srcPaths.add(AVMConstants.getCorrespondingPath(uploadPath, sandboxName));
}
srcPaths.add(AVMConstants.getCorrespondingPath(this.formInstanceData.getPath(), sandboxName));
}
else
@@ -358,6 +364,7 @@ public class CreateWebContentWizard extends BaseContentWizard
srcPaths.add(AVMConstants.getCorrespondingPath(this.createdPath, sandboxName));
}
LOGGER.debug("creating workflow package with " + srcPaths.size() + " files");
final NodeRef packageNodeRef =
AVMWorkflowUtil.createWorkflowPackage(srcPaths,
storeId,

View File

@@ -29,8 +29,10 @@ import javax.servlet.http.HttpServletRequest;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.avm.AVMNodeConverter;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.domain.PropertyValue;
import org.alfresco.service.cmr.avm.AVMNodeDescriptor;
import org.alfresco.service.cmr.avm.AVMService;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
@@ -46,6 +48,7 @@ import org.alfresco.web.bean.wcm.AVMNode;
import org.alfresco.web.forms.*;
import org.alfresco.web.ui.common.Utils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@@ -97,7 +100,7 @@ public class XFormsBean
private ChibaBean chibaBean;
private final SchemaFormBuilder schemaFormBuilder;
private final HashMap<String, NodeRef> uploads = new HashMap<String, NodeRef>();
private final Set<NodeRef> uploads = new HashSet<NodeRef>();
private final List<XMLEvent> eventLog = new LinkedList<XMLEvent>();
public XFormsSession(final Document formInstanceData,
@@ -113,9 +116,14 @@ public class XFormsBean
baseUrl);
}
public void addUpload(final NodeRef nr)
{
this.uploads.add(nr);
}
public NodeRef[] getUploadedFiles()
{
return (NodeRef[])this.uploads.values().toArray(new NodeRef[0]);
return (NodeRef[])this.uploads.toArray(new NodeRef[0]);
}
public void destroy()
@@ -172,14 +180,6 @@ public class XFormsBean
this.avmBrowseBean = avmBrowseBean;
}
/**
* @param nodeService the nodeService to set.
*/
public void setNodeService(final NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* @param avmService the avmService to set.
*/
@@ -509,15 +509,21 @@ public class XFormsBean
String uploadId = null;
String currentPath = null;
String filename = null;
String returnPage = null;
InputStream fileInputStream = null;
for (FileItem item : fileItems)
{
LOGGER.debug("item = " + item);
if (item.isFormField() && item.getFieldName().equals("id"))
if (item.isFormField() && item.getFieldName().equals("upload-id"))
{
uploadId = item.getString();
LOGGER.debug("uploadId is " + uploadId);
}
if (item.isFormField() && item.getFieldName().equals("return-page"))
{
returnPage = item.getString();
LOGGER.debug("returnPage is " + returnPage);
}
else if (item.isFormField() && item.getFieldName().equals("currentPath"))
{
final String previewStorePath =
@@ -529,39 +535,37 @@ public class XFormsBean
}
else
{
filename = item.getName();
int idx = filename.lastIndexOf('\\');
if (idx == -1)
{
idx = filename.lastIndexOf('/');
}
if (idx != -1)
{
filename = filename.substring(idx + File.separator.length());
}
filename = FilenameUtils.getName(item.getName());
fileInputStream = item.getInputStream();
LOGGER.debug("parsed file " + filename);
LOGGER.debug("uploading file " + filename);
}
}
LOGGER.debug("saving file " + filename + " to " + currentPath);
try
{
FileCopyUtils.copy(fileInputStream,
this.avmService.createFile(currentPath, filename));
final NodeRef uploadNodeRef =
AVMNodeConverter.ToNodeRef(-1, currentPath + "/" + filename);
final Map<QName, Serializable> props = new HashMap<QName, Serializable>(2, 1.0f);
props.put(ContentModel.PROP_TITLE, filename);
final Map<QName, PropertyValue> props = new HashMap<QName, PropertyValue>(2, 1.0f);
props.put(ContentModel.PROP_TITLE, new PropertyValue(DataTypeDefinition.TEXT, filename));
props.put(ContentModel.PROP_DESCRIPTION,
"Uploaded for form " + this.xformsSession.getForm().getName());
this.nodeService.addAspect(uploadNodeRef,
ContentModel.ASPECT_TITLED,
props);
new PropertyValue(DataTypeDefinition.TEXT,
"Uploaded for form " + this.xformsSession.getForm().getName()));
this.avmService.setNodeProperties(currentPath + "/" + filename, props);
this.avmService.addAspect(currentPath + "/" + filename, ContentModel.ASPECT_TITLED);
this.xformsSession.uploads.put(uploadId, uploadNodeRef);
this.xformsSession.addUpload(AVMNodeConverter.ToNodeRef(-1, currentPath + "/" + filename));
returnPage = returnPage.replace("${_FILE_TYPE_IMAGE}",
Utils.getFileTypeImage(facesContext, filename, true));
}
catch (Exception e)
{
LOGGER.debug(e.getMessage(), e);
returnPage = returnPage.replace("${_UPLOAD_ERROR}", e.getMessage());
}
LOGGER.debug("upload complete. sending response");
LOGGER.debug("upload complete. sending response: " + returnPage);
final Document result = XMLUtil.newDocument();
final Element htmlEl = result.createElement("html");
result.appendChild(htmlEl);
@@ -571,9 +575,7 @@ public class XFormsBean
final Element scriptEl = result.createElement("script");
bodyEl.appendChild(scriptEl);
scriptEl.setAttribute("type", "text/javascript");
final Node scriptText =
result.createTextNode("window.parent.FilePickerWidget." +
"_upload_completeHandler('" + uploadId + "');");
final Node scriptText = result.createTextNode(returnPage);
scriptEl.appendChild(scriptText);
final ResponseWriter out = facesContext.getResponseWriter();

View File

@@ -54,7 +54,8 @@ public class XFormsProcessor
"/scripts/ajax/dojo/" + (LOGGER.isDebugEnabled()
? "dojo.js.uncompressed.js"
: "dojo.js"),
"/scripts/ajax/xforms.js"
"/scripts/ajax/xforms.js",
"/scripts/upload_helper.js",
};

View File

@@ -3158,10 +3158,6 @@
<property-name>avmService</property-name>
<value>#{AVMService}</value>
</managed-property>
<managed-property>
<property-name>nodeService</property-name>
<value>#{NodeService}</value>
</managed-property>
<managed-property>
<property-name>avmBrowseBean</property-name>
<value>#{AVMBrowseBean}</value>

View File

@@ -85,7 +85,6 @@
{
padding: 2px;
border: 1px solid #003366;
font-weight: bold;
margin: 2px 5%;
text-align: center;
text-overflow: ellipsis;

View File

@@ -263,7 +263,8 @@ dojo.declare("alfresco.xforms.FilePicker",
dojo.html.prependClass(this.domNode, "xformsFilePicker");
attach_point.appendChild(this.domNode);
//XXXarielb support readonly and disabled
this.widget = new FilePickerWidget(this.domNode,
this.widget = new FilePickerWidget(this.id,
this.domNode,
this.getInitialValue(),
false,
this._filePicker_changeHandler,
@@ -2222,8 +2223,9 @@ if (!Array.prototype.indexOf)
}
}
function FilePickerWidget(node, value, readonly, change_callback, resize_callback)
function FilePickerWidget(uploadId, node, value, readonly, change_callback, resize_callback)
{
this.uploadId = uploadId;
this.node = node;
this.value = value == null || value.length == 0 ? null : value;
this.readonly = readonly || false;
@@ -2234,56 +2236,28 @@ function FilePickerWidget(node, value, readonly, change_callback, resize_callbac
FilePickerWidget._uploads = [];
FilePickerWidget._handleUpload = function(id, fileInput, webappRelativePath, widget)
{
id = id.substring(0, id.indexOf("-widget"));
var d = fileInput.ownerDocument;
var iframe = d.createElement("iframe");
iframe.style.display = "none";
iframe.name = id + "_upload_frame";
iframe.id = iframe.name;
document.body.appendChild(iframe);
// makes it possible to target the frame properly in ie.
window.frames[id + "_upload_frame"].name = iframe.name;
FilePickerWidget._uploads[id] =
{
widget:widget,
path: fileInput.value,
webappRelativePath: webappRelativePath,
fileName: fileInput.value.substring(fileInput.value.lastIndexOf("/") + 1)
webappRelativePath: webappRelativePath
};
var form = document.createElement("form");
form.style.display = "none";
d.body.appendChild(form);
form.id = id + "_upload_form";
form.name = form.id;
form.method = "post";
form.encoding = "multipart/form-data";
form.enctype = "multipart/form-data";
form.target = iframe.name;
form.action = alfresco_xforms_constants.WEBAPP_CONTEXT + "/ajax/invoke/XFormsBean.uploadFile";
form.appendChild(fileInput.cloneNode(true));
var rp = d.createElement("input");
rp.type = "hidden";
rp.name = "id";
rp.value = id;
form.appendChild(rp);
var rp = d.createElement("input");
rp.name = "currentPath";
rp.value = webappRelativePath;
rp.type = "hidden";
form.appendChild(rp);
form.submit();
handle_upload_helper(fileInput,
id,
FilePickerWidget._upload_completeHandler,
alfresco_xforms_constants.WEBAPP_CONTEXT,
"/ajax/invoke/XFormsBean.uploadFile",
{ currentPath: webappRelativePath });
}
FilePickerWidget._upload_completeHandler = function(id)
FilePickerWidget._upload_completeHandler = function(id, path, fileName, fileTypeImage, error)
{
var upload = FilePickerWidget._uploads[id];
upload.widget._upload_completeHandler(upload.fileName,
upload.webappRelativePath);
upload.widget._upload_completeHandler(fileName,
upload.webappRelativePath,
fileTypeImage,
error);
}
FilePickerWidget.prototype = {
@@ -2317,14 +2291,20 @@ render: function()
{
this._showSelectedValue();
},
_showStatus: function(text)
_showStatus: function(text, isError)
{
var d = this.node.ownerDocument;
if (!this.statusDiv || !this.statusDiv.parentNode)
{
this.statusDiv = d.createElement("div");
this.statusDiv.setAttribute("id", this.uploadId + "-status");
this.statusDiv.widget = this;
this.node.insertBefore(this.statusDiv, this.node.firstChild);
dojo.html.setClass(this.statusDiv, "infoText xformsFilePickerStatus");
if (isError)
{
dojo.html.addClass(this.statusDiv, "statusErrorText");
}
this.statusDiv.appendChild(d.createTextNode(text));
this.node.style.height = (parseInt(this.node.style.height) +
dojo.style.getMarginHeight(this.statusDiv) +
@@ -2335,22 +2315,38 @@ _showStatus: function(text)
{
this.statusDiv.firstChild.nodeValue = text;
}
setTimeout("var _status = document.getElementById('" + this.uploadId +
"-status'); if (_status && _status) { _status.widget._hideStatus(); }", 5000);
},
_hideStatus: function()
{
if (this.statusDiv)
{
this.node.style.height = (parseInt(this.node.style.height) -
this.statusDiv.offsetHeight) + "px";
dojo.dom.removeChildren(this.statusDiv);
dojo.dom.removeNode(this.statusDiv);
this.resize_callback(this);
var anim = dojo.lfx.html.fadeOut(this.statusDiv, 500);
var _fp_widget = this;
anim.onEnd = function()
{
if (_fp_widget.statusDiv && _fp_widget.statusDiv.parentNode)
{
_fp_widget.node.style.height = (parseInt(_fp_widget.node.style.height) -
_fp_widget.statusDiv.offsetHeight) + "px";
dojo.dom.removeChildren(_fp_widget.statusDiv);
dojo.dom.removeNode(_fp_widget.statusDiv);
_fp_widget.resize_callback(_fp_widget);
_fp_widget.statusDiv = null;
}
};
anim.play();
}
},
_showSelectedValue: function()
{
var d = this.node.ownerDocument;
dojo.dom.removeChildren(this.node);
this.statusDiv = null;
this.contentDiv = null;
this.addContentDiv = null;
this.node.style.height = "20px";
this.node.style.lineHeight = this.node.style.height;
@@ -2418,6 +2414,7 @@ _showPicker: function(data)
parseInt(this.statusDiv.style.marginTop) +
parseInt(this.statusDiv.style.marginBottom))
: 0) + "px");
this.resize_callback(this);
var currentPath = data.getElementsByTagName("current-node")[0];
@@ -2492,8 +2489,15 @@ _showPicker: function(data)
"onclick",
function(event)
{
var t = event.target;
t.filePickerWidget._showAddContentPanel(t, t.getAttribute("webappRelativePath"));
var w = event.target.filePickerWidget;
if (w.addContentDiv)
{
w._hideAddContent();
}
else
{
w._showAddContent(event.target.getAttribute("webappRelativePath"));
}
});
var addContentImage = d.createElement("img");
@@ -2562,12 +2566,11 @@ _showPicker: function(data)
var w = event.target.filePickerWidget;
w._showSelectedValue();
});
this.contentDiv.style.height = (this.node.offsetHeight -
(this.statusDiv ? this.statusDiv.offsetHeight : 0) -
footerDiv.offsetHeight -
headerDiv.offsetHeight - 10) + "px";
// this.contentDiv.style.overflowY = "auto";
var childNodes = data.getElementsByTagName("child-node");
for (var i = 0; i < childNodes.length; i++)
{
@@ -2575,15 +2578,24 @@ _showPicker: function(data)
{
continue;
}
var path = childNodes[i].getAttribute("webappRelativePath");
var name = path.replace(/.*\/([^/]+)/, "$1");
var row = d.createElement("div");
row.setAttribute("id", name + "-row");
var webappRelativePath = childNodes[i].getAttribute("webappRelativePath");
var fileName = webappRelativePath.replace(/.*\/([^/]+)/, "$1");
var row = this._createRow(fileName,
webappRelativePath,
childNodes[i].getAttribute("type") == "directory",
childNodes[i].getAttribute("image"),
"xformsRow" + (i % 2 ? "Even" : "Odd"));
this.contentDiv.appendChild(row);
row.rowIndex = i;
dojo.html.setClass(row, "xformsFilePickerRow xformsRow" + (row.rowIndex % 2 ? "Even" : "Odd"));
dojo.event.browser.addListener(row,
}
},
_createRow: function(fileName, webappRelativePath, isDirectory, fileTypeImage, rowClass)
{
var d = this.contentDiv.ownerDocument;
var result = d.createElement("div");
result.setAttribute("id", fileName + "-row");
dojo.html.setClass(result, "xformsFilePickerRow " + rowClass);
dojo.event.browser.addListener(result,
"mouseover",
function(event)
{
@@ -2593,10 +2605,10 @@ _showPicker: function(data)
dojo.html.removeClass(prevHover, "xformsRowHover");
}
event.currentTarget.parentNode.hoverNode = event.currentTarget;
dojo.html.addClass(event.currentTarget, "xformsRowHover")
dojo.html.addClass(event.currentTarget, "xformsRowHover");
},
true);
dojo.event.browser.addListener(row,
dojo.event.browser.addListener(result,
"mouseout",
function(event)
{
@@ -2611,49 +2623,62 @@ _showPicker: function(data)
var e = d.createElement("img");
e.align = "absmiddle";
e.style.margin = "0px 4px 0px 4px";
e.setAttribute("src", alfresco_xforms_constants.WEBAPP_CONTEXT + childNodes[i].getAttribute("image"));
row.appendChild(e);
e.setAttribute("src", alfresco_xforms_constants.WEBAPP_CONTEXT + fileTypeImage);
result.appendChild(e);
if (childNodes[i].getAttribute("type") == "directory")
if (isDirectory)
{
e = d.createElement("a");
e.filePickerWidget = this;
e.setAttribute("href", "javascript:void(0)");
e.setAttribute("webappRelativePath", path);
e.setAttribute("webappRelativePath", webappRelativePath);
dojo.event.connect(e, "onclick", function(event)
{
var w = event.target.filePickerWidget;
w._navigateToNode(event.target.getAttribute("webappRelativePath"));
return true;
});
e.appendChild(d.createTextNode(name));
row.appendChild(e);
e.appendChild(d.createTextNode(fileName));
result.appendChild(e);
}
else
{
row.appendChild(d.createTextNode(name));
result.appendChild(d.createTextNode(fileName));
}
e = d.createElement("input");
e.filePickerWidget = this;
e.type = "button";
e.name = path;
e.name = webappRelativePath;
e.value = "Select";
row.appendChild(e);
result.appendChild(e);
e.style.position = "absolute";
e.style.right = "10px";
e.style.top = (.5 * row.offsetHeight) - (.5 * e.offsetHeight) + "px";
e.style.top = (.5 * result.offsetHeight) - (.5 * e.offsetHeight) + "px";
dojo.event.connect(e, "onclick", function(event)
{
var w = event.target.filePickerWidget;
w.setValue(event.target.name);
w._showSelectedValue();
});
return result;
},
_hideAddContent: function()
{
if (this.addContentDiv)
{
dojo.dom.removeChildren(this.addContentDiv);
dojo.dom.removeNode(this.addContentDiv);
this.addContentDiv = null;
}
},
_showAddContentPanel: function(addContentLink, currentPath)
_showAddContent: function(currentPath)
{
if (this.addContentDiv)
{
return;
}
var d = this.node.ownerDocument;
this.addContentDiv = d.createElement("div");
dojo.html.setClass(this.addContentDiv, "xformsFilePickerAddContent");
@@ -2676,7 +2701,7 @@ _showAddContentPanel: function(addContentLink, currentPath)
var fileInput = d.createElement("input");
fileInput.type = "file";
fileInput.widget = this;
fileInput.name = this.node.getAttribute("id") + "_file_input";
fileInput.name = this.uploadId + "_file_input";
fileInput.size = "35";
fileInput.setAttribute("webappRelativePath", currentPath);
fileInputDiv.appendChild(fileInput);
@@ -2689,22 +2714,53 @@ _showAddContentPanel: function(addContentLink, currentPath)
function(event)
{
var w = event.target.widget;
FilePickerWidget._handleUpload(w.node.getAttribute("id"),
if (w.addContentDiv)
{
var d = w.addContentDiv.ownerDocument;
dojo.dom.removeChildren(w.addContentDiv);
var fileName = event.target.value.replace(/.*[\/\\]([^\/\\]+)/, "$1");
w.addContentDiv.appendChild(d.createTextNode("Upload: " + fileName));
var img = d.createElement("img");
img.setAttribute("src", alfresco_xforms_constants.WEBAPP_CONTEXT +
"/images/icons/process_animation.gif");
img.style.position = "absolute";
img.style.right = "10px";
img.style.height = (.5 * w.addContentDiv.offsetHeight) + "px";
img.style.top = (.25 * w.addContentDiv.offsetHeight) + "px";
w.addContentDiv.appendChild(img);
}
FilePickerWidget._handleUpload(w.uploadId,
event.target,
event.target.getAttribute("webappRelativePath"),
w);
if (w.addContentDiv)
{
dojo.dom.removeChildren(w.addContentDiv);
dojo.dom.removeNode(w.addContentDiv);
w.addContentDiv = null;
}
});
},
_upload_completeHandler: function(fileName, webappRelativePath)
_upload_completeHandler: function(fileName, webappRelativePath, fileTypeImage, error)
{
this._showStatus("Successfully uploaded " + fileName + "."); // " into " + webappRelativePath);
this._navigateToNode(webappRelativePath);
if (error)
{
this._showStatus(error, true);
this._hideAddContent();
this._showAddContent(webappRelativePath);
}
else
{
var nextRow = dojo.dom.nextElement(this.addContentDiv);
var rowClass = (nextRow
? ("xformsRow" + (dojo.html.hasClass(nextRow, "xformsRowEven")
? "Odd"
: "Even"))
: "xformsRowEvent");
var row = this._createRow(fileName,
webappRelativePath == "/" ? "/" + fileName : webappRelativePath + "/" + fileName,
false,
fileTypeImage,
rowClass);
this.contentDiv.replaceChild(row, this.addContentDiv);
this.addContentDiv = null;
}
},
_closeParentPathMenu: function()
{
@@ -2796,7 +2852,8 @@ _openParentPathMenu: function(target, path)
parentNodeImage.align = "absmiddle";
parentNodeImage.style.marginRight = "4px";
parentNodeDiv.appendChild(parentNodeImage);
parentNodeImage.setAttribute("src", alfresco_xforms_constants.WEBAPP_CONTEXT + "/images/icons/space_small.gif");
parentNodeImage.setAttribute("src", alfresco_xforms_constants.WEBAPP_CONTEXT +
"/images/icons/space_small.gif");
parentNodeDiv.appendChild(parentNodeImage);
parentNodeDiv.appendChild(d.createTextNode(path));
dojo.event.connect(parentNodeDiv,

View File

@@ -3,7 +3,9 @@ var _uploads = [];
function handle_upload_helper(fileInputElement,
uploadId,
callback,
contextPath)
contextPath,
actionUrl,
params)
{
var id = fileInputElement.getAttribute("name");
var d = fileInputElement.ownerDocument;
@@ -20,12 +22,15 @@ function handle_upload_helper(fileInputElement,
var form = d.createElement("form");
d.body.appendChild(form);
form.id = id + "_upload_form";
form.name = form.id;
form.style.display = "none";
form.method = "post";
form.encoding = "multipart/form-data";
form.enctype = "multipart/form-data";
form.target = iframe.name;
form.action = contextPath + "/uploadFileServlet";
actionUrl = actionUrl || "/uploadFileServlet";
form.action = contextPath + actionUrl;
form.appendChild(fileInputElement);
var id = document.createElement("input");
@@ -34,17 +39,31 @@ function handle_upload_helper(fileInputElement,
id.name = "upload-id";
id.value = uploadId;
for (var i in params)
{
var p = document.createElement("input");
p.type = "hidden";
form.appendChild(p);
id.name = i;
id.value = params[i];
}
var rp = document.createElement("input");
rp.type = "hidden";
form.appendChild(rp);
rp.name = "return-page";
rp.value = "javascript:window.parent.upload_complete_helper('" + uploadId + "')";
rp.value = "javascript:window.parent.upload_complete_helper('" + uploadId +
"',{error: '${_UPLOAD_ERROR}', fileTypeImage: '${_FILE_TYPE_IMAGE}'})";
form.submit();
}
function upload_complete_helper(id)
function upload_complete_helper(id, args)
{
var upload = _uploads[id];
upload.callback(id, upload.path, upload.path.replace(/.*[\/\\]([^\/\\]+)/, "$1"));
upload.callback(id,
upload.path,
upload.path.replace(/.*[\/\\]([^\/\\]+)/, "$1"),
args.fileTypeImage,
args.error != "${_UPLOAD_ERROR}" ? args.error : null);
}