Skip to content
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

Fixed nested CompositeViews by passing correct context to children #482

Merged
1 commit merged into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dist/views/CompositeView.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion dist/views/CompositeView.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/views/CompositeView.js.map

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion src/views/CompositeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export interface ICompositeViewPluginDesc extends IViewPluginDesc {
linkedSelections: ILinkedSelection[];
}

export function isCompositeViewPluginDesc(desc: any): desc is ICompositeViewPluginDesc {
return Array.isArray(desc.elements);
}

export interface IACompositeViewOptions {
showHeaders: boolean;
}
Expand All @@ -49,6 +53,8 @@ export interface IACompositeViewOptions {
export interface ICompositeInfo {
key: string;

desc: IElementDesc;

create(context: IViewContext, selection: ISelection, parent: HTMLElement, options?: any): IView;

options?: any;
Expand Down Expand Up @@ -246,7 +252,15 @@ export class CompositeView extends EventHandler implements IView {
if (links.length > 0 && !links.some((l) => l.fromKey === '_input' && l.toKey === d.key)) {
s = {idtype: this.selection.idtype, range: Range.none()};
}
const instance = d.create(this.context, s, helper, d.options);
// Fix for nested CompositeViews:
// Previously, nested CompositeViews were not possible, i.e. when using a CompositeView as element of a CompositeView.
// This is due to the fact that the context given to the children of a CompositeView is the context of the CompositeView.
// This of course breaks when a child is a CompositeView, as it expects its "own" desc. When you give it the parent desc, it extracts
// the children from the parent causing an infinite loop as it receives itself as child.
// I have to admit that I do not know why we give it the desc of the CompositeView parent, and not its own.
// The fix is to override the desc with the child desc if it is a CompositeViewDesc, such that it receives the "correct" children.
const patchedContext = isCompositeViewPluginDesc(d.desc) ? {...this.context, desc: d.desc} : this.context;
const instance = d.create(patchedContext, s, helper, d.options);
if (links.length === 0 || links.some((l) => l.fromKey === d.key && l.toKey === '_item')) {
instance.on(AView.EVENT_ITEM_SELECT, debounceItemSelection);
}
Expand Down Expand Up @@ -461,6 +475,7 @@ export class CompositeView extends EventHandler implements IView {
const descOptions = desc.options || {};
return Promise.resolve(desc.loader()).then((instance) => (<ICompositeInfo>{
key: desc.key,
desc,
options: Object.assign({}, descOptions, this.options), // also pass the view options from the ViewWrapper to all views
create: PluginRegistry.getInstance().getFactoryMethod(instance, desc.factory || 'create')
}));
Expand Down