*/
public void render(Renderer renderer, StringOutput sb, Component source,
URLBuilder ubu, Translator translator, RenderResult renderResult,
String[] args) {
// Use translator with flexi form elements fallback
Translator trans = Util.createPackageTranslator(FileElementRenderer.class, translator.getLocale(), translator);
//
FileElementComponent fileComp = (FileElementComponent) source;
FileElementImpl fileElem = fileComp.getFileElementImpl();
String id = fileComp.getFormDispatchId();
// Calculate current file name: either from already uploaded file or
// from initial file or empty
String fileName = fileElem.getUploadFileName();
if (fileName == null) {
// try fallback: default file
File initialFile = fileElem.getInitialFile();
if (initialFile != null) {
fileName = initialFile.getName();
} else {
fileName = "";
}
}
// Read-write view
if (source.isEnabled()) {
sb.append("<div class='b_fileinput'>");
// input.Browse is the real filebrowser, but set to be transparent.
// the div.b_fileinput_fakechooser is layered below the input.Browse and represents the visual GUI.
// Since input.Browse is layered above div.b_fileinput_fakechooser, all click events to go input.Browse
// See http://www.quirksmode.org/dom/inputfile.html
sb.append("<input type='file' onkeydown='return false;' name=\"");
sb.append(id); // name for form labeling
sb.append("\" id=\"");
sb.append(id); // id to make dirty button work
sb.append("\" class='b_fileinput_realchooser' ");
// Add on* event handlers
StringBuilder eventHandlers = FormJSHelper.getRawJSFor(fileElem.getRootForm(), id, fileElem.getAction());
int onChangePos = eventHandlers.indexOf("onchange=");
if (onChangePos != -1) {
// add file upload change handler
sb.append(eventHandlers.substring(0, onChangePos + 10));
sb.append("b_handleFileUploadFormChange(this, this.form.fake_").append(id).append(", this.form.upload);");
sb.append(eventHandlers.substring(onChangePos + 10, eventHandlers.length()));
} else {
sb.append(eventHandlers);
sb.append(" onchange=\"b_handleFileUploadFormChange(this, this.form.fake_").append(id).append(", this.form.upload)\"");
}
// Add mime type restriction
Set<String> mimeTypes = fileElem.getMimeTypeLimitations();
if (mimeTypes.size() > 0 ) {
sb.append(" accept=\"");
Iterator iterator = mimeTypes.iterator();
while (iterator.hasNext()) {
String type = (String) iterator.next();
sb.append(type);
if (iterator.hasNext()) sb.append(",");
}
sb.append("\"");
}
// Add pseudo focus marker on fake file chooser button
sb.append(" onfocus=\"this.form.fake_").append(id).append(".nextSibling.style.border = '1px dotted black';\"");
sb.append(" onblur=\"this.form.fake_").append(id).append(".nextSibling.style.border = '0';\"");
// Add select text (hover)
sb.append(" title=\"").append(StringEscapeUtils.escapeHtml(trans.translate("file.element.select"))).append("\"/>");
sb.append("<div class='b_fileinput_fakechooser'>");
// Add the visible but fake input field and a styled faked file chooser button
sb.append("<input name='fake_").append(id).append("' value=\"").append(StringEscapeUtils.escapeHtml(fileName)).append("\"/>");
sb.append("<a href='#' class='b_with_small_icon_left b_fileinput_icon'><span>").append(trans.translate("file.element.select")).append("</span></a>");
// Add Max upload size
if (fileElem.getMaxUploadSizeKB() != FileElement.UPLOAD_UNLIMITED) {
String maxUpload = Formatter.roundToString((fileElem.getMaxUploadSizeKB()+0f) / 1024, 1);
sb.append("<span class='b_fileinput_maxsize'>(").append(trans.translate("file.element.select.maxsize", new String[]{maxUpload})).append(")</span>");
}
sb.append("</div></div>");
// Add IE fix to deal with SSL and server timeouts
// See http://bugs.olat.org/jira/browse/OLAT-1299