| OLD | NEW |
| 1 part of angular.directive; | 1 part of angular.directive; |
| 2 | 2 |
| 3 /** | 3 /** |
| 4 * ## Overview | 4 * ## Overview |
| 5 * `ngPluralize` is a directive that displays messages according to locale rules
. | 5 * `ngPluralize` is a directive that displays messages according to locale rules
. |
| 6 * | 6 * |
| 7 * You configure ngPluralize directive by specifying the mappings between plural | 7 * You configure ngPluralize directive by specifying the mappings between plural |
| 8 * categories and the strings to be displayed. | 8 * categories and the strings to be displayed. |
| 9 * | 9 * |
| 10 * ## Plural categories and explicit number rules | 10 * ## Plural categories and explicit number rules |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 * shown. When three people view the document, no explicit number rule is found, | 75 * shown. When three people view the document, no explicit number rule is found, |
| 76 * so an offset of 2 is taken off 3, and Angular uses 1 to decide the plural | 76 * so an offset of 2 is taken off 3, and Angular uses 1 to decide the plural |
| 77 * category. In this case, plural category 'one' is matched and "John, Marry and | 77 * category. In this case, plural category 'one' is matched and "John, Marry and |
| 78 * one other person are viewing" is shown. | 78 * one other person are viewing" is shown. |
| 79 * | 79 * |
| 80 * Note that when you specify offsets, you must provide explicit number rules | 80 * Note that when you specify offsets, you must provide explicit number rules |
| 81 * for numbers from 0 up to and including the offset. If you use an offset of 3, | 81 * for numbers from 0 up to and including the offset. If you use an offset of 3, |
| 82 * for example, you must provide explicit number rules for 0, 1, 2 and 3. You | 82 * for example, you must provide explicit number rules for 0, 1, 2 and 3. You |
| 83 * must also provide plural strings for at least the "other" plural category. | 83 * must also provide plural strings for at least the "other" plural category. |
| 84 */ | 84 */ |
| 85 @NgDirective( | 85 @Decorator( |
| 86 selector: 'ng-pluralize', | 86 selector: 'ng-pluralize', |
| 87 map: const { 'count': '=>count' }) | 87 map: const { 'count': '=>count' }) |
| 88 @NgDirective( | 88 @Decorator( |
| 89 selector: '[ng-pluralize]', | 89 selector: '[ng-pluralize]', |
| 90 map: const { 'count': '=>count' }) | 90 map: const { 'count': '=>count' }) |
| 91 class NgPluralizeDirective { | 91 class NgPluralize { |
| 92 final dom.Element element; | 92 final dom.Element _element; |
| 93 final Scope scope; | 93 final Scope _scope; |
| 94 final Interpolate interpolate; | 94 final Interpolate _interpolate; |
| 95 final AstParser parser; | 95 int _offset; |
| 96 int offset; | 96 final _discreteRules = <String, String>{}; |
| 97 var discreteRules = <String, String>{}; | 97 final _categoryRules = <Symbol, String>{}; |
| 98 var categoryRules = <Symbol, String>{}; | 98 final _expressionCache = <String, String>{}; |
| 99 FormatterMap _formatters; |
| 100 |
| 101 Watch _watch; |
| 99 | 102 |
| 100 static final RegExp IS_WHEN = new RegExp(r'^when-(minus-)?.'); | 103 static final RegExp IS_WHEN = new RegExp(r'^when-(minus-)?.'); |
| 104 |
| 101 static const Map<String, Symbol> SYMBOLS = const { | 105 static const Map<String, Symbol> SYMBOLS = const { |
| 102 'zero' : #zero, | 106 'zero' : #zero, |
| 103 'one' : #one, | 107 'one' : #one, |
| 104 'two' : #two, | 108 'two' : #two, |
| 105 'few' : #few, | 109 'few' : #few, |
| 106 'many' : #many, | 110 'many' : #many, |
| 107 'other' : #other, | 111 'other' : #other, |
| 108 }; | 112 }; |
| 109 | 113 |
| 110 NgPluralizeDirective(this.scope, this.element, this.interpolate, | 114 NgPluralize(this._scope, this._element, this._interpolate, this._formatters) { |
| 111 NodeAttrs attributes, this.parser) { | 115 var attrs = _element.attributes; |
| 112 Map<String, String> whens = attributes['when'] == null | 116 final whens = attrs['when'] == null |
| 113 ? {} | 117 ? <String, String>{} |
| 114 : scope.eval(attributes['when']); | 118 : _scope.eval(attrs['when']); |
| 115 offset = attributes['offset'] == null ? 0 : int.parse(attributes['offset']); | 119 _offset = attrs['offset'] == null ? 0 : int.parse(attrs['offset']); |
| 116 | 120 |
| 117 element.attributes.keys.where((k) => IS_WHEN.hasMatch(k)).forEach((k) { | 121 _element.attributes.keys.where((k) => IS_WHEN.hasMatch(k)).forEach((k) { |
| 118 var ruleName = k.replaceFirst('when-', '').replaceFirst('minus-', '-'); | 122 var ruleName = k |
| 119 whens[ruleName] = element.attributes[k]; | 123 .replaceFirst(new RegExp('^when-'), '') |
| 124 .replaceFirst(new RegExp('^minus-'), '-'); |
| 125 whens[ruleName] = _element.attributes[k]; |
| 120 }); | 126 }); |
| 121 | 127 |
| 122 if (whens['other'] == null) { | 128 if (whens['other'] == null) { |
| 123 throw "ngPluralize error! The 'other' plural category must always be " | 129 throw "ngPluralize error! The 'other' plural category must always be " |
| 124 "specified"; | 130 "specified"; |
| 125 } | 131 } |
| 126 | 132 |
| 127 whens.forEach((k, v) { | 133 whens.forEach((k, v) { |
| 128 Symbol symbol = SYMBOLS[k]; | 134 Symbol symbol = SYMBOLS[k]; |
| 129 if (symbol != null) { | 135 if (symbol != null) { |
| 130 this.categoryRules[symbol] = v; | 136 _categoryRules[symbol] = v; |
| 131 } else { | 137 } else { |
| 132 this.discreteRules[k] = v; | 138 _discreteRules[k] = v; |
| 133 } | 139 } |
| 134 }); | 140 }); |
| 135 } | 141 } |
| 136 | 142 |
| 137 set count(value) { | 143 void set count(value) { |
| 138 if (value is! num) { | 144 if (value is! num) { |
| 139 try { | 145 try { |
| 140 value = int.parse(value); | 146 value = num.parse(value); |
| 141 } catch(e) { | 147 } catch(e) { |
| 142 try { | 148 _element.text = ''; |
| 143 value = double.parse(value); | 149 return; |
| 144 } catch(e) { | |
| 145 element.text = ''; | |
| 146 return; | |
| 147 } | |
| 148 } | 150 } |
| 149 } | 151 } |
| 150 | 152 |
| 151 String stringValue = value.toString(); | 153 String stringValue = value.toString(); |
| 152 int intValue = value.toInt(); | 154 int intValue = value.toInt(); |
| 153 | 155 |
| 154 if (discreteRules[stringValue] != null) { | 156 if (_discreteRules[stringValue] != null) { |
| 155 _setAndWatch(discreteRules[stringValue]); | 157 _setAndWatch(_discreteRules[stringValue]); |
| 156 } else { | 158 } else { |
| 157 intValue -= offset; | 159 intValue -= _offset; |
| 158 var exp = Function.apply(Intl.plural, [intValue], categoryRules); | 160 var exp = Function.apply(Intl.plural, [intValue], _categoryRules); |
| 159 if (exp != null) { | 161 if (exp != null) { |
| 160 exp = exp.replaceAll(r'{}', (value - offset).toString()); | 162 exp = exp.replaceAll(r'{}', (value - _offset).toString()); |
| 161 _setAndWatch(exp); | 163 _setAndWatch(exp); |
| 162 } | 164 } |
| 163 } | 165 } |
| 164 } | 166 } |
| 165 | 167 |
| 166 _setAndWatch(expression) { | 168 void _setAndWatch(template) { |
| 167 var interpolation = interpolate(expression, false, '\${', '}'); | 169 if (_watch != null) _watch.remove(); |
| 168 interpolation.setter = (text) => element.text = text; | 170 var expression = _expressionCache.putIfAbsent(template, () => |
| 169 interpolation.setter(expression); | 171 _interpolate(template, false, r'${', '}')); |
| 170 var items = interpolation.expressions.map((exp) => parser(exp)).toList(); | 172 _watch = _scope.watch(expression, _updateMarkup, formatters: _formatters); |
| 171 AST ast = new PureFunctionAST(expression, new ArrayFn(), items); | 173 } |
| 172 scope.watch(ast, interpolation.call); | 174 |
| 175 void _updateMarkup(text, previousText) { |
| 176 if (text != previousText) _element.text = text; |
| 173 } | 177 } |
| 174 } | 178 } |
| OLD | NEW |