@@ -9,10 +9,10 @@ import './ng_dev_mode';
9
9
10
10
import { assertEqual } from './assert' ;
11
11
import { LContext , MONKEY_PATCH_KEY_NAME } from './interfaces/context' ;
12
- import { LElementNode , TNode , TNodeFlags } from './interfaces/node' ;
12
+ import { TNode , TNodeFlags } from './interfaces/node' ;
13
13
import { RElement } from './interfaces/renderer' ;
14
14
import { CONTEXT , HEADER_OFFSET , HOST , LViewData , TVIEW } from './interfaces/view' ;
15
- import { getComponentViewByIndex , readElementValue , readPatchedData } from './util' ;
15
+ import { getComponentViewByIndex , getNativeByTNode , readElementValue , readPatchedData } from './util' ;
16
16
17
17
18
18
/** Returns the matching `LContext` data for a given DOM node, directive or component instance.
@@ -66,11 +66,11 @@ export function getContext(target: any): LContext|null {
66
66
// are expensive. Instead, only the target data (the element, compontent or
67
67
// directive details) are filled into the context. If called multiple times
68
68
// with different target values then the missing target data will be filled in.
69
- const lNode = getLNodeFromViewData ( lViewData , nodeIndex ) ! ;
70
- const existingCtx = readPatchedData ( lNode . native ) ;
69
+ const native = readElementValue ( lViewData [ nodeIndex ] ) ;
70
+ const existingCtx = readPatchedData ( native ) ;
71
71
const context : LContext = ( existingCtx && ! Array . isArray ( existingCtx ) ) ?
72
72
existingCtx :
73
- createLContext ( lViewData , nodeIndex , lNode . native ) ;
73
+ createLContext ( lViewData , nodeIndex , native ) ;
74
74
75
75
// only when the component has been discovered then update the monkey-patch
76
76
if ( component && context . component === undefined ) {
@@ -114,9 +114,9 @@ export function getContext(target: any): LContext|null {
114
114
115
115
const index = findViaNativeElement ( lViewData , rElement ) ;
116
116
if ( index >= 0 ) {
117
- const lNode = getLNodeFromViewData ( lViewData , index ) ! ;
118
- const context = createLContext ( lViewData , index , lNode . native ) ;
119
- attachPatchData ( lNode . native , context ) ;
117
+ const native = readElementValue ( lViewData [ index ] ) ;
118
+ const context = createLContext ( lViewData , index , native ) ;
119
+ attachPatchData ( native , context ) ;
120
120
mpValue = context ;
121
121
break ;
122
122
}
@@ -129,10 +129,10 @@ export function getContext(target: any): LContext|null {
129
129
/**
130
130
* Creates an empty instance of a `LContext` context
131
131
*/
132
- function createLContext ( lViewData : LViewData , lNodeIndex : number , native : RElement ) : LContext {
132
+ function createLContext ( lViewData : LViewData , nodeIndex : number , native : RElement ) : LContext {
133
133
return {
134
134
lViewData,
135
- nodeIndex : lNodeIndex , native,
135
+ nodeIndex : nodeIndex , native,
136
136
component : undefined ,
137
137
directives : undefined ,
138
138
localRefs : undefined ,
@@ -150,9 +150,9 @@ export function getComponentViewByInstance(componentInstance: {}): LViewData {
150
150
let view : LViewData ;
151
151
152
152
if ( Array . isArray ( lViewData ) ) {
153
- const lNodeIndex = findViaComponent ( lViewData , componentInstance ) ;
154
- view = getComponentViewByIndex ( lNodeIndex , lViewData ) ;
155
- const context = createLContext ( lViewData , lNodeIndex , ( view [ HOST ] as LElementNode ) . native ) ;
153
+ const nodeIndex = findViaComponent ( lViewData , componentInstance ) ;
154
+ view = getComponentViewByIndex ( nodeIndex , lViewData ) ;
155
+ const context = createLContext ( lViewData , nodeIndex , view [ HOST ] as RElement ) ;
156
156
context . component = componentInstance ;
157
157
attachPatchData ( componentInstance , context ) ;
158
158
attachPatchData ( context . native , context ) ;
@@ -182,11 +182,11 @@ export function isDirectiveInstance(instance: any): boolean {
182
182
/**
183
183
* Locates the element within the given LViewData and returns the matching index
184
184
*/
185
- function findViaNativeElement ( lViewData : LViewData , native : RElement ) : number {
185
+ function findViaNativeElement ( lViewData : LViewData , target : RElement ) : number {
186
186
let tNode = lViewData [ TVIEW ] . firstChild ;
187
187
while ( tNode ) {
188
- const lNode = getLNodeFromViewData ( lViewData , tNode . index ) ! ;
189
- if ( lNode . native === native ) {
188
+ const native = getNativeByTNode ( tNode , lViewData ) ! ;
189
+ if ( native === target ) {
190
190
return tNode . index ;
191
191
}
192
192
tNode = traverseNextElement ( tNode ) ;
@@ -261,18 +261,6 @@ function assertDomElement(element: any) {
261
261
assertEqual ( element . nodeType , 1 , 'The provided value must be an instance of an HTMLElement' ) ;
262
262
}
263
263
264
- /**
265
- * Retruns the instance of the LElementNode at the given index in the LViewData.
266
- *
267
- * This function will also unwrap the inner value incase it's stuffed into an
268
- * array (which is what happens when [style] and [class] bindings are present
269
- * in the view instructions for the element being returned).
270
- */
271
- function getLNodeFromViewData ( lViewData : LViewData , lElementIndex : number ) : LElementNode | null {
272
- const value = lViewData [ lElementIndex ] ;
273
- return value ? readElementValue ( value ) : null ;
274
- }
275
-
276
264
/**
277
265
* Returns a list of directives extracted from the given view based on the
278
266
* provided list of directive index values.
@@ -294,17 +282,16 @@ export function discoverDirectives(
294
282
* Returns a map of local references (local reference name => element or directive instance) that
295
283
* exist on a given element.
296
284
*/
297
- export function discoverLocalRefs ( lViewData : LViewData , lNodeIndex : number ) : { [ key : string ] : any } |
285
+ export function discoverLocalRefs ( lViewData : LViewData , nodeIndex : number ) : { [ key : string ] : any } |
298
286
null {
299
- const tNode = lViewData [ TVIEW ] . data [ lNodeIndex ] as TNode ;
287
+ const tNode = lViewData [ TVIEW ] . data [ nodeIndex ] as TNode ;
300
288
if ( tNode && tNode . localNames ) {
301
289
const result : { [ key : string ] : any } = { } ;
302
290
for ( let i = 0 ; i < tNode . localNames . length ; i += 2 ) {
303
291
const localRefName = tNode . localNames [ i ] ;
304
292
const directiveIndex = tNode . localNames [ i + 1 ] as number ;
305
- result [ localRefName ] = directiveIndex === - 1 ?
306
- getLNodeFromViewData ( lViewData , lNodeIndex ) ! . native :
307
- lViewData [ directiveIndex ] ;
293
+ result [ localRefName ] =
294
+ directiveIndex === - 1 ? getNativeByTNode ( tNode , lViewData ) ! : lViewData [ directiveIndex ] ;
308
295
}
309
296
return result ;
310
297
}
0 commit comments