Skip to content

Commit

Permalink
Merge pull request #482 from datavisyn/mp/481_fix_nested_composite_views
Browse files Browse the repository at this point in the history
Fixed nested CompositeViews by passing correct context to children
  • Loading branch information
Anita Steiner authored Feb 11, 2021
2 parents 3b9e3bc + f8b7688 commit 45d3152
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
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

0 comments on commit 45d3152

Please sign in to comment.