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

66 lines
1.7 KiB
Plaintext
Raw Normal View History

import ModalFunctionality from "discourse/mixins/modal-functionality";
import { default as computed } from "ember-addons/ember-computed-decorators";
import { popupAjaxError } from "discourse/lib/ajax-error";
2018-08-31 03:23:15 +08:00
import { THEMES, COMPONENTS } from "admin/models/theme";
2018-08-31 03:23:15 +08:00
const MIN_NAME_LENGTH = 4;
export default Ember.Controller.extend(ModalFunctionality, {
2018-08-31 03:23:15 +08:00
saving: false,
triggerError: false,
themesController: Ember.inject.controller("adminCustomizeThemes"),
types: [
2018-08-31 03:23:15 +08:00
{ name: I18n.t("admin.customize.theme.theme"), value: THEMES },
{ name: I18n.t("admin.customize.theme.component"), value: COMPONENTS }
],
2018-08-31 03:23:15 +08:00
@computed("triggerError", "nameTooShort")
showError(trigger, tooShort) {
return trigger && tooShort;
},
@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("themesController.currentTab")
selectedType(tab) {
return tab;
},
@computed("selectedType")
component(type) {
2018-08-31 03:23:15 +08:00
return type === COMPONENTS;
},
actions: {
createTheme() {
2018-08-31 03:23:15 +08:00
if (this.get("nameTooShort")) {
this.set("triggerError", true);
return;
}
this.set("saving", true);
const theme = this.store.createRecord("theme");
theme
.save({ name: this.get("name"), component: this.get("component") })
.then(() => {
this.get("themesController").send("addTheme", theme);
this.send("closeModal");
})
.catch(popupAjaxError)
2018-08-31 03:23:15 +08:00
.finally(() => this.set("saving", false));
}
}
});