discourse/app/assets/javascripts/admin/controllers/modals/admin-import-theme.js.es6

72 lines
2.1 KiB
Plaintext
Raw Normal View History

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