From 6efc5cc98db8377188a8d2b3c485e2818497d513 Mon Sep 17 00:00:00 2001
From: Darren Oh <darrenoh@30772.no-reply.drupal.org>
Date: Mon, 19 Feb 2018 11:09:56 -0500
Subject: [PATCH] Issue #2662932 by droplet: Fix file upload progress bar

---
 core/modules/file/file.es6.js                 | 5 +++--
 core/modules/file/file.js                     | 5 +++--
 core/modules/file/src/Element/ManagedFile.php | 2 +-
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/core/modules/file/file.es6.js b/core/modules/file/file.es6.js
index 9ab5dc2da3..960a858883 100644
--- a/core/modules/file/file.es6.js
+++ b/core/modules/file/file.es6.js
@@ -86,7 +86,6 @@
     attach(context) {
       const $context = $(context);
       $context.find('.js-form-submit').on('mousedown', Drupal.file.disableFields);
-      $context.find('.js-form-managed-file .js-form-submit').on('mousedown', Drupal.file.progressBar);
     },
     detach(context) {
       const $context = $(context);
@@ -167,6 +166,7 @@
      *   The event triggered. For example `change.autoFileUpload`.
      */
     triggerUploadButton(event) {
+      Drupal.file.progressBar(event);
       $(event.target).closest('.js-form-managed-file').find('.js-form-submit').trigger('mousedown');
     },
 
@@ -217,7 +217,7 @@
      *   The event triggered, most likely a `mousedown` event.
      */
     progressBar(event) {
-      const $clickedButton = $(this);
+      const $clickedButton = $(event.target).closest('.js-form-managed-file').find('.js-form-submit');
       const $progressId = $clickedButton.closest('div.js-form-managed-file').find('input.file-progress');
       if ($progressId.length) {
         const originalName = $progressId.attr('name');
@@ -228,6 +228,7 @@
         // Restore the original name after the upload begins.
         setTimeout(() => {
           $progressId.attr('name', originalName);
+          $clickedButton.closest('div.js-form-managed-file').find('input.form-file').remove();
         }, 1000);
       }
       // Show the progress bar if the upload takes longer than half a second.
diff --git a/core/modules/file/file.js b/core/modules/file/file.js
index 4d51bb0fa0..a23a3f6567 100644
--- a/core/modules/file/file.js
+++ b/core/modules/file/file.js
@@ -50,7 +50,6 @@
     attach: function attach(context) {
       var $context = $(context);
       $context.find('.js-form-submit').on('mousedown', Drupal.file.disableFields);
-      $context.find('.js-form-managed-file .js-form-submit').on('mousedown', Drupal.file.progressBar);
     },
     detach: function detach(context) {
       var $context = $(context);
@@ -90,6 +89,7 @@
       }
     },
     triggerUploadButton: function triggerUploadButton(event) {
+      Drupal.file.progressBar(event);
       $(event.target).closest('.js-form-managed-file').find('.js-form-submit').trigger('mousedown');
     },
     disableFields: function disableFields(event) {
@@ -111,7 +111,7 @@
       }, 1000);
     },
     progressBar: function progressBar(event) {
-      var $clickedButton = $(this);
+      var $clickedButton = $(event.target).closest('.js-form-managed-file').find('.js-form-submit');
       var $progressId = $clickedButton.closest('div.js-form-managed-file').find('input.file-progress');
       if ($progressId.length) {
         var originalName = $progressId.attr('name');
@@ -120,6 +120,7 @@
 
         setTimeout(function () {
           $progressId.attr('name', originalName);
+          $clickedButton.closest('div.js-form-managed-file').find('input.form-file').remove();
         }, 1000);
       }
 
diff --git a/core/modules/file/src/Element/ManagedFile.php b/core/modules/file/src/Element/ManagedFile.php
index ca4e887a1b..ae79ecd025 100644
--- a/core/modules/file/src/Element/ManagedFile.php
+++ b/core/modules/file/src/Element/ManagedFile.php
@@ -227,6 +227,7 @@ public static function processManagedFile(&$element, FormStateInterface $form_st
         ],
       ],
       'wrapper' => $ajax_wrapper_id,
+      'event' => 'fileUpload',
       'effect' => 'fade',
       'progress' => [
         'type' => $element['#progress_indicator'],
@@ -239,7 +240,6 @@ public static function processManagedFile(&$element, FormStateInterface $form_st
       '#name' => $parents_prefix . '_upload_button',
       '#type' => 'submit',
       '#value' => t('Upload'),
-      '#attributes' => ['class' => ['js-hide']],
       '#validate' => [],
       '#submit' => ['file_managed_file_submit'],
       '#limit_validation_errors' => [$element['#parents']],
-- 
2.14.3 (Apple Git-98)

