ratings.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* JavaScript for user ratings.
  2. * ===================================================================
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License as
  5. * published by the Free Software Foundation; either version 2 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. * 02111-1307 USA
  17. * ===================================================================
  18. * $Id: ratings.js,v 1.3 2005/03/19 03:40:10 matt Exp $
  19. * ===================================================================
  20. */
  21. var MAX_RATING = 5;
  22. var XML_REQ = null;
  23. /**
  24. * StarSet object
  25. *
  26. * Object to handle the rating star image mouse events. It is assumed
  27. * the star images are grouped together in some sort of container tag,
  28. * which is by default hidden via CSS. The image objects are assumed
  29. * to have nothing except whitespace between each of them. This object
  30. * will remove the space from the DOM when it initializes, and then
  31. * set the visibility of the parent container to visible. The
  32. * wsUrl parameter is assumed to end with the rating parameter, i.e.
  33. * '/path/to/service?rating=' so that the rating can simply be appended
  34. * to that URL.
  35. */
  36. function StarSet(imagePath, wsUrl, setNumber, currentRating, recipeId) {
  37. this.imgArray = new Array();
  38. this.imgPath = imagePath;
  39. this.setNum = setNumber;
  40. this.currRating = currentRating;
  41. this.recipeId = recipeId;
  42. this.wsUrl = wsUrl;
  43. for ( var i = 1; i <= MAX_RATING; i++ ) {
  44. var starImg = document.getElementById('star.'+this.setNum+'.'+i);
  45. if ( !starImg ) {
  46. alert("Can't find star " +this.setNum +'.' +i);
  47. return;
  48. }
  49. this.imgArray[(i-1)] = starImg;
  50. starImg.onmouseover = handleStarMouseOver
  51. starImg.onmouseout = handleStarMouseOut
  52. starImg.onclick = handleStarClick
  53. starImg.rating = i;
  54. starImg.starSet = this;
  55. // little hack to remove extra text from between img tags, as
  56. // browsers render whitespace if it's there and XSLT might format
  57. // XHTML output with spaces
  58. if ( starImg.previousSibling && starImg.previousSibling.nodeType == 3 ) {
  59. // nodeType 3 == Node.TEXT_NODE (IE does not support DOM constants)
  60. starImg.parentNode.removeChild(starImg.previousSibling);
  61. }
  62. }
  63. this.resetRating();
  64. // now make stars visible by setting parent node to visible
  65. this.imgArray[0].parentNode.style.visibility = 'visible';
  66. }
  67. function setRating(rating) {
  68. // TODO call XML web service to store the rating...
  69. this.currRating = rating;
  70. loadXMLDoc(this.wsUrl+rating);
  71. }
  72. function highlightRating(rating) {
  73. for ( var i = 0; i < rating; i++ ) {
  74. this.imgArray[i].src = this.imgPath + 'star-active.png';
  75. }
  76. for ( var i = rating; i < MAX_RATING; i++ ) {
  77. this.imgArray[i].src = this.imgPath + 'star-off.png';
  78. }
  79. }
  80. function resetRating() {
  81. for ( var i = 0; i < this.currRating; i++ ) {
  82. this.imgArray[i].src = this.imgPath + 'star-on.png';
  83. }
  84. for ( var i = this.currRating; i < MAX_RATING; i++ ) {
  85. this.imgArray[i].src = this.imgPath + 'star-off.png';
  86. }
  87. }
  88. /* StarSet class methods */
  89. StarSet.prototype.setRating = setRating;
  90. StarSet.prototype.highlightRating = highlightRating;
  91. StarSet.prototype.resetRating = resetRating;
  92. /* mouseOver function traps... 'this' refers to <img> object */
  93. function handleStarMouseOver() {
  94. this.starSet.highlightRating(this.rating);
  95. }
  96. function handleStarMouseOut() {
  97. this.starSet.resetRating();
  98. }
  99. function handleStarClick() {
  100. this.starSet.setRating(this.rating);
  101. }
  102. /* XML HTTP request support */
  103. function loadXMLDoc(url) {
  104. // branch for native XMLHttpRequest object
  105. if (window.XMLHttpRequest) {
  106. XML_REQ = new XMLHttpRequest();
  107. XML_REQ.onreadystatechange = processReqChange;
  108. XML_REQ.open("GET", url, true);
  109. XML_REQ.send(null);
  110. // branch for IE/Windows ActiveX version
  111. } else if (window.ActiveXObject) {
  112. XML_REQ = new ActiveXObject("Microsoft.XMLHTTP");
  113. if (XML_REQ) {
  114. XML_REQ.onreadystatechange = processReqChange;
  115. XML_REQ.open("GET", url, true);
  116. XML_REQ.send();
  117. }
  118. }
  119. }
  120. function processReqChange() {
  121. // only if req shows "loaded"
  122. if (XML_REQ.readyState == 4) {
  123. // only if "OK"
  124. if (XML_REQ.status == 200) {
  125. // ...all's well
  126. // alert("who-hoo! " +XML_REQ.responseText);
  127. } else {
  128. alert("There was a problem retrieving the XML data:\n" +
  129. XML_REQ.statusText);
  130. }
  131. }
  132. }