Skip to content

Commit

Permalink
feat(portal): render context
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed Jan 16, 2018
1 parent 72b515b commit 3737839
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 Vildan Softic
Copyright (c) 2017 Binh Vo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@
"release": {
"verifyConditions": "condition-circle"
}
}
}
18 changes: 15 additions & 3 deletions src/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@ import { PLATFORM } from 'aurelia-pal';

const document: Document = PLATFORM.global.document;

type PortalTarget = string | Element | null | undefined

@templateController()
@customAttribute('portal')
export class Portal {

private static getTarget(target: string | Element | null | undefined): Element | null {
private static getTarget(target: PortalTarget, context?: PortalTarget): Element | null {
if (target) {
if (typeof target === 'string') {
target = document.querySelector(target);
let queryContext: Element | Document = document;
if (context) {
if (typeof context === 'string') {
context = document.querySelector(context);
}
if (context !== null && (context instanceof Element)) {
queryContext = context;
}
}
target = queryContext.querySelector(target);
}
if (target && (target instanceof Element)) {
return target;
Expand All @@ -41,6 +52,7 @@ export class Portal {
})
public target: string | Element | null | undefined;

@bindable({ changeHandler: 'targetChanged' }) public renderContext: string | Element | null | undefined;
@bindable() public strict: boolean = false;
@bindable() public initialRender: boolean = false;

Expand Down Expand Up @@ -83,7 +95,7 @@ export class Portal {
}

private getTarget(): Element | null {
let target = Portal.getTarget(this.target);
let target = Portal.getTarget(this.target, this.renderContext);
if (target === null) {
if (this.strict) {
throw new Error('Render target not found.');
Expand Down
24 changes: 24 additions & 0 deletions test/unit/portal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ describe('Portal attribute', () => {
h('div', { class: 'square1' }),
h('div', { class: 'square2' }),
h('div', { class: 'square3' }),
h('div', { class: 'round' },
h('div', { id: 'square1', class: 'square1' })
),
h('div', { id: 'portal-container-element' },
viewSlotAnchor = document.createComment('anchor')
)
Expand Down Expand Up @@ -107,6 +110,27 @@ describe('Portal attribute', () => {
expect(document.querySelector('.square3 form')).not.toBeFalsy();
});

it('renders with renderContext', () => {
portal.target = '.square1';
portal.renderContext = '.round';

portal.bind(bindingContext, overrideContext);
portal.attached();

expect(document.querySelector('.round form')).not.toBeFalsy();
});

it('renders to first square with renderContext selector query with no result', () => {
portal.target = '.square1';
portal.renderContext = '.super-round';

portal.bind(bindingContext, overrideContext);
portal.attached();

expect(document.querySelector('.round form')).toBe(null);
expect(document.querySelector('.square1 form')).not.toBeFalsy();
});

it('re-renders after target has changed', () => {
portal.target = '.square2';
portal.bind(bindingContext, overrideContext);
Expand Down

0 comments on commit 3737839

Please sign in to comment.