Rating.js 3.7 KB

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