0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-10 12:22:54 +01:00
discourse-custom-wizard/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6

124 Zeilen
2,9 KiB
Text

import { ajax } from 'discourse/lib/ajax';
2019-08-07 10:18:46 +02:00
import { default as computed } from 'ember-addons/ember-computed-decorators';
export default Ember.Controller.extend({
init() {
this._super();
2019-08-07 10:18:46 +02:00
this.setProperties({
selected: Ember.A()
});
},
2019-08-07 10:18:46 +02:00
@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: {
checkChanged(event) {
2019-08-07 10:18:46 +02:00
this.set('exportMessage', '');
let selected = this.get('selected');
2019-08-07 10:18:46 +02:00
if (event.target.checked) {
2019-08-07 10:18:46 +02:00
selected.addObject(event.target.id);
} else if (!event.target.checked) {
2019-08-07 10:18:46 +02:00
selected.removeObject(event.target.id);
}
2019-08-07 10:18:46 +02:00
this.set('selected', selected);
},
export() {
2019-08-07 10:18:46 +02:00
const wizards = this.get('selected');
if (!wizards.length) {
2019-08-07 10:18:46 +02:00
this.set('exportMessage', I18n.t("admin.wizard.transfer.export.none_selected"));
} else {
2019-08-07 10:18:46 +02:00
this.set('exportMessage', '');
let url = Discourse.BaseUrl;
let route = '/admin/wizards/transfer/export';
url += route + '?';
wizards.forEach((wizard) => {
let step = 'wizards[]=' + wizard;
step += '&';
2019-08-07 10:18:46 +02:00
url += step;
});
2019-08-07 10:18:46 +02:00
location.href = url;
}
},
setFilePath(event) {
2019-08-07 10:18:46 +02:00
this.set('importMessage', '');
// 512 kb is the max file size
let maxFileSize = 512 * 1024;
2019-08-07 10:18:46 +02:00
if (event.target.files[0] === undefined) {
2019-08-07 10:18:46 +02:00
this.set('filePath', null);
return;
}
2019-08-07 10:18:46 +02:00
if (maxFileSize < event.target.files[0].size) {
2019-08-07 12:16:05 +02:00
this.setProperties({
importMessage: I18n.t('admin.wizard.transfer.import.file_size_error'),
filePath: null
});
$('#file-url').val('');
} else {
2019-08-07 10:18:46 +02:00
this.set('filePath', event.target.files[0]);
}
},
import() {
2019-08-07 10:18:46 +02:00
const filePath = this.get('filePath');
let $formData = new FormData();
2019-08-07 10:18:46 +02:00
if (filePath) {
$formData.append('file', filePath);
ajax('/admin/wizards/transfer/import', {
type: 'POST',
data: $formData,
processData: false,
contentType: false,
}).then(result => {
if (result.error) {
2019-08-07 12:16:05 +02:00
this.set('importMessage', result.error);
} else {
2019-08-07 10:18:46 +02:00
this.setProperties({
successIds: result.success,
failureIds: result.failed,
fileName: $('#file-url')[0].files[0].name
});
}
2019-08-07 10:18:46 +02:00
this.set('filePath', null);
$('#file-url').val('');
});
} else {
2019-08-07 10:18:46 +02:00
this.set('importMessage', I18n.t("admin.wizard.transfer.import.no_file"));
}
}
}
});