1 | define([ |
---|
2 | "dojo/_base/declare", |
---|
3 | "dojo/_base/lang", |
---|
4 | "dojo/dom-attr", |
---|
5 | "dojo/dom-class", |
---|
6 | "dojo/string", |
---|
7 | "dojo/query", |
---|
8 | "dijit/form/_FormWidget" |
---|
9 | ], function(declare, lang, domAttr, domClass, string, query, FormWidget){ |
---|
10 | |
---|
11 | /*===== |
---|
12 | FormWidget = dijit.form._FormWidget; |
---|
13 | =====*/ |
---|
14 | return declare("dojox.form.Rating", FormWidget,{ |
---|
15 | // summary: |
---|
16 | // A widget for rating using stars. |
---|
17 | // |
---|
18 | // required: Boolean |
---|
19 | // TODO: Can be true or false, default is false. |
---|
20 | // required: false, |
---|
21 | |
---|
22 | templateString: null, |
---|
23 | |
---|
24 | // numStars: Integer/Float |
---|
25 | // The number of stars to show, default is 3. |
---|
26 | numStars: 3, |
---|
27 | // value: Integer/Float |
---|
28 | // The current value of the Rating |
---|
29 | value: 0, |
---|
30 | |
---|
31 | constructor:function(/*Object*/params){ |
---|
32 | // Build the templateString. The number of stars is given by this.numStars, |
---|
33 | // which is normally an attribute to the widget node. |
---|
34 | lang.mixin(this, params); |
---|
35 | |
---|
36 | // TODO actually "dijitInline" should be applied to the surrounding div, but FF2 |
---|
37 | // screws up when we query() for the star nodes, it orders them randomly, because of the use |
---|
38 | // of display:--moz-inline-box ... very strange bug |
---|
39 | // Since using ul and li in combintaion with dijitInline this problem doesnt exist anymore. |
---|
40 | |
---|
41 | // The focusNode is normally used to store the value, i dont know if that is right here, but seems standard for _FormWidgets |
---|
42 | var tpl = '<div dojoAttachPoint="domNode" class="dojoxRating dijitInline">' + |
---|
43 | '<input type="hidden" value="0" dojoAttachPoint="focusNode" /><ul>${stars}</ul>' + |
---|
44 | '</div>'; |
---|
45 | // The value-attribute is used to "read" the value for processing in the widget class |
---|
46 | var starTpl = '<li class="dojoxRatingStar dijitInline" dojoAttachEvent="onclick:onStarClick,onmouseover:_onMouse,onmouseout:_onMouse" value="${value}"></li>'; |
---|
47 | var rendered = ""; |
---|
48 | for(var i = 0; i < this.numStars; i++){ |
---|
49 | rendered += string.substitute(starTpl, {value:i+1}); |
---|
50 | } |
---|
51 | this.templateString = string.substitute(tpl, {stars:rendered}); |
---|
52 | }, |
---|
53 | |
---|
54 | postCreate: function(){ |
---|
55 | this.inherited(arguments); |
---|
56 | this._renderStars(this.value); |
---|
57 | }, |
---|
58 | |
---|
59 | _onMouse: function(evt){ |
---|
60 | if(this.hovering){ |
---|
61 | var hoverValue = +domAttr.get(evt.target, "value"); |
---|
62 | this.onMouseOver(evt, hoverValue); |
---|
63 | this._renderStars(hoverValue, true); |
---|
64 | }else{ |
---|
65 | this._renderStars(this.value); |
---|
66 | } |
---|
67 | }, |
---|
68 | |
---|
69 | _renderStars: function(value, hover){ |
---|
70 | // summary: Render the stars depending on the value. |
---|
71 | query(".dojoxRatingStar", this.domNode).forEach(function(star, i){ |
---|
72 | if(i + 1 > value){ |
---|
73 | domClass.remove(star, "dojoxRatingStarHover"); |
---|
74 | domClass.remove(star, "dojoxRatingStarChecked"); |
---|
75 | }else{ |
---|
76 | domClass.remove(star, "dojoxRatingStar" + (hover ? "Checked" : "Hover")); |
---|
77 | domClass.add(star, "dojoxRatingStar" + (hover ? "Hover" : "Checked")); |
---|
78 | } |
---|
79 | }); |
---|
80 | }, |
---|
81 | |
---|
82 | onStarClick:function(/* Event */evt){ |
---|
83 | // summary: Connect on this method to get noticed when a star was clicked. |
---|
84 | // example: connect(widget, "onStarClick", function(event){ ... }) |
---|
85 | var newVal = +domAttr.get(evt.target, "value"); |
---|
86 | this.setAttribute("value", newVal == this.value ? 0 : newVal); |
---|
87 | this._renderStars(this.value); |
---|
88 | this.onChange(this.value); // Do I have to call this by hand? |
---|
89 | }, |
---|
90 | |
---|
91 | onMouseOver: function(/*evt, value*/){ |
---|
92 | // summary: Connect here, the value is passed to this function as the second parameter! |
---|
93 | }, |
---|
94 | |
---|
95 | setAttribute: function(/*String*/key, /**/value){ |
---|
96 | // summary: When calling setAttribute("value", 4), set the value and render the stars accordingly. |
---|
97 | this.set(key, value); |
---|
98 | if(key=="value"){ |
---|
99 | this._renderStars(this.value); |
---|
100 | this.onChange(this.value); // Do I really have to call this by hand? :-( |
---|
101 | } |
---|
102 | } |
---|
103 | }); |
---|
104 | }); |
---|