Skip to content

Commit

Permalink
docs: add docs, fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nnixaa committed Jan 17, 2019
1 parent 0241672 commit 73d73e4
Show file tree
Hide file tree
Showing 15 changed files with 442 additions and 32 deletions.
12 changes: 12 additions & 0 deletions src/app/playground-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,18 @@ export const PLAYGROUND_COMPONENTS: ComponentLink[] = [
component: 'PopoverNoopComponent',
name: 'Popover Noop',
},
{
path: 'popover-dynamic.component',
link: '/popover/popover-dynamic.component',
component: 'PopoverDynamicComponent',
name: 'Popover Dynamic',
},
{
path: 'popover-dynamic-code.component',
link: '/popover/popover-dynamic-code.component',
component: 'PopoverDynamicCodeComponent',
name: 'Popover Dynamic Code',
},
],
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ export class NbDynamicOverlayHandler {
}

if (this.isContainerRerenderRequired()) {
this.dynamicOverlay.setContext(this._context);
this.dynamicOverlay.setContent(this._content);
this.dynamicOverlay.setContentAndContext(this._content, this._context);
}

if (this.isComponentTypeUpdateRequired()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComponentFactoryResolver, ComponentRef, Injectable, Type } from '@angular/core';
import { takeUntil, takeWhile } from 'rxjs/operators';
import { filter, takeUntil, takeWhile } from 'rxjs/operators';
import { Subject } from 'rxjs';

import {
Expand Down Expand Up @@ -67,6 +67,15 @@ export class NbDynamicOverlay {
}
}

setContentAndContext(content: NbOverlayContent, context: Object) {
this.content = content;
this.context = context;

if (this.container) {
this.updateContext();
}
}

