Skip to content

Commit e38cb84

Browse files
committed
Merge pull request NativeScript#194 from NativeScript/hdeshev/third-party-code-snippets
Third party code snippets.
2 parents b3973fc + b7654d1 commit e38cb84

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

tests/app/tests/third-party-view.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {ContentView} from "ui/content-view";
2+
3+
// >> third-party-simple-view
4+
export class SimpleTag extends ContentView {
5+
//...
6+
}
7+
// << third-party-simple-view

tests/app/tests/third-party.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//make sure you import mocha-config before angular2/core
2+
import {assert} from "./test-config";
3+
import {Component, Directive, ElementRef, ViewContainerRef, TemplateRef, Inject} from "angular2/core";
4+
import {View} from "ui/core/view";
5+
import {Label} from "ui/label";
6+
import {TestApp} from "./test-app";
7+
8+
// >> third-party-simple-view-registration
9+
import {registerElement} from "nativescript-angular/element-registry";
10+
registerElement("third-party-view", () => require("./third-party-view").SimpleTag);
11+
// << third-party-simple-view-registration
12+
13+
// >> third-party-simple-view-container
14+
@Component({
15+
selector: 'simple-view-container',
16+
template: `
17+
<third-party-view prop1="value1"></third-party-view>
18+
`
19+
})
20+
export class SimpleViewContainer {
21+
}
22+
// << third-party-simple-view-container
23+
//
24+
// >> third-party-document-form-component
25+
@Component({
26+
selector: 'document-form',
27+
template: ''
28+
})
29+
export class DocumentFormComponent {
30+
// >> (hide)
31+
public static titleLabel: Label;
32+
// << (hide)
33+
34+
constructor() {
35+
}
36+
37+
public setTitleView(view: View) {
38+
// pass view parameter to native element...
39+
// >> (hide)
40+
DocumentFormComponent.titleLabel = <Label>view;
41+
// << (hide)
42+
}
43+
}
44+
// << third-party-document-form-component
45+
46+
// >> third-party-template-directive
47+
@Directive({
48+
selector: '[documentTitle]'
49+
})
50+
export class DocumentTitleDirective {
51+
public static titleLabel: Label;
52+
constructor(
53+
private ownerForm: DocumentFormComponent,
54+
private viewContainer: ViewContainerRef,
55+
private template: TemplateRef
56+
) {
57+
}
58+
59+
ngOnInit() {
60+
const viewRef = this.viewContainer.createEmbeddedView(this.template);
61+
//filter out whitespace nodes
62+
const titleViews = viewRef.rootNodes.filter((node) =>
63+
node && node.nodeName !== '#text')
64+
65+
if (titleViews.length > 0) {
66+
const titleView = titleViews[0];
67+
this.ownerForm.setTitleView(titleView);
68+
}
69+
}
70+
}
71+
// << third-party-template-directive
72+
73+
// >> third-party-document-form-container
74+
@Component({
75+
selector: 'document-form-container',
76+
directives: [DocumentFormComponent, DocumentTitleDirective],
77+
template: `
78+
<document-form src="document1.pdf">
79+
<Label *documentTitle text="Document1"></Label>
80+
</document-form>
81+
`
82+
})
83+
export class DocumentFormContainer {
84+
}
85+
// << third-party-document-form-container
86+
87+
describe('Third party component snippets', () => {
88+
let testApp: TestApp = null;
89+
90+
before(() => {
91+
registerElement("document-form", () => require("ui/layouts/stack-layout").StackLayout);
92+
93+
return TestApp.create().then((app) => {
94+
testApp = app;
95+
})
96+
});
97+
98+
after(() => {
99+
testApp.dispose();
100+
});
101+
102+
it("instantiates SimpleView from markup", () => {
103+
return testApp.loadComponent(SimpleViewContainer).then((componentRef) => {
104+
const componentInstance = componentRef.instance;
105+
assert.instanceOf(componentInstance, SimpleViewContainer);
106+
});
107+
});
108+
109+
it("renders DocumentForm with title template", () => {
110+
return testApp.loadComponent(DocumentFormContainer).then((componentRef) => {
111+
assert.equal("Document1", DocumentFormComponent.titleLabel.text);
112+
});
113+
});
114+
})

0 commit comments

Comments
 (0)