1
1
import { View } from 'ui/core/view' ;
2
+ import { Observable , EventData } from 'data/observable' ;
2
3
import { Injectable } from 'angular2/angular2' ;
3
4
import { MapWrapper } from 'angular2/src/facade/collection' ;
4
5
import { DomProtoView , resolveInternalDomProtoView } from 'angular2/src/render/dom/view/proto_view' ;
5
6
import { Renderer , RenderElementRef , RenderProtoViewRef , RenderViewRef , EventDispatcher } from 'angular2/src/render/api' ;
7
+ import { AST } from 'angular2/src/change_detection/parser/ast' ;
6
8
import { NG_BINDING_CLASS } from 'angular2/src/render/dom/util' ;
7
9
import { DOM } from 'angular2/src/dom/dom_adapter' ;
8
10
import { topmost } from 'ui/frame' ;
@@ -13,6 +15,8 @@ import {Label} from 'ui/label';
13
15
import { TextField } from 'ui/text-field' ;
14
16
15
17
export class NativeScriptView {
18
+ public eventDispatcher : EventDispatcher ;
19
+
16
20
constructor ( public proto : DomProtoView ,
17
21
public rootChildElements ,
18
22
public boundElements : Array < ViewNode > ,
@@ -36,6 +40,8 @@ interface ViewClass {
36
40
new ( ) : View
37
41
}
38
42
43
+ type EventHandler = ( args : EventData ) => void ;
44
+
39
45
export class ViewNode {
40
46
//TODO: move element registration and imports to a new module
41
47
private static allowedElements : Map < string , ViewClass > = new Map < string , ViewClass > ( [
@@ -45,6 +51,8 @@ export class ViewNode {
45
51
[ "label" , Label ]
46
52
] ) ;
47
53
54
+ private eventListeners : Map < string , EventHandler > = new Map < string , EventHandler > ( ) ;
55
+
48
56
private ui : View ;
49
57
private _parentView : View ;
50
58
private _attachedToView : boolean = false ;
@@ -102,6 +110,7 @@ export class ViewNode {
102
110
103
111
if ( ( < any > this . parentView ) . _addChildFromBuilder ) {
104
112
( < any > this . parentView ) . _addChildFromBuilder ( this . viewName , this . ui ) ;
113
+ this . attachUIEvents ( ) ;
105
114
} else {
106
115
throw new Error ( "Parent view can't have children! " + this . _parentView ) ;
107
116
}
@@ -129,6 +138,13 @@ export class ViewNode {
129
138
}
130
139
}
131
140
141
+ private attachUIEvents ( ) {
142
+ this . eventListeners . forEach ( ( callback , eventName ) => {
143
+ console . log ( 'Attaching event listener for: ' + eventName ) ;
144
+ this . ui . addEventListener ( eventName , callback ) ;
145
+ } ) ;
146
+ }
147
+
132
148
public insertChildAt ( index : number , childNode : ViewNode ) {
133
149
this . children [ index ] = childNode ;
134
150
childNode . parentNode = this ;
@@ -139,15 +155,20 @@ export class ViewNode {
139
155
140
156
setProperty ( name : string , value : any ) {
141
157
console . log ( this . viewName + ' setProperty ' + name + ' ' + value ) ;
142
- //this.ui._setValue(name, value);
143
158
if ( this . ui ) {
144
- //var page = topmost().currentPage;
145
- //var button = <Button> page.content;
146
- //(<Button>this.ui).text = value;
147
159
console . log ( 'actual setProperty ' ) ;
148
160
this . ui [ name ] = value ;
149
161
}
150
162
}
163
+
164
+ createEventListener ( view : NativeScriptView , bindingIndex : number , eventName : string , eventLocals : AST ) {
165
+ console . log ( 'createEventListener ' + this . viewName + ' ' + eventName + ' ' + eventLocals ) ;
166
+ this . eventListeners . set ( eventName , ( args : EventData ) => {
167
+ var locals = new Map < string , any > ( ) ;
168
+ locals . set ( '$event' , args ) ;
169
+ view . eventDispatcher . dispatchEvent ( bindingIndex , eventName , locals ) ;
170
+ } ) ;
171
+ }
151
172
}
152
173
153
174
@Injectable ( )
@@ -219,17 +240,22 @@ export class NativeScriptRenderer extends Renderer {
219
240
var element = view . boundElements [ location . boundElementIndex ] ;
220
241
element . setProperty ( propertyName , propertyValue ) ;
221
242
}
243
+
244
+ /**
245
+ * Calls a method on an element.
246
+ */
247
+ invokeElementMethod ( location : RenderElementRef , methodName : string , args : List < any > ) {
248
+ console . log ( "NativeScriptRenderer.invokeElementMethod " + methodName + " " + args ) ;
249
+ }
222
250
223
- callAction ( viewRef : RenderViewRef , elementIndex : number , actionExpression : string , actionArgs : any ) {
224
- console . log ( "NativeScriptRenderer.callAction " ) ;
225
- }
226
-
227
- setText ( viewRef : RenderViewRef , textNodeIndex : number , text : string ) {
228
- console . log ( "NativeScriptRenderer.setText " ) ;
229
- }
251
+ setText ( viewRef : RenderViewRef , textNodeIndex : number , text : string ) {
252
+ console . log ( "NativeScriptRenderer.setText " ) ;
253
+ }
230
254
231
255
setEventDispatcher ( viewRef : RenderViewRef , dispatcher : EventDispatcher ) {
232
256
console . log ( "NativeScriptRenderer.setEventDispatcher " ) ;
257
+ var view = ( < NativeScriptViewRef > viewRef ) . resolveView ( ) ;
258
+ view . eventDispatcher = dispatcher ;
233
259
}
234
260
235
261
_createView ( proto : DomProtoView , isRoot = false ) : NativeScriptView {
@@ -249,6 +275,19 @@ export class NativeScriptRenderer extends Renderer {
249
275
var boundTextNodes = this . _createBoundTextNodes ( proto , boundElements ) ;
250
276
var view = new NativeScriptView ( proto , nativeElements , boundElements , boundTextNodes ) ;
251
277
278
+ var binders = proto . elementBinders ;
279
+ for ( var binderIdx = 0 ; binderIdx < binders . length ; binderIdx ++ ) {
280
+ var binder = binders [ binderIdx ] ;
281
+ var viewNode = boundElements [ binderIdx ] ;
282
+
283
+ // events
284
+ if ( binder . eventLocals != null && binder . localEvents != null ) {
285
+ for ( var i = 0 ; i < binder . localEvents . length ; i ++ ) {
286
+ viewNode . createEventListener ( view , binderIdx , binder . localEvents [ i ] . name , binder . eventLocals ) ;
287
+ }
288
+ }
289
+ }
290
+
252
291
return view ;
253
292
}
254
293
0 commit comments