Rating.js 3.5 KB

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