FIX: Some re-render functionality in widgets, added more coverage
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { diff, patch } from 'virtual-dom';
|
||||
import { WidgetClickHook } from 'discourse/widgets/click-hook';
|
||||
import { renderedKey } from 'discourse/widgets/widget';
|
||||
import { renderedKey, queryRegistry } from 'discourse/widgets/widget';
|
||||
|
||||
const _cleanCallbacks = {};
|
||||
export function addWidgetCleanCallback(widgetName, fn) {
|
||||
@@ -17,7 +17,9 @@ export default Ember.Component.extend({
|
||||
|
||||
init() {
|
||||
this._super();
|
||||
this._widgetClass = this.container.lookupFactory(`widget:${this.get('widget')}`);
|
||||
const name = this.get('widget');
|
||||
|
||||
this._widgetClass = queryRegistry(name) || this.container.lookupFactory(`widget:${name}`);
|
||||
this._connected = [];
|
||||
},
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ WidgetClickHook.setupDocumentCallback = function() {
|
||||
while (node) {
|
||||
const widget = node[CLICK_ATTRIBUTE_NAME];
|
||||
if (widget) {
|
||||
return widget.click(e);
|
||||
return widget.rerenderResult(() => widget.click(e));
|
||||
}
|
||||
node = node.parentNode;
|
||||
}
|
||||
|
||||
@@ -18,8 +18,6 @@ export default createWidget('post-gap', {
|
||||
if (state.loading) { return; }
|
||||
state.loading = true;
|
||||
|
||||
this.scheduleRerender();
|
||||
|
||||
const args = { gap: attrs.gap, post: this.model };
|
||||
return this.sendWidgetAction(attrs.pos === 'before' ? 'fillGapBefore' : 'fillGapAfter', args);
|
||||
}
|
||||
|
||||
@@ -401,9 +401,7 @@ export default createWidget('post', {
|
||||
const likeAction = post.get('likeAction');
|
||||
|
||||
if (likeAction && likeAction.get('canToggle')) {
|
||||
const promise = likeAction.togglePromise(post);
|
||||
this.scheduleRerender();
|
||||
return promise;
|
||||
return likeAction.togglePromise(post);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -15,6 +15,10 @@ export function renderedKey(key) {
|
||||
delete _dirty[key];
|
||||
}
|
||||
|
||||
export function queryRegistry(name) {
|
||||
return _registry[name];
|
||||
}
|
||||
|
||||
const _decorators = {};
|
||||
|
||||
export function decorateWidget(widgetName, cb) {
|
||||
@@ -203,7 +207,7 @@ export default class Widget {
|
||||
}
|
||||
}
|
||||
|
||||
sendComponentAction(name, param) {
|
||||
_sendComponentAction(name, param) {
|
||||
const view = this._findAncestorWithProperty('_emberView');
|
||||
|
||||
let promise;
|
||||
@@ -233,9 +237,7 @@ export default class Widget {
|
||||
}
|
||||
}
|
||||
|
||||
if (promise) {
|
||||
return promise.then(() => this.scheduleRerender());
|
||||
}
|
||||
return this.rerenderResult(() => promise);
|
||||
}
|
||||
|
||||
findAncestorModel() {
|
||||
@@ -245,19 +247,25 @@ export default class Widget {
|
||||
}
|
||||
}
|
||||
|
||||
sendWidgetAction(name, param) {
|
||||
const widget = this._findAncestorWithProperty(name);
|
||||
if (widget) {
|
||||
const result = widget[name](param);
|
||||
if (result && result.then) {
|
||||
return result.then(() => this.scheduleRerender());
|
||||
} else {
|
||||
this.scheduleRerender();
|
||||
return result;
|
||||
}
|
||||
rerenderResult(fn) {
|
||||
this.scheduleRerender();
|
||||
const result = fn();
|
||||
// re-render after any promises complete, too!
|
||||
if (result && result.then) {
|
||||
return result.then(() => this.scheduleRerender());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return this.sendComponentAction(name, param || this.findAncestorModel());
|
||||
sendWidgetAction(name, param) {
|
||||
return this.rerenderResult(() => {
|
||||
const widget = this._findAncestorWithProperty(name);
|
||||
if (widget) {
|
||||
return widget[name](param);
|
||||
}
|
||||
|
||||
return this._sendComponentAction(name, param || this.findAncestorModel());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user