This is an automated email from the ASF dual-hosted git repository.

reiern70 pushed a commit to branch reiern70/WICKET-7033
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/reiern70/WICKET-7033 by this 
push:
     new ff30454860 [WICKET-7033] WIP
ff30454860 is described below

commit ff30454860197adc4594b775854f0e0deaa62b1f
Author: reiern70 <[email protected]>
AuthorDate: Sat Apr 8 09:15:09 2023 +0300

    [WICKET-7033] WIP
---
 .../examples/upload/UploadToResourcePage.html      |  3 ++
 .../examples/upload/UploadToResourcePage.java      |  7 ++-
 .../markup/html/form/upload/UploadProgressBar.java | 61 ++++++++++++++++++----
 .../ajax/markup/html/form/upload/progressbar.js    | 44 +++++++++++-----
 4 files changed, 92 insertions(+), 23 deletions(-)

diff --git 
a/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.html
 
b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.html
index 7cb95a8b76..161734a282 100644
--- 
a/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.html
+++ 
b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.html
@@ -19,6 +19,9 @@
                        <label wicket:for="fileInput">File</label>
                        <input wicket:id="fileInput" type="file"/>
            </p>
+                <div>
+                        <span wicket:id="progress">[[upload 
progressbar]]</span>
+                </div>
                 <input wicket:id="upload" type="button" value="Upload to a 
resource"/>
                 <input wicket:id="cancelUpload" type="button" value="Cancel 
upload"/>
         </fieldset>
diff --git 
a/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.java
 
b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.java
index 32de1f3542..17f5c7cba4 100644
--- 
a/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.java
+++ 
b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.java
@@ -23,6 +23,7 @@ import org.apache.wicket.ajax.markup.html.AjaxLink;
 import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.event.IEvent;
 import org.apache.wicket.examples.WicketExamplePage;
+import 
org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar;
 import org.apache.wicket.markup.html.form.upload.resource.IUploadsFileManager;
 import 
org.apache.wicket.markup.html.form.upload.resource.SingleFileUploadToResourceField;
 import org.apache.wicket.markup.html.panel.FeedbackPanel;
@@ -32,9 +33,8 @@ import org.apache.wicket.util.file.Files;
 /**
  * Upload example.
  *
- * @author Eelco Hillenius
+ * @author reiern70
  */
-@SuppressWarnings("serial")
 public class UploadToResourcePage extends WicketExamplePage
 {
        private static class UpdateInEachAjaxRequestBehavior extends Behavior
@@ -97,12 +97,15 @@ public class UploadToResourcePage extends WicketExamplePage
                };
                add(singleFileUploadToResourceField);
 
