ng_plural.js
5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Attribute, Directive, Host, Input, TemplateRef, ViewContainerRef } from '@angular/core';
import { NgLocalization, getPluralCategory } from '../localization';
import { SwitchView } from './ng_switch';
/**
* \@ngModule CommonModule
*
* \@whatItDoes Adds / removes DOM sub-trees based on a numeric value. Tailored for pluralization.
*
* \@howToUse
* ```
* <some-element [ngPlural]="value">
* <template ngPluralCase="=0">there is nothing</template>
* <template ngPluralCase="=1">there is one</template>
* <template ngPluralCase="few">there are a few</template>
* </some-element>
* ```
*
* \@description
*
* Displays DOM sub-trees that match the switch expression value, or failing that, DOM sub-trees
* that match the switch expression's pluralization category.
*
* To use this directive you must provide a container element that sets the `[ngPlural]` attribute
* to a switch expression. Inner elements with a `[ngPluralCase]` will display based on their
* expression:
* - if `[ngPluralCase]` is set to a value starting with `=`, it will only display if the value
* matches the switch expression exactly,
* - otherwise, the view will be treated as a "category match", and will only display if exact
* value matches aren't found and the value maps to its category for the defined locale.
*
* See http://cldr.unicode.org/index/cldr-spec/plural-rules
*
* \@experimental
*/
export var NgPlural = (function () {
/**
* @param {?} _localization
*/
function NgPlural(_localization) {
this._localization = _localization;
this._caseViews = {};
}
Object.defineProperty(NgPlural.prototype, "ngPlural", {
/**
* @param {?} value
* @return {?}
*/
set: function (value) {
this._switchValue = value;
this._updateView();
},
enumerable: true,
configurable: true
});
/**
* @param {?} value
* @param {?} switchView
* @return {?}
*/
NgPlural.prototype.addCase = function (value, switchView) { this._caseViews[value] = switchView; };
/**
* @return {?}
*/
NgPlural.prototype._updateView = function () {
this._clearViews();
var /** @type {?} */ cases = Object.keys(this._caseViews);
var /** @type {?} */ key = getPluralCategory(this._switchValue, cases, this._localization);
this._activateView(this._caseViews[key]);
};
/**
* @return {?}
*/
NgPlural.prototype._clearViews = function () {
if (this._activeView)
this._activeView.destroy();
};
/**
* @param {?} view
* @return {?}
*/
NgPlural.prototype._activateView = function (view) {
if (view) {
this._activeView = view;
this._activeView.create();
}
};
NgPlural.decorators = [
{ type: Directive, args: [{ selector: '[ngPlural]' },] },
];
/** @nocollapse */
NgPlural.ctorParameters = function () { return [
{ type: NgLocalization, },
]; };
NgPlural.propDecorators = {
'ngPlural': [{ type: Input },],
};
return NgPlural;
}());
function NgPlural_tsickle_Closure_declarations() {
/** @type {?} */
NgPlural.decorators;
/**
* @nocollapse
* @type {?}
*/
NgPlural.ctorParameters;
/** @type {?} */
NgPlural.propDecorators;
/** @type {?} */
NgPlural.prototype._switchValue;
/** @type {?} */
NgPlural.prototype._activeView;
/** @type {?} */
NgPlural.prototype._caseViews;
/** @type {?} */
NgPlural.prototype._localization;
}
/**
* \@ngModule CommonModule
*
* \@whatItDoes Creates a view that will be added/removed from the parent {\@link NgPlural} when the
* given expression matches the plural expression according to CLDR rules.
*
* \@howToUse
* ```
* <some-element [ngPlural]="value">
* <template ngPluralCase="=0">...</template>
* <template ngPluralCase="other">...</template>
* </some-element>
* ```
*
* See {\@link NgPlural} for more details and example.
*
* \@experimental
*/
export var NgPluralCase = (function () {
/**
* @param {?} value
* @param {?} template
* @param {?} viewContainer
* @param {?} ngPlural
*/
function NgPluralCase(value, template, viewContainer, ngPlural) {
this.value = value;
var isANumber = !isNaN(Number(value));
ngPlural.addCase(isANumber ? "=" + value : value, new SwitchView(viewContainer, template));
}
NgPluralCase.decorators = [
{ type: Directive, args: [{ selector: '[ngPluralCase]' },] },
];
/** @nocollapse */
NgPluralCase.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Attribute, args: ['ngPluralCase',] },] },
{ type: TemplateRef, },
{ type: ViewContainerRef, },
{ type: NgPlural, decorators: [{ type: Host },] },
]; };
return NgPluralCase;
}());
function NgPluralCase_tsickle_Closure_declarations() {
/** @type {?} */
NgPluralCase.decorators;
/**
* @nocollapse
* @type {?}
*/
NgPluralCase.ctorParameters;
/** @type {?} */
NgPluralCase.prototype.value;
}
//# sourceMappingURL=ng_plural.js.map