[483] | 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 | // The hidden value node is attached as "focusNode" because tabIndex, id, etc. are getting mapped there. |
---|
| 40 | var tpl = '<div dojoAttachPoint="domNode" class="dojoxRating dijitInline">' + |
---|
| 41 | '<input type="hidden" value="0" dojoAttachPoint="focusNode" /><ul data-dojo-attach-point="list">${stars}</ul>' + |
---|
| 42 | '</div>'; |
---|
| 43 | // The value-attribute is used to "read" the value for processing in the widget class |
---|
| 44 | var starTpl = '<li class="dojoxRatingStar dijitInline" value="${value}"></li>'; |
---|
| 45 | var rendered = ""; |
---|
| 46 | for(var i = 0; i < this.numStars; i++){ |
---|
| 47 | rendered += string.substitute(starTpl, {value:i + 1}); |
---|
| 48 | } |
---|
| 49 | this.templateString = string.substitute(tpl, {stars:rendered}); |
---|
| 50 | |
---|
| 51 | this.inherited(arguments); |
---|
| 52 | }, |
---|
| 53 | |
---|
| 54 | postCreate: function(){ |
---|
| 55 | this.inherited(arguments); |
---|
| 56 | this._renderStars(this.value); |
---|
| 57 | this.own( |
---|
| 58 | // Fire when mouse is moved over one of the stars. |
---|
| 59 | on(this.list, on.selector(".dojoxRatingStar", "mouseover"), lang.hitch(this, "_onMouse")), |
---|
| 60 | on(this.list, on.selector(".dojoxRatingStar", "click"), lang.hitch(this, "onStarClick")), |
---|
| 61 | on(this.list, mouse.leave, lang.hitch(this, function(){ |
---|
| 62 | // go from hover display back to dormant display |
---|
| 63 | this._renderStars(this.value); |
---|
| 64 | })) |
---|
| 65 | ); |
---|
| 66 | }, |
---|
| 67 | |
---|
| 68 | _onMouse: function(evt){ |
---|
| 69 | // summary: |
---|
| 70 | // Called when mouse is moved over one of the stars |
---|
| 71 | var hoverValue = +domAttr.get(evt.target, "value"); |
---|
| 72 | this._renderStars(hoverValue, true); |
---|
| 73 | this.onMouseOver(evt, hoverValue); |
---|
| 74 | }, |
---|
| 75 | |
---|
| 76 | _renderStars: function(value, hover){ |
---|
| 77 | // summary: |
---|
| 78 | // Render the stars depending on the value. |
---|
| 79 | query(".dojoxRatingStar", this.domNode).forEach(function(star, i){ |
---|
| 80 | if(i + 1 > value){ |
---|
| 81 | domClass.remove(star, "dojoxRatingStarHover"); |
---|
| 82 | domClass.remove(star, "dojoxRatingStarChecked"); |
---|
| 83 | }else{ |
---|
| 84 | domClass.remove(star, "dojoxRatingStar" + (hover ? "Checked" : "Hover")); |
---|
| 85 | domClass.add(star, "dojoxRatingStar" + (hover ? "Hover" : "Checked")); |
---|
| 86 | } |
---|
| 87 | }); |
---|
| 88 | }, |
---|
| 89 | |
---|
| 90 | onStarClick: function(/*Event*/ evt){ |
---|
| 91 | // summary: |
---|
| 92 | // Connect on this method to get noticed when a star was clicked. |
---|
| 93 | // example: |
---|
| 94 | // | connect(widget, "onStarClick", function(event){ ... }) |
---|
| 95 | var newVal = +domAttr.get(evt.target, "value"); |
---|
| 96 | this.setAttribute("value", newVal == this.value ? 0 : newVal); |
---|
| 97 | this._renderStars(this.value); |
---|
| 98 | this.onChange(this.value); // Do I have to call this by hand? |
---|
| 99 | }, |
---|
| 100 | |
---|
| 101 | onMouseOver: function(/*=====evt, value=====*/ ){ |
---|
| 102 | // summary: |
---|
| 103 | // Connect here, the value is passed to this function as the second parameter! |
---|
| 104 | }, |
---|
| 105 | |
---|
| 106 | setAttribute: function(/*String*/ key, /*Number*/ value){ |
---|
| 107 | // summary: |
---|
| 108 | // Deprecated. Use set("value", ...) instead. |
---|
| 109 | this.set(key, value); |
---|
| 110 | }, |
---|
| 111 | |
---|
| 112 | _setValueAttr: function(val){ |
---|
| 113 | this.focusNode.value = val; // reflect the value in our hidden field, for form submission |
---|
| 114 | this._set("value", val); |
---|
| 115 | this._renderStars(val); |
---|
| 116 | this.onChange(val); // Do I really have to call this by hand? :-( |
---|
| 117 | } |
---|
| 118 | }); |
---|
| 119 | }); |
---|