Skip to content

Commit ed39c93

Browse files
committed
Added tests and example for Rating component
1 parent 5374197 commit ed39c93

File tree

8 files changed

+381
-75
lines changed

8 files changed

+381
-75
lines changed

lib/rating/rating.dart

Lines changed: 53 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// All rights reserved. Please see the LICENSE.md file.
44
library angular.ui.rating;
55

6+
import 'dart:html' as dom;
67
import 'package:angular/angular.dart';
78

89
import 'package:angular_ui/utils/extend.dart';
@@ -26,57 +27,58 @@ class RatingConfig {
2627
}
2728

2829
@NgComponent(
29-
selector: 'rating',
30+
selector: 'rating[ng-model]',
3031
publishAs: 'ctrl',
3132
templateUrl: 'packages/angular_ui/rating/rating.html',
3233
applyAuthorStyles: true
3334
)
3435
@NgComponent(
35-
selector: '[rating]',
36+
selector: '[rating][ng-model]',
3637
publishAs: 'ctrl',
3738
templateUrl: 'packages/angular_ui/rating/rating.html',
3839
applyAuthorStyles: true
3940
)
40-
class RatingComponent implements NgAttachAware {
41-
final Scope _scope;
42-
final RatingConfig _config;
43-
44-
int _max; // could be restored to simple field after #431 is fixed (after 0.9.5+2) - see also attach below
45-
//@NgOneWay('max') int max;
46-
@NgOneWay('max') set max(int value) {
47-
print('Max: $value');
48-
_max = value;
49-
attachTmp();
50-
}
51-
int get max => _max;
52-
53-
int _value = 0;
54-
@NgTwoWay('value') int get value => _value;
55-
set value(int val) {
56-
_value = val;
57-
reset();
58-
}
59-
@NgTwoWay('readonly') bool isReadonly = false;
60-
@NgCallback('on-hover') var onHover;
61-
@NgCallback('on-leave') var onLeave;
62-
@NgTwoWay('rating-states') List<Map<String,String>> ratingStates;
63-
@NgTwoWay('state-on') String stateOn;
64-
@NgTwoWay('state-off') String stateOff;
65-
41+
class RatingComponent {
42+
43+
int maxRange = 0;
44+
String stateOn;
45+
String stateOff;
46+
List<Map<String, String>> range;
6647
int val = 0;
67-
List<Map> range;
68-
69-
RatingComponent(this._scope, this._config ) {
70-
_log.fine('RatingComponent');
48+
49+
@NgOneWay('readonly')
50+
bool readonly = false;
51+
52+
@NgCallback('on-hover')
53+
var onHover;
54+
55+
@NgCallback('on-leave')
56+
var onLeave;
57+
58+
Scope _scope;
59+
dom.Element _element;
60+
NodeAttrs _attrs;
61+
NgModel _ngModel;
62+
RatingConfig _ratingConfig;
63+
64+
RatingComponent(this._scope, this._element, this._attrs, this._ngModel, this._ratingConfig) {
65+
maxRange = _attrs.containsKey('max') ? _scope.parentScope.eval(_attrs['max']) : _ratingConfig.max;
66+
stateOn = _attrs.containsKey('state-on') ? _scope.parentScope.eval(_attrs['state-on']) : _ratingConfig.stateOn;
67+
stateOff = _attrs.containsKey('state-off') ? _scope.parentScope.eval(_attrs['state-off']) : _ratingConfig.stateOff;
68+
69+
_ngModel.render = (value) {
70+
val = _ngModel.viewValue;
71+
};
72+
range = _buildTemplateObjects(_attrs.containsKey('rating-states') ? _scope.parentScope.eval(_attrs['rating-states']) : new List(maxRange));
7173
}
72-
73-
List<Map<String,String>> createRateObjects(List<Map<String,String>> states) {
74+
75+
List _buildTemplateObjects(List<Map<String, String>>states) {
7476
Map<String,String> defaultOptions =
7577
{
7678
'stateOn': this.stateOn,
7779
'stateOff': this.stateOff
7880
};
79-
81+
8082
if(states != null) {
8183
for (int i = 0, n = states.length; i < n; i++) {
8284
while(states.length <= i) {
@@ -87,49 +89,31 @@ class RatingComponent implements NgAttachAware {
8789
}
8890
return states;
8991
}
90-
91-
void rate (int value) {
92-
if(this.value != value && !isReadonly) {
93-
this.value = value;
92+
93+
void rate(value) {
94+
if (!readonly ) {
95+
_ngModel.viewValue = value;
96+
_ngModel.render(_ngModel.modelValue);
9497
}
9598
}
96-
97-
void enter(int value) {
98-
if(!isReadonly) {
99-
this.val = value;
99+
100+
void enter(value) {
101+
if (!readonly) {
102+
val = value;
100103
}
101104
onHover({'value': value});
102105
}
103-
106+
104107
void reset() {
105-
val = value;
108+
val = _ngModel.viewValue;
106109
onLeave();
107110
}
108-
109-
String stateClass(int index, Map r) {
110-
String c;
111-
if(index < val) {
112-
c = (r['stateOn'] != null) ? r['stateOn'] : 'glyphicon-star';
111+
112+
String stateClass(index, Map<String, String> r) {
113+
if (index < val) {
114+
return r.containsKey('stateOn') && r['stateOn'] != null ? r['stateOn'] : 'glyphicon-star';
113115
} else {
114-
c = (r['stateOff'] != null) ? r['stateOff'] : 'glyphicon-star-empty';
116+
return r.containsKey('stateOff') && r['stateOff'] != null ? r['stateOff'] : 'glyphicon-star-empty';
115117
}
116-
return c;
117-
}
118-
119-
void attachTmp() { // move content back to attach when bug #431 is fixed (after 0.9.5+2)
120-
_log.fine('Max: $max');
121-
int maxRange = max != null ? max : _config.max;
122-
stateOn = stateOn != null ? stateOn : _config.stateOn;
123-
stateOff = stateOff != null ? stateOff : _config.stateOff;
124-
125-
range = ratingStates != null ?
126-
createRateObjects(copy(ratingStates)) :
127-
this.createRateObjects(new List(maxRange));
128-
reset();
129-
130-
//_scope.$watch(() => value, reset);
131-
132-
}
133-
void attach() {
134118
}
135119
}

lib/rating/rating.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<!DOCTYPE html>
2-
<span ng-mouseleave="ctrl.reset()">
3-
<i ng-repeat="r in ctrl.range" ng-mouseenter="ctrl.enter($index + 1)" ng-click="ctrl.rate($index + 1)" class="glyphicon" ng-class="ctrl.stateClass($index, r)"></i>
4-
</span>
2+
<span ng-mouseleave="ctrl.reset()" ng-mouseout="ctrl.reset()">
3+
<i ng-repeat="r in ctrl.range" ng-mouseenter="ctrl.enter($index + 1)" ng-mouseover="ctrl.enter($index + 1)" ng-click="ctrl.rate($index + 1)" class="glyphicon" ng-class="ctrl.stateClass($index, r)"></i>
4+
</span>

0 commit comments

Comments
 (0)