Skip to content

Commit c3b01ea

Browse files
committed
Add router insertBefore and remove widget closes #3
1 parent 9f3e371 commit c3b01ea

File tree

8 files changed

+94
-28
lines changed

8 files changed

+94
-28
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ Thumbs.db
4747

4848

4949
/deploy
50+
/.vscode

projects/angular-nodegui/src/lib/components/view.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,27 @@ export class NgView extends QWidget implements NgComponent {
1111
return;
1212
}
1313
if (!this.layout) {
14-
const flexLayout = new FlexLayout();
15-
flexLayout.setFlexNode(this.getFlexNode());
16-
this.setLayout(flexLayout);
17-
this.layout = flexLayout;
14+
this.createLayout();
1815
}
1916

2017
this.layout.addWidget(newChild);
2118
}
2219

2320
public insertBefore(newChild: any, refChild: any) {
24-
if (!this.layout) {
25-
console.warn('parent has no layout to insert child before another child');
21+
if (!newChild) {
2622
return;
2723
}
24+
if (!this.layout) {
25+
this.createLayout();
26+
}
2827
(this.layout as FlexLayout).insertChildBefore(newChild, this);
2928
}
3029

30+
removeChild(oldChild: NgView): void {
31+
oldChild.hide(); // it's necessary that there's no overlapping of widgets when removing
32+
(this.layout as FlexLayout).removeWidget(oldChild);
33+
}
34+
3135
public setNgAttribute(
3236
name: string,
3337
value: string,
@@ -62,13 +66,18 @@ export class NgView extends QWidget implements NgComponent {
6266
removeAttribute(name: string, namespace?: string): void {
6367
throw new Error('Method not implemented.');
6468
}
65-
removeChild(oldChild: any): void {
66-
throw new Error('Method not implemented.');
67-
}
69+
6870
removeClass(name: string): void {
6971
throw new Error('Method not implemented.');
7072
}
7173
removeStyle(style: string, flags?: RendererStyleFlags2): void {
7274
throw new Error('Method not implemented.');
7375
}
76+
77+
private createLayout() {
78+
const flexLayout = new FlexLayout();
79+
flexLayout.setFlexNode(this.getFlexNode());
80+
this.setLayout(flexLayout);
81+
this.layout = flexLayout;
82+
}
7483
}

projects/angular-nodegui/src/lib/renderer.ts

+23-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import { ComponentsMap, NgComponentClass } from './components/components-map';
1313
import { TextField } from './components/nodes';
1414
import { NgView } from './components/view';
1515

16+
export interface ElementReference {
17+
previous: NgView;
18+
next: NgView;
19+
}
20+
1621
@Injectable()
1722
export class NodeguiRendererFactory implements RendererFactory2 {
1823
protected renderer: Renderer2;
@@ -87,12 +92,17 @@ export class NodeguiRenderer implements Renderer2 {
8792

8893
destroy(): void {}
8994

90-
insertBefore(parent: NgComponent, newChild: any, refChild: any): void {
91-
// TODO: insert before for router-outlet
92-
console.log('insertBefore');
93-
console.log(parent, newChild, refChild);
95+
insertBefore(
96+
parent: NgComponent,
97+
newChild: any,
98+
{ previous, next }: ElementReference
99+
): void {
100+
newChild.parent = previous;
101+
previous.insertBefore(newChild, next);
102+
}
94103

95-
parent.insertBefore(newChild, refChild);
104+
removeChild(parent: NgComponent, oldChild: NgView): void {
105+
parent.removeChild(oldChild);
96106
}
97107

98108
listen(
@@ -107,11 +117,16 @@ export class NodeguiRenderer implements Renderer2 {
107117
return () => target.removeEventListener(eventName, callbackFunc);
108118
}
109119

110-
nextSibling(node: any): any {
111-
console.log('nextSibling');
120+
nextSibling(node: any): ElementReference {
121+
return {
122+
previous: node,
123+
next: node.nextSibling
124+
};
112125
}
113126

114-
parentNode(node: any): any {}
127+
parentNode(node: any): any {
128+
return node.parent ? node.parent : node;
129+
}
115130

116131
removeAttribute(
117132
el: NgComponent,
@@ -121,10 +136,6 @@ export class NodeguiRenderer implements Renderer2 {
121136
el.removeAttribute(name, namespace);
122137
}
123138

124-
removeChild(parent: NgComponent, oldChild: any): void {
125-
parent.removeChild(oldChild);
126-
}
127-
128139
removeClass(el: NgComponent, name: string): void {
129140
el.removeClass(name);
130141
}

src/app/about/about.component.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-about',
5+
template: `
6+
<text>
7+
about works!
8+
</text>
9+
`,
10+
styles: [],
11+
encapsulation: ViewEncapsulation.None
12+
})
13+
export class AboutComponent implements OnInit {
14+
constructor() {}
15+
16+
ngOnInit() {}
17+
}

src/app/app.component.html

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
<view id="app">
33
<image id="image" [src]="'./src/assets/angular.png'" [aspectRatioMode]="aspectRatioMode"></image>
44

5-
<!-- <app-hello [name]="name"></app-hello> -->
5+
<app-hello [name]="name"></app-hello>
66

77
<text>Github username</text>
88
<linedit [text]="name" [placeholderText]="'Insert github username'" (textChanged)="textChanged($event)"></linedit>
99

10-
<button (clicked)="setRoute()">
11-
to home route
10+
<button (clicked)="goToPage('/')">
11+
first page
12+
</button>
13+
<button (clicked)="goToPage('/home')">
14+
home page
15+
</button>
16+
<button (clicked)="goToPage('/about')">
17+
about page
1218
</button>
1319
<!-- <checkbox [checked]="true"></checkbox>
1420
<checkbox [checked]="true" [enabled]="false"></checkbox>

src/app/app.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export class AppComponent implements OnInit {
3030
win.setMinimumSize(480, 620);
3131
}
3232

33-
setRoute() {
34-
this.router.navigateByUrl('/hello');
33+
goToPage(url: string) {
34+
this.router.navigateByUrl(url);
3535
}
3636

3737
textChanged(val: string) {

src/app/app.module.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import { AppComponent } from './app.component';
77
import { HelloComponent } from './hello/hello.component';
88
import { GithubService } from './github.service';
99
import { NodeguiRouterModule } from '../../projects/angular-nodegui/src/lib/router/router.module';
10+
import { HomeComponent } from './home/home.component';
11+
import { AboutComponent } from './about/about.component';
1012

11-
const appRoutes: Routes = [{ path: 'hello', component: HelloComponent }];
13+
const appRoutes: Routes = [
14+
{ path: 'home', component: HomeComponent },
15+
{ path: 'about', component: AboutComponent }
16+
];
1217

1318
@NgModule({
14-
declarations: [AppComponent, HelloComponent],
19+
declarations: [AppComponent, HelloComponent, HomeComponent, AboutComponent],
1520
imports: [
1621
NodeguiLibModule,
1722
HttpClientModule,

src/app/home/home.component.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-home',
5+
template: `
6+
<text>
7+
home works!
8+
</text>
9+
`,
10+
styles: [],
11+
encapsulation: ViewEncapsulation.None
12+
})
13+
export class HomeComponent implements OnInit {
14+
constructor() {}
15+
16+
ngOnInit() {}
17+
}

0 commit comments

Comments
 (0)