Skip to content

Commit e96a5f4

Browse files
committed
feat: attach component custom props on initialize without ToppyOverlay
1 parent b2fef92 commit e96a5f4

File tree

13 files changed

+52
-101
lines changed

13 files changed

+52
-101
lines changed

docs/app/app.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
>
9292
</li>
9393
<li>
94-
<a (click)="scrollTo('communication')" [ngClass]="{ active: currentSection === 'communication' }"
95-
><span class="toppy-icon icon-rss"></span> Communication</a
94+
<a (click)="scrollTo('content-properties')" [ngClass]="{ active: currentSection === 'content-properties' }"
95+
><span class="toppy-icon icon-rss"></span> Content Properties</a
9696
>
9797
</li>
9898
<li>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Component } from '@angular/core';
2-
import { ToppyOverlay } from 'toppy';
32

43
@Component({
54
selector: 'app-hero-screen',
65
templateUrl: './hero-screen.component.html',
76
styles: []
87
})
98
export class HeroScreenComponent {
10-
constructor(public overlay: ToppyOverlay) {}
9+
close;
10+
constructor() {}
1111
dispose() {
12-
this.overlay.close();
12+
this.close();
1313
}
1414
}

docs/app/test/test.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<div class="box">
22
<p>Hello Everyone!</p>
3-
<span (click)="close()">close</span>
4-
</div>
3+
<span (click)="dismiss()">close</span>
4+
</div>

docs/app/test/test.component.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { ToppyOverlay } from 'toppy';
32

43
@Component({
54
selector: 'app-test',
65
templateUrl: './test.component.html',
76
styleUrls: ['./test.component.scss']
87
})
98
export class TestComponent implements OnInit {
10-
constructor(private oc: ToppyOverlay) {}
9+
close;
10+
constructor() {}
1111

12-
ngOnInit() {
13-
console.log('ID', this.oc);
14-
}
15-
close() {
16-
this.oc.close();
12+
ngOnInit() {}
13+
dismiss() {
14+
this.close();
1715
}
1816
}

docs/app/utils/content/content.component.html

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ <h5 id="fullscreen-position"><a href="#fullscreen-position">Fullscreen position<
120120
</p>
121121
<pre><code class="hljs language-typescript"><span class="hljs-keyword">new</span> FullscreenPosition();
122122
</code></pre>
123-
<h3 id="communication"><a href="#communication"><span class="toppy-icon icon-rss"></span> Communication</a></h3>
124-
<h5 id="component-data"><a href="#component-data">Component Data</a></h5>
125-
<p>When you host a component, you can control the overlay through <code>ToppyOverlay</code> service. Using this service you can access all properties that is provided in content. Also the properties comes with <code>close</code>.</p>
123+
<h3 id="content-properties"><a href="#content-properties"><span class="toppy-icon icon-rss"></span> Content Properties</a></h3>
124+
<p>When you are adding content, optionally you can add additional custom data to them. Which can be accessed later inside the content. This applies only if the content type is <strong>Component</strong> or <strong>TemplateRef</strong>. The data will be auto attached with the content and comes with <code>close</code> property, to dismiss the overlay programatically.</p>
125+
<h5 id="component-props"><a href="#component-props">Component Props</a></h5>
126126
<pre><code class="hljs language-typescript"><span class="hljs-keyword">this</span>.overlay = <span class="hljs-keyword">this</span>._toppy
127127
.position(position)
128128
.content(HelloComponent, &#123; propName: <span class="hljs-string">'toppy-test-prop'</span> &#125;)
@@ -133,30 +133,29 @@ <h5 id="component-data"><a href="#component-data">Component Data</a></h5>
133133
template: <span class="hljs-string">'&#x3C;div>Some text&#x3C;/div>'</span>
134134
&#125;)
135135
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> HelloComponent &#123;
136-
<span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">public</span> overlay: ToppyOverlay</span>) &#123;
137-
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.overlay.props.propName); <span class="hljs-comment">// will return 'toppy-test-prop'</span>
136+
propName; <span class="hljs-comment">// declare. Else TSlint throws error</span>
137+
close; <span class="hljs-comment">// just declare</span>
138+
<span class="hljs-keyword">constructor</span>(<span class="hljs-params"></span>) &#123;
139+
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.propName); <span class="hljs-comment">// will return 'toppy-test-prop'</span>
138140
&#125;
139141

