2017-05-10 05:20:28 +08:00
|
|
|
import ModalFunctionality from 'discourse/mixins/modal-functionality';
|
|
|
|
import { ajax } from 'discourse/lib/ajax';
|
2017-05-11 02:43:05 +08:00
|
|
|
import { default as computed, observes } from 'ember-addons/ember-computed-decorators';
|
2017-05-10 05:20:28 +08:00
|
|
|
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
|
|
|
|
2018-03-29 00:57:11 +08:00
|
|
|
const THEME_FIELD_VARIABLE_TYPE_IDS = [2, 3, 4];
|
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
export default Ember.Controller.extend(ModalFunctionality, {
|
|
|
|
adminCustomizeThemesShow: Ember.inject.controller(),
|
|
|
|
|
2017-05-11 02:43:05 +08:00
|
|
|
onShow() {
|
|
|
|
this.set('name', null);
|
|
|
|
this.set('fileSelected', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
enabled: Em.computed.and('nameValid', 'fileSelected'),
|
|
|
|
disabled: Em.computed.not('enabled'),
|
|
|
|
|
2018-03-29 00:57:11 +08:00
|
|
|
@computed('name', 'adminCustomizeThemesShow.model.theme_fields')
|
|
|
|
nameValid(name, themeFields) {
|
|
|
|
return name &&
|
|
|
|
name.match(/^[a-z_][a-z0-9_-]*$/i) &&
|
|
|
|
!themeFields.some(tf => THEME_FIELD_VARIABLE_TYPE_IDS.includes(tf.type_id) && name === tf.name);
|
2017-05-11 02:43:05 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
@observes('name')
|
2017-12-19 23:10:44 +08:00
|
|
|
uploadChanged() {
|
|
|
|
const file = $('#file-input')[0];
|
2017-05-11 02:43:05 +08:00
|
|
|
this.set('fileSelected', file && file.files[0]);
|
|
|
|
},
|
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
actions: {
|
2017-12-19 23:10:44 +08:00
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
updateName() {
|
|
|
|
let name = this.get('name');
|
|
|
|
if (Em.isEmpty(name)) {
|
|
|
|
name = $('#file-input')[0].files[0].name;
|
|
|
|
this.set('name', name.split(".")[0]);
|
|
|
|
}
|
2017-05-11 02:43:05 +08:00
|
|
|
this.uploadChanged();
|
2017-05-10 05:20:28 +08:00
|
|
|
},
|
2017-12-19 23:10:44 +08:00
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
upload() {
|
2017-12-19 23:10:44 +08:00
|
|
|
const file = $('#file-input')[0].files[0];
|
2017-05-10 05:20:28 +08:00
|
|
|
|
2017-12-19 23:10:44 +08:00
|
|
|
const options = {
|
|
|
|
type: 'POST',
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
data: new FormData()
|
2017-05-10 05:20:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
options.data.append('file', file);
|
|
|
|
|
2017-12-19 23:10:44 +08:00
|
|
|
ajax('/admin/themes/upload_asset', options).then(result => {
|
|
|
|
const upload = {
|
2017-05-10 05:20:28 +08:00
|
|
|
upload_id: result.upload_id,
|
|
|
|
name: this.get('name'),
|
|
|
|
original_filename: file.name
|
|
|
|
};
|
|
|
|
this.get('adminCustomizeThemesShow').send('addUpload', upload);
|
|
|
|
this.send('closeModal');
|
|
|
|
}).catch(e => {
|
|
|
|
popupAjaxError(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|