Debian Codegraph @

dojox/form/Rating.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare",
3 "dojo/_base/lang",
4 "dojo/dom-attr",
5 "dojo/dom-class",
6 "dojo/mouse",
7 "dojo/on",
8 "dojo/string",
9 "dojo/query",
10 "dijit/form/_FormWidget"
11 ], function(declare, lang, domAttr, domClass, mouse, on, string, query, FormWidget){
12
13
14 return declare("dojox.form.Rating", FormWidget, {
15 // summary:
16 // A widget for rating using stars.
17
18 /*=====
19 // required: Boolean
20 // TODO: Can be true or false, default is false.
21 required: false,
22 =====*/
23
24 templateString: null,
25
26 // numStars: Integer|Float
27 // The number of stars to show, default is 3.
28 numStars: 3,
29
30 // value: Integer|Float
31 // The current value of the Rating
32 value: 0,
33
34 buildRendering: function(/*Object*/ params){
35 // summary:
36 // Build the templateString. The number of stars is given by this.numStars,
37 // which is normally an attribute to the widget node.
38
39 this.name = this.name || 'rating-' + Math.random().toString(36).substring(2);
40
41 // The radio input used to display and select stars
42 var starTpl = '<label class="dojoxRatingStar dijitInline ${hidden}">' +
43 '<span class="dojoxRatingLabel">${value} stars</span>' +
44 '<input type="radio" name="' + this.name + '" value="${value}" dojoAttachPoint="focusNode" class="dojoxRatingInput">' +
45 '</label>';
46
47 // The hidden value node is attached as "focusNode" because tabIndex, id, etc. are getting mapped there.
48 var tpl = '<div dojoAttachPoint="domNode" class="dojoxRating dijitInline">' +
49 '<div data-dojo-attach-point="list">' +
50 string.substitute(starTpl, {value:0, hidden: 'dojoxRatingHidden'}) +
51 '${stars}' +
52 '</div></div>';
53
54 var rendered = "";
55 for(var i = 0; i < this.numStars; i++){
56 rendered += string.substitute(starTpl, {value:i + 1, hidden: ''});
57 }
58 this.templateString = string.substitute(tpl, {stars:rendered});
59
60 this.inherited(arguments);
61 },
62
63 postCreate: function(){
64 this.inherited(arguments);
65 this._renderStars(this.value);
66 this.own(
67 // Fire when mouse is moved over one of the stars.
68 on(this.list, on.selector(".dojoxRatingStar", "mouseover"), lang.hitch(this, "_onMouse")),
69 on(this.list, on.selector(".dojoxRatingStar", "click"), lang.hitch(this, "_onClick")),
70 on(this.list, on.selector(".dojoxRatingInput", "change"), lang.hitch(this, "onStarChange")),
71 on(this.list, mouse.leave, lang.hitch(this, function(){
72 // go from hover display back to dormant display
73 this._renderStars(this.value);
74 }))
75 );
76 },
77
78 _onMouse: function(evt){
79 // summary:
80 // Called when mouse is moved over one of the stars
81 var hoverValue = +domAttr.get(evt.target.querySelector('input'), "value");
82 this._renderStars(hoverValue, true);
83 this.onMouseOver(evt, hoverValue);
84 },
85
86 _onClick: function(evt) {
87 if (evt.target.tagName === 'LABEL') {
88 var clickedValue = +domAttr.get(evt.target.querySelector('input'), "value");
89 // for backwards compatibility with previous dojo versions' onStarClick event
90 evt.target.value = clickedValue;
91 this.onStarClick(evt, clickedValue);
92
93 // check for clicking current value
94 if (clickedValue == this.value) {
95 evt.preventDefault();
96 this.onStarChange(evt);
97 }
98 }
99 },
100
101 _renderStars: function(value, hover){
102 // summary:
103 // Render the stars depending on the value.
104 query(".dojoxRatingStar", this.domNode).forEach(function(star, i){
105 if(i > value){
106 domClass.remove(star, "dojoxRatingStarHover");
107 domClass.remove(star, "dojoxRatingStarChecked");
108 }else{
109 domClass.remove(star, "dojoxRatingStar" + (hover ? "Checked" : "Hover"));
110 domClass.add(star, "dojoxRatingStar" + (hover ? "Hover" : "Checked"));
111 }
112 });
113 },
114
115 onStarChange: function(evt) {
116 var newVal = +domAttr.get(evt.target, "value");
117 this.setAttribute("value", newVal == this.value ? 0 : newVal);
118 this._renderStars(this.value);
119
120 this.onChange(this.value);
121 },
122
123 onStarClick: function(/*Event*/ evt, /*Number*/ value){
124 // summary:
125 // Connect on this method to get noticed when the star value was clicked.
126 },
127
128 onMouseOver: function(/*=====evt, value=====*/ ){
129 // summary:
130 // Connect here, the value is passed to this function as the second parameter!
131 },
132
133 setAttribute: function(/*String*/ key, /*Number*/ value){
134 // summary:
135 // Deprecated. Use set("value", ...) instead.
136 this.set(key, value);
137 },
138
139 _setValueAttr: function(val){
140 this._set("value", val);
141 this._renderStars(val);
142 var input = query("input[type=radio]", this.domNode)[val];
143 if (input) {
144 input.checked = true;
145 }
146 this.onChange(val);
147 }
148 });
149 });