|
| 1 | +import {StringWrapper, RegExpWrapper, BaseException, isPresent, isBlank, isString, stringify} from 'angular2/src/facade/lang'; |
| 2 | +import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection'; |
| 3 | +import {DOM} from 'angular2/src/dom/dom_adapter'; |
| 4 | +import {reflector} from 'angular2/src/reflection/reflection'; |
| 5 | + |
| 6 | +var DASH_CASE_REGEXP = RegExpWrapper.create('-([a-z])'); |
| 7 | +var CAMEL_CASE_REGEXP = RegExpWrapper.create('([A-Z])'); |
| 8 | + |
| 9 | +export function dashCaseToCamelCase(input:string): string { |
| 10 | + return StringWrapper.replaceAllMapped(input, DASH_CASE_REGEXP, (m) => { |
| 11 | + return m[1].toUpperCase(); |
| 12 | + }); |
| 13 | +} |
| 14 | + |
| 15 | +export function camelCaseToDashCase(input:string): string { |
| 16 | + return StringWrapper.replaceAllMapped(input, CAMEL_CASE_REGEXP, (m) => { |
| 17 | + return '-' + m[1].toLowerCase(); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +const STYLE_SEPARATOR = '.'; |
| 22 | +var propertySettersCache = StringMapWrapper.create(); |
| 23 | +var innerHTMLSetterCache; |
| 24 | + |
| 25 | +export function setterFactory(property: string): Function { |
| 26 | + var setterFn, styleParts, styleSuffix; |
| 27 | + if (StringWrapper.startsWith(property, ATTRIBUTE_PREFIX)) { |
| 28 | + setterFn = attributeSetterFactory(StringWrapper.substring(property, ATTRIBUTE_PREFIX.length)); |
| 29 | + } else if (StringWrapper.startsWith(property, CLASS_PREFIX)) { |
| 30 | + setterFn = classSetterFactory(StringWrapper.substring(property, CLASS_PREFIX.length)); |
| 31 | + } else if (StringWrapper.startsWith(property, STYLE_PREFIX)) { |
| 32 | + styleParts = property.split(STYLE_SEPARATOR); |
| 33 | + styleSuffix = styleParts.length > 2 ? ListWrapper.get(styleParts, 2) : ''; |
| 34 | + setterFn = styleSetterFactory(ListWrapper.get(styleParts, 1), styleSuffix); |
| 35 | + } else if (StringWrapper.equals(property, 'innerHtml')) { |
| 36 | + if (isBlank(innerHTMLSetterCache)) { |
| 37 | + innerHTMLSetterCache = (el, value) => DOM.setInnerHTML(el, value); |
| 38 | + } |
| 39 | + setterFn = innerHTMLSetterCache; |
| 40 | + } else { |
| 41 | + property = resolvePropertyName(property); |
| 42 | + setterFn = StringMapWrapper.get(propertySettersCache, property); |
| 43 | + if (isBlank(setterFn)) { |
| 44 | + var propertySetterFn = reflector.setter(property); |
| 45 | + setterFn = function(receiver, value) { |
| 46 | + if (DOM.hasProperty(receiver, property)) { |
| 47 | + return propertySetterFn(receiver, value); |
| 48 | + } |
| 49 | + } |
| 50 | + StringMapWrapper.set(propertySettersCache, property, setterFn); |
| 51 | + } |
| 52 | + } |
| 53 | + return setterFn; |
| 54 | +} |
| 55 | + |
| 56 | +const ATTRIBUTE_PREFIX = 'attr.'; |
| 57 | +var attributeSettersCache = StringMapWrapper.create(); |
| 58 | + |
| 59 | +function _isValidAttributeValue(attrName:string, value: any): boolean { |
| 60 | + if (attrName == "role") { |
| 61 | + return isString(value); |
| 62 | + } else { |
| 63 | + return isPresent(value); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +function attributeSetterFactory(attrName:string): Function { |
| 68 | + var setterFn = StringMapWrapper.get(attributeSettersCache, attrName); |
| 69 | + var dashCasedAttributeName; |
| 70 | + |
| 71 | + if (isBlank(setterFn)) { |
| 72 | + dashCasedAttributeName = camelCaseToDashCase(attrName); |
| 73 | + setterFn = function(element, value) { |
| 74 | + if (_isValidAttributeValue(dashCasedAttributeName, value)) { |
| 75 | + DOM.setAttribute(element, dashCasedAttributeName, stringify(value)); |
| 76 | + } else { |
| 77 | + if (isPresent(value)) { |
| 78 | + throw new BaseException("Invalid " + dashCasedAttributeName + |
| 79 | + " attribute, only string values are allowed, got '" + stringify(value) + "'"); |
| 80 | + } |
| 81 | + DOM.removeAttribute(element, dashCasedAttributeName); |
| 82 | + } |
| 83 | + }; |
| 84 | + StringMapWrapper.set(attributeSettersCache, attrName, setterFn); |
| 85 | + } |
| 86 | + |
| 87 | + return setterFn; |
| 88 | +} |
| 89 | + |
| 90 | +const CLASS_PREFIX = 'class.'; |
| 91 | +var classSettersCache = StringMapWrapper.create(); |
| 92 | + |
| 93 | +function classSetterFactory(className:string): Function { |
| 94 | + var setterFn = StringMapWrapper.get(classSettersCache, className); |
| 95 | + |
| 96 | + if (isBlank(setterFn)) { |
| 97 | + setterFn = function(element, value) { |
| 98 | + if (value) { |
| 99 | + DOM.addClass(element, className); |
| 100 | + } else { |
| 101 | + DOM.removeClass(element, className); |
| 102 | + } |
| 103 | + }; |
| 104 | + StringMapWrapper.set(classSettersCache, className, setterFn); |
| 105 | + } |
| 106 | + |
| 107 | + return setterFn; |
| 108 | +} |
| 109 | + |
| 110 | +const STYLE_PREFIX = 'style.'; |
| 111 | +var styleSettersCache = StringMapWrapper.create(); |
| 112 | + |
| 113 | +function styleSetterFactory(styleName:string, styleSuffix:string): Function { |
| 114 | + var cacheKey = styleName + styleSuffix; |
| 115 | + var setterFn = StringMapWrapper.get(styleSettersCache, cacheKey); |
| 116 | + var dashCasedStyleName; |
| 117 | + |
| 118 | + if (isBlank(setterFn)) { |
| 119 | + dashCasedStyleName = camelCaseToDashCase(styleName); |
| 120 | + setterFn = function(element, value) { |
| 121 | + var valAsStr; |
| 122 | + if (isPresent(value)) { |
| 123 | + valAsStr = stringify(value); |
| 124 | + DOM.setStyle(element, dashCasedStyleName, valAsStr + styleSuffix); |
| 125 | + } else { |
| 126 | + DOM.removeStyle(element, dashCasedStyleName); |
| 127 | + } |
| 128 | + }; |
| 129 | + StringMapWrapper.set(styleSettersCache, cacheKey, setterFn); |
| 130 | + } |
| 131 | + |
| 132 | + return setterFn; |
| 133 | +} |
| 134 | + |
| 135 | +function resolvePropertyName(attrName:string): string { |
| 136 | + var mappedPropName = StringMapWrapper.get(DOM.attrToPropMap, attrName); |
| 137 | + return isPresent(mappedPropName) ? mappedPropName : attrName; |
| 138 | +} |
0 commit comments