UNPKG

6.04 kBJavaScriptView Raw
1import { Component, forwardRef, Input } from '@angular/core';
2import { NG_VALUE_ACCESSOR } from '@angular/forms';
3var noop = function () {
4};
5export var RATING_CONTROL_VALUE_ACCESSOR = {
6 provide: NG_VALUE_ACCESSOR,
7 useExisting: forwardRef(function () { return Ionic2Rating; }),
8 multi: true
9};
10export var Ionic2Rating = (function () {
11 function Ionic2Rating() {
12 this._max = 5;
13 this._readOnly = false;
14 this._emptyStarIconName = 'star-outline';
15 this._halfStarIconName = 'star-half';
16 this._starIconName = 'star';
17 this._nullable = false;
18 this.onChangeCallback = noop;
19 }
20 Object.defineProperty(Ionic2Rating.prototype, "max", {
21 get: function () {
22 return this._max;
23 },
24 set: function (val) {
25 this._max = this.getNumberPropertyValue(val);
26 },
27 enumerable: true,
28 configurable: true
29 });
30 Object.defineProperty(Ionic2Rating.prototype, "readOnly", {
31 get: function () {
32 return this._readOnly;
33 },
34 set: function (val) {
35 this._readOnly = this.isTrueProperty(val);
36 },
37 enumerable: true,
38 configurable: true
39 });
40 Object.defineProperty(Ionic2Rating.prototype, "emptyStarIconName", {
41 get: function () {
42 return this._emptyStarIconName;
43 },
44 set: function (val) {
45 this._emptyStarIconName = val;
46 },
47 enumerable: true,
48 configurable: true
49 });
50 Object.defineProperty(Ionic2Rating.prototype, "halfStarIconName", {
51 get: function () {
52 return this._halfStarIconName;
53 },
54 set: function (val) {
55 this._halfStarIconName = val;
56 },
57 enumerable: true,
58 configurable: true
59 });
60 Object.defineProperty(Ionic2Rating.prototype, "starIconName", {
61 get: function () {
62 return this._starIconName;
63 },
64 set: function (val) {
65 this._starIconName = val;
66 },
67 enumerable: true,
68 configurable: true
69 });
70 Object.defineProperty(Ionic2Rating.prototype, "nullable", {
71 get: function () {
72 return this._nullable;
73 },
74 set: function (val) {
75 this._nullable = this.isTrueProperty(val);
76 },
77 enumerable: true,
78 configurable: true
79 });
80 Ionic2Rating.prototype.ngOnInit = function () {
81 // ngFor needs an array
82 this.starIndexes = Array(this.max).fill(1).map(function (x, i) { return i; });
83 };
84 Ionic2Rating.prototype.getStarIconName = function (starIndex) {
85 if (this.value === undefined) {
86 return this.emptyStarIconName;
87 }
88 if (this.value > starIndex) {
89 if (this.value < starIndex + 1) {
90 return this.halfStarIconName;
91 }
92 else {
93 return this.starIconName;
94 }
95 }
96 else {
97 return this.emptyStarIconName;
98 }
99 };
100 Object.defineProperty(Ionic2Rating.prototype, "value", {
101 get: function () {
102 return this.innerValue;
103 },
104 set: function (value) {
105 if (value !== this.innerValue) {
106 this.innerValue = value;
107 this.onChangeCallback(value);
108 }
109 },
110 enumerable: true,
111 configurable: true
112 });
113 Ionic2Rating.prototype.writeValue = function (value) {
114 if (value !== this.innerValue) {
115 this.innerValue = value;
116 }
117 };
118 Ionic2Rating.prototype.registerOnChange = function (fn) {
119 this.onChangeCallback = fn;
120 };
121 Ionic2Rating.prototype.registerOnTouched = function (fn) {
122 };
123 Ionic2Rating.prototype.onKeyDown = function (event) {
124 if (/(37|38|39|40)/.test(event.which)) {
125 event.preventDefault();
126 event.stopPropagation();
127 var newValue = this.value + ((event.which == 38 || event.which == 39) ? 1 : -1);
128 return this.rate(newValue);
129 }
130 };
131 Ionic2Rating.prototype.rate = function (value) {
132 if (this.readOnly || value < 0 || value > this.max) {
133 return;
134 }
135 if (value === this.value && this.nullable) {
136 value = null;
137 }
138 this.value = value;
139 };
140 Ionic2Rating.prototype.isTrueProperty = function (val) {
141 if (typeof val === 'string') {
142 val = val.toLowerCase().trim();
143 return (val === 'true' || val === 'on');
144 }
145 return !!val;
146 };
147 Ionic2Rating.prototype.getNumberPropertyValue = function (val) {
148 if (typeof val === 'string') {
149 return parseInt(val.trim());
150 }
151 return val;
152 };
153 Ionic2Rating.decorators = [
154 { type: Component, args: [{
155 selector: 'nxtlife-rating',
156 styles: ["\n ul.rating li {\n display: inline;\n border: 0px;\n background: none;\n padding: 5px 10px;\n }\n ul.rating li i {\n font-size: 25px;\n }\n .rating {\n color: #2ec95c;\n padding-left: 0px;\n font-size: large;\n }\n "],
157 template: "\n <ul class=\"rating\" (keydown)=\"onKeyDown($event)\">\n <li *ngFor=\"let starIndex of starIndexes\" tappable (click)=\"rate(starIndex + 1)\">\n <ion-icon [name]=\"getStarIconName(starIndex)\">\n </ion-icon>\n </li>\n </ul>",
158 providers: [RATING_CONTROL_VALUE_ACCESSOR]
159 },] },
160 ];
161 /** @nocollapse */
162 Ionic2Rating.ctorParameters = [];
163 Ionic2Rating.propDecorators = {
164 'max': [{ type: Input },],
165 'readOnly': [{ type: Input },],
166 'emptyStarIconName': [{ type: Input },],
167 'halfStarIconName': [{ type: Input },],
168 'starIconName': [{ type: Input },],
169 'nullable': [{ type: Input },],
170 };
171 return Ionic2Rating;
172}());
173//# sourceMappingURL=ionic2-rating.js.map
\No newline at end of file