2019-02-21 03:58:31 +08:00
|
|
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import {
|
|
|
|
default as computed,
|
|
|
|
observes
|
|
|
|
} from "ember-addons/ember-computed-decorators";
|
|
|
|
import { THEMES, COMPONENTS } from "admin/models/theme";
|
2019-06-03 22:47:17 +08:00
|
|
|
import { POPULAR_THEMES } from "discourse-common/helpers/popular-themes";
|
2019-02-21 03:58:31 +08:00
|
|
|
|
|
|
|
const MIN_NAME_LENGTH = 4;
|
|
|
|
|
|
|
|
export default Ember.Controller.extend(ModalFunctionality, {
|
|
|
|
popular: Ember.computed.equal("selection", "popular"),
|
|
|
|
local: Ember.computed.equal("selection", "local"),
|
|
|
|
remote: Ember.computed.equal("selection", "remote"),
|
|
|
|
create: Ember.computed.equal("selection", "create"),
|
|
|
|
selection: "popular",
|
|
|
|
adminCustomizeThemes: Ember.inject.controller(),
|
|
|
|
loading: false,
|
|
|
|
keyGenUrl: "/admin/themes/generate_key_pair",
|
|
|
|
importUrl: "/admin/themes/import",
|
2019-02-28 18:48:30 +08:00
|
|
|
recordType: "theme",
|
2019-02-21 03:58:31 +08:00
|
|
|
checkPrivate: Ember.computed.match("uploadUrl", /^git/),
|
|
|
|
localFile: null,
|
|
|
|
uploadUrl: null,
|
|
|
|
urlPlaceholder: "https://github.com/discourse/sample_theme",
|
|
|
|
advancedVisible: false,
|
|
|
|
themesController: Ember.inject.controller("adminCustomizeThemes"),
|
|
|
|
selectedType: Ember.computed.alias("themesController.currentTab"),
|
|
|
|
component: Ember.computed.equal("selectedType", COMPONENTS),
|
|
|
|
|
2019-05-28 18:15:12 +08:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this.createTypes = [
|
|
|
|
{ name: I18n.t("admin.customize.theme.theme"), value: THEMES },
|
|
|
|
{ name: I18n.t("admin.customize.theme.component"), value: COMPONENTS }
|
|
|
|
];
|
|
|
|
},
|
|
|
|
|
2019-02-21 03:58:31 +08:00
|
|
|
@computed("themesController.installedThemes")
|
|
|
|
themes(installedThemes) {
|
|
|
|
return POPULAR_THEMES.map(t => {
|
|
|
|
if (installedThemes.includes(t.name)) {
|
|
|
|
Ember.set(t, "installed", true);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed(
|
|
|
|
"loading",
|
|
|
|
"remote",
|
|
|
|
"uploadUrl",
|
|
|
|
"local",
|
|
|
|
"localFile",
|
|
|
|
"create",
|
|
|
|
"nameTooShort"
|
|
|
|
)
|
|
|
|
installDisabled(
|
|
|
|
isLoading,
|
|
|
|
isRemote,
|
|
|
|
uploadUrl,
|
|
|
|
isLocal,
|
|
|
|
localFile,
|
|
|
|
isCreate,
|
|
|
|
nameTooShort
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
isLoading ||
|
|
|
|
(isRemote && !uploadUrl) ||
|
|
|
|
(isLocal && !localFile) ||
|
|
|
|
(isCreate && nameTooShort)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
@observes("privateChecked")
|
|
|
|
privateWasChecked() {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.privateChecked
|
2019-02-21 03:58:31 +08:00
|
|
|
? this.set("urlPlaceholder", "git@github.com:discourse/sample_theme.git")
|
|
|
|
: this.set("urlPlaceholder", "https://github.com/discourse/sample_theme");
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
const checked = this.privateChecked;
|
2019-02-21 03:58:31 +08:00
|
|
|
if (checked && !this._keyLoading) {
|
|
|
|
this._keyLoading = true;
|
2019-05-27 16:15:39 +08:00
|
|
|
ajax(this.keyGenUrl, { method: "POST" })
|
2019-02-21 03:58:31 +08:00
|
|
|
.then(pair => {
|
2019-02-21 05:22:38 +08:00
|
|
|
this.setProperties({
|
|
|
|
privateKey: pair.private_key,
|
|
|
|
publicKey: pair.public_key
|
|
|
|
});
|
2019-02-21 03:58:31 +08:00
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => {
|
|
|
|
this._keyLoading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("name")
|
|
|
|
nameTooShort(name) {
|
|
|
|
return !name || name.length < MIN_NAME_LENGTH;
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("component")
|
|
|
|
placeholder(component) {
|
|
|
|
if (component) {
|
|
|
|
return I18n.t("admin.customize.theme.component_name");
|
|
|
|
} else {
|
|
|
|
return I18n.t("admin.customize.theme.theme_name");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("selection")
|
|
|
|
submitLabel(selection) {
|
|
|
|
return `admin.customize.theme.${
|
|
|
|
selection === "create" ? "create" : "install"
|
|
|
|
}`;
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("privateChecked", "checkPrivate", "publicKey")
|
|
|
|
showPublicKey(privateChecked, checkPrivate, publicKey) {
|
|
|
|
return privateChecked && checkPrivate && publicKey;
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
uploadLocaleFile() {
|
|
|
|
this.set("localFile", $("#file-input")[0].files[0]);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleAdvanced() {
|
|
|
|
this.toggleProperty("advancedVisible");
|
|
|
|
},
|
|
|
|
|
|
|
|
installThemeFromList(url) {
|
|
|
|
this.set("uploadUrl", url);
|
|
|
|
this.send("installTheme");
|
|
|
|
},
|
|
|
|
|
|
|
|
installTheme() {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.create) {
|
2019-02-21 03:58:31 +08:00
|
|
|
this.set("loading", true);
|
2019-05-27 16:15:39 +08:00
|
|
|
const theme = this.store.createRecord(this.recordType);
|
2019-02-21 03:58:31 +08:00
|
|
|
theme
|
2019-05-27 16:15:39 +08:00
|
|
|
.save({ name: this.name, component: this.component })
|
2019-02-21 03:58:31 +08:00
|
|
|
.then(() => {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.themesController.send("addTheme", theme);
|
2019-02-21 03:58:31 +08:00
|
|
|
this.send("closeModal");
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => this.set("loading", false));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
type: "POST"
|
|
|
|
};
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.local) {
|
2019-02-21 03:58:31 +08:00
|
|
|
options.processData = false;
|
|
|
|
options.contentType = false;
|
|
|
|
options.data = new FormData();
|
2019-05-27 16:15:39 +08:00
|
|
|
options.data.append("theme", this.localFile);
|
2019-02-21 03:58:31 +08:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.remote || this.popular) {
|
2019-02-21 03:58:31 +08:00
|
|
|
options.data = {
|
2019-05-27 16:15:39 +08:00
|
|
|
remote: this.uploadUrl,
|
|
|
|
branch: this.branch
|
2019-02-21 03:58:31 +08:00
|
|
|
};
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.privateChecked) {
|
|
|
|
options.data.private_key = this.privateKey;
|
2019-02-21 03:58:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("model.user_id")) {
|
|
|
|
// Used by theme-creator
|
|
|
|
options.data["user_id"] = this.get("model.user_id");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set("loading", true);
|
2019-05-27 16:15:39 +08:00
|
|
|
ajax(this.importUrl, options)
|
2019-02-21 03:58:31 +08:00
|
|
|
.then(result => {
|
2019-05-27 16:42:53 +08:00
|
|
|
const theme = this.store.createRecord(this.recordType, result.theme);
|
2019-05-27 16:15:39 +08:00
|
|
|
this.adminCustomizeThemes.send("addTheme", theme);
|
2019-02-21 03:58:31 +08:00
|
|
|
this.send("closeModal");
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.setProperties({ privateKey: null, publicKey: null });
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => this.set("loading", false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|