2017-04-12 22:52:52 +08:00
|
|
|
import { default as computed } from 'ember-addons/ember-computed-decorators';
|
|
|
|
import { url } from 'discourse/lib/computed';
|
2017-04-18 04:55:53 +08:00
|
|
|
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
2017-05-10 05:20:28 +08:00
|
|
|
import showModal from 'discourse/lib/show-modal';
|
|
|
|
|
|
|
|
const THEME_UPLOAD_VAR = 2;
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
|
|
|
|
2017-04-18 03:56:13 +08:00
|
|
|
@computed("model", "allThemes")
|
|
|
|
parentThemes(model, allThemes) {
|
|
|
|
let parents = allThemes.filter(theme =>
|
|
|
|
_.contains(theme.get("childThemes"), model));
|
|
|
|
return parents.length === 0 ? null : parents;
|
|
|
|
},
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
@computed("model.theme_fields.@each")
|
|
|
|
hasEditedFields(fields) {
|
|
|
|
return fields.any(f=>!Em.isBlank(f.value));
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed('model.theme_fields.@each')
|
|
|
|
editedDescriptions(fields) {
|
|
|
|
let descriptions = [];
|
|
|
|
let description = target => {
|
|
|
|
let current = fields.filter(field => field.target === target && !Em.isBlank(field.value));
|
|
|
|
if (current.length > 0) {
|
|
|
|
let text = I18n.t('admin.customize.theme.'+target);
|
|
|
|
let localized = current.map(f=>I18n.t('admin.customize.theme.'+f.name + '.text'));
|
|
|
|
return text + ": " + localized.join(" , ");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
['common','desktop','mobile'].forEach(target=> {
|
|
|
|
descriptions.push(description(target));
|
|
|
|
});
|
|
|
|
return descriptions.reject(d=>Em.isBlank(d));
|
|
|
|
},
|
|
|
|
|
2017-04-15 01:35:12 +08:00
|
|
|
previewUrl: url('model.id', '/admin/themes/%@/preview'),
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
@computed("colorSchemeId", "model.color_scheme_id")
|
|
|
|
colorSchemeChanged(colorSchemeId, existingId) {
|
|
|
|
colorSchemeId = colorSchemeId === null ? null : parseInt(colorSchemeId);
|
|
|
|
return colorSchemeId !== existingId;
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("availableChildThemes", "model.childThemes.@each", "model", "allowChildThemes")
|
|
|
|
selectableChildThemes(available, childThemes, model, allowChildThemes) {
|
|
|
|
if (!allowChildThemes && (!childThemes || childThemes.length === 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let themes = [];
|
|
|
|
available.forEach(t=> {
|
|
|
|
if (!childThemes || (childThemes.indexOf(t) === -1)) {
|
|
|
|
themes.push(t);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return themes.length === 0 ? null : themes;
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("allThemes", "allThemes.length", "model")
|
|
|
|
availableChildThemes(allThemes, count) {
|
|
|
|
if (count === 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let excludeIds = [this.get("model.id")];
|
|
|
|
|
|
|
|
let themes = [];
|
|
|
|
allThemes.forEach(theme => {
|
|
|
|
if (excludeIds.indexOf(theme.get("id")) === -1) {
|
|
|
|
themes.push(theme);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return themes;
|
|
|
|
},
|
|
|
|
|
|
|
|
downloadUrl: url('model.id', '/admin/themes/%@'),
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
|
|
|
|
updateToLatest() {
|
|
|
|
this.set("updatingRemote", true);
|
2017-04-18 04:55:53 +08:00
|
|
|
this.get("model").updateToLatest()
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(()=>{
|
|
|
|
this.set("updatingRemote", false);
|
|
|
|
});
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
checkForThemeUpdates() {
|
|
|
|
this.set("updatingRemote", true);
|
2017-04-18 04:55:53 +08:00
|
|
|
this.get("model").checkForUpdates()
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(()=>{
|
|
|
|
this.set("updatingRemote", false);
|
|
|
|
});
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
addUploadModal() {
|
|
|
|
showModal('admin-add-upload', {admin: true, name: ''});
|
|
|
|
},
|
|
|
|
|
|
|
|
addUpload(info) {
|
|
|
|
let model = this.get("model");
|
|
|
|
model.setField('common', info.name, '', info.upload_id, THEME_UPLOAD_VAR);
|
|
|
|
model.saveChanges('theme_fields').catch(e => popupAjaxError(e));
|
|
|
|
},
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
cancelChangeScheme() {
|
|
|
|
this.set("colorSchemeId", this.get("model.color_scheme_id"));
|
|
|
|
},
|
|
|
|
changeScheme(){
|
|
|
|
let schemeId = this.get("colorSchemeId");
|
|
|
|
this.set("model.color_scheme_id", schemeId === null ? null : parseInt(schemeId));
|
|
|
|
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() {
|
2017-04-13 23:09:31 +08:00
|
|
|
let edit = ()=>this.transitionToRoute('adminCustomizeThemes.edit', this.get('model.id'), 'common', 'scss');
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
if (this.get("model.remote_theme")) {
|
|
|
|
bootbox.confirm(I18n.t("admin.customize.theme.edit_confirm"), result => {
|
|
|
|
if (result) {
|
|
|
|
edit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
edit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
applyDefault() {
|
|
|
|
const model = this.get("model");
|
|
|
|
model.saveChanges("default").then(()=>{
|
|
|
|
if (model.get("default")) {
|
|
|
|
this.get("allThemes").forEach(theme=>{
|
|
|
|
if (theme !== model && theme.get('default')) {
|
|
|
|
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) {
|
|
|
|
this.get("model").removeField(upload);
|
|
|
|
},
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
removeChildTheme(theme) {
|
|
|
|
this.get("model").removeChildTheme(theme);
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|