2018-08-08 12:46:34 +08:00
|
|
|
import {
|
|
|
|
default as computed,
|
|
|
|
observes
|
|
|
|
} from "ember-addons/ember-computed-decorators";
|
2018-06-15 23:03:24 +08:00
|
|
|
import { url } from "discourse/lib/computed";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import showModal from "discourse/lib/show-modal";
|
|
|
|
import ThemeSettings from "admin/models/theme-settings";
|
2018-08-31 03:23:15 +08:00
|
|
|
import { THEMES, COMPONENTS } from "admin/models/theme";
|
2017-05-10 05:20:28 +08:00
|
|
|
|
|
|
|
const THEME_UPLOAD_VAR = 2;
|
2018-08-24 09:30:00 +08:00
|
|
|
const SETTINGS_TYPE_ID = 5;
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
2018-06-15 23:03:24 +08:00
|
|
|
editRouteName: "adminCustomizeThemes.edit",
|
2018-03-29 06:17:23 +08:00
|
|
|
|
2018-08-08 12:46:34 +08:00
|
|
|
@observes("allowChildThemes")
|
|
|
|
setSelectedThemeId() {
|
|
|
|
const available = this.get("selectableChildThemes");
|
|
|
|
if (
|
|
|
|
!this.get("selectedChildThemeId") &&
|
|
|
|
available &&
|
|
|
|
available.length > 0
|
|
|
|
) {
|
|
|
|
this.set("selectedChildThemeId", available[0].get("id"));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-08-24 09:30:00 +08:00
|
|
|
@computed("model", "allThemes", "model.component")
|
2017-04-18 03:56:13 +08:00
|
|
|
parentThemes(model, allThemes) {
|
2018-08-24 09:30:00 +08:00
|
|
|
if (!model.get("component")) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-04-18 03:56:13 +08:00
|
|
|
let parents = allThemes.filter(theme =>
|
2018-06-15 23:03:24 +08:00
|
|
|
_.contains(theme.get("childThemes"), model)
|
|
|
|
);
|
2017-04-18 03:56:13 +08:00
|
|
|
return parents.length === 0 ? null : parents;
|
|
|
|
},
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
@computed("model.theme_fields.@each")
|
|
|
|
hasEditedFields(fields) {
|
2018-08-24 09:30:00 +08:00
|
|
|
return fields.any(
|
|
|
|
f => !Em.isBlank(f.value) && f.type_id !== SETTINGS_TYPE_ID
|
|
|
|
);
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("model.theme_fields.@each")
|
2017-04-12 22:52:52 +08:00
|
|
|
editedDescriptions(fields) {
|
|
|
|
let descriptions = [];
|
|
|
|
let description = target => {
|
2018-06-15 23:03:24 +08:00
|
|
|
let current = fields.filter(
|
|
|
|
field => field.target === target && !Em.isBlank(field.value)
|
|
|
|
);
|
2017-04-12 22:52:52 +08:00
|
|
|
if (current.length > 0) {
|
2018-06-15 23:03:24 +08:00
|
|
|
let text = I18n.t("admin.customize.theme." + target);
|
|
|
|
let localized = current.map(f =>
|
|
|
|
I18n.t("admin.customize.theme." + f.name + ".text")
|
|
|
|
);
|
2017-04-12 22:52:52 +08:00
|
|
|
return text + ": " + localized.join(" , ");
|
|
|
|
}
|
|
|
|
};
|
2018-06-15 23:03:24 +08:00
|
|
|
["common", "desktop", "mobile"].forEach(target => {
|
2017-04-12 22:52:52 +08:00
|
|
|
descriptions.push(description(target));
|
|
|
|
});
|
2018-06-15 23:03:24 +08:00
|
|
|
return descriptions.reject(d => Em.isBlank(d));
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
previewUrl: url("model.id", "/admin/themes/%@/preview"),
|
2017-04-15 01:35:12 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
@computed("colorSchemeId", "model.color_scheme_id")
|
|
|
|
colorSchemeChanged(colorSchemeId, existingId) {
|
|
|
|
colorSchemeId = colorSchemeId === null ? null : parseInt(colorSchemeId);
|
2018-06-15 23:03:24 +08:00
|
|
|
return colorSchemeId !== existingId;
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed(
|
|
|
|
"availableChildThemes",
|
|
|
|
"model.childThemes.@each",
|
|
|
|
"model",
|
|
|
|
"allowChildThemes"
|
|
|
|
)
|
2018-08-24 09:30:00 +08:00
|
|
|
selectableChildThemes(available, childThemes, allowChildThemes) {
|
2017-04-12 22:52:52 +08:00
|
|
|
if (!allowChildThemes && (!childThemes || childThemes.length === 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let themes = [];
|
2018-06-15 23:03:24 +08:00
|
|
|
available.forEach(t => {
|
2018-08-24 09:30:00 +08:00
|
|
|
if (!childThemes || childThemes.indexOf(t) === -1) {
|
2017-04-12 22:52:52 +08:00
|
|
|
themes.push(t);
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2017-04-12 22:52:52 +08:00
|
|
|
});
|
|
|
|
return themes.length === 0 ? null : themes;
|
|
|
|
},
|
|
|
|
|
2018-08-24 09:30:00 +08:00
|
|
|
@computed("allThemes", "allThemes.length", "model.component", "model")
|
|
|
|
availableChildThemes(allThemes, count, component) {
|
|
|
|
if (count === 1 || component) {
|
2017-04-12 22:52:52 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-24 09:30:00 +08:00
|
|
|
const themeId = this.get("model.id");
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
let themes = [];
|
|
|
|
allThemes.forEach(theme => {
|
2018-08-24 09:30:00 +08:00
|
|
|
if (themeId !== theme.get("id") && theme.get("component")) {
|
2017-04-12 22:52:52 +08:00
|
|
|
themes.push(theme);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return themes;
|
|
|
|
},
|
|
|
|
|
2018-08-24 09:30:00 +08:00
|
|
|
@computed("model.component")
|
2018-08-31 03:23:15 +08:00
|
|
|
convertKey(component) {
|
2018-08-24 09:30:00 +08:00
|
|
|
const type = component ? "component" : "theme";
|
2018-08-31 03:23:15 +08:00
|
|
|
return `admin.customize.theme.convert_${type}`;
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("model.component")
|
|
|
|
convertIcon(component) {
|
|
|
|
return component ? "cube" : "";
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("model.component")
|
|
|
|
convertTooltip(component) {
|
|
|
|
const type = component ? "component" : "theme";
|
|
|
|
return `admin.customize.theme.convert_${type}_tooltip`;
|
2018-08-24 09:30:00 +08:00
|
|
|
},
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
@computed("model.settings")
|
|
|
|
settings(settings) {
|
|
|
|
return settings.map(setting => ThemeSettings.create(setting));
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("settings")
|
|
|
|
hasSettings(settings) {
|
|
|
|
return settings.length > 0;
|
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
downloadUrl: url("model.id", "/admin/themes/%@"),
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2018-08-31 03:23:15 +08:00
|
|
|
commitSwitchType() {
|
|
|
|
const model = this.get("model");
|
|
|
|
const newValue = !model.get("component");
|
|
|
|
model.set("component", newValue);
|
|
|
|
|
|
|
|
if (newValue) {
|
|
|
|
// component
|
|
|
|
this.set("parentController.currentTab", COMPONENTS);
|
|
|
|
} else {
|
|
|
|
this.set("parentController.currentTab", THEMES);
|
|
|
|
}
|
|
|
|
|
|
|
|
model
|
|
|
|
.saveChanges("component")
|
|
|
|
.then(() => {
|
|
|
|
this.set("colorSchemeId", null);
|
|
|
|
|
|
|
|
model.setProperties({
|
|
|
|
default: false,
|
|
|
|
color_scheme_id: null,
|
|
|
|
user_selectable: false,
|
|
|
|
child_themes: [],
|
|
|
|
childThemes: []
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get("parentController.model.content").forEach(theme => {
|
|
|
|
const children = Array.from(theme.get("childThemes"));
|
|
|
|
const rawChildren = Array.from(theme.get("child_themes") || []);
|
|
|
|
const index = children ? children.indexOf(model) : -1;
|
|
|
|
if (index > -1) {
|
|
|
|
children.splice(index, 1);
|
|
|
|
rawChildren.splice(index, 1);
|
|
|
|
theme.setProperties({
|
|
|
|
childThemes: children,
|
|
|
|
child_themes: rawChildren
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
},
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
actions: {
|
|
|
|
updateToLatest() {
|
|
|
|
this.set("updatingRemote", true);
|
2018-06-15 23:03:24 +08:00
|
|
|
this.get("model")
|
|
|
|
.updateToLatest()
|
2017-04-18 04:55:53 +08:00
|
|
|
.catch(popupAjaxError)
|
2018-06-15 23:03:24 +08:00
|
|
|
.finally(() => {
|
2017-04-18 04:55:53 +08:00
|
|
|
this.set("updatingRemote", false);
|
|
|
|
});
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
checkForThemeUpdates() {
|
|
|
|
this.set("updatingRemote", true);
|
2018-06-15 23:03:24 +08:00
|
|
|
this.get("model")
|
|
|
|
.checkForUpdates()
|
2017-04-18 04:55:53 +08:00
|
|
|
.catch(popupAjaxError)
|
2018-06-15 23:03:24 +08:00
|
|
|
.finally(() => {
|
2017-04-18 04:55:53 +08:00
|
|
|
this.set("updatingRemote", false);
|
|
|
|
});
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
addUploadModal() {
|
2018-06-15 23:03:24 +08:00
|
|
|
showModal("admin-add-upload", { admin: true, name: "" });
|
2017-05-10 05:20:28 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addUpload(info) {
|
|
|
|
let model = this.get("model");
|
2018-06-15 23:03:24 +08:00
|
|
|
model.setField("common", info.name, "", info.upload_id, THEME_UPLOAD_VAR);
|
|
|
|
model.saveChanges("theme_fields").catch(e => popupAjaxError(e));
|
2017-05-10 05:20:28 +08:00
|
|
|
},
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
cancelChangeScheme() {
|
|
|
|
this.set("colorSchemeId", this.get("model.color_scheme_id"));
|
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
changeScheme() {
|
2017-04-12 22:52:52 +08:00
|
|
|
let schemeId = this.get("colorSchemeId");
|
2018-06-15 23:03:24 +08:00
|
|
|
this.set(
|
|
|
|
"model.color_scheme_id",
|
|
|
|
schemeId === null ? null : parseInt(schemeId)
|
|
|
|
);
|
2017-04-12 22:52:52 +08:00
|
|
|
this.get("model").saveChanges("color_scheme_id");
|
|
|
|
},
|
|
|
|
startEditingName() {
|
|
|
|
this.set("oldName", this.get("model.name"));
|
|
|
|
this.set("editingName", true);
|
|
|
|
},
|
|
|
|
cancelEditingName() {
|
|
|
|
this.set("model.name", this.get("oldName"));
|
|
|
|
this.set("editingName", false);
|
|
|
|
},
|
|
|
|
finishedEditingName() {
|
|
|
|
this.get("model").saveChanges("name");
|
|
|
|
this.set("editingName", false);
|
|
|
|
},
|
|
|
|
|
|
|
|
editTheme() {
|
2018-06-15 23:03:24 +08:00
|
|
|
let edit = () =>
|
|
|
|
this.transitionToRoute(
|
|
|
|
this.get("editRouteName"),
|
|
|
|
this.get("model.id"),
|
|
|
|
"common",
|
|
|
|
"scss"
|
|
|
|
);
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
if (this.get("model.remote_theme")) {
|
2018-06-15 23:03:24 +08:00
|
|
|
bootbox.confirm(
|
|
|
|
I18n.t("admin.customize.theme.edit_confirm"),
|
|
|
|
result => {
|
|
|
|
if (result) {
|
|
|
|
edit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2017-04-12 22:52:52 +08:00
|
|
|
} else {
|
|
|
|
edit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
applyDefault() {
|
|
|
|
const model = this.get("model");
|
2018-06-15 23:03:24 +08:00
|
|
|
model.saveChanges("default").then(() => {
|
2017-04-12 22:52:52 +08:00
|
|
|
if (model.get("default")) {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.get("allThemes").forEach(theme => {
|
|
|
|
if (theme !== model && theme.get("default")) {
|
2017-04-12 22:52:52 +08:00
|
|
|
theme.set("default", false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
applyUserSelectable() {
|
|
|
|
this.get("model").saveChanges("user_selectable");
|
|
|
|
},
|
|
|
|
|
|
|
|
addChildTheme() {
|
|
|
|
let themeId = parseInt(this.get("selectedChildThemeId"));
|
|
|
|
let theme = this.get("allThemes").findBy("id", themeId);
|
|
|
|
this.get("model").addChildTheme(theme);
|
|
|
|
},
|
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
removeUpload(upload) {
|
2017-05-11 02:43:05 +08:00
|
|
|
return bootbox.confirm(
|
2018-06-15 23:03:24 +08:00
|
|
|
I18n.t("admin.customize.theme.delete_upload_confirm"),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
result => {
|
|
|
|
if (result) {
|
|
|
|
this.get("model").removeField(upload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2017-05-10 05:20:28 +08:00
|
|
|
},
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
removeChildTheme(theme) {
|
|
|
|
this.get("model").removeChildTheme(theme);
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return bootbox.confirm(
|
|
|
|
I18n.t("admin.customize.delete_confirm"),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
result => {
|
|
|
|
if (result) {
|
|
|
|
const model = this.get("model");
|
|
|
|
model.destroyRecord().then(() => {
|
|
|
|
this.get("allThemes").removeObject(model);
|
|
|
|
this.transitionToRoute("adminCustomizeThemes");
|
|
|
|
});
|
|
|
|
}
|
2017-04-12 22:52:52 +08:00
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2018-08-24 09:30:00 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
switchType() {
|
2018-08-31 03:23:15 +08:00
|
|
|
const relatives = this.get("model.component")
|
|
|
|
? this.get("parentThemes")
|
|
|
|
: this.get("model.childThemes");
|
|
|
|
if (relatives && relatives.length > 0) {
|
|
|
|
const names = relatives.map(relative => relative.get("name"));
|
|
|
|
bootbox.confirm(
|
|
|
|
I18n.t(`${this.get("convertKey")}_alert`, {
|
|
|
|
relatives: names.join(", ")
|
|
|
|
}),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
result => {
|
|
|
|
if (result) {
|
|
|
|
this.commitSwitchType();
|
|
|
|
}
|
2018-08-24 09:30:00 +08:00
|
|
|
}
|
2018-08-31 03:23:15 +08:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.commitSwitchType();
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2017-04-12 22:52:52 +08:00
|
|
|
}
|
|
|
|
});
|