// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.googlesource.gerrit.plugins.imagare.client;

import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;

public class UploadByFileSelection extends HorizontalPanel {
  UploadByFileSelection() {
    this("All-Projects");
  }

  UploadByFileSelection(String project) {
    init0();

    FormPanel form = new FormPanel();
    FileUpload upload = new FileUpload();
    upload.setName("Select Image");
    upload.getElement().setAttribute("onChange", "imagareSubmit(event)");
    form.add(upload);

    add(form);
  }

  private static native void init0() /*-{
    $wnd.imagareSubmit = function submit(event) {
      var f = event.target.files[0];
      if (f) {
        var r = new FileReader();
        r.onload = function(e) {
          if (f.type.match('image/.*')) {
            @com.googlesource.gerrit.plugins.imagare.client.ImageUploader::stageImage(Ljava/lang/String;Ljava/lang/String;)(e.target.result, f.name);
          } else {
            $wnd.Gerrit.showError('no image file');
          }
        }
        r.readAsDataURL(f);
      } else {
        $wnd.Gerrit.showError('Failed to load file.');
      }
    }
  }-*/;
}

@@ -1099,24 +1099,86 @@
 ct Image%22);%0A
+    upload.getElement().setAttribute(%22multiple%22, %22multiple%22);%0A
     upload.g
@@ -1370,24 +1370,85 @@
 it(event) %7B%0A
+      for(var i = 0; i %3C event.target.files.length; i++) %7B%0A  
       var f 
@@ -1468,17 +1468,17 @@
 t.files%5B
-0
+i
 %5D;%0A     
@@ -1474,24 +1474,26 @@
 s%5Bi%5D;%0A      
+  
 if (f) %7B%0A   
@@ -1501,105 +1501,208 @@
     
-var r = new FileReader();%0A        r.onload = function(e) %7B%0A          if (f.type.match('image/.*')
+  if (!f.type.match('image/.*')) %7B%0A            $wnd.Gerrit.showError('no image file: ' + f.name);%0A          %7D%0A%0A          var r = new FileReader();%0A          r.file = f;%0A          r.onload = function(e
 ) %7B%0A
@@ -1846,85 +1846,22 @@
 lt, 
-f.name);%0A          %7D else %7B%0A            $wnd.Gerrit.showError('no image file'
+this.file.name
 );%0A 
@@ -1873,24 +1873,16 @@
     %7D%0A  
-      %7D%0A
         
@@ -1907,16 +1907,18 @@
 ;%0A      
+  
 %7D else %7B
@@ -1918,16 +1918,18 @@
  else %7B%0A
+  
         
@@ -1974,12 +1974,32 @@
 file
-.');
+: ' + f.name);%0A        %7D
 %0A   

Allow to upload multiple images at once from file upload