mirror of
https://github.com/discourse/discourse.git
synced 2025-02-07 00:15:15 +08:00
New PluginAPI for widget settings
This commit is contained in:
parent
98ab64dc89
commit
627ef54efe
|
@ -5,7 +5,7 @@ import { addButton } from 'discourse/widgets/post-menu';
|
||||||
import { includeAttributes } from 'discourse/lib/transform-post';
|
import { includeAttributes } from 'discourse/lib/transform-post';
|
||||||
import { addToolbarCallback } from 'discourse/components/d-editor';
|
import { addToolbarCallback } from 'discourse/components/d-editor';
|
||||||
import { addWidgetCleanCallback } from 'discourse/components/mount-widget';
|
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';
|
import { onPageChange } from 'discourse/lib/page-tracker';
|
||||||
|
|
||||||
class PluginApi {
|
class PluginApi {
|
||||||
|
@ -241,6 +241,18 @@ class PluginApi {
|
||||||
onPageChange(fn);
|
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;
|
let _pluginv01;
|
||||||
|
|
|
@ -77,12 +77,16 @@ createWidget('reply-to-tab', {
|
||||||
createWidget('post-avatar', {
|
createWidget('post-avatar', {
|
||||||
tagName: 'div.topic-avatar',
|
tagName: 'div.topic-avatar',
|
||||||
|
|
||||||
|
settings: {
|
||||||
|
size: 'large'
|
||||||
|
},
|
||||||
|
|
||||||
html(attrs) {
|
html(attrs) {
|
||||||
let body;
|
let body;
|
||||||
if (!attrs.user_id) {
|
if (!attrs.user_id) {
|
||||||
body = h('i', { className: 'fa fa-trash-o deleted-user-avatar' });
|
body = h('i', { className: 'fa fa-trash-o deleted-user-avatar' });
|
||||||
} else {
|
} else {
|
||||||
body = avatarFor.call(this, 'large', {
|
body = avatarFor.call(this, this.settings.size, {
|
||||||
template: attrs.avatar_template,
|
template: attrs.avatar_template,
|
||||||
username: attrs.username,
|
username: attrs.username,
|
||||||
url: attrs.usernameUrl,
|
url: attrs.usernameUrl,
|
||||||
|
|
|
@ -37,6 +37,12 @@ export function applyDecorators(widget, type, attrs, state) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const _customSettings = {};
|
||||||
|
export function changeSetting(widgetName, settingName, newValue) {
|
||||||
|
_customSettings[widgetName] = _customSettings[widgetName] || {};
|
||||||
|
_customSettings[widgetName][settingName] = newValue;
|
||||||
|
}
|
||||||
|
|
||||||
function drawWidget(builder, attrs, state) {
|
function drawWidget(builder, attrs, state) {
|
||||||
const properties = {};
|
const properties = {};
|
||||||
|
|
||||||
|
@ -113,6 +119,13 @@ export default class Widget {
|
||||||
this.currentUser = container.lookup('current-user:main');
|
this.currentUser = container.lookup('current-user:main');
|
||||||
this.store = container.lookup('store:main');
|
this.store = container.lookup('store:main');
|
||||||
this.appEvents = container.lookup('app-events: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() {
|
defaultState() {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { moduleForWidget, widgetTest } from 'helpers/widget-test';
|
import { moduleForWidget, widgetTest } from 'helpers/widget-test';
|
||||||
import { decorateWidget, createWidget } from 'discourse/widgets/widget';
|
import { createWidget } from 'discourse/widgets/widget';
|
||||||
|
import { withPluginApi } from 'discourse/lib/plugin-api';
|
||||||
|
|
||||||
moduleForWidget('base');
|
moduleForWidget('base');
|
||||||
|
|
||||||
|
@ -177,12 +178,14 @@ widgetTest('widget decorating', {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
decorateWidget('decorate-test:before', dec => {
|
withPluginApi('0.1', api => {
|
||||||
return dec.h('b', 'before');
|
api.decorateWidget('decorate-test:before', dec => {
|
||||||
});
|
return dec.h('b', 'before');
|
||||||
|
});
|
||||||
|
|
||||||
decorateWidget('decorate-test:after', dec => {
|
api.decorateWidget('decorate-test:after', dec => {
|
||||||
return dec.h('i', 'after');
|
return dec.h('i', 'after');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -192,3 +195,51 @@ widgetTest('widget decorating', {
|
||||||
assert.equal(this.$('.decorate i').text(), 'after');
|
assert.equal(this.$('.decorate i').text(), 'after');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
widgetTest('widget settings', {
|
||||||
|
template: `{{mount-widget widget="settings-test"}}`,
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
createWidget('settings-test', {
|
||||||
|
tagName: 'div.settings',
|
||||||
|
|
||||||
|
settings: {
|
||||||
|
age: 36
|
||||||
|
},
|
||||||
|
|
||||||
|
html() {
|
||||||
|
return `age is ${this.settings.age}`;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
test(assert) {
|
||||||
|
assert.equal(this.$('.settings').text(), 'age is 36');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
widgetTest('override settings', {
|
||||||
|
template: `{{mount-widget widget="ov-settings-test"}}`,
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
createWidget('ov-settings-test', {
|
||||||
|
tagName: 'div.settings',
|
||||||
|
|
||||||
|
settings: {
|
||||||
|
age: 36
|
||||||
|
},
|
||||||
|
|
||||||
|
html() {
|
||||||
|
return `age is ${this.settings.age}`;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
withPluginApi('0.1', api => {
|
||||||
|
api.changeWidgetSetting('ov-settings-test', 'age', 37);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
test(assert) {
|
||||||
|
assert.equal(this.$('.settings').text(), 'age is 37');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user