Files
mattermost/webapp/stores/preference_store.jsx
Harrison Healey 167dd22eef PLT-1752/PLT-3567/PLT-3998 Highlighting links in search, unit tests for autolinking (#3865)
* Added highlighting to links when their URL includes the search term

* Decoupling UserStore from react-router to allow for unit tests involving it

* PLT-3998 Added SiteURL as an option to be passed into the text formatting code

* Removed reference to PreferenceStore and window from TextFormatting

* Refactored TextFormatting to remove remaining browser-only code

* Updated ChannelHeader and MessageWrapper to match the changes to TextFormatting

* Increased max listeners for Preference and Emoji stores

* PLT-3832 Added automated unit tests for autolinking

* PLT-3567 Rerender posts when mention keywords change

* Updated RHS and search to match the changes to TextFormatting

* Broke TextFormatting's dependency on the UserStore
2016-08-29 09:50:00 -04:00

148 lines
4.0 KiB
JavaScript

// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import Constants from 'utils/constants.jsx';
const ActionTypes = Constants.ActionTypes;
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
import EventEmitter from 'events';
const CHANGE_EVENT = 'change';
class PreferenceStore extends EventEmitter {
constructor() {
super();
this.handleEventPayload = this.handleEventPayload.bind(this);
this.dispatchToken = AppDispatcher.register(this.handleEventPayload);
this.preferences = new Map();
this.setMaxListeners(600);
}
getKey(category, name) {
return `${category}--${name}`;
}
get(category, name, defaultValue = '') {
const key = this.getKey(category, name);
if (!this.preferences.has(key)) {
return defaultValue;
}
return this.preferences.get(key);
}
getBool(category, name, defaultValue = false) {
const key = this.getKey(category, name);
if (!this.preferences.has(key)) {
return defaultValue;
}
return this.preferences.get(key) !== 'false';
}
getInt(category, name, defaultValue = 0) {
const key = this.getKey(category, name);
if (!this.preferences.has(key)) {
return defaultValue;
}
return parseInt(this.preferences.get(key), 10);
}
getObject(category, name, defaultValue = null) {
const key = this.getKey(category, name);
if (!this.preferences.has(key)) {
return defaultValue;
}
return JSON.parse(this.preferences.get(key));
}
getCategory(category) {
const prefix = category + '--';
const preferences = new Map();
for (const [key, value] of this.preferences) {
if (key.startsWith(prefix)) {
preferences.set(key.substring(prefix.length), value);
}
}
return preferences;
}
setPreference(category, name, value) {
this.preferences.set(this.getKey(category, name), value);
}
setPreferencesFromServer(newPreferences) {
for (const preference of newPreferences) {
this.setPreference(preference.category, preference.name, preference.value);
}
}
deletePreference(preference) {
this.preferences.delete(this.getKey(preference.category, preference.name));
}
clear() {
this.preferences.clear();
}
emitChange(category) {
this.emit(CHANGE_EVENT, category);
}
addChangeListener(callback) {
this.on(CHANGE_EVENT, callback);
}
removeChangeListener(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
getTheme(teamId) {
if (this.preferences.has(this.getKey(Constants.Preferences.CATEGORY_THEME, teamId))) {
return this.getObject(Constants.Preferences.CATEGORY_THEME, teamId);
}
if (this.preferences.has(this.getKey(Constants.Preferences.CATEGORY_THEME, ''))) {
return this.getObject(Constants.Preferences.CATEGORY_THEME, '');
}
return Constants.THEMES.default;
}
handleEventPayload(payload) {
const action = payload.action;
switch (action.type) {
case ActionTypes.RECEIVED_PREFERENCE: {
const preference = action.preference;
this.setPreference(preference.category, preference.name, preference.value);
this.emitChange(preference.category);
break;
}
case ActionTypes.RECEIVED_PREFERENCES:
this.setPreferencesFromServer(action.preferences);
this.emitChange();
break;
case ActionTypes.DELETED_PREFERENCES:
for (const preference of action.preferences) {
this.deletePreference(preference);
}
this.emitChange();
break;
}
}
}
global.PreferenceStore = new PreferenceStore();
export default global.PreferenceStore;