setComponent(componentType: Type<NbRenderableContainer>) {
this.componentType = componentType;

Expand All @@ -88,6 +97,7 @@ export class NbDynamicOverlay {
.pipe(
takeWhile(() => this.alive),
takeUntil(this.positionStrategyChange$),
filter(() => !!this.container),
)
.subscribe((position: NbPosition) => patch(this.container, { position }));

Expand Down Expand Up @@ -155,9 +165,9 @@ export class NbDynamicOverlay {

/**
* Dimensions of the container may be changed after updating the content, so, we have to update
* container position.
* container position. But we need to delay the execution because of how the portalOverlay gets detached
* */
this.ref.updatePosition();
setTimeout(() => this.ref.updatePosition());
}

protected createContainerContext(): Object {
Expand Down
13 changes: 8 additions & 5 deletions src/framework/theme/components/cdk/overlay/overlay-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export class NbOverlayContainerComponent {

constructor(protected vcr: ViewContainerRef,
protected injector: Injector, private changeDetectorRef: ChangeDetectorRef) {

}

get isStringContent(): boolean {
Expand All @@ -78,28 +77,32 @@ export class NbOverlayContainerComponent {
attachComponentPortal<T>(portal: NbComponentPortal<T>): ComponentRef<T> {
portal.injector = this.createChildInjector(portal.componentFactoryResolver);
const componentRef = this.portalOutlet.attachComponentPortal(portal);
componentRef.changeDetectorRef.markForCheck();
componentRef.changeDetectorRef.detectChanges();
this.isAttached = true;
return componentRef;
}

attachTemplatePortal<C>(portal: NbTemplatePortal<C>): EmbeddedViewRef<C> {
const templateRef = this.portalOutlet.attachTemplatePortal(portal);
templateRef.detectChanges();
this.isAttached = true;
return templateRef;
}

attachStringContent(content: string) {
this.content = content;
this.isAttached = true;
this.changeDetectorRef.markForCheck();
this.changeDetectorRef.detectChanges();
this.isAttached = true;
}

detach() {
if (this.isAttached) {
this.vcr.remove();
this.isAttached = false;
if (this.portalOutlet.hasAttached()) {
this.portalOutlet.detach();
}
this.attachStringContent(null);
this.isAttached = false
}

protected createChildInjector(cfr: ComponentFactoryResolver): NbPortalInjector {
Expand Down
9 changes: 8 additions & 1 deletion src/framework/theme/components/popover/popover.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import { Component, ComponentFactoryResolver, Input, TemplateRef, Type, ViewChild } from '@angular/core';
import {
Component,
ComponentFactoryResolver,
Input,
TemplateRef,
Type,
ViewChild,
} from '@angular/core';
import {
NbComponentPortal,
NbOverlayContainerComponent,
Expand Down
15 changes: 0 additions & 15 deletions src/playground/with-layout/popover/components/dynamic.component.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Component, Input } from '@angular/core';

@Component({
selector: 'nb-dynamic-to-add',
template: `
<div>
<strong>Hello from custom component: {{ text }}</strong>
</div>
`,
})
export class DynamicToAddComponent {

@Input()
text: string = '';
}

@Component({
selector: 'nb-popover-list',
styles: [`
nb-card {
margin-bottom: 0;
}
`],
template: `
<nb-card>
<nb-card-header>Component Shopping list</nb-card-header>
<nb-list>
<nb-list-item>Apple</nb-list-item>
<nb-list-item>Orange</nb-list-item>
</nb-list>
</nb-card>
`,
})
export class PopoverListComponent {
}

@Component({
selector: 'nb-popover-tabs',
styles: [`
nb-card {
margin-bottom: 0;
}
`],
template: `
<nb-card>
<nb-card-header>Component Shopping list</nb-card-header>
<nb-tabset>
<nb-tab tabTitle="Fruits">
<nb-card>
<nb-card-body>Fruits list</nb-card-body>
</nb-card>
</nb-tab>
<nb-tab tabTitle="Vegetables">
<nb-card>
<nb-card-body>Vegetables list</nb-card-body>
</nb-card>
</nb-tab>
</nb-tabset>
</nb-card>
`,
})
export class PopoverTabsComponent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Component } from '@angular/core';
import { DynamicToAddComponent } from './components/dynamic.component';
import { DynamicToAddComponent } from './components/dynamic.components';

@Component({
selector: 'nb-popover-custom-component',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<div class="popover">
<button [nbPopover]="componentList" nbPopoverPlacement="bottom" nbButton>Show popover</button>
</div>

<div>
<h3>Change popover config via code</h3>
<h4>Change Component</h4>
<button nbButton size="small" status="success" (click)="changeComponent(componentList)">Use List Component</button>
<button nbButton size="small" status="success" (click)="changeComponent(componentTabs)">Use Tabs Component</button>
<button nbButton size="small" status="success" (click)="changeComponent(list)">Use List Template</button>
<button nbButton size="small" status="success" (click)="changeComponent(tabs)">Use Tabs Template</button>
<button nbButton size="small" status="success" (click)="changeComponent(textContent)">Use String</button>
<button *ngIf="!interval" nbButton size="small" status="warning" (click)="startRuntimeChange()">Enable Runtime Change</button>
<button *ngIf="interval" nbButton size="small" status="warning" (click)="stopRuntimeChange()">Disable Runtime Change</button>

<div>
<small>When "Runtime Change" is enabled, open the Popover to see a component change every 2 seconds.</small>
</div>

<h4>Change trigger</h4>
<button nbButton size="small" status="success" (click)="changeTrigger('click')">Click Trigger</button>
<button nbButton size="small" status="success" (click)="changeTrigger('hover')">Hover Trigger</button>
<button nbButton size="small" status="success" (click)="changeTrigger('hint')">Hint Trigger</button>

<h4>Change placement</h4>
<button nbButton size="small" status="success" (click)="changePlacement('top')">Top Placement</button>
<button nbButton size="small" status="success" (click)="changePlacement('right')">Right Placement</button>
<button nbButton size="small" status="success" (click)="changePlacement('bottom')">Bottom Placement</button>
<button nbButton size="small" status="success" (click)="changePlacement('left')">Left Placement</button>
</div>

<ng-template #tabs>
<nb-card class="margin-bottom-0">
<nb-card-header>Template Shopping list</nb-card-header>
<nb-tabset>
<nb-tab tabTitle="Fruits">
<nb-card class="margin-bottom-0">
<nb-card-body>Fruits list</nb-card-body>
</nb-card>
</nb-tab>
<nb-tab tabTitle="Vegetables">
<nb-card class="margin-bottom-0">
<nb-card-body>Vegetables list</nb-card-body>
</nb-card>
</nb-tab>
</nb-tabset>
</nb-card>
</ng-template>

<ng-template #list>
<nb-card class="margin-bottom-0">
<nb-card-header>Template Shopping list</nb-card-header>
<nb-list>
<nb-list-item>Apple</nb-list-item>
<nb-list-item>Orange</nb-list-item>
</nb-list>
</nb-card>
</ng-template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import { AfterViewInit, Component, OnDestroy, TemplateRef, ViewChild } from '@angular/core';
import { PopoverListComponent, PopoverTabsComponent } from './components/dynamic.components';
import { NbPopoverDirective } from '@nebular/theme';


@Component({
selector: 'nb-popover-dynamic-code',
templateUrl: './popover-dynamic-code.component.html',
styles: [`
:host {
display: flex;
flex-direction: column;
}
.margin-bottom-0 {
margin-bottom: 0;
}
.popover {
display: flex;
justify-content: center;
padding: 12rem;
}
button {
margin-right: 1rem;
margin-top: 1rem;
}
`],
})
export class PopoverDynamicCodeComponent implements OnDestroy, AfterViewInit {

@ViewChild(NbPopoverDirective) popover: NbPopoverDirective;
@ViewChild('tabs', { read: TemplateRef }) templateTabs: TemplateRef<any>;
@ViewChild('list', { read: TemplateRef }) templateList: TemplateRef<any>;

componentList = PopoverListComponent;
componentTabs = PopoverTabsComponent;
textContent = 'Hello World';
component: any = this.componentList;
items = [];

interval: any;

ngAfterViewInit() {
this.items = [
this.componentList,
this.componentTabs,
this.templateList,
this.templateTabs,
this.textContent,
];
}

startRuntimeChange() {
if (!this.interval) {
this.interval = setInterval(() => {
const random = this.items[Math.floor(Math.random() * this.items.length)];
this.changeComponent(random);
}, 2000);
}
}

stopRuntimeChange() {
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
}

changeComponent(component) {
this.popover.content = component;
this.popover.rebuild();
}

changeTrigger(trigger) {
this.popover.trigger = trigger;
this.popover.rebuild();
}

changePlacement(placement) {
this.popover.position = placement;
this.popover.rebuild();
}

ngOnDestroy() {
clearInterval(this.interval);
}
}
Loading

0 comments on commit 73d73e4

Please sign in to comment.