-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRating.js
executable file
·124 lines (109 loc) · 4 KB
/
Rating.js
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
define([
"dojo/_base/declare",
"dojo/_base/lang",
"dojo/dom-attr",
"dojo/dom-class",
"dojo/mouse",
"dojo/on",
"dojo/string",
"dojo/query",
"dijit/form/_FormWidget"
], function(declare, lang, domAttr, domClass, mouse, on, string, query, FormWidget){
return declare("dojox.form.Rating", FormWidget, {
// summary:
// A widget for rating using stars.
/*=====
// required: Boolean
// TODO: Can be true or false, default is false.
required: false,
=====*/
templateString: null,
// numStars: Integer|Float
// The number of stars to show, default is 3.
numStars: 3,
// value: Integer|Float
// The current value of the Rating
value: 0,
disableStar:false,
buildRendering: function(/*Object*/ params){
// summary:
// Build the templateString. The number of stars is given by this.numStars,
// which is normally an attribute to the widget node.
// The hidden value node is attached as "focusNode" because tabIndex, id, etc. are getting mapped there.
var tpl = '<div dojoAttachPoint="domNode" class="dojoxRating dijitInline">' +
'<input type="hidden" value="0" dojoAttachPoint="focusNode" /><ul data-dojo-attach-point="list">${stars}</ul>' +
'</div>';
// The value-attribute is used to "read" the value for processing in the widget class
var starTpl = '<li class="dojoxRatingStar dijitInline" value="${value}"></li>';
var rendered = "";
for(var i = 0; i < this.numStars; i++){
rendered += string.substitute(starTpl, {value:i + 1});
}
this.templateString = string.substitute(tpl, {stars:rendered});
this.inherited(arguments);
},
postCreate: function(){
this.inherited(arguments);
this._renderStars(this.value);
this.own(
// Fire when mouse is moved over one of the stars.
on(this.list, on.selector(".dojoxRatingStar", "mouseover"), lang.hitch(this, "_onMouse")),
on(this.list, on.selector(".dojoxRatingStar", "click"), lang.hitch(this, "onStarClick")),
on(this.list, mouse.leave, lang.hitch(this, function(){
// go from hover display back to dormant display
this._renderStars(this.value);
}))
);
},
_onMouse: function(evt){
// summary:
// Called when mouse is moved over one of the stars
if(!this.get('disableStar')) {
var hoverValue = +domAttr.get(evt.target, "value");
this._renderStars(hoverValue, true);
this.onMouseOver(evt, hoverValue);
}
},
_renderStars: function(value, hover){
// summary:
// Render the stars depending on the value.
query(".dojoxRatingStar", this.domNode).forEach(function(star, i){
if(i + 1 > value){
domClass.remove(star, "dojoxRatingStarHover");
domClass.remove(star, "dojoxRatingStarChecked");
}else{
domClass.remove(star, "dojoxRatingStar" + (hover ? "Checked" : "Hover"));
domClass.add(star, "dojoxRatingStar" + (hover ? "Hover" : "Checked"));
}
});
},
onStarClick: function(/*Event*/ evt){
// summary:
// Connect on this method to get noticed when a star was clicked.
// example:
// | connect(widget, "onStarClick", function(event){ ... })
if(!this.get('disableStar')) {
var newVal = +domAttr.get(evt.target, "value");
this.setAttribute("value", newVal == this.value ? 0 : newVal);
this._renderStars(this.value);
this.onChange(this.value); // Do I have to call this by hand?
this.disable = true;
}
},
onMouseOver: function(/*=====evt, value=====*/ ){
// summary:
// Connect here, the value is passed to this function as the second parameter!
},
setAttribute: function(/*String*/ key, /*Number*/ value){
// summary:
// Deprecated. Use set("value", ...) instead.
this.set(key, value);
},
_setValueAttr: function(val){
this.focusNode.value = val; // reflect the value in our hidden field, for form submission
this._set("value", val);
this._renderStars(val);
this.onChange(val); // Do I really have to call this by hand? :-(
}
});
});