2018-09-08 09:24:11 -04:00
|
|
|
import { default as computed } from "ember-addons/ember-computed-decorators";
|
2018-06-15 11:03:24 -04: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-30 15:23:15 -04:00
|
|
|
import { THEMES, COMPONENTS } from "admin/models/theme";
|
2017-05-09 17:20:28 -04:00
|
|
|
|
|
|
|
const THEME_UPLOAD_VAR = 2;
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
2019-01-23 09:40:21 -05:00
|
|
|
downloadUrl: url("model.id", "/admin/customize/themes/%@/export"),
|
2018-09-06 14:56:00 -04:00
|
|
|
previewUrl: url("model.id", "/admin/themes/%@/preview"),
|
2019-01-11 11:54:23 -05:00
|
|
|
addButtonDisabled: Ember.computed.empty("selectedChildThemeId"),
|
2018-10-11 08:20:04 -04:00
|
|
|
editRouteName: "adminCustomizeThemes.edit",
|
2018-08-08 00:46:34 -04:00
|
|
|
|
2018-09-06 14:56:00 -04:00
|
|
|
@computed("model.editedFields")
|
2018-09-08 09:24:11 -04:00
|
|
|
editedFieldsFormatted() {
|
2018-09-06 14:56:00 -04:00
|
|
|
const descriptions = [];
|
2018-06-15 11:03:24 -04:00
|
|
|
["common", "desktop", "mobile"].forEach(target => {
|
2018-09-06 14:56:00 -04:00
|
|
|
const fields = this.editedFieldsForTarget(target);
|
|
|
|
if (fields.length < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let resultString = I18n.t("admin.customize.theme." + target);
|
|
|
|
const formattedFields = fields
|
|
|
|
.map(f => I18n.t("admin.customize.theme." + f.name + ".text"))
|
|
|
|
.join(" , ");
|
|
|
|
resultString += `: ${formattedFields}`;
|
|
|
|
descriptions.push(resultString);
|
2017-04-12 10:52:52 -04:00
|
|
|
});
|
2018-09-06 14:56:00 -04:00
|
|
|
return descriptions;
|
2017-04-12 10:52:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed("colorSchemeId", "model.color_scheme_id")
|
|
|
|
colorSchemeChanged(colorSchemeId, existingId) {
|
|
|
|
colorSchemeId = colorSchemeId === null ? null : parseInt(colorSchemeId);
|
2018-06-15 11:03:24 -04:00
|
|
|
return colorSchemeId !== existingId;
|
2017-04-12 10:52:52 -04:00
|
|
|
},
|
|
|
|
|
2019-01-10 05:06:01 -05:00
|
|
|
@computed("availableChildThemes", "model.childThemes.[]", "model")
|
2018-09-06 14:56:00 -04:00
|
|
|
selectableChildThemes(available, childThemes) {
|
|
|
|
if (available) {
|
|
|
|
const themes = !childThemes
|
|
|
|
? available
|
|
|
|
: available.filter(theme => childThemes.indexOf(theme) === -1);
|
|
|
|
return themes.length === 0 ? null : themes;
|
2017-04-12 10:52:52 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-09-06 14:56:00 -04:00
|
|
|
@computed("allThemes", "model.component", "model")
|
|
|
|
availableChildThemes(allThemes) {
|
|
|
|
if (!this.get("model.component")) {
|
|
|
|
const themeId = this.get("model.id");
|
|
|
|
return allThemes.filter(
|
|
|
|
theme => theme.get("id") !== themeId && theme.get("component")
|
|
|
|
);
|
2017-04-12 10:52:52 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-08-23 21:30:00 -04:00
|
|
|
@computed("model.component")
|
2018-08-30 15:23:15 -04:00
|
|
|
convertKey(component) {
|
2018-08-23 21:30:00 -04:00
|
|
|
const type = component ? "component" : "theme";
|
2018-08-30 15:23:15 -04: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-23 21:30:00 -04:00
|
|
|
},
|
|
|
|
|
2018-03-04 19:04:23 -05:00
|
|
|
@computed("model.settings")
|
|
|
|
settings(settings) {
|
|
|
|
return settings.map(setting => ThemeSettings.create(setting));
|
|
|
|
},
|
|
|
|
|
2019-01-17 06:46:11 -05:00
|
|
|
hasSettings: Ember.computed.notEmpty("settings"),
|
|
|
|
|
|
|
|
@computed("model.translations")
|
|
|
|
translations(translations) {
|
|
|
|
return translations.map(setting => ThemeSettings.create(setting));
|
2018-03-04 19:04:23 -05:00
|
|
|
},
|
2018-09-08 09:24:11 -04:00
|
|
|
|
2019-01-17 06:46:11 -05:00
|
|
|
hasTranslations: Ember.computed.notEmpty("translations"),
|
|
|
|
|
2018-09-08 09:24:11 -04:00
|
|
|
@computed("model.remoteError", "updatingRemote")
|
|
|
|
showRemoteError(errorMessage, updating) {
|
|
|
|
return errorMessage && !updating;
|
|
|
|
},
|
|
|
|
|
2018-09-06 14:56:00 -04:00
|
|
|
editedFieldsForTarget(target) {
|
|
|
|
return this.get("model.editedFields").filter(
|
|
|
|
field => field.target === target
|
|
|
|
);
|
|
|
|
},
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-08-30 15:23:15 -04:00
|
|
|
commitSwitchType() {
|
2019-05-27 04:15:39 -04:00
|
|
|
const model = this.model;
|
2018-08-30 15:23:15 -04:00
|
|
|
const newValue = !model.get("component");
|
|
|
|
model.set("component", newValue);
|
|
|
|
|
|
|
|
if (newValue) {
|
|
|
|
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 => {
|
2018-11-26 05:15:05 -05:00
|
|
|
const children = Ember.makeArray(theme.get("childThemes"));
|
|
|
|
const rawChildren = Ember.makeArray(theme.get("child_themes"));
|
2018-08-30 15:23:15 -04:00
|
|
|
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);
|
|
|
|
},
|
2018-09-06 14:56:00 -04:00
|
|
|
transitionToEditRoute() {
|
|
|
|
this.transitionToRoute(
|
2019-05-27 04:15:39 -04:00
|
|
|
this.editRouteName,
|
2018-09-06 14:56:00 -04:00
|
|
|
this.get("model.id"),
|
|
|
|
"common",
|
|
|
|
"scss"
|
|
|
|
);
|
|
|
|
},
|
2019-02-28 12:01:59 -05:00
|
|
|
sourceIsHttp: Ember.computed.match(
|
|
|
|
"model.remote_theme.remote_url",
|
|
|
|
/^http(s)?:\/\//
|
|
|
|
),
|
2017-04-12 10:52:52 -04:00
|
|
|
actions: {
|
|
|
|
updateToLatest() {
|
|
|
|
this.set("updatingRemote", true);
|
2019-05-27 04:15:39 -04:00
|
|
|
this.model
|
2018-06-15 11:03:24 -04:00
|
|
|
.updateToLatest()
|
2017-04-17 16:55:53 -04:00
|
|
|
.catch(popupAjaxError)
|
2018-06-15 11:03:24 -04:00
|
|
|
.finally(() => {
|
2017-04-17 16:55:53 -04:00
|
|
|
this.set("updatingRemote", false);
|
|
|
|
});
|
2017-04-12 10:52:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
checkForThemeUpdates() {
|
|
|
|
this.set("updatingRemote", true);
|
2019-05-27 04:15:39 -04:00
|
|
|
this.model
|
2018-06-15 11:03:24 -04:00
|
|
|
.checkForUpdates()
|
2017-04-17 16:55:53 -04:00
|
|
|
.catch(popupAjaxError)
|
2018-06-15 11:03:24 -04:00
|
|
|
.finally(() => {
|
2017-04-17 16:55:53 -04:00
|
|
|
this.set("updatingRemote", false);
|
|
|
|
});
|
2017-04-12 10:52:52 -04:00
|
|
|
},
|
|
|
|
|
2017-05-09 17:20:28 -04:00
|
|
|
addUploadModal() {
|
2018-06-15 11:03:24 -04:00
|
|
|
showModal("admin-add-upload", { admin: true, name: "" });
|
2017-05-09 17:20:28 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
addUpload(info) {
|
2019-05-27 04:15:39 -04:00
|
|
|
let model = this.model;
|
2018-06-15 11:03:24 -04:00
|
|
|
model.setField("common", info.name, "", info.upload_id, THEME_UPLOAD_VAR);
|
|
|
|
model.saveChanges("theme_fields").catch(e => popupAjaxError(e));
|
2017-05-09 17:20:28 -04:00
|
|
|
},
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
cancelChangeScheme() {
|
|
|
|
this.set("colorSchemeId", this.get("model.color_scheme_id"));
|
|
|
|
},
|
2018-06-15 11:03:24 -04:00
|
|
|
changeScheme() {
|
2019-05-27 04:15:39 -04:00
|
|
|
let schemeId = this.colorSchemeId;
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set(
|
|
|
|
"model.color_scheme_id",
|
|
|
|
schemeId === null ? null : parseInt(schemeId)
|
|
|
|
);
|
2019-05-27 04:15:39 -04:00
|
|
|
this.model.saveChanges("color_scheme_id");
|
2017-04-12 10:52:52 -04:00
|
|
|
},
|
|
|
|
startEditingName() {
|
|
|
|
this.set("oldName", this.get("model.name"));
|
|
|
|
this.set("editingName", true);
|
|
|
|
},
|
|
|
|
cancelEditingName() {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.set("model.name", this.oldName);
|
2017-04-12 10:52:52 -04:00
|
|
|
this.set("editingName", false);
|
|
|
|
},
|
|
|
|
finishedEditingName() {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.model.saveChanges("name");
|
2017-04-12 10:52:52 -04:00
|
|
|
this.set("editingName", false);
|
|
|
|
},
|
|
|
|
|
|
|
|
editTheme() {
|
2019-01-23 09:40:21 -05:00
|
|
|
if (this.get("model.remote_theme.is_git")) {
|
2018-06-15 11:03:24 -04:00
|
|
|
bootbox.confirm(
|
|
|
|
I18n.t("admin.customize.theme.edit_confirm"),
|
|
|
|
result => {
|
|
|
|
if (result) {
|
2018-09-06 14:56:00 -04:00
|
|
|
this.transitionToEditRoute();
|
2018-06-15 11:03:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2017-04-12 10:52:52 -04:00
|
|
|
} else {
|
2018-09-06 14:56:00 -04:00
|
|
|
this.transitionToEditRoute();
|
2017-04-12 10:52:52 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
applyDefault() {
|
2019-05-27 04:15:39 -04:00
|
|
|
const model = this.model;
|
2018-06-15 11:03:24 -04:00
|
|
|
model.saveChanges("default").then(() => {
|
2017-04-12 10:52:52 -04:00
|
|
|
if (model.get("default")) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.allThemes.forEach(theme => {
|
2018-06-15 11:03:24 -04:00
|
|
|
if (theme !== model && theme.get("default")) {
|
2017-04-12 10:52:52 -04:00
|
|
|
theme.set("default", false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
applyUserSelectable() {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.model.saveChanges("user_selectable");
|
2017-04-12 10:52:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
addChildTheme() {
|
2019-05-27 04:15:39 -04:00
|
|
|
let themeId = parseInt(this.selectedChildThemeId);
|
|
|
|
let theme = this.allThemes.findBy("id", themeId);
|
|
|
|
this.model.addChildTheme(theme);
|
2017-04-12 10:52:52 -04:00
|
|
|
},
|
|
|
|
|
2017-05-09 17:20:28 -04:00
|
|
|
removeUpload(upload) {
|
2017-05-10 14:43:05 -04:00
|
|
|
return bootbox.confirm(
|
2018-06-15 11:03:24 -04:00
|
|
|
I18n.t("admin.customize.theme.delete_upload_confirm"),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
result => {
|
|
|
|
if (result) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.model.removeField(upload);
|
2018-06-15 11:03:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2017-05-09 17:20:28 -04:00
|
|
|
},
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
removeChildTheme(theme) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.model.removeChildTheme(theme);
|
2017-04-12 10:52:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy() {
|
2018-06-15 11:03:24 -04:00
|
|
|
return bootbox.confirm(
|
2019-05-10 14:14:12 -04:00
|
|
|
I18n.t("admin.customize.delete_confirm", {
|
|
|
|
theme_name: this.get("model.name")
|
|
|
|
}),
|
2018-06-15 11:03:24 -04:00
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
result => {
|
|
|
|
if (result) {
|
2019-05-27 04:15:39 -04:00
|
|
|
const model = this.model;
|
2018-06-15 11:03:24 -04:00
|
|
|
model.destroyRecord().then(() => {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.allThemes.removeObject(model);
|
2018-06-15 11:03:24 -04:00
|
|
|
this.transitionToRoute("adminCustomizeThemes");
|
|
|
|
});
|
|
|
|
}
|
2017-04-12 10:52:52 -04:00
|
|
|
}
|
2018-06-15 11:03:24 -04:00
|
|
|
);
|
2018-08-23 21:30:00 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
switchType() {
|
2018-08-30 15:23:15 -04:00
|
|
|
const relatives = this.get("model.component")
|
2019-05-27 04:15:39 -04:00
|
|
|
? this.parentThemes
|
2018-08-30 15:23:15 -04:00
|
|
|
: this.get("model.childThemes");
|
|
|
|
if (relatives && relatives.length > 0) {
|
|
|
|
const names = relatives.map(relative => relative.get("name"));
|
|
|
|
bootbox.confirm(
|
2019-05-27 04:15:39 -04:00
|
|
|
I18n.t(`${this.convertKey}_alert`, {
|
2018-08-30 15:23:15 -04:00
|
|
|
relatives: names.join(", ")
|
|
|
|
}),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
result => {
|
|
|
|
if (result) {
|
|
|
|
this.commitSwitchType();
|
|
|
|
}
|
2018-08-23 21:30:00 -04:00
|
|
|
}
|
2018-08-30 15:23:15 -04:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.commitSwitchType();
|
|
|
|
}
|
2019-07-03 04:18:11 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
enableComponent() {
|
|
|
|
this.model.set("enabled", true);
|
|
|
|
this.model
|
|
|
|
.saveChanges("enabled")
|
|
|
|
.catch(() => this.model.set("enabled", false));
|
|
|
|
},
|
|
|
|
|
|
|
|
disableComponent() {
|
|
|
|
this.model.set("enabled", false);
|
|
|
|
this.model
|
|
|
|
.saveChanges("enabled")
|
|
|
|
.catch(() => this.model.set("enabled", true));
|
2018-06-15 11:03:24 -04:00
|
|
|
}
|
2017-04-12 10:52:52 -04:00
|
|
|
}
|
|
|
|
});
|