New PluginAPI for widget settings

This commit is contained in:
Robin Ward
2016-02-26 14:26:29 -05:00
parent 98ab64dc89
commit 627ef54efe
4 changed files with 88 additions and 8 deletions

View File

@@ -5,7 +5,7 @@ import { addButton } from 'discourse/widgets/post-menu';
import { includeAttributes } from 'discourse/lib/transform-post';
import { addToolbarCallback } from 'discourse/components/d-editor';
import { addWidgetCleanCallback } from 'discourse/components/mount-widget';
import { decorateWidget } from 'discourse/widgets/widget';
import { decorateWidget, changeSetting } from 'discourse/widgets/widget';
import { onPageChange } from 'discourse/lib/page-tracker';
class PluginApi {
@@ -241,6 +241,18 @@ class PluginApi {
onPageChange(fn);
}
/**
* Changes a setting associated with a widget. For example, if
* you wanted small avatars in the post stream:
*
* ```javascript
* api.changeWidgetSetting('post-avatar', 'size', 'small');
* ```
*
**/
changeWidgetSetting(widgetName, settingName, newValue) {
changeSetting(widgetName, settingName, newValue);
}
}
let _pluginv01;

View File

@@ -77,12 +77,16 @@ createWidget('reply-to-tab', {
createWidget('post-avatar', {
tagName: 'div.topic-avatar',
settings: {
size: 'large'
},
html(attrs) {
let body;
if (!attrs.user_id) {
body = h('i', { className: 'fa fa-trash-o deleted-user-avatar' });
} else {
body = avatarFor.call(this, 'large', {
body = avatarFor.call(this, this.settings.size, {
template: attrs.avatar_template,
username: attrs.username,
url: attrs.usernameUrl,

View File

@@ -37,6 +37,12 @@ export function applyDecorators(widget, type, attrs, state) {
return [];
}
const _customSettings = {};
export function changeSetting(widgetName, settingName, newValue) {
_customSettings[widgetName] = _customSettings[widgetName] || {};
_customSettings[widgetName][settingName] = newValue;
}
function drawWidget(builder, attrs, state) {
const properties = {};
@@ -113,6 +119,13 @@ export default class Widget {
this.currentUser = container.lookup('current-user:main');
this.store = container.lookup('store:main');
this.appEvents = container.lookup('app-events:main');
if (this.name) {
const custom = _customSettings[this.name];
if (custom) {
Object.keys(custom).forEach(k => this.settings[k] = custom[k]);
}
}
}
defaultState() {