From 04b5516bf2611ab55e9ab84c7fae09fac34403e0 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 10 May 2017 14:43:05 -0400 Subject: [PATCH] improve upload functionality --- .../admin-customize-themes-show.js.es6 | 9 +++++++- .../modals/admin-add-upload.js.es6 | 22 ++++++++++++++++++- .../admin/templates/customize-themes-show.hbs | 9 ++++++-- .../templates/modal/admin-add-upload.hbs | 10 +++++---- .../stylesheets/common/admin/customize.scss | 14 ++++++++---- app/controllers/admin/themes_controller.rb | 2 +- config/locales/client.en.yml | 3 ++- .../admin/themes_controller_spec.rb | 4 ++-- spec/models/theme_field_spec.rb | 1 + 9 files changed, 58 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/admin-customize-themes-show.js.es6 b/app/assets/javascripts/admin/controllers/admin-customize-themes-show.js.es6 index 65f3dbc5624..2873f62a9f4 100644 --- a/app/assets/javascripts/admin/controllers/admin-customize-themes-show.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-customize-themes-show.js.es6 @@ -168,7 +168,14 @@ export default Ember.Controller.extend({ }, removeUpload(upload) { - this.get("model").removeField(upload); + return bootbox.confirm( + I18n.t("admin.customize.theme.delete_upload_confirm"), + I18n.t("no_value"), + I18n.t("yes_value"), result => { + if (result) { + this.get("model").removeField(upload); + } + }); }, removeChildTheme(theme) { diff --git a/app/assets/javascripts/admin/controllers/modals/admin-add-upload.js.es6 b/app/assets/javascripts/admin/controllers/modals/admin-add-upload.js.es6 index 9b29d3b0d53..41a2f857154 100644 --- a/app/assets/javascripts/admin/controllers/modals/admin-add-upload.js.es6 +++ b/app/assets/javascripts/admin/controllers/modals/admin-add-upload.js.es6 @@ -1,11 +1,30 @@ import ModalFunctionality from 'discourse/mixins/modal-functionality'; import { ajax } from 'discourse/lib/ajax'; -// import computed from 'ember-addons/ember-computed-decorators'; +import { default as computed, observes } from 'ember-addons/ember-computed-decorators'; import { popupAjaxError } from 'discourse/lib/ajax-error'; export default Ember.Controller.extend(ModalFunctionality, { adminCustomizeThemesShow: Ember.inject.controller(), + onShow() { + this.set('name', null); + this.set('fileSelected', false); + }, + + enabled: Em.computed.and('nameValid', 'fileSelected'), + disabled: Em.computed.not('enabled'), + + @computed('name') + nameValid(name) { + return name && name.match(/^[a-zA-Z0-9-_]+$/) + }, + + @observes('name') + uploadChanged(){ + let file = $('#file-input')[0]; + this.set('fileSelected', file && file.files[0]); + }, + actions: { updateName() { let name = this.get('name'); @@ -13,6 +32,7 @@ export default Ember.Controller.extend(ModalFunctionality, { name = $('#file-input')[0].files[0].name; this.set('name', name.split(".")[0]); } + this.uploadChanged(); }, upload() { diff --git a/app/assets/javascripts/admin/templates/customize-themes-show.hbs b/app/assets/javascripts/admin/templates/customize-themes-show.hbs index b4908d71430..6e89af47678 100644 --- a/app/assets/javascripts/admin/templates/customize-themes-show.hbs +++ b/app/assets/javascripts/admin/templates/customize-themes-show.hbs @@ -91,7 +91,12 @@ {{#if model.uploads}} {{else}} @@ -113,7 +118,7 @@ {{else}} {{/unless}} diff --git a/app/assets/javascripts/admin/templates/modal/admin-add-upload.hbs b/app/assets/javascripts/admin/templates/modal/admin-add-upload.hbs index 108b6832bcf..5023927af2d 100644 --- a/app/assets/javascripts/admin/templates/modal/admin-add-upload.hbs +++ b/app/assets/javascripts/admin/templates/modal/admin-add-upload.hbs @@ -1,12 +1,14 @@ -{{#d-modal-body class='upload-selector' title="admin.customize.theme.add_upload"}} +{{#d-modal-body class='add-upload-modal' title="admin.customize.theme.add_upload"}}
-

- {{i18n 'admin.customize.theme.upload_file_tip'}} + {{i18n 'admin.customize.theme.upload_file_tip'}}
+
{{/d-modal-body}} diff --git a/app/assets/stylesheets/common/admin/customize.scss b/app/assets/stylesheets/common/admin/customize.scss index 541a6ff1bbb..cf656c9d939 100644 --- a/app/assets/stylesheets/common/admin/customize.scss +++ b/app/assets/stylesheets/common/admin/customize.scss @@ -193,15 +193,21 @@ margin-left: 0; li { display: table-row; - .first { - padding-right: 8px; + .col:first-child { + padding-right: 10px; padding-bottom: 10px; - min-width: 100px; + min-width: 80px; } - .first, .second { + .col { display: table-cell; } } } } +.add-upload-modal { + label { + margin-top: 20px; + } +} + diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rb index fa4226443ff..e4de8709bbd 100644 --- a/app/controllers/admin/themes_controller.rb +++ b/app/controllers/admin/themes_controller.rb @@ -13,7 +13,7 @@ class Admin::ThemesController < Admin::AdminController File.open(path) do |file| upload = Upload.create_for(current_user.id, file, - params[:original_filename] || File.basename(path), + params[:file]&.original_filename || File.basename(path), File.size(path), for_theme: true) if upload.errors.count > 0 diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 84f541f9134..d890d827ab7 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2845,12 +2845,13 @@ en: no_uploads: "You can upload assets associated with your theme such as fonts and images" add_upload: "Add Upload" upload_file_tip: "Choose an asset to upload (png, woff2, etc...)" + variable_name: "SCSS var name:" upload: "Upload" child_themes_check: "Theme includes other child themes" css_html: "Custom CSS/HTML" edit_css_html: "Edit CSS/HTML" edit_css_html_help: "You have not edited any CSS or HTML" - upload_name: "Name" + delete_upload_confirm: "Delete this upload? (Theme CSS may stop working!)" import_web_tip: "Repository containing theme" import_file_tip: ".dcstyle.json file containing theme" about_theme: "About Theme" diff --git a/spec/controllers/admin/themes_controller_spec.rb b/spec/controllers/admin/themes_controller_spec.rb index fefa8fb9795..efc9a51f475 100644 --- a/spec/controllers/admin/themes_controller_spec.rb +++ b/spec/controllers/admin/themes_controller_spec.rb @@ -22,9 +22,9 @@ describe Admin::ThemesController do end it 'can create a theme upload' do - xhr :post, :upload_asset, file: upload, original_filename: 'wooof.woff2' + xhr :post, :upload_asset, file: upload expect(response.status).to eq(201) - upload = Upload.find_by(original_filename: "wooof.woff2") + upload = Upload.find_by(original_filename: "test.woff2") expect(upload.id).not_to be_nil expect(JSON.parse(response.body)["upload_id"]).to eq(upload.id) end diff --git a/spec/models/theme_field_spec.rb b/spec/models/theme_field_spec.rb index 9dd0a099772..00c88834d87 100644 --- a/spec/models/theme_field_spec.rb +++ b/spec/models/theme_field_spec.rb @@ -27,4 +27,5 @@ HTML expect(field.error).to eq(nil) end + end