| OLD | NEW |
| 1 part of angular.directive; | 1 part of angular.directive; |
| 2 | 2 |
| 3 /** | 3 /** |
| 4 * Allows adding and removing the boolean attributes from the element. | 4 * Allows adding and removing the boolean attributes from the element. |
| 5 * | 5 * |
| 6 * Using `<button disabled="{{false}}">` does not work since it would result | 6 * Using `<button disabled="{{false}}">` does not work since it would result |
| 7 * in `<button disabled="false">` rather than `<button>`. | 7 * in `<button disabled="false">` rather than `<button>`. |
| 8 * Browsers change behavior based on presence/absence of the attribute rather | 8 * Browsers change behavior based on presence/absence of attribute rather the |
| 9 * its value. | 9 * its value. |
| 10 * | 10 * |
| 11 * For this reason we provide alternate `ng-`attribute directives to | 11 * For this reason we provide alternate `ng-`attribute directives to |
| 12 * add/remove boolean attributes such as `<button ng-disabled="{{false}}">` | 12 * add/remove boolean attributes such as `<button ng-disabled="{{false}}">` |
| 13 * which will result in proper removal of the attribute. | 13 * which will result in proper removal of the attribute. |
| 14 * | 14 * |
| 15 * The full list of supported attributes are: | 15 * The full list of supported attributes are: |
| 16 * | 16 * |
| 17 * - [ng-checked] | 17 * - [ng-checked] |
| 18 * - [ng-disabled] | 18 * - [ng-disabled] |
| 19 * - [ng-multiple] | 19 * - [ng-multiple] |
| 20 * - [ng-open] | 20 * - [ng-open] |
| 21 * - [ng-readonly] | 21 * - [ng-readonly] |
| 22 * - [ng-required] | 22 * - [ng-required] |
| 23 * - [ng-selected] | 23 * - [ng-selected] |
| 24 */ | 24 */ |
| 25 @Decorator(selector: '[ng-checked]', map: const {'ng-checked': '=>checked'}) | 25 @NgDirective(selector: '[ng-checked]', map: const {'ng-checked': '=>checked'}) |
| 26 @Decorator(selector: '[ng-disabled]', map: const {'ng-disabled': '=>disabled'}) | 26 @NgDirective(selector: '[ng-disabled]', map: const {'ng-disabled': '=>disabled'}
) |
| 27 @Decorator(selector: '[ng-multiple]', map: const {'ng-multiple': '=>multiple'}) | 27 @NgDirective(selector: '[ng-multiple]', map: const {'ng-multiple': '=>multiple'}
) |
| 28 @Decorator(selector: '[ng-open]', map: const {'ng-open': '=>open'}) | 28 @NgDirective(selector: '[ng-open]', map: const {'ng-open': '=>open'}) |
| 29 @Decorator(selector: '[ng-readonly]', map: const {'ng-readonly': '=>readonly'}) | 29 @NgDirective(selector: '[ng-readonly]', map: const {'ng-readonly': '=>readonly'}
) |
| 30 @Decorator(selector: '[ng-required]', map: const {'ng-required': '=>required'}) | 30 @NgDirective(selector: '[ng-required]', map: const {'ng-required': '=>required'}
) |
| 31 @Decorator(selector: '[ng-selected]', map: const {'ng-selected': '=>selected'}) | 31 @NgDirective(selector: '[ng-selected]', map: const {'ng-selected': '=>selected'}
) |
| 32 class NgBooleanAttribute { | 32 class NgBooleanAttributeDirective { |
| 33 final NgElement _ngElement; | 33 final NodeAttrs attrs; |
| 34 NgBooleanAttributeDirective(this.attrs); |
| 34 | 35 |
| 35 NgBooleanAttribute(this._ngElement); | 36 _setBooleanAttribute(name, value) => attrs[name] = (toBool(value) ? '' : null)
; |
| 36 | 37 |
| 37 void set checked(on) => _toggleAttribute('checked', on); | 38 set checked(value) => _setBooleanAttribute('checked', value); |
| 38 void set disabled(on) => _toggleAttribute('disabled', on); | 39 set disabled(value) => _setBooleanAttribute('disabled', value); |
| 39 void set multiple(on) => _toggleAttribute('multiple', on); | 40 set multiple(value) => _setBooleanAttribute('multiple', value); |
| 40 void set open(on) => _toggleAttribute('open', on); | 41 set open(value) => _setBooleanAttribute('open', value); |
| 41 void set readonly(on) => _toggleAttribute('readonly', on); | 42 set readonly(value) => _setBooleanAttribute('readonly', value); |
| 42 void set required(on) => _toggleAttribute('required', on); | 43 set required(value) => _setBooleanAttribute('required', value); |
| 43 void set selected(on) => _toggleAttribute('selected', on); | 44 set selected(value) => _setBooleanAttribute('selected', value); |
| 44 | |
| 45 void _toggleAttribute(attrName, on) { | |
| 46 if (toBool(on)) { | |
| 47 _ngElement.setAttribute(attrName); | |
| 48 } else { | |
| 49 _ngElement.removeAttribute(attrName); | |
| 50 } | |
| 51 } | |
| 52 } | 45 } |
| 53 | 46 |
| 54 /** | 47 /** |
| 55 * In browser some attributes have network side-effect. If the attribute | 48 * In browser some attributes have network side-effect. If the attribute |
| 56 * has `{{interpolation}}` in it it may cause browser to fetch bogus URLs. | 49 * has `{{interpolation}}` in it it may cause browser to fetch bogus URLs. |
| 57 * | 50 * |
| 58 * Example: In `<img src="{{username}}.png">` the browser will fetch the image | 51 * Example: In `<img src="{{username}}.png">` the browser will fetch the image |
| 59 * `http://server/{{username}}.png` before Angular has a chance to replace the | 52 * `http://server/{{username}}.png` before Angular has a chance to replace the |
| 60 * attribute with data-bound url. | 53 * attribute with data-bound url. |
| 61 * | 54 * |
| 62 * For this reason we provide `ng-`prefixed attributes which avoid the issues | 55 * For this reason we provide `ng-`prefixed attributes which avoid the issues |
| 63 * mentioned above as in this example: `<img ng-src="{{username}}.png">`. | 56 * mentioned above as in this example: `<img ng-src="{{username}}.png">`. |
| 64 * | 57 * |
| 65 * The full list of supported attributes are: | 58 * The full list of supported attributes are: |
| 66 * | 59 * |
| 67 * - [ng-href] | 60 * - [ng-href] |
| 68 * - [ng-src] | 61 * - [ng-src] |
| 69 * - [ng-srcset] | 62 * - [ng-srcset] |
| 70 */ | 63 */ |
| 71 @Decorator(selector: '[ng-href]', map: const {'ng-href': '@href'}) | 64 @NgDirective(selector: '[ng-href]', map: const {'ng-href': '@href'}) |
| 72 @Decorator(selector: '[ng-src]', map: const {'ng-src': '@src'}) | 65 @NgDirective(selector: '[ng-src]', map: const {'ng-src': '@src'}) |
| 73 @Decorator(selector: '[ng-srcset]', map: const {'ng-srcset': '@srcset'}) | 66 @NgDirective(selector: '[ng-srcset]', map: const {'ng-srcset': '@srcset'}) |
| 74 class NgSource { | 67 class NgSourceDirective { |
| 75 final NgElement _ngElement; | 68 final NodeAttrs attrs; |
| 76 NgSource(this._ngElement); | 69 NgSourceDirective(this.attrs); |
| 77 | 70 |
| 78 void set href(value) => _ngElement.setAttribute('href', value); | 71 set href(value) => attrs['href'] = value; |
| 79 void set src(value) => _ngElement.setAttribute('src', value); | 72 set src(value) => attrs['src'] = value; |
| 80 void set srcset(value) => _ngElement.setAttribute('srcset', value); | 73 set srcset(value) => attrs['srcset'] = value; |
| 81 | 74 |
| 82 } | 75 } |
| 83 | 76 |
| 84 /** | 77 /** |
| 85 * In SVG some attributes have a specific syntax. Placing `{{interpolation}}` in | 78 * In SVG some attributes have a specific syntax. Placing `{{interpolation}}` in |
| 86 * those attributes will break the attribute syntax, and browser will clear the | 79 * those attributes will break the attribute syntax, and browser will clear the |
| 87 * attribute. | 80 * attribute. |
| 88 * | 81 * |
| 89 * The `ng-attr-*` is a generic way to use interpolation without breaking the | 82 * The `ng-attr-*` is a generic way to use interpolation without breaking the |
| 90 * attribute syntax validator. The `ng-attr-` part get stripped. | 83 * attribute syntax validator. The `ng-attr-` part get stripped. |
| 91 * | 84 * |
| 92 * @example | 85 * @example |
| 93 * <svg> | 86 * <svg> |
| 94 * <circle ng-attr-cx="{{cx}}"></circle> | 87 * <circle ng-attr-cx="{{cx}}"></circle> |
| 95 * </svg> | 88 * </svg> |
| 96 */ | 89 */ |
| 97 @Decorator(selector: '[ng-attr-*]') | 90 @NgDirective(selector: '[ng-attr-*]') |
| 98 class NgAttribute implements AttachAware { | 91 class NgAttributeDirective implements NgAttachAware { |
| 99 final NodeAttrs _attrs; | 92 final NodeAttrs _attrs; |
| 100 | 93 |
| 101 NgAttribute(this._attrs); | 94 NgAttributeDirective(this._attrs); |
| 102 | 95 |
| 103 void attach() { | 96 void attach() { |
| 104 String ngAttrPrefix = 'ng-attr-'; | 97 String ngAttrPrefix = 'ng-attr-'; |
| 105 _attrs.forEach((key, value) { | 98 _attrs.forEach((key, value) { |
| 106 if (key.startsWith(ngAttrPrefix)) { | 99 if (key.startsWith(ngAttrPrefix)) { |
| 107 var newKey = key.substring(ngAttrPrefix.length); | 100 var newKey = key.substring(ngAttrPrefix.length); |
| 108 _attrs[newKey] = value; | 101 _attrs[newKey] = value; |
| 109 _attrs.observe(key, (newValue) => _attrs[newKey] = newValue ); | 102 _attrs.observe(key, (newValue) => _attrs[newKey] = newValue ); |
| 110 } | 103 } |
| 111 }); | 104 }); |
| 112 } | 105 } |
| 113 } | 106 } |
| OLD | NEW |