Change Widget#attach signature

Take a fallback option instead of a list of names
This commit is contained in:
Daniel Waterworth 2019-06-12 14:57:25 +01:00 committed by Robin Ward
parent 37b8f5d989
commit 65a6e2c860
3 changed files with 37 additions and 31 deletions

View File

@ -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))
];
}

View File

@ -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"));

View File

@ -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}`);
}
}