2019-07-27 23:08:22 +02:00
|
|
|
import {ajax} from 'discourse/lib/ajax';
|
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
|
|
|
init() {
|
|
|
|
this._super();
|
2019-07-30 19:04:18 +02:00
|
|
|
this.set('selected', Ember.A());
|
|
|
|
this.set('filePath', Ember.A());
|
2019-07-27 23:08:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
checkChanged(event) {
|
2019-07-29 10:00:56 +02:00
|
|
|
let selected = this.get('selected');
|
2019-07-27 23:08:22 +02:00
|
|
|
if (event.target.checked) {
|
2019-07-30 19:04:18 +02:00
|
|
|
selected.addObject(event.target.id)
|
2019-07-27 23:08:22 +02:00
|
|
|
} else if (!event.target.checked) {
|
2019-07-30 19:04:18 +02:00
|
|
|
selected.removeObject(event.target.id)
|
2019-07-27 23:08:22 +02:00
|
|
|
}
|
2019-07-29 10:00:56 +02:00
|
|
|
console.log(selected);
|
2019-07-27 23:08:22 +02:00
|
|
|
this.set('selected', selected)
|
|
|
|
},
|
|
|
|
|
|
|
|
export() {
|
2019-07-29 10:00:56 +02:00
|
|
|
let wizards = this.get('selected');
|
|
|
|
let url = Discourse.BaseUrl;
|
|
|
|
let route = '/admin/wizards/transfer/export';
|
|
|
|
url += route + '?';
|
2019-07-30 19:04:18 +02:00
|
|
|
if (!wizards.length) {
|
|
|
|
this.set('noneSelected', "Please select atleast one wizard")
|
|
|
|
} else {
|
|
|
|
this.set('noneSelected', '')
|
|
|
|
wizards.forEach((wizard) => {
|
|
|
|
let step = 'wizards[]=' + wizard;
|
|
|
|
step += '&';
|
|
|
|
url += step
|
|
|
|
});
|
|
|
|
location.href = url;
|
|
|
|
}
|
2019-07-27 23:08:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
setFilePath(event) {
|
2019-07-29 10:00:56 +02:00
|
|
|
// 512 kb is the max file size
|
|
|
|
let maxFileSize = 512 * 1024;
|
2019-07-30 19:04:18 +02:00
|
|
|
if (event.target.files[0] === undefined) {
|
|
|
|
this.get('filePath').length = 0
|
|
|
|
return
|
|
|
|
}
|
2019-07-29 10:00:56 +02:00
|
|
|
if (maxFileSize < event.target.files[0].size) {
|
|
|
|
this.set('fileError', 'The file size is too big')
|
|
|
|
} else {
|
2019-07-30 19:04:18 +02:00
|
|
|
// 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])
|
|
|
|
console.log(this.get('filePath'))
|
2019-07-29 10:00:56 +02:00
|
|
|
}
|
2019-07-30 19:04:18 +02:00
|
|
|
},
|
2019-07-27 23:08:22 +02:00
|
|
|
|
|
|
|
import() {
|
2019-07-29 10:00:56 +02:00
|
|
|
let $formData = new FormData();
|
2019-07-30 19:04:18 +02:00
|
|
|
console.log(this.get('filePath'))
|
|
|
|
if (this.get('filePath').length) {
|
|
|
|
this.set('noFile', '')
|
|
|
|
$formData.append('file', this.get('filePath')[0]);
|
|
|
|
console.log($formData);
|
|
|
|
ajax('/admin/wizards/transfer/import', {
|
|
|
|
type: 'POST',
|
|
|
|
data: $formData,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
}).then(result => {
|
|
|
|
if (result.error) {
|
|
|
|
this.set('error', result.error)
|
|
|
|
} else {
|
|
|
|
this.set('success_ids', result.success)
|
|
|
|
this.set('failure_ids', result.failed)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.set('noFile', 'Please choose a file to export')
|
|
|
|
}
|
2019-07-27 23:08:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|