discourse/app/assets/javascripts/admin/models/theme.js.es6

164 lines
3.8 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import RestModel from "discourse/models/rest";
import { default as computed } from "ember-addons/ember-computed-decorators";
const THEME_UPLOAD_VAR = 2;
const Theme = RestModel.extend({
2018-03-29 05:02:34 +08:00
FIELDS_IDS: [0, 1],
2018-06-15 23:03:24 +08:00
@computed("theme_fields")
themeFields(fields) {
if (!fields) {
2018-06-15 23:03:24 +08:00
this.set("theme_fields", []);
return {};
}
let hash = {};
fields.forEach(field => {
2018-06-15 23:03:24 +08:00
if (!field.type_id || this.get("FIELDS_IDS").includes(field.type_id)) {
hash[this.getKey(field)] = field;
}
});
return hash;
},
2018-06-15 23:03:24 +08:00
@computed("theme_fields", "theme_fields.@each")
uploads(fields) {
if (!fields) {
return [];
}
2018-06-15 23:03:24 +08:00
return fields.filter(
f => f.target === "common" && f.type_id === THEME_UPLOAD_VAR
);
},
2018-06-15 23:03:24 +08:00
getKey(field) {
return `${field.target} ${field.name}`;
},
2018-06-15 23:03:24 +08:00
hasEdited(target, name) {
if (name) {
return !Em.isEmpty(this.getField(target, name));
} else {
let fields = this.get("theme_fields") || [];
2018-06-15 23:03:24 +08:00
return fields.any(
field => field.target === target && !Em.isEmpty(field.value)
);
}
},
getError(target, name) {
let themeFields = this.get("themeFields");
2018-06-15 23:03:24 +08:00
let key = this.getKey({ target, name });
let field = themeFields[key];
return field ? field.error : "";
},
getField(target, name) {
let themeFields = this.get("themeFields");
2018-06-15 23:03:24 +08:00
let key = this.getKey({ target, name });
let field = themeFields[key];
return field ? field.value : "";
},
removeField(field) {
this.set("changed", true);
field.upload_id = null;
field.value = null;
return this.saveChanges("theme_fields");
},
setField(target, name, value, upload_id, type_id) {
this.set("changed", true);
let themeFields = this.get("themeFields");
2018-06-15 23:03:24 +08:00
let field = { name, target, value, upload_id, type_id };
// slow path for uploads and so on
if (type_id && type_id > 1) {
let fields = this.get("theme_fields");
2018-06-15 23:03:24 +08:00
let existing = fields.find(
f => f.target === target && f.name === name && f.type_id === type_id
);
if (existing) {
existing.value = value;
existing.upload_id = upload_id;
} else {
fields.push(field);
}
return;
}
// fast path
2018-06-15 23:03:24 +08:00
let key = this.getKey({ target, name });
let existingField = themeFields[key];
if (!existingField) {
this.theme_fields.push(field);
themeFields[key] = field;
} else {
existingField.value = value;
}
},
@computed("childThemes.@each")
child_theme_ids(childThemes) {
if (childThemes) {
return childThemes.map(theme => Ember.get(theme, "id"));
}
},
removeChildTheme(theme) {
const childThemes = this.get("childThemes");
childThemes.removeObject(theme);
return this.saveChanges("child_theme_ids");
},
2018-06-15 23:03:24 +08:00
addChildTheme(theme) {
let childThemes = this.get("childThemes");
if (!childThemes) {
childThemes = [];
2018-06-15 23:03:24 +08:00
this.set("childThemes", childThemes);
}
childThemes.removeObject(theme);
childThemes.pushObject(theme);
return this.saveChanges("child_theme_ids");
},
2018-06-15 23:03:24 +08:00
@computed("name", "default")
description: function(name, isDefault) {
if (isDefault) {
2018-06-15 23:03:24 +08:00
return I18n.t("admin.customize.theme.default_name", { name: name });
} else {
return name;
}
},
checkForUpdates() {
2018-06-15 23:03:24 +08:00
return this.save({ remote_check: true }).then(() =>
this.set("changed", false)
);
},
updateToLatest() {
2018-06-15 23:03:24 +08:00
return this.save({ remote_update: true }).then(() =>
this.set("changed", false)
);
},
changed: false,
saveChanges() {
const hash = this.getProperties.apply(this, arguments);
2018-06-15 23:03:24 +08:00
return this.save(hash).then(() => this.set("changed", false));
},
saveSettings(name, value) {
const settings = {};
settings[name] = value;
return this.save({ settings });
}
});
export default Theme;