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