-
Notifications
You must be signed in to change notification settings - Fork 643
PRO-8256: allow createWidgetOperations to be filtered based on a list of widget types #5057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Requires the module options and an area field's .options property | ||
|
|
||
| import getWidgets from './get-widgets.js'; | ||
|
|
||
| export default function filterCreateWidgetOperations(moduleOptions, areaOptions) { | ||
| const widgets = getWidgets(areaOptions); | ||
| return moduleOptions.createWidgetOperations.filter(createWidgetOperation => { | ||
| if (createWidgetOperation.ifTypesIntersect) { | ||
| const types = Object.keys(widgets); | ||
| if (!types.some(type => createWidgetOperation.ifTypesIntersect.includes(type))) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Requires an area field's .options property | ||
|
|
||
| export default function getWidgets(options) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't strictly have to factor this out today, but in the long run I'd like to have one implementation, so this is the right direction. |
||
| let widgets = options.widgets || {}; | ||
| if (options.groups) { | ||
| for (const group of Object.keys(options.groups)) { | ||
| widgets = { | ||
| ...widgets, | ||
| ...options.groups[group].widgets | ||
| }; | ||
| } | ||
| } | ||
| return widgets; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,29 +123,6 @@ module.exports = { | |
| origin: null, | ||
| preview: true | ||
| }, | ||
| handlers(self) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Registering the widget operations late prevents the section template library from altering them cleanly if some modules are configured before and some after, which is likely because of our support for |
||
| return { | ||
| 'apostrophe:modulesRegistered': { | ||
| composeWidgetOperations() { | ||
| self.widgetOperations = Object.entries(self.widgetOperations) | ||
| .map(([ name, operation ]) => { | ||
| if (!operation.label || !operation.modal) { | ||
| throw self.apos.error('invalid', 'widgetOperations requires label and modal properties.'); | ||
| } | ||
|
|
||
| if (operation.secondaryLevel !== true && !operation.icon) { | ||
| throw self.apos.error('invalid', 'widgetOperations requires the icon property at primary level.'); | ||
| } | ||
|
|
||
| return { | ||
| name, | ||
| ...operation | ||
| }; | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
| }, | ||
| init(self) { | ||
| self.isExplicitOrigin = self.options.origin !== null; | ||
| self.options.origin = self.options.origin || 'right'; | ||
|
|
@@ -170,6 +147,7 @@ module.exports = { | |
| self.label = self.options.label; | ||
|
|
||
| self.composeSchema(); | ||
| self.composeWidgetOperations(); | ||
|
|
||
| self.apos.area.setWidgetManager(self.name, self); | ||
|
|
||
|
|
@@ -187,6 +165,7 @@ module.exports = { | |
| }, | ||
| methods(self) { | ||
| return { | ||
|
|
||
| composeSchema() { | ||
| self.schema = self.apos.schema.compose({ | ||
| addFields: self.apos.schema.fieldsToArray(`Module ${self.__meta.name}`, self.fields), | ||
|
|
@@ -203,6 +182,24 @@ module.exports = { | |
| }); | ||
| }, | ||
|
|
||
| composeWidgetOperations() { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no actual changes to the logic here. |
||
| self.widgetOperations = Object.entries(self.widgetOperations) | ||
| .map(([ name, operation ]) => { | ||
| if (!operation.label || !operation.modal) { | ||
| throw self.apos.error('invalid', 'widgetOperations requires label and modal properties.'); | ||
| } | ||
|
|
||
| if (operation.secondaryLevel !== true && !operation.icon) { | ||
| throw self.apos.error('invalid', 'widgetOperations requires the icon property at primary level.'); | ||
| } | ||
|
|
||
| return { | ||
| name, | ||
| ...operation | ||
| }; | ||
| }); | ||
| }, | ||
|
|
||
| // Returns markup for the widget. Invoked via `{% widget ... %}` in the | ||
| // `@apostrophecms/area` module as it iterates over widgets in | ||
| // an area. The default behavior is to render the template for the widget, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a new issue, but I avoided further duplication within the client side.
Right now we have some ESM/commonjs issues that prevent us from sharing code as well as I'd like between front and back end.