2017-04-12 22:52:52 +08:00
|
|
|
import ModalFunctionality from 'discourse/mixins/modal-functionality';
|
|
|
|
import { ajax } from 'discourse/lib/ajax';
|
2018-03-05 08:04:23 +08:00
|
|
|
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
2018-03-09 13:14:21 +08:00
|
|
|
import { observes } from 'ember-addons/ember-computed-decorators';
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
export default Ember.Controller.extend(ModalFunctionality, {
|
|
|
|
local: Ember.computed.equal('selection', 'local'),
|
|
|
|
remote: Ember.computed.equal('selection', 'remote'),
|
|
|
|
selection: 'local',
|
|
|
|
adminCustomizeThemes: Ember.inject.controller(),
|
2018-03-05 08:04:23 +08:00
|
|
|
loading: false,
|
2018-03-29 06:17:48 +08:00
|
|
|
keyGenUrl: '/admin/themes/generate_key_pair',
|
|
|
|
importUrl: '/admin/themes/import',
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2018-03-09 13:14:21 +08:00
|
|
|
checkPrivate: Ember.computed.match('uploadUrl', /^git/),
|
|
|
|
|
|
|
|
@observes('privateChecked')
|
|
|
|
privateWasChecked() {
|
|
|
|
const checked = this.get('privateChecked');
|
|
|
|
if (checked && !this._keyLoading) {
|
|
|
|
this._keyLoading = true;
|
2018-03-29 06:17:48 +08:00
|
|
|
ajax(this.get('keyGenUrl'), {method: 'POST'})
|
2018-03-09 13:14:21 +08:00
|
|
|
.then(pair => {
|
|
|
|
this.set('privateKey', pair.private_key);
|
|
|
|
this.set('publicKey', pair.public_key);
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(()=>{
|
|
|
|
this._keyLoading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
actions: {
|
|
|
|
importTheme() {
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
type: 'POST'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.get('local')) {
|
|
|
|
options.processData = false;
|
|
|
|
options.contentType = false;
|
|
|
|
options.data = new FormData();
|
|
|
|
options.data.append('theme', $('#file-input')[0].files[0]);
|
|
|
|
} else {
|
2018-03-09 13:14:21 +08:00
|
|
|
options.data = {
|
|
|
|
remote: this.get('uploadUrl')
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.get('privateChecked')){
|
|
|
|
options.data.private_key = this.get('privateKey');
|
|
|
|
}
|
2017-04-12 22:52:52 +08:00
|
|
|
}
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
this.set('loading', true);
|
2018-03-29 06:17:48 +08:00
|
|
|
ajax(this.get('importUrl'), options).then(result=>{
|
2017-04-12 22:52:52 +08:00
|
|
|
const theme = this.store.createRecord('theme',result.theme);
|
|
|
|
this.get('adminCustomizeThemes').send('addTheme', theme);
|
|
|
|
this.send('closeModal');
|
2018-03-09 13:14:21 +08:00
|
|
|
})
|
|
|
|
.then(()=>{
|
|
|
|
this.set('privateKey', null);
|
|
|
|
this.set('publicKey', null);
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => this.set('loading', false));
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|