1
0
Fork 0

client-side improvements

Dieser Commit ist enthalten in:
Angus McLeod 2019-08-07 18:18:46 +10:00
Ursprung 861ab40a5a
Commit ee71719793
4 geänderte Dateien mit 144 neuen und 76 gelöschten Zeilen

Datei anzeigen

@ -1,67 +1,97 @@
import { ajax } from 'discourse/lib/ajax'; import { ajax } from 'discourse/lib/ajax';
import { default as computed } from 'ember-addons/ember-computed-decorators';
export default Ember.Controller.extend({ export default Ember.Controller.extend({
init() { init() {
this._super(); this._super();
this.set('selected', Ember.A()); this.setProperties({
this.set('filePath', Ember.A()); selected: Ember.A()
});
}, },
@computed('successIds', 'failureIds')
logs(successIds, failureIds) {
let logs = [];
if (successIds) {
logs.push(...successIds.map(id => {
return { id, type: 'success' };
}));
}
if (failureIds) {
logs.push(...failureIds.map(id => {
return { id, type: 'failure' };
}));
}
return logs;
},
hasLogs: Ember.computed.notEmpty('logs'),
actions: { actions: {
checkChanged(event) { checkChanged(event) {
this.set('noneSelected','') this.set('exportMessage', '');
let selected = this.get('selected'); let selected = this.get('selected');
if (event.target.checked) { if (event.target.checked) {
selected.addObject(event.target.id) selected.addObject(event.target.id);
} else if (!event.target.checked) { } else if (!event.target.checked) {
selected.removeObject(event.target.id) selected.removeObject(event.target.id);
} }
this.set('selected', selected)
this.set('selected', selected);
}, },
export() { export() {
let wizards = this.get('selected'); const wizards = this.get('selected');
if (!wizards.length) {
this.set('exportMessage', I18n.t("admin.wizard.transfer.export.none_selected"));
} else {
this.set('exportMessage', '');
let url = Discourse.BaseUrl; let url = Discourse.BaseUrl;
let route = '/admin/wizards/transfer/export'; let route = '/admin/wizards/transfer/export';
url += route + '?'; url += route + '?';
if (!wizards.length) {
this.set('noneSelected', I18n.t("admin.wizard.transfer.export.noneSelected"))
} else {
this.set('noneSelected', '')
wizards.forEach((wizard) => { wizards.forEach((wizard) => {
let step = 'wizards[]=' + wizard; let step = 'wizards[]=' + wizard;
step += '&'; step += '&';
url += step url += step;
}); });
location.href = url; location.href = url;
} }
}, },
setFilePath(event) { setFilePath(event) {
this.set('noFile', '') this.set('importMessage', '');
// 512 kb is the max file size // 512 kb is the max file size
let maxFileSize = 512 * 1024; let maxFileSize = 512 * 1024;
if (event.target.files[0] === undefined) { if (event.target.files[0] === undefined) {
this.get('filePath').length = 0 this.set('filePath', null);
return return;
} }
if (maxFileSize < event.target.files[0].size) { if (maxFileSize < event.target.files[0].size) {
this.set('fileSizeError', I18n.t('admin.wizard.transfer.import.fileSizeError')) this.set('importMessage', I18n.t('admin.wizard.transfer.import.file_size_error'));
} else { } else {
this.set('fileSizeError', '') this.set('filePath', event.target.files[0]);
// emptying the array as we allow only one file upload at a time
this.get('filePath').length = 0
// interestingly, this.get gives us the actual reference to the object so modifying it
// actually modifies the actual value
this.get('filePath').addObject(event.target.files[0])
} }
}, },
import() { import() {
const filePath = this.get('filePath');
let $formData = new FormData(); let $formData = new FormData();
if (this.get('filePath').length) {
this.set('noFile', '') if (filePath) {
$formData.append('file', this.get('filePath')[0]); $formData.append('file', filePath);
ajax('/admin/wizards/transfer/import', { ajax('/admin/wizards/transfer/import', {
type: 'POST', type: 'POST',
data: $formData, data: $formData,
@ -69,14 +99,20 @@ export default Ember.Controller.extend({
contentType: false, contentType: false,
}).then(result => { }).then(result => {
if (result.error) { if (result.error) {
this.set('error', result.error) this.set('error', result.error);
} else { } else {
this.set('success_ids', result.success) this.setProperties({
this.set('failure_ids', result.failed) successIds: result.success,
failureIds: result.failed,
fileName: $('#file-url')[0].files[0].name
});
} }
})
this.set('filePath', null);
$('#file-url').val('');
});
} else { } else {
this.set('noFile',I18n.t("admin.wizard.transfer.import.noFile")) this.set('importMessage', I18n.t("admin.wizard.transfer.import.no_file"));
} }
} }
} }

Datei anzeigen

@ -1,44 +1,61 @@
<div class="container export"> <div class="container export">
<h2>{{i18n 'admin.wizard.transfer.export.label'}}</h2> <h2>{{i18n 'admin.wizard.transfer.export.label'}}</h2>
<ul class="wizard-list-select"> <ul class="wizard-list-select">
{{#each model as |w|}} {{#each model as |w|}}
<li> <li>
{{input type="checkbox" id=(dasherize w.id) change=(action 'checkChanged')}} {{input type="checkbox"
{{#link-to "adminWizard" (dasherize w.id)}}{{w.name}}{{/link-to}} id=(dasherize w.id)
change=(action 'checkChanged')}}
{{#link-to "adminWizard" (dasherize w.id)}}
{{w.name}}
{{/link-to}}
</li> </li>
{{/each}} {{/each}}
</ul> </ul>
{{d-button id="export-button" class="btn btn-primary side" label="admin.wizard.transfer.export.label" action=(action "export")}}
{{#if this.noneSelected}} {{d-button id="export-button"
<p>{{this.noneSelected}}</p> class="btn btn-primary side"
label="admin.wizard.transfer.export.label"
action=(action "export")}}
{{#if exportMessage}}
<div class="export-message">
{{exportMessage}}
</div>
{{/if}} {{/if}}
</div> </div>
<div class="container import"> <div class="container import">
<h2>{{i18n 'admin.wizard.transfer.import.label'}}</h2> <h2>{{i18n 'admin.wizard.transfer.import.label'}}</h2>
{{input id='file_url' type="file" change=(action "setFilePath")}}
{{d-button id="import-button" class="btn btn-primary side" label="admin.wizard.transfer.import.label" action=(action "import")}}
{{#if this.noFile}}
<p>{{this.noFile}}</p>
{{/if}}
{{#if this.fileSizeError}} <div class="controls">
<p>{{this.fileSizeError}}</p> {{input id='file-url' type="file" change=(action "setFilePath")}}
{{/if}} {{d-button id="import-button"
class="btn btn-primary side"
label="admin.wizard.transfer.import.label"
action=(action "import")}}
{{#if this.success_ids}} {{#if importMessage}}
{{#each this.success_ids as |id|}} <div class="import-message">
<p>{{i18n "admin.wizard.transfer.import.success" id=id}}</p> {{importMessage}}
{{/each}} </div>
{{/if}}
{{#if this.failure_ids}}
{{#each this.failure_ids as |id|}}
<p>{{i18n "admin.wizard.transfer.import.failure" id=id}}</p>
{{/each}}
{{/if}} {{/if}}
</div> </div>
{{!-- server side error--}}
{{#if this.error}} {{#if hasLogs}}
<p>{{this.error}}</p> <div class="import-logs">
<div class="title">
{{i18n 'admin.wizard.transfer.import.logs' fileName=fileName}}
</div>
<ul>
{{#each logs as |l|}}
<li class="import-log">
{{i18n (concat 'admin.wizard.transfer.import.' l.type) id=l.id}}
</li>
{{/each}}
</ul>
</div>
{{/if}} {{/if}}
</div>

Datei anzeigen

@ -482,7 +482,12 @@
} }
// Tansfer tab // Tansfer tab
#file_url {
.admin-wizards-transfer .admin-container .container {
padding-top: 20px;
}
#file-url {
display: block; display: block;
margin-bottom: 10px; margin-bottom: 10px;
} }
@ -495,7 +500,16 @@
flex-direction: column; flex-direction: column;
} }
.container{ .import-logs {
margin-bottom: 20px; margin-top: 20px;
.title {
font-weight: 800;
margin-bottom: 10px;
}
ul {
list-style: none;
}
} }

Datei anzeigen

@ -219,13 +219,14 @@ en:
nav_label: "Transfer" nav_label: "Transfer"
export: export:
label: "Export" label: "Export"
noneSelected: "Please select atleast one wizard" none_selected: "Please select atleast one wizard"
import: import:
label: "Import" label: "Import"
success: "Wizard: {{id}} saved successfully" logs: "Import logs for {{fileName}}"
failure: "Wizard: {{id}} could not be saved" success: 'Wizard "{{id}}" saved successfully'
noFile: "Please choose a file to import" failure: 'Wizard "{{id}}" could not be saved'
fileSizeError: "The file size is too big" no_file: "Please choose a file to import"
file_size_error: "The file size is too big"
wizard_js: wizard_js:
location: location: