This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/controllers/preferences/interface.js.es6
OsamaSayegh decf1f27cf FEATURE: Groundwork for user-selectable theme components
* Phase 0 for user-selectable theme components

- Drops `key` column from the `themes` table
- Drops `theme_key` column from the `user_options` table
- Adds `theme_ids` (array of ints default []) column to the `user_options` table and migrates data from `theme_key` to the new column.
- Removes the `default_theme_key` site setting and adds `default_theme_id` instead.
- Replaces `theme_key` cookie with a new one called `theme_ids`
- no longer need Theme.settings_for_client
2018-07-12 14:18:21 +10:00

119 lines
2.7 KiB
JavaScript

import PreferencesTabController from "discourse/mixins/preferences-tab-controller";
import { setDefaultHomepage } from "discourse/lib/utilities";
import {
default as computed,
observes
} from "ember-addons/ember-computed-decorators";
import {
currentThemeId,
listThemes,
previewTheme,
setLocalTheme
} from "discourse/lib/theme-selector";
import { popupAjaxError } from "discourse/lib/ajax-error";
const USER_HOMES = {
1: "latest",
2: "categories",
3: "unread",
4: "new",
5: "top"
};
export default Ember.Controller.extend(PreferencesTabController, {
@computed("makeThemeDefault")
saveAttrNames(makeDefault) {
let attrs = [
"locale",
"external_links_in_new_tab",
"dynamic_favicon",
"enable_quoting",
"disable_jump_reply",
"automatically_unpin_topics",
"allow_private_messages",
"homepage_id"
];
if (makeDefault) {
attrs.push("theme_ids");
}
return attrs;
},
preferencesController: Ember.inject.controller("preferences"),
makeThemeDefault: true,
@computed()
availableLocales() {
return JSON.parse(this.siteSettings.available_locales);
},
@computed()
themeId() {
return currentThemeId();
},
userSelectableThemes: function() {
return listThemes(this.site);
}.property(),
@computed("userSelectableThemes")
showThemeSelector(themes) {
return themes && themes.length > 1;
},
@observes("themeId")
themeIdChanged() {
const id = this.get("themeId");
previewTheme(id);
},
homeChanged() {
const siteHome = this.siteSettings.top_menu.split("|")[0].split(",")[0];
const userHome = USER_HOMES[this.get("model.user_option.homepage_id")];
setDefaultHomepage(userHome || siteHome);
},
@computed()
userSelectableHome() {
let homeValues = _.invert(USER_HOMES);
let result = [];
this.siteSettings.top_menu.split("|").forEach(m => {
let id = homeValues[m];
if (id) {
result.push({ name: I18n.t(`filters.${m}.title`), value: Number(id) });
}
});
return result;
},
actions: {
save() {
this.set("saved", false);
const makeThemeDefault = this.get("makeThemeDefault");
if (makeThemeDefault) {
this.set("model.user_option.theme_ids", [this.get("themeId")]);
}
return this.get("model")
.save(this.get("saveAttrNames"))
.then(() => {
this.set("saved", true);
if (!makeThemeDefault) {
setLocalTheme(
[this.get("themeId")],
this.get("model.user_option.theme_key_seq")
);
}
this.homeChanged();
})
.catch(popupAjaxError);
}
}
});