140-
close() &#123;
141-
<span class="hljs-keyword">this</span>.overlay.close();
142+
dismiss() &#123;
143+
<span class="hljs-keyword">this</span>.close(); <span class="hljs-comment">// auto binded</span>
142144
&#125;
143145
&#125;
144146
</code></pre>
145-
<h5 id="template-data"><a href="#template-data">Template Data</a></h5>
146-
<p>This is very similar to above one. When you use template as a content, you can pass additional data to it.</p>
147+
<h5 id="template-props"><a href="#template-props">Template props</a></h5>
147148
<pre><code class="hljs language-typescript"><span class="hljs-keyword">this</span>.overlay = <span class="hljs-keyword">this</span>._toppy
148149
.position(position)
149150
.content(template, &#123; name: <span class="hljs-string">'Johny'</span> &#125;)
150151
.create();
151152
</code></pre>
152-
<p>Then in your template you can refer the data like this,</p>
153153
<pre><code class="hljs language-html" id="tpl"><span class="hljs-tag">&#x3C;<span class="hljs-name">ng-template</span> #<span class="hljs-attr">tpl</span> <span class="hljs-attr">let-toppy</span>></span>
154154
<span class="hljs-tag">&#x3C;<span class="hljs-name">div</span>></span>Hello <span class="hljs-tag">&#x3C;<span class="hljs-name">span</span> [<span class="hljs-attr">innerText</span>]=<span class="hljs-string">"toppy.name"</span>></span><span class="hljs-tag">&#x3C;/<span class="hljs-name">span</span>></span> !<span class="hljs-tag">&#x3C;/<span class="hljs-name">div</span>></span>
155155
<span class="hljs-tag">&#x3C;<span class="hljs-name">button</span> (<span class="hljs-attr">click</span>)=<span class="hljs-string">"toppy.close()"</span>></span>Close<span class="hljs-tag">&#x3C;/<span class="hljs-name">button</span>></span>
156156
<span class="hljs-tag">&#x3C;/<span class="hljs-name">ng-template</span>></span>
157157
</code></pre>
158-
<p>Method <code>close</code> is automatically binded.</p>
159-
<h5 id="plain-text"><a href="#plain-text">Plain text</a></h5>
158+
<h5 id="plain-text-props"><a href="#plain-text-props">Plain text props</a></h5>
160159
<p>When you use Plain text as a content, optionally you can able to set a class name to that <code>div</code> block.</p>
161160
<pre><code class="hljs language-typescript"><span class="hljs-keyword">this</span>.overlay = <span class="hljs-keyword">this</span>._toppy
162161
.position(position)

docs/markdown/main.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ new SlidePosition({
157157
new FullscreenPosition();
158158
```
159159

160-
### [ ]{.toppy-icon .icon-rss} Communication
160+
### [ ]{.toppy-icon .icon-rss} Content Properties
161161

162-
##### Component Data
162+
When you are adding content, optionally you can add additional custom data to them. Which can be accessed later inside the content. This applies only if the content type is **Component** or **TemplateRef**. The data will be auto attached with the content and comes with `close` property, to dismiss the overlay programatically.
163163

164-
When you host a component, you can control the overlay through `ToppyOverlay` service. Using this service you can access all properties that is provided in content. Also the properties comes with `close`.
164+
##### Component Props
165165

166166
```typescript
167167
this.overlay = this._toppy
@@ -176,19 +176,19 @@ this.overlay = this._toppy
176176
template: '<div>Some text</div>'
177177
})
178178
export class HelloComponent {
179-
constructor(public overlay: ToppyOverlay) {
180-
console.log(this.overlay.props.propName); // will return 'toppy-test-prop'
179+
propName; // declare. Else TSlint throws error
180+
close; // just declare
181+
constructor() {
182+
console.log(this.propName); // will return 'toppy-test-prop'
181183
}
182184

183-
close() {
184-
this.overlay.close();
185+
dismiss() {
186+
this.close(); // auto binded
185187
}
186188
}
187189
```
188190

189-
##### Template Data
190-
191-
This is very similar to above one. When you use template as a content, you can pass additional data to it.
191+
##### Template props
192192

193193
```typescript
194194
this.overlay = this._toppy
@@ -197,18 +197,14 @@ this.overlay = this._toppy
197197
.create();
198198
```
199199

200-
Then in your template you can refer the data like this,
201-
202200
```html
203201
<ng-template #tpl let-toppy>
204202
<div>Hello <span [innerText]="toppy.name"></span> !</div>
205203
<button (click)="toppy.close()">Close</button>
206204
</ng-template>
207205
```
208206

209-
Method `close` is automatically binded.
210-
211-
##### Plain text
207+
##### Plain text props
212208

213209
When you use Plain text as a content, optionally you can able to set a class name to that `div` block.
214210

projects/toppy/src/lib/template.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<ng-container *ngSwitchCase="'t'">
99
<ng-container *ngTemplateOutlet="content.data; context: { $implicit: content.props }"></ng-container>
1010
</ng-container>
11-
<ng-container *ngSwitchCase="'c'">
12-
<ng-container *ngComponentOutlet="content.data; injector: overlayInj || createInj"></ng-container>
13-
</ng-container>
11+
<ng-container *ngSwitchCase="'c'"> <ng-container *ngComponentOutlet="content.data"></ng-container> </ng-container>
1412
</ng-container>
1513
</div>

projects/toppy/src/lib/toppy-overlay.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

projects/toppy/src/lib/toppy.component.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import { Observable, Subject } from 'rxjs';
1212
import { startWith, takeUntil, tap } from 'rxjs/operators';
1313
import { Content, ContentType, TID, ToppyConfig } from './models';
1414
import { ToppyPosition } from './position/position';
15-
import { ToppyOverlay } from './toppy-overlay';
16-
import { Bus, cssClass, newInjector, toCss } from './utils';
15+
import { Bus, cssClass, toCss } from './utils';
1716

1817
@Component({
1918
// tslint:disable-next-line:component-selector
@@ -34,7 +33,6 @@ export class ToppyComponent implements OnInit, AfterViewInit, OnDestroy {
3433
el: HTMLElement | any;
3534
wrapperEl: HTMLElement | any;
3635
extra: string;
37-
overlayInj: Injector = null;
3836
private die: Subject<1> = new Subject();
3937

4038
constructor(private inj: Injector, private cd: ChangeDetectorRef, private elRef: ElementRef) {}
@@ -49,24 +47,15 @@ export class ToppyComponent implements OnInit, AfterViewInit, OnDestroy {
4947
this.el.setAttribute('data-tid', this.tid);
5048
cssClass('add', cls, `[data-tid='${[this.tid]}']`);
5149
cssClass('add', [this.config.bodyClass]);
50+
if (this.content.type === ContentType.COMPONENT) {
51+
Object.assign(this.content.data['prototype'], this.content.props);
52+
}
5253
}
5354

5455
ngAfterViewInit() {
5556
this.listenPos().subscribe();
5657
}
5758

58-
get createInj(): Injector {
59-
this.overlayInj = newInjector(
60-
{
61-
provide: ToppyOverlay,
62-
useFactory: () => new ToppyOverlay(this.content.props),
63-
deps: []
64-
},
65-
this.inj
66-
);
67-
return this.overlayInj;
68-
}
69-
7059
updateTextContent(data: string): void {
7160
if (this.content.type === ContentType.STRING) {
7261
this.content.data = data;

projects/toppy/src/lib/toppy.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ContentData, ContentProps, ContentType, Inputs, InsidePlacement, TID, T
44
import { GlobalPosition } from './position';
55
import { ToppyPosition } from './position/position';
66
import { ToppyControl } from './toppy-control';
7-
import { Bus, createId, getContent, newInjector } from './utils';
7+
import { Bus, createId, getContent } from './utils';
88

99
@Injectable({
1010
providedIn: 'root'
@@ -41,13 +41,16 @@ export class Toppy {
4141
create(key: string = null): ToppyControl {
4242
this.tid = this.inputs.tid = key || createId();
4343

44-
const injector = newInjector(
45-
{
46-
provide: ToppyControl,
47-
deps: [ApplicationRef, ComponentFactoryResolver, Injector]
48-
},
44+
const injector = Injector.create(
45+
[
46+
{
47+
provide: ToppyControl,
48+
deps: [ApplicationRef, ComponentFactoryResolver, Injector]
49+
}
50+
],
4951
this.injector
5052
);
53+
5154
const tc = injector.get(ToppyControl);
5255
if (Toppy.controls[this.tid]) {
5356
this.tid = createId();

0 commit comments

Comments
 (0)