FIX: Do not destroy $.fileupload element (#9888)

FIX: Do not destroy $.fileupload element (#9888)

conditional-loading-section component rerendered the element and lost the necessary event handlers for jQuery-File-Upload.

diff --git a/app/assets/javascripts/discourse/app/components/emoji-uploader.js b/app/assets/javascripts/discourse/app/components/emoji-uploader.js
index 9693811..0be91fe 100644
--- a/app/assets/javascripts/discourse/app/components/emoji-uploader.js
+++ b/app/assets/javascripts/discourse/app/components/emoji-uploader.js
@@ -1,4 +1,4 @@
-import { notEmpty, not } from "@ember/object/computed";
+import { notEmpty } from "@ember/object/computed";
 import { action } from "@ember/object";
 import Component from "@ember/component";
 import discourseComputed from "discourse-common/utils/decorators";
@@ -11,7 +11,6 @@ export default Component.extend(UploadMixin, {
   uploadUrl: "/admin/customize/emojis",
   hasName: notEmpty("name"),
   hasGroup: notEmpty("group"),
-  addDisabled: not("hasName"),
   group: "default",
   emojiGroups: null,
   newEmojiGroups: null,
@@ -23,6 +22,11 @@ export default Component.extend(UploadMixin, {
     this.set("newEmojiGroups", this.emojiGroups);
   },
 
+  @discourseComputed("hasName", "uploading")
+  addDisabled() {
+    return !this.hasName || this.uploading;
+  },
+
   uploadOptions() {
     return { sequentialUploads: true };
   },
diff --git a/app/assets/javascripts/discourse/app/mixins/upload.js b/app/assets/javascripts/discourse/app/mixins/upload.js
index 85fdbc0..b739466 100644
--- a/app/assets/javascripts/discourse/app/mixins/upload.js
+++ b/app/assets/javascripts/discourse/app/mixins/upload.js
@@ -39,8 +39,10 @@ export default Mixin.create({
 
   _initialize: on("didInsertElement", function() {
     const $upload = $(this.element);
-    const reset = () =>
+    const reset = () => {
       this.setProperties({ uploading: false, uploadProgress: 0 });
+      document.getElementsByClassName("hidden-upload-field")[0].value = "";
+    };
     const maxFiles = this.getWithDefault(
       "maxFiles",
       this.siteSettings.simultaneous_uploads
diff --git a/app/assets/javascripts/discourse/app/templates/components/emoji-uploader.hbs b/app/assets/javascripts/discourse/app/templates/components/emoji-uploader.hbs
index 7e15063..6839c6c 100644
--- a/app/assets/javascripts/discourse/app/templates/components/emoji-uploader.hbs
+++ b/app/assets/javascripts/discourse/app/templates/components/emoji-uploader.hbs
@@ -1,48 +1,51 @@
-{{#conditional-loading-section isLoading=uploading}}
-  <div class="emoji-uploader">
-    <div class="control">
-      <span class="label">
-        {{i18n "admin.emoji.name"}}
-      </span>
-      <div class="input">
-        {{input
-          name="name"
-          placeholderKey="admin.emoji.name"
-          value=(readonly name)
-          input=(action (mut name) value="target.value")
-        }}
-      </div>
+<div class="emoji-uploader">
+  <div class="control">
+    <span class="label">
+      {{i18n "admin.emoji.name"}}
+    </span>
+    <div class="input">
+      {{input
+        name="name"
+        placeholderKey="admin.emoji.name"
+        value=(readonly name)
+        input=(action (mut name) value="target.value")
+      }}
     </div>
-    <div class="control">
-      <span class="label">
-        {{i18n "admin.emoji.group"}}
-      </span>
-      <div class="input">
-        {{combo-box
-          name="group"
-          value=group
-          content=newEmojiGroups
-          onChange=(action "createEmojiGroup")
-          valueProperty=null
-          nameProperty=null
-          options=(hash
-            allowAny=true
-          )
-        }}
-      </div>
+  </div>
+  <div class="control">
+    <span class="label">
+      {{i18n "admin.emoji.group"}}
+    </span>
+    <div class="input">
+      {{combo-box
+        name="group"
+        value=group
+        content=newEmojiGroups
+        onChange=(action "createEmojiGroup")
+        valueProperty=null
+        nameProperty=null
+        options=(hash
+          allowAny=true
+        )
+      }}
     </div>
-    <div class="control">
-      <div class="input">
-        <label class="btn btn-default btn-primary {{if addDisabled "disabled"}}">
+  </div>
+  <div class="control">
+    <div class="input">
+      <label class="btn btn-default btn-primary {{if addDisabled "disabled"}}">
+        {{#if uploading}}
+          {{d-icon "spinner" class="loading-icon"}}
+          <span>{{i18n "admin.emoji.uploading"}}</span>
+        {{else}}
           {{d-icon "plus"}}
-          {{i18n "admin.emoji.add"}}
-          <input
-            class="hidden-upload-field"
-            disabled={{addDisabled}}
-            type="file"
-            accept=".png,.gif">
-        </label>
-      </div>
+          <span>{{i18n "admin.emoji.add"}}</span>
+        {{/if}}
+        <input
+          class="hidden-upload-field"
+          disabled={{addDisabled}}
+          type="file"
+          accept=".png,.gif">
+      </label>
     </div>
   </div>
-{{/conditional-loading-section}}
+</div>
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 3c85e21..95830e3 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4673,6 +4673,7 @@ en:
         title: "Emoji"
         help: "Add new emoji that will be available to everyone. (PROTIP: drag & drop multiple files at once)"
         add: "Add New Emoji"
+        uploading: "Uploading..."
         name: "Name"
         group: "Group"
         image: "Image"

GitHub sha: d76ea9fa

1 Like

This commit appears in #9888 which was merged by SamSaffron.