+               final UploadProgressBar uploadProgressBar = new 
UploadProgressBar("progress", singleFileUploadToResourceField);
+               add(uploadProgressBar);
                add(new AjaxLink<Void>("upload") {
                        @Override
                        public void onClick(AjaxRequestTarget target)
                        {
                                uploading = true;
                                
singleFileUploadToResourceField.startUpload(target);
+                               uploadProgressBar.start(target);
                        }
 
                        @Override
diff --git 
a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
 
b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
index 04307b8aeb..fa4bfff362 100644
--- 
a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
+++ 
b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
@@ -21,6 +21,7 @@ import java.util.Formatter;
 import org.apache.wicket.Application;
 import org.apache.wicket.IInitializer;
 import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.core.request.handler.IPartialPageRequestHandler;
 import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
 import org.apache.wicket.markup.head.CssHeaderItem;
 import org.apache.wicket.markup.head.IHeaderResponse;
@@ -30,7 +31,6 @@ import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.form.Form;
 import org.apache.wicket.markup.html.form.upload.FileUploadField;
 import org.apache.wicket.markup.html.panel.Panel;
-import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.StringResourceModel;
 import org.apache.wicket.request.resource.CssResourceReference;
 import org.apache.wicket.request.resource.JavaScriptResourceReference;
@@ -112,7 +112,7 @@ public class UploadProgressBar extends Panel
 
        private static final long serialVersionUID = 1L;
 
-       private final Form<?> form;
+       private Form<?> form;
 
        private MarkupContainer statusDiv;
 
@@ -120,6 +120,30 @@ public class UploadProgressBar extends Panel
 
        private final FileUploadField uploadField;
 
+       /**
+        * Constructor that will display the upload progress bar for every 
submit of the given form.
+        *
+        * @param id
+        *            component id (not null)
+        * @param uploadField
+        *            the file upload field to check for a file upload, or null 
to display the upload
+        *            field for every submit of the given form
+        */
+       public UploadProgressBar(final String id,  final FileUploadField 
uploadField)
+       {
+               super(id);
+
+               this.uploadField = uploadField;
+               if (uploadField != null)
+               {
+                       uploadField.setOutputMarkupId(true);
+               }
+
+               setRenderBodyOnly(true);
+       }
+
+
+
        /**
         * Constructor that will display the upload progress bar for every 
submit of the given form.
         * 
@@ -166,7 +190,10 @@ public class UploadProgressBar extends Panel
        protected void onInitialize()
        {
                super.onInitialize();
-               getCallbackForm().setOutputMarkupId(true);
+               Form<?> form = getCallbackForm();
+               if (form != null) {
+                       form.setOutputMarkupId(true);
+               }
 
                barDiv = newBarComponent("bar");
                add(barDiv);
@@ -231,22 +258,34 @@ public class UploadProgressBar extends Panel
 
                ResourceReference ref = new 
SharedResourceReference(RESOURCE_NAME);
 
-               final String uploadFieldId = (uploadField == null) ? "" : 
uploadField.getMarkupId();
+               final String uploadFieldId = (uploadField == null) ? "null" : 
("'" + uploadField.getMarkupId() + "'");
 
-               final String status = new 
StringResourceModel(RESOURCE_STARTING, this, (IModel<?>)null).getString();
+               final String status = new 
StringResourceModel(RESOURCE_STARTING, this, null).getString();
 
-               CharSequence url = urlFor(ref, 
UploadStatusResource.newParameter(getPage().getId()));
+               CharSequence url = form != null ? urlFor(ref, 
UploadStatusResource.newParameter(getPage().getId())) :
+                               urlFor(ref, 
UploadStatusResource.newParameter(uploadField.getMarkupId()));
 
                StringBuilder builder = new StringBuilder(128);
                Formatter formatter = new Formatter(builder);
 
-               formatter.format(
-                       "new Wicket.WUPB('%s', '%s', '%s', '%s', '%s', '%s');",
-                               getCallbackForm().getMarkupId(), 
statusDiv.getMarkupId(), barDiv.getMarkupId(), url, uploadFieldId,
+               Form<?> form = getCallbackForm();
+
+               formatter.format(getVarName() + " = new Wicket.WUPB(%s, '%s', 
'%s', '%s', %s, '%s');",
+                               form != null ? "'" + form.getMarkupId() + "'" : 
"null", statusDiv.getMarkupId(), barDiv.getMarkupId(), url, uploadFieldId,
                        status);
                
response.render(OnDomReadyHeaderItem.forScript(builder.toString()));
        }
 
+       private String getVarName() {
+               return "window.upb_" + barDiv.getMarkupId();
+       }
+
+
+       public void start(IPartialPageRequestHandler handler)
+       {
+               handler.appendJavaScript(getVarName() + ".start();");
+       }
+
        /**
         * Form on where will be installed the JavaScript callback to present 
the progress bar.
         * {@link ModalWindow} is designed to hold nested forms and the 
progress bar callback JavaScript
@@ -256,6 +295,10 @@ public class UploadProgressBar extends Panel
         */
        private Form<?> getCallbackForm()
        {
+               if (form == null)
+               {
+                       return null;
+               }
                Boolean insideModal = form.visitParents(ModalWindow.class,
                        new IVisitor<ModalWindow, Boolean>()
                        {
diff --git 
a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js
 
b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js
index 6cd0ad930b..5ff99e87c5 100644
--- 
a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js
+++ 
b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js
@@ -32,9 +32,11 @@
                        this.fileid = fileid;
                        this.initialStatus = initialStatus;
 
-                       var formElement = Wicket.$(formid);
-                       this.originalCallback = formElement.onsubmit;
-                       formElement.onsubmit = Wicket.bind(this.submitCallback, 
this);
+                       if (formid) {
+                               var formElement = Wicket.$(formid);
+                               this.originalCallback = formElement.onsubmit;
+                               formElement.onsubmit = 
Wicket.bind(this.submitCallback, this);
+                       }
                },
 
                submitCallback : function() {
@@ -55,8 +57,14 @@
                        if (displayprogress) {
                                this.setPercent(0);
                                this.setStatus(this.initialStatus);
-                               
Wicket.$(this.statusid).removeAttribute('hidden');
-                               Wicket.$(this.barid).removeAttribute('hidden');
+                               var $statusId = Wicket.$(this.statusid);
+                               if ($statusId != null) {
+                                       $statusId.removeAttribute('hidden');
+                               }
+                               var $barid = Wicket.$(this.barid)
+                               if ($barid != null) {
+                                       $barid.removeAttribute('hidden');
+                               }
                                this.scheduleUpdate();
                        }
                },
@@ -64,15 +72,21 @@
                setStatus : function(status) {
                        var label = document.createElement("label");
                        label.innerHTML = status;
-                       var oldLabel = Wicket.$(this.statusid).firstChild;
-                       if( oldLabel != null){
-                               Wicket.$(this.statusid).removeChild(oldLabel);
+                       var $statusId = Wicket.$(this.statusid);
+                       if ($statusId != null) {
+                               var oldLabel = $statusId.firstChild;
+                               if (oldLabel != null){
+                                       $statusId.removeChild(oldLabel);
+                               }
+                               $statusId.appendChild(label);
                        }
-                       Wicket.$(this.statusid).appendChild(label);
                },
 
                setPercent : function(progressPercent) {
-                       Wicket.$(this.barid).firstChild.firstChild.style.width 
= progressPercent + '%';
+                       var barId = Wicket.$(this.barid);
+                       if (barId != null && barId.firstChild != null && 
barId.firstChild.firstChild != null) {
+                               barId.firstChild.firstChild.style.width = 
progressPercent + '%';
+                       }
                },
 
                scheduleUpdate : function(){
@@ -117,8 +131,14 @@
                        this.iframe = null;
 
                        if (progressPercent === '100') {
-                               Wicket.$(this.statusid).setAttribute('hidden', 
'');
-                               Wicket.$(this.barid).setAttribute('hidden', '');
+                               var $statusId = Wicket.$(this.statusid);
+                               if ($statusId != null) {
+                                       $statusId.setAttribute('hidden', '');
+                               }
+                               var $barid = Wicket.$(this.barid);
+                               if ($barid != null) {
+                                       $barid.setAttribute('hidden', '');
+                               }
                        } else {
                                this.scheduleUpdate();
                        }

Reply via email to