1- import { int , isPresent , isBlank , Type , BaseException , StringWrapper , RegExpWrapper , isString , stringify } from 'angular2/src/facade/lang' ;
2- import { DOM } from 'angular2/src/dom/dom_adapter' ;
1+ import { int , isPresent , isBlank } from 'angular2/src/facade/lang' ;
32import { ListWrapper , List , MapWrapper , StringMapWrapper } from 'angular2/src/facade/collection' ;
43
54import { reflector } from 'angular2/src/reflection/reflection' ;
@@ -11,88 +10,8 @@ import {DirectiveMetadata} from '../directive_metadata';
1110import { CompileStep } from './compile_step' ;
1211import { CompileElement } from './compile_element' ;
1312import { CompileControl } from './compile_control' ;
14- import { dashCaseToCamelCase , camelCaseToDashCase } from './util' ;
15-
16- var DOT_REGEXP = RegExpWrapper . create ( '\\.' ) ;
17-
18- const ATTRIBUTE_PREFIX = 'attr.' ;
19- var attributeSettersCache = StringMapWrapper . create ( ) ;
20-
21- function _isValidAttributeValue ( attrName :string , value : any ) {
22- if ( attrName == "role" ) {
23- return isString ( value ) ;
24- } else {
25- return isPresent ( value ) ;
26- }
27- }
28-
29- function attributeSetterFactory ( attrName :string ) {
30- var setterFn = StringMapWrapper . get ( attributeSettersCache , attrName ) ;
31- var dashCasedAttributeName ;
32-
33- if ( isBlank ( setterFn ) ) {
34- dashCasedAttributeName = camelCaseToDashCase ( attrName ) ;
35- setterFn = function ( element , value ) {
36- if ( _isValidAttributeValue ( dashCasedAttributeName , value ) ) {
37- DOM . setAttribute ( element , dashCasedAttributeName , stringify ( value ) ) ;
38- } else {
39- DOM . removeAttribute ( element , dashCasedAttributeName ) ;
40- if ( isPresent ( value ) ) {
41- throw new BaseException ( "Invalid " + dashCasedAttributeName + " attribute, only string values are allowed, got '" + stringify ( value ) + "'" ) ;
42- }
43- }
44- } ;
45- StringMapWrapper . set ( attributeSettersCache , attrName , setterFn ) ;
46- }
47-
48- return setterFn ;
49- }
50-
51- const CLASS_PREFIX = 'class.' ;
52- var classSettersCache = StringMapWrapper . create ( ) ;
53-
54- function classSetterFactory ( className :string ) {
55- var setterFn = StringMapWrapper . get ( classSettersCache , className ) ;
56-
57- if ( isBlank ( setterFn ) ) {
58- setterFn = function ( element , value ) {
59- if ( value ) {
60- DOM . addClass ( element , className ) ;
61- } else {
62- DOM . removeClass ( element , className ) ;
63- }
64- } ;
65- StringMapWrapper . set ( classSettersCache , className , setterFn ) ;
66- }
67-
68- return setterFn ;
69- }
70-
71- const STYLE_PREFIX = 'style.' ;
72- var styleSettersCache = StringMapWrapper . create ( ) ;
73-
74- function styleSetterFactory ( styleName :string , stylesuffix :string ) {
75- var cacheKey = styleName + stylesuffix ;
76- var setterFn = StringMapWrapper . get ( styleSettersCache , cacheKey ) ;
77- var dashCasedStyleName ;
78-
79- if ( isBlank ( setterFn ) ) {
80- dashCasedStyleName = camelCaseToDashCase ( styleName ) ;
81- setterFn = function ( element , value ) {
82- var valAsStr ;
83- if ( isPresent ( value ) ) {
84- valAsStr = stringify ( value ) ;
85- DOM . setStyle ( element , dashCasedStyleName , valAsStr + stylesuffix ) ;
86- } else {
87- DOM . removeStyle ( element , dashCasedStyleName ) ;
88- }
89- } ;
90- StringMapWrapper . set ( classSettersCache , cacheKey , setterFn ) ;
91- }
92-
93- return setterFn ;
94- }
95-
13+ import { dashCaseToCamelCase } from './util' ;
14+ import { setterFactory } from '../property_setter_factory'
9615
9716/**
9817 * Creates the ElementBinders and adds watches to the
@@ -178,28 +97,7 @@ export class ElementBinderBuilder extends CompileStep {
17897
17998 _bindElementProperties ( protoView , compileElement ) {
18099 MapWrapper . forEach ( compileElement . propertyBindings , ( expression , property ) => {
181- var setterFn , styleParts , styleSuffix ;
182-
183- if ( StringWrapper . startsWith ( property , ATTRIBUTE_PREFIX ) ) {
184- setterFn = attributeSetterFactory ( StringWrapper . substring ( property , ATTRIBUTE_PREFIX . length ) ) ;
185- } else if ( StringWrapper . startsWith ( property , CLASS_PREFIX ) ) {
186- setterFn = classSetterFactory ( StringWrapper . substring ( property , CLASS_PREFIX . length ) ) ;
187- } else if ( StringWrapper . startsWith ( property , STYLE_PREFIX ) ) {
188- styleParts = StringWrapper . split ( property , DOT_REGEXP ) ;
189- styleSuffix = styleParts . length > 2 ? ListWrapper . get ( styleParts , 2 ) : '' ;
190- setterFn = styleSetterFactory ( ListWrapper . get ( styleParts , 1 ) , styleSuffix ) ;
191- } else if ( StringWrapper . equals ( property , 'innerHtml' ) ) {
192- setterFn = ( element , value ) => DOM . setInnerHTML ( element , value ) ;
193- } else {
194- property = this . _resolvePropertyName ( property ) ;
195- var propertySetterFn = reflector . setter ( property ) ;
196- setterFn = function ( receiver , value ) {
197- if ( DOM . hasProperty ( receiver , property ) ) {
198- return propertySetterFn ( receiver , value ) ;
199- }
200- }
201- }
202-
100+ var setterFn = setterFactory ( property ) ;
203101 protoView . bindElementProperty ( expression . ast , property , setterFn ) ;
204102 } ) ;
205103 }
@@ -263,9 +161,4 @@ export class ElementBinderBuilder extends CompileStep {
263161 _splitBindConfig ( bindConfig :string ) {
264162 return ListWrapper . map ( bindConfig . split ( '|' ) , ( s ) => s . trim ( ) ) ;
265163 }
266-
267- _resolvePropertyName ( attrName :string ) {
268- var mappedPropName = StringMapWrapper . get ( DOM . attrToPropMap , attrName ) ;
269- return isPresent ( mappedPropName ) ? mappedPropName : attrName ;
270- }
271164}
0 commit comments