2018-06-15 23:03:24 +08:00
|
|
|
import { url } from "discourse/lib/computed";
|
|
|
|
import {
|
|
|
|
default as computed,
|
|
|
|
observes
|
|
|
|
} from "ember-addons/ember-computed-decorators";
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
|
|
|
maximized: false,
|
|
|
|
section: null,
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
editRouteName: "adminCustomizeThemes.edit",
|
2018-03-29 04:35:24 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
targets: [
|
2018-06-15 23:03:24 +08:00
|
|
|
{ id: 0, name: "common" },
|
|
|
|
{ id: 1, name: "desktop" },
|
|
|
|
{ id: 2, name: "mobile" },
|
|
|
|
{ id: 3, name: "settings" }
|
2017-04-12 22:52:52 +08:00
|
|
|
],
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
fieldsForTarget: function(target) {
|
|
|
|
const common = [
|
|
|
|
"scss",
|
|
|
|
"head_tag",
|
|
|
|
"header",
|
|
|
|
"after_header",
|
|
|
|
"body_tag",
|
|
|
|
"footer"
|
|
|
|
];
|
|
|
|
switch (target) {
|
|
|
|
case "common":
|
|
|
|
return [...common, "embedded_scss"];
|
|
|
|
case "desktop":
|
|
|
|
return common;
|
|
|
|
case "mobile":
|
|
|
|
return common;
|
|
|
|
case "settings":
|
|
|
|
return ["yaml"];
|
2018-03-05 08:04:23 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("onlyOverridden")
|
2017-04-20 03:24:00 +08:00
|
|
|
showCommon() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return this.shouldShow("common");
|
2017-04-20 03:24:00 +08:00
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("onlyOverridden")
|
2017-04-20 03:24:00 +08:00
|
|
|
showDesktop() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return this.shouldShow("desktop");
|
2017-04-20 03:24:00 +08:00
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("onlyOverridden")
|
2017-04-20 03:24:00 +08:00
|
|
|
showMobile() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return this.shouldShow("mobile");
|
2017-04-20 03:24:00 +08:00
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@observes("onlyOverridden")
|
2017-04-20 03:24:00 +08:00
|
|
|
onlyOverriddenChanged() {
|
2018-06-15 23:03:24 +08:00
|
|
|
if (this.get("onlyOverridden")) {
|
|
|
|
if (
|
|
|
|
!this.get("model").hasEdited(
|
|
|
|
this.get("currentTargetName"),
|
|
|
|
this.get("fieldName")
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
let target =
|
|
|
|
(this.get("showCommon") && "common") ||
|
|
|
|
(this.get("showDesktop") && "desktop") ||
|
|
|
|
(this.get("showMobile") && "mobile");
|
|
|
|
|
|
|
|
let fields = this.get("model.theme_fields");
|
|
|
|
let field = fields && fields.find(f => f.target === target);
|
|
|
|
this.replaceRoute(
|
|
|
|
this.get("editRouteName"),
|
|
|
|
this.get("model.id"),
|
|
|
|
target,
|
|
|
|
field && field.name
|
|
|
|
);
|
2017-04-20 03:24:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
shouldShow(target) {
|
|
|
|
if (!this.get("onlyOverridden")) {
|
2017-04-20 03:24:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.get("model").hasEdited(target);
|
|
|
|
},
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
currentTarget: 0,
|
|
|
|
|
|
|
|
setTargetName: function(name) {
|
2018-06-15 23:03:24 +08:00
|
|
|
const target = this.get("targets").find(t => t.name === name);
|
2018-03-05 08:04:23 +08:00
|
|
|
this.set("currentTarget", target && target.id);
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed("currentTarget")
|
2018-03-05 08:04:23 +08:00
|
|
|
currentTargetName(id) {
|
2018-06-15 23:03:24 +08:00
|
|
|
const target = this.get("targets").find(t => t.id === parseInt(id, 10));
|
2018-03-05 08:04:23 +08:00
|
|
|
return target && target.name;
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed("fieldName")
|
|
|
|
activeSectionMode(fieldName) {
|
2018-03-05 08:04:23 +08:00
|
|
|
if (fieldName === "yaml") return "yaml";
|
2017-04-14 00:05:27 +08:00
|
|
|
return fieldName && fieldName.indexOf("scss") > -1 ? "scss" : "html";
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
2017-04-20 04:46:28 +08:00
|
|
|
@computed("currentTargetName", "fieldName", "saving")
|
|
|
|
error(target, fieldName) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return this.get("model").getError(target, fieldName);
|
2017-04-20 04:46:28 +08:00
|
|
|
},
|
|
|
|
|
2017-04-14 04:21:46 +08:00
|
|
|
@computed("fieldName", "currentTargetName")
|
|
|
|
editorId(fieldName, currentTarget) {
|
|
|
|
return fieldName + "|" + currentTarget;
|
|
|
|
},
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
@computed("fieldName", "currentTargetName", "model")
|
|
|
|
activeSection: {
|
|
|
|
get(fieldName, target, model) {
|
|
|
|
return model.getField(target, fieldName);
|
|
|
|
},
|
|
|
|
set(value, fieldName, target, model) {
|
|
|
|
model.setField(target, fieldName, value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
@computed("currentTargetName", "onlyOverridden")
|
2017-04-20 03:24:00 +08:00
|
|
|
fields(target, onlyOverridden) {
|
2018-03-05 08:04:23 +08:00
|
|
|
let fields = this.fieldsForTarget(target);
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2017-04-20 03:24:00 +08:00
|
|
|
if (onlyOverridden) {
|
|
|
|
const model = this.get("model");
|
|
|
|
const targetName = this.get("currentTargetName");
|
|
|
|
fields = fields.filter(name => model.hasEdited(targetName, name));
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
return fields.map(name => {
|
2017-04-12 22:52:52 +08:00
|
|
|
let hash = {
|
2018-06-15 23:03:24 +08:00
|
|
|
key: `admin.customize.theme.${name}.text`,
|
2017-04-12 22:52:52 +08:00
|
|
|
name: name
|
|
|
|
};
|
|
|
|
|
|
|
|
if (name.indexOf("_tag") > 0) {
|
|
|
|
hash.icon = "file-text-o";
|
|
|
|
}
|
|
|
|
|
|
|
|
hash.title = I18n.t(`admin.customize.theme.${name}.title`);
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
previewUrl: url("model.id", "/admin/themes/%@/preview"),
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
maximizeIcon: function() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return this.get("maximized") ? "compress" : "expand";
|
|
|
|
}.property("maximized"),
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
saveButtonText: function() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return this.get("model.isSaving")
|
|
|
|
? I18n.t("saving")
|
|
|
|
: I18n.t("admin.customize.save");
|
|
|
|
}.property("model.isSaving"),
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
saveDisabled: function() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return !this.get("model.changed") || this.get("model.isSaving");
|
|
|
|
}.property("model.changed", "model.isSaving"),
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
actions: {
|
|
|
|
save() {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.set("saving", true);
|
|
|
|
this.get("model")
|
|
|
|
.saveChanges("theme_fields")
|
|
|
|
.finally(() => {
|
|
|
|
this.set("saving", false);
|
|
|
|
});
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleMaximize: function() {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.toggleProperty("maximized");
|
|
|
|
Em.run.next(() => {
|
|
|
|
this.appEvents.trigger("ace:resize");
|
2017-05-16 04:10:07 +08:00
|
|
|
});
|
2017-04-12 22:52:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|