[5921] | 1 | function makeNiceRatingForm(options) |
---|
| 2 | { |
---|
| 3 | gRatingOptions = options || {}; |
---|
| 4 | var form = $('rateForm'); |
---|
| 5 | if (!form) return; |
---|
| 6 | |
---|
| 7 | gRatingButtons = form.getElements('input'); |
---|
| 8 | gUserRating = ""; |
---|
| 9 | |
---|
| 10 | gRatingButtons.each(function(el){ if ( el.type=="button" ) { gUserRating = el.value; } }); |
---|
| 11 | |
---|
| 12 | gRatingButtons.each(function(el, i){ |
---|
| 13 | |
---|
| 14 | el.initialRateValue = el.value; // save it as a property |
---|
| 15 | |
---|
| 16 | try { el.type = "button"; } catch (e){}// avoid normal submit (use ajax); not working in IE6 |
---|
| 17 | |
---|
| 18 | //hide the text IE/Opera - breaks safari |
---|
| 19 | if (navigator.userAgent.indexOf('AppleWebKit/') == -1 ) el.value = ""; |
---|
| 20 | |
---|
| 21 | el.setStyles({ |
---|
| 22 | 'textIndent' : "-50px", |
---|
| 23 | 'marginLeft' : 0, |
---|
| 24 | 'marginRight' : 0 |
---|
| 25 | }); |
---|
| 26 | |
---|
| 27 | if (i!=gRatingButtons.length-1 && el.nextSibling.nodeType == 3 /*TEXT_NODE*/) |
---|
| 28 | el.parentNode.removeChild(el.nextSibling); |
---|
| 29 | |
---|
| 30 | if (i>0 && el.previousSibling.nodeType == 3 /*TEXT_NODE*/) |
---|
| 31 | el.parentNode.removeChild(el.previousSibling); |
---|
| 32 | |
---|
| 33 | el.addEvents({ |
---|
| 34 | 'mouseenter' : function(){updateRating(el)}, |
---|
| 35 | 'mouseleave' : function(){resetRating(el)}, |
---|
| 36 | 'click' : function(e){submitRating(e)} |
---|
| 37 | }); |
---|
| 38 | |
---|
| 39 | }); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | function resetRating(el) |
---|
| 43 | { |
---|
| 44 | for (i = 1; i<gRatingButtons.length; i++){ |
---|
| 45 | gRatingButtons[i].removeClass('rateButtonUserFull2'); |
---|
| 46 | gRatingButtons[i].removeClass('rateButtonUserHalf2'); |
---|
| 47 | gRatingButtons[i].removeClass('rateButtonUserEmpty2'); |
---|
| 48 | gRatingButtons[i].removeClass('rateButtonFull2'); |
---|
| 49 | gRatingButtons[i].removeClass('rateButtonHalf2'); |
---|
| 50 | gRatingButtons[i].removeClass('rateButtonEmpty2'); |
---|
| 51 | if(gRatingButtons[i].value > el.value) break; |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | function updateRating(el) |
---|
| 56 | { |
---|
| 57 | for (i = 1; i<gRatingButtons.length; i++){ |
---|
| 58 | var newClass = 'rateButton'; |
---|
| 59 | if(gRatingButtons[i].title <= el.title) newClass = newClass+'User'; |
---|
| 60 | if(gRatingButtons[i].hasClass('rateButtonFull') || gRatingButtons[i].hasClass('rateButtonUserFull')) newClass = newClass+'Full2'; |
---|
| 61 | else if(gRatingButtons[i].hasClass('rateButtonHalf') || gRatingButtons[i].hasClass('rateButtonUserHalf')) newClass = newClass+'Half2'; |
---|
| 62 | else newClass = newClass+'Empty2'; |
---|
| 63 | gRatingButtons[i].addClass(newClass); |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
[12531] | 67 | function setNewRate(score, count, stdev, newRating) |
---|
[5921] | 68 | { |
---|
[12531] | 69 | var score = score+""; |
---|
| 70 | ratingSplitted = score.split('.'); |
---|
[5921] | 71 | starFull = ratingSplitted[0]; |
---|
| 72 | starHalf = (ratingSplitted[1] > 49) ? true : false; |
---|
| 73 | |
---|
| 74 | for (i = 1; i<gRatingButtons.length; i++){ |
---|
| 75 | gRatingButtons[i].removeClass('rateButtonFull'); |
---|
| 76 | gRatingButtons[i].removeClass('rateButtonHalf'); |
---|
| 77 | gRatingButtons[i].removeClass('rateButtonEmpty'); |
---|
| 78 | gRatingButtons[i].removeClass('rateButtonUserFull'); |
---|
| 79 | gRatingButtons[i].removeClass('rateButtonUserHalf'); |
---|
| 80 | gRatingButtons[i].removeClass('rateButtonUserEmpty'); |
---|
| 81 | gRatingButtons[i].removeClass('rateButtonFull2'); |
---|
| 82 | gRatingButtons[i].removeClass('rateButtonHalf2'); |
---|
| 83 | gRatingButtons[i].removeClass('rateButtonEmpty2'); |
---|
| 84 | gRatingButtons[i].removeClass('rateButtonUserFull2'); |
---|
| 85 | gRatingButtons[i].removeClass('rateButtonUserHalf2'); |
---|
| 86 | gRatingButtons[i].removeClass('rateButtonUserEmpty2'); |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | if(gRatingButtons[i].title <= starFull) gRatingButtons[i].addClass((gRatingButtons[i].title <= newRating) ? 'rateButtonUserFull': 'rateButtonFull'); |
---|
| 90 | else if(gRatingButtons[i].title == starFull.toInt()+1) |
---|
| 91 | if(gRatingButtons[i].title <= newRating) gRatingButtons[i].addClass((starHalf) ? 'rateButtonUserHalf': 'rateButtonUserEmpty'); |
---|
| 92 | else gRatingButtons[i].addClass((starHalf) ? 'rateButtonHalf': 'rateButtonEmpty'); |
---|
| 93 | else gRatingButtons[i].addClass((gRatingButtons[i].title <= newRating) ? 'rateButtonUserEmpty': 'rateButtonEmpty'); |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | function submitRating(e) |
---|
| 98 | { |
---|
| 99 | var rateButton = e.target; |
---|
| 100 | if (rateButton.initialRateValue == gUserRating) |
---|
| 101 | return false; //nothing to do |
---|
| 102 | |
---|
| 103 | for (var i=0; i<gRatingButtons.length; i++) gRatingButtons[i].disabled=true; |
---|
| 104 | var y = new PwgWS(gRatingOptions.rootUrl); |
---|
| 105 | y.callService( |
---|
| 106 | "pwg.images.rate", {image_id: gRatingOptions.image_id, rate: rateButton.initialRateValue } , |
---|
| 107 | { |
---|
| 108 | onFailure: function(num, text) { |
---|
| 109 | alert(num + " " + text); |
---|
| 110 | document.location = rateButton.form.action + "&rate="+rateButton.initialRateValue; |
---|
| 111 | }, |
---|
| 112 | onSuccess: function(result) { |
---|
| 113 | gUserRating = rateButton.initialRateValue; |
---|
| 114 | for (var i=0; i<gRatingButtons.length; i++) gRatingButtons[i].disabled=false; |
---|
[12531] | 115 | setNewRate(result.score, result.count, result.stdev, rateButton.initialRateValue); |
---|
[5921] | 116 | if (gRatingOptions.ratingSummaryElement) |
---|
| 117 | { |
---|
| 118 | var t = gRatingOptions.ratingSummaryText; |
---|
[12533] | 119 | var args =[result.score, result.count, result.stdev], idx = 0, rexp = new RegExp( /%\.?\d*[sdf]/ ); |
---|
[5921] | 120 | _xxx = t.match( rexp ); |
---|
| 121 | while (idx<args.length) t=t.replace(rexp, args[idx++]); |
---|
| 122 | gRatingOptions.ratingSummaryElement.innerHTML = t; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | ); |
---|
| 127 | return false; |
---|
| 128 | } |
---|