improve upload functionality

This commit is contained in:
Sam 2017-05-10 14:43:05 -04:00
parent 27362c5a4b
commit 04b5516bf2
9 changed files with 58 additions and 16 deletions

View File

@ -168,7 +168,14 @@ export default Ember.Controller.extend({
}, },
removeUpload(upload) { removeUpload(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); this.get("model").removeField(upload);
}
});
}, },
removeChildTheme(theme) { removeChildTheme(theme) {

View File

@ -1,11 +1,30 @@
import ModalFunctionality from 'discourse/mixins/modal-functionality'; import ModalFunctionality from 'discourse/mixins/modal-functionality';
import { ajax } from 'discourse/lib/ajax'; 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'; import { popupAjaxError } from 'discourse/lib/ajax-error';
export default Ember.Controller.extend(ModalFunctionality, { export default Ember.Controller.extend(ModalFunctionality, {
adminCustomizeThemesShow: Ember.inject.controller(), 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: { actions: {
updateName() { updateName() {
let name = this.get('name'); let name = this.get('name');
@ -13,6 +32,7 @@ export default Ember.Controller.extend(ModalFunctionality, {
name = $('#file-input')[0].files[0].name; name = $('#file-input')[0].files[0].name;
this.set('name', name.split(".")[0]); this.set('name', name.split(".")[0]);
} }
this.uploadChanged();
}, },
upload() { upload() {

View File

@ -91,7 +91,12 @@
{{#if model.uploads}} {{#if model.uploads}}
<ul class='removable-list'> <ul class='removable-list'>
{{#each model.uploads as |upload|}} {{#each model.uploads as |upload|}}
<li><span class='first'>${{upload.name}}: <a href={{upload.url}} target='_blank'>{{upload.filename}}</a></span>{{d-button action="removeUpload" actionParam=upload class="second btn-small cancel-edit" icon="times"}}</li> <li>
<span class='col'>${{upload.name}}: <a href={{upload.url}} target='_blank'>{{upload.filename}}</a></span>
<span class='col'>
{{d-button action="removeUpload" actionParam=upload class="second btn-small cancel-edit" icon="times"}}
</span>
</li>
{{/each}} {{/each}}
</ul> </ul>
{{else}} {{else}}
@ -113,7 +118,7 @@
{{else}} {{else}}
<ul class='removable-list'> <ul class='removable-list'>
{{#each model.childThemes as |child|}} {{#each model.childThemes as |child|}}
<li>{{#link-to 'adminCustomizeThemes.show' child replace=true class='first'}}{{child.name}}{{/link-to}} {{d-button action="removeChildTheme" actionParam=child class="btn-small cancel-edit second" icon="times"}}</li> <li>{{#link-to 'adminCustomizeThemes.show' child replace=true class='col'}}{{child.name}}{{/link-to}} {{d-button action="removeChildTheme" actionParam=child class="btn-small cancel-edit col" icon="times"}}</li>
{{/each}} {{/each}}
</ul> </ul>
{{/unless}} {{/unless}}

View File

@ -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"}}
<div class="inputs"> <div class="inputs">
<input id="name" placeholder={{i18n 'admin.customize.theme.upload_name'}} value={{name}}><br>
<input onchange={{action "updateName"}} type="file" id="file-input" accept='*'><br> <input onchange={{action "updateName"}} type="file" id="file-input" accept='*'><br>
<span class="description">{{i18n 'admin.customize.theme.upload_file_tip'}}</span> <span class="description">{{i18n 'admin.customize.theme.upload_file_tip'}}</span><br>
<label>{{i18n 'admin.customize.theme.variable_name'}}
{{input id="name" value=name}}<br>
</label>
</div> </div>
{{/d-modal-body}} {{/d-modal-body}}
<div class="modal-footer"> <div class="modal-footer">
{{d-button action="upload" disabled=loading class='btn btn-primary' icon='upload' label='admin.customize.theme.upload'}} {{d-button action="upload" disabled=disabled class='btn btn-primary' icon='upload' label='admin.customize.theme.upload'}}
<a href {{action "closeModal"}}>{{i18n 'cancel'}}</a> <a href {{action "closeModal"}}>{{i18n 'cancel'}}</a>
</div> </div>

View File

@ -193,15 +193,21 @@
margin-left: 0; margin-left: 0;
li { li {
display: table-row; display: table-row;
.first { .col:first-child {
padding-right: 8px; padding-right: 10px;
padding-bottom: 10px; padding-bottom: 10px;
min-width: 100px; min-width: 80px;
} }
.first, .second { .col {
display: table-cell; display: table-cell;
} }
} }
} }
} }
.add-upload-modal {
label {
margin-top: 20px;
}
}

View File

@ -13,7 +13,7 @@ class Admin::ThemesController < Admin::AdminController
File.open(path) do |file| File.open(path) do |file|
upload = Upload.create_for(current_user.id, upload = Upload.create_for(current_user.id,
file, file,
params[:original_filename] || File.basename(path), params[:file]&.original_filename || File.basename(path),
File.size(path), File.size(path),
for_theme: true) for_theme: true)
if upload.errors.count > 0 if upload.errors.count > 0

View File

@ -2845,12 +2845,13 @@ en:
no_uploads: "You can upload assets associated with your theme such as fonts and images" no_uploads: "You can upload assets associated with your theme such as fonts and images"
add_upload: "Add Upload" add_upload: "Add Upload"
upload_file_tip: "Choose an asset to upload (png, woff2, etc...)" upload_file_tip: "Choose an asset to upload (png, woff2, etc...)"
variable_name: "SCSS var name:"
upload: "Upload" upload: "Upload"
child_themes_check: "Theme includes other child themes" child_themes_check: "Theme includes other child themes"
css_html: "Custom CSS/HTML" css_html: "Custom CSS/HTML"
edit_css_html: "Edit CSS/HTML" edit_css_html: "Edit CSS/HTML"
edit_css_html_help: "You have not edited any CSS or 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_web_tip: "Repository containing theme"
import_file_tip: ".dcstyle.json file containing theme" import_file_tip: ".dcstyle.json file containing theme"
about_theme: "About Theme" about_theme: "About Theme"

View File

@ -22,9 +22,9 @@ describe Admin::ThemesController do
end end
it 'can create a theme upload' do 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) 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(upload.id).not_to be_nil
expect(JSON.parse(response.body)["upload_id"]).to eq(upload.id) expect(JSON.parse(response.body)["upload_id"]).to eq(upload.id)
end end

View File

@ -27,4 +27,5 @@ HTML
expect(field.error).to eq(nil) expect(field.error).to eq(nil)
end end
end end