| 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 @Decorator( | 85 @NgDirective( |
| 86 selector: 'ng-pluralize', | 86 selector: 'ng-pluralize', |
| 87 map: const { 'count': '=>count' }) | 87 map: const { 'count': '=>count' }) |
| 88 @Decorator( | 88 @NgDirective( |
| 89 selector: '[ng-pluralize]', | 89 selector: '[ng-pluralize]', |
| 90 map: const { 'count': '=>count' }) | 90 map: const { 'count': '=>count' }) |
| 91 class NgPluralize { | 91 class NgPluralizeDirective { |
| 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 int _offset; | 95 final AstParser parser; |
| 96 final _discreteRules = <String, String>{}; | 96 int offset; |
| 97 final _categoryRules = <Symbol, String>{}; | 97 var discreteRules = <String, String>{}; |
| 98 final _expressionCache = <String, String>{}; | 98 var categoryRules = <Symbol, String>{}; |
| 99 FormatterMap _formatters; | |
| 100 | |
| 101 Watch _watch; | |
| 102 | 99 |
| 103 static final RegExp IS_WHEN = new RegExp(r'^when-(minus-)?.'); | 100 static final RegExp IS_WHEN = new RegExp(r'^when-(minus-)?.'); |
| 104 | |
| 105 static const Map<String, Symbol> SYMBOLS = const { | 101 static const Map<String, Symbol> SYMBOLS = const { |
| 106 'zero' : #zero, | 102 'zero' : #zero, |
| 107 'one' : #one, | 103 'one' : #one, |
| 108 'two' : #two, | 104 'two' : #two, |
| 109 'few' : #few, | 105 'few' : #few, |
| 110 'many' : #many, | 106 'many' : #many, |
| 111 'other' : #other, | 107 'other' : #other, |
| 112 }; | 108 }; |
| 113 | 109 |
| 114 NgPluralize(this._scope, this._element, this._interpolate, this._formatters) { | 110 NgPluralizeDirective(this.scope, this.element, this.interpolate, |
| 115 var attrs = _element.attributes; | 111 NodeAttrs attributes, this.parser) { |
| 116 final whens = attrs['when'] == null | 112 Map<String, String> whens = attributes['when'] == null |
| 117 ? <String, String>{} | 113 ? {} |
| 118 : _scope.eval(attrs['when']); | 114 : scope.eval(attributes['when']); |
| 119 _offset = attrs['offset'] == null ? 0 : int.parse(attrs['offset']); | 115 offset = attributes['offset'] == null ? 0 : int.parse(attributes['offset']); |
| 120 | 116 |
| 121 _element.attributes.keys.where((k) => IS_WHEN.hasMatch(k)).forEach((k) { | 117 element.attributes.keys.where((k) => IS_WHEN.hasMatch(k)).forEach((k) { |
| 122 var ruleName = k | 118 var ruleName = k.replaceFirst('when-', '').replaceFirst('minus-', '-'); |
| 123 .replaceFirst(new RegExp('^when-'), '') | 119 whens[ruleName] = element.attributes[k]; |
| 124 .replaceFirst(new RegExp('^minus-'), '-'); | |
| 125 whens[ruleName] = _element.attributes[k]; | |
| 126 }); | 120 }); |
| 127 | 121 |
| 128 if (whens['other'] == null) { | 122 if (whens['other'] == null) { |
| 129 throw "ngPluralize error! The 'other' plural category must always be " | 123 throw "ngPluralize error! The 'other' plural category must always be " |
| 130 "specified"; | 124 "specified"; |
| 131 } | 125 } |
| 132 | 126 |
| 133 whens.forEach((k, v) { | 127 whens.forEach((k, v) { |
| 134 Symbol symbol = SYMBOLS[k]; | 128 Symbol symbol = SYMBOLS[k]; |
| 135 if (symbol != null) { | 129 if (symbol != null) { |
| 136 _categoryRules[symbol] = v; | 130 this.categoryRules[symbol] = v; |
| 137 } else { | 131 } else { |
| 138 _discreteRules[k] = v; | 132 this.discreteRules[k] = v; |
| 139 } | 133 } |
| 140 }); | 134 }); |
| 141 } | 135 } |
| 142 | 136 |
| 143 void set count(value) { | 137 set count(value) { |
| 144 if (value is! num) { | 138 if (value is! num) { |
| 145 try { | 139 try { |
| 146 value = num.parse(value); | 140 value = int.parse(value); |
| 147 } catch(e) { | 141 } catch(e) { |
| 148 _element.text = ''; | 142 try { |
| 149 return; | 143 value = double.parse(value); |
| 144 } catch(e) { |
| 145 element.text = ''; |
| 146 return; |
| 147 } |
| 150 } | 148 } |
| 151 } | 149 } |
| 152 | 150 |
| 153 String stringValue = value.toString(); | 151 String stringValue = value.toString(); |
| 154 int intValue = value.toInt(); | 152 int intValue = value.toInt(); |
| 155 | 153 |
| 156 if (_discreteRules[stringValue] != null) { | 154 if (discreteRules[stringValue] != null) { |
| 157 _setAndWatch(_discreteRules[stringValue]); | 155 _setAndWatch(discreteRules[stringValue]); |
| 158 } else { | 156 } else { |
| 159 intValue -= _offset; | 157 intValue -= offset; |
| 160 var exp = Function.apply(Intl.plural, [intValue], _categoryRules); | 158 var exp = Function.apply(Intl.plural, [intValue], categoryRules); |
| 161 if (exp != null) { | 159 if (exp != null) { |
| 162 exp = exp.replaceAll(r'{}', (value - _offset).toString()); | 160 exp = exp.replaceAll(r'{}', (value - offset).toString()); |
| 163 _setAndWatch(exp); | 161 _setAndWatch(exp); |
| 164 } | 162 } |
| 165 } | 163 } |
| 166 } | 164 } |
| 167 | 165 |
| 168 void _setAndWatch(template) { | 166 _setAndWatch(expression) { |
| 169 if (_watch != null) _watch.remove(); | 167 var interpolation = interpolate(expression, false, '\${', '}'); |
| 170 var expression = _expressionCache.putIfAbsent(template, () => | 168 interpolation.setter = (text) => element.text = text; |
| 171 _interpolate(template, false, r'${', '}')); | 169 interpolation.setter(expression); |
| 172 _watch = _scope.watch(expression, _updateMarkup, formatters: _formatters); | 170 var items = interpolation.expressions.map((exp) => parser(exp)).toList(); |
| 173 } | 171 AST ast = new PureFunctionAST(expression, new ArrayFn(), items); |
| 174 | 172 scope.watch(ast, interpolation.call); |
| 175 void _updateMarkup(text, previousText) { | |
| 176 if (text != previousText) _element.text = text; | |
| 177 } | 173 } |
| 178 } | 174 } |
| OLD | NEW |