mirror of
https://github.com/discourse/discourse.git
synced 2025-01-30 06:28:03 +08:00
Change Widget#attach signature
Take a fallback option instead of a list of names
This commit is contained in:
parent
37b8f5d989
commit
65a6e2c860
|
@ -15,13 +15,13 @@ createWidget("large-notification-item", {
|
|||
const notificationName =
|
||||
this.site.notificationLookup[attrs.notification_type];
|
||||
|
||||
const widgetNames = [
|
||||
`${notificationName.dasherize()}-notification-item`,
|
||||
"default-notification-item"
|
||||
];
|
||||
|
||||
return [
|
||||
this.attach(widgetNames, attrs),
|
||||
this.attach(
|
||||
`${notificationName.dasherize()}-notification-item`,
|
||||
attrs,
|
||||
{},
|
||||
{ fallbackWidgetName: 'default-notification-item' },
|
||||
),
|
||||
h("span.time", dateNode(attrs.created_at))
|
||||
];
|
||||
}
|
||||
|
|
|
@ -94,12 +94,14 @@ export default createWidget("user-notifications", {
|
|||
const notificationName =
|
||||
this.site.notificationLookup[notificationAttrs.notification_type];
|
||||
|
||||
const widgetNames = [
|
||||
`${notificationName.dasherize()}-notification-item`,
|
||||
"default-notification-item"
|
||||
];
|
||||
|
||||
return this.attach(widgetNames, notificationAttrs);
|
||||
return (
|
||||
this.attach(
|
||||
`${notificationName.dasherize()}-notification-item`,
|
||||
notificationAttrs,
|
||||
{},
|
||||
{ fallbackWidgetName: 'default-notification-item' }
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
result.push(h("hr"));
|
||||
|
|
|
@ -196,27 +196,31 @@ export default class Widget {
|
|||
}
|
||||
}
|
||||
|
||||
attach(widgetNames, attrs, opts) {
|
||||
widgetNames = [].concat(widgetNames);
|
||||
let WidgetClass = null;
|
||||
lookupWidgetClass(widgetName) {
|
||||
let WidgetClass = _registry[widgetName];
|
||||
if (WidgetClass) {
|
||||
return WidgetClass;
|
||||
}
|
||||
|
||||
for (let widgetName of widgetNames) {
|
||||
WidgetClass = _registry[widgetName];
|
||||
if (WidgetClass) {
|
||||
break;
|
||||
}
|
||||
if (!this.register) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("couldn't find register");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!this.register) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("couldn't find register");
|
||||
return;
|
||||
}
|
||||
WidgetClass = this.register.lookupFactory(`widget:${widgetName}`);
|
||||
if (WidgetClass && WidgetClass.class) {
|
||||
return WidgetClass.class;
|
||||
}
|
||||
|
||||
WidgetClass = this.register.lookupFactory(`widget:${widgetName}`);
|
||||
if (WidgetClass && WidgetClass.class) {
|
||||
WidgetClass = WidgetClass.class;
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
attach(widgetName, attrs, opts, otherOpts = {}) {
|
||||
let WidgetClass = this.lookupWidgetClass(widgetName);
|
||||
|
||||
if (!WidgetClass && otherOpts.fallbackWidgetName) {
|
||||
WidgetClass = this.lookupWidgetClass(otherOpts.fallbackWidgetName);
|
||||
}
|
||||
|
||||
if (WidgetClass) {
|
||||
|
@ -225,7 +229,7 @@ export default class Widget {
|
|||
result.dirtyKeys = this.dirtyKeys;
|
||||
return result;
|
||||
} else {
|
||||
throw new Error(`Couldn't find ${widgetNames} factory`);
|
||||
throw new Error(`Couldn't find ${widgetName} or fallback ${otherOpts.fallbackWidgetName}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user