1 // Wikilens Javascript functions.
2 // $Id: wikilens.js 7138 2009-09-17 10:11:31Z rurban $
7 var data_path = '/phpwiki-cvs';
8 var pagename = 'HomePage';
9 var script_url= '/wikicvs';
10 var stylepath = data_path+'/themes/MonoBook/';
13 var rating = new Array; var prediction = new Array;
14 var avg = new Array; var numusers = new Array;
15 var canRate = new Array;
16 var msg_rating_votes = "Rating: %.1f (%d votes)";
17 var msg_curr_rating = "Your current rating: ";
18 var msg_curr_prediction = "Your current prediction: ";
19 //var msg_chg_rating = "Change your rating from ";
21 var msg_add_rating = "Add your rating: ";
22 var msg_thanks = "Thanks!";
23 var msg_rating_deleted = "Rating deleted!";
25 //var rateit_imgsrc = '/phpwiki-cvs/themes/MonoBook/images/RateIt';
26 var rateit_imgsrc = '/wiki/themes/fusionforge/images/RateIt';
27 var rateit_action = 'RateIt';
31 function displayRating(imgId, imgPrefix, ratingvalue, pred, init) {
32 var ratings = new Array('Not Rated','Awful','Very Poor','Poor','Below Average',
33 'Average','Above Average','Good','Very Good','Excellent','Outstanding');
34 var cancel = imgId + imgPrefix + 'Cancel';
35 var curr_rating = rating[imgId];
36 var curr_pred = prediction[imgId];
38 var imgSrc = rateit_imgsrc;
39 if (init) { // re-initialize titles
40 title = msg_curr_rating+curr_rating+' '+ratings[curr_rating*2];
41 var linebreak = '. '; //
 or 
 within IE only;
43 title = title+' '+msg_curr_prediction+ curr_pred+' '+ratings[curr_pred*2];
47 document.getElementById(cancel).style.display = 'inline';
50 document.getElementById(cancel).style.display = 'none';
54 for (var i=1; i<=10; i++) {
55 var imgName = imgId + i;
57 //document[imgName].title = title;
59 if (ratingvalue > 0) {
61 document.getElementById(imgName).onmouseout = function() { displayRating(imgId,imgPrefix,avg[imgId],0,0) };
62 } else if (curr_pred) {
63 document.getElementById(imgName).onmouseout = function() { displayRating(imgId,imgPrefix,curr_pred,1,0) };
65 if (curr_rating != ratingvalue && typeof(msg_chg_rating) != "undefined") {
66 document.getElementById(imgName).title = msg_chg_rating+curr_rating+' '+ratings[curr_rating*2]+msg_to+j+' '+ratings[i];
69 document.getElementById(imgName).onmouseout = function() { displayRating(imgId,imgPrefix,avg[imgId],0,0) };
70 document.getElementById(imgName).title = msg_add_rating+j+' '+ratings[i];
76 document.getElementById(imgName).title = title+linebreak+msg_add_rating+ratings[i];
78 } else if (i<=(ratingvalue*2)) {
81 document.getElementById(imgName).src = imgSrc + imgPrefix + imgType + ((i%2) ? 'k1' : 'k0') + '.png';
85 function displayRating2(imgId, imgPrefix, ratingvalue, pred, init) {
86 var ratings = new Array('Not Rated','Awful','Very Poor','Poor','Below Average',
87 'Average','Above Average','Good','Very Good','Excellent','Outstanding');
88 var cancel = imgId + imgPrefix + 'Cancel';
89 var curr_rating = rating[imgId];
90 var curr_pred = prediction[imgId];
92 var imgSrc = rateit_imgsrc;
93 if (init) { // re-initialize titles
94 title = msg_curr_rating+curr_rating+' '+ratings[curr_rating*2];
95 var linebreak = '. '; //
 or 
 within IE only;
97 title = title+' '+msg_curr_prediction+ curr_pred+' '+ratings[curr_pred*2];
100 document.getElementById(cancel).style.display = 'inline';
103 document.getElementById(cancel).style.display = 'none';
106 for (var i=1; i<=10; i++) {
107 var imgName = imgId + i;
109 //document[imgName].title = title;
111 if (ratingvalue > 0) {
113 document.getElementById(imgName).onmouseout = function() { displayRating(imgId,imgPrefix,avg[imgId],0,0) };
114 } else if (curr_pred) {
115 document.getElementById(imgName).onmouseout = function() { displayRating(imgId,imgPrefix,curr_pred,1,0) };
117 if (curr_rating != ratingvalue && typeof(msg_chg_rating) != "undefined") {
118 document.getElementById(imgName).title = msg_chg_rating+curr_rating+' '+ratings[curr_rating*2]+msg_to+j+' '+ratings[i];
121 document.getElementById(imgName).onmouseout = function() { displayRating(imgId,imgPrefix,avg[imgId],0,0) };
122 document.getElementById(imgName).title = msg_add_rating+j+' '+ratings[i];
128 document.getElementById(imgName).title = title+linebreak+msg_add_rating+ratings[i];
131 if (ratingvalue <= curr_rating) {
132 if (i <= (ratingvalue * 2))
135 if (i <= (curr_rating * 2))
139 if (i <= (curr_rating * 2))
142 if (i <= (ratingvalue * 2))
146 document.getElementById(imgName).src = imgSrc + imgPrefix + imgType + ((i%2) ? 'k1' : 'k0') + '.png';
150 function sprintfRating(s, num, count) {
151 var num1 = Math.round(num * 10) / 10;
152 if (count < 2) s = s.replace(/votes/, 'vote');
153 return s.replace(/\%.1f/, num1).replace(/\%d/, count);
155 function clickRating(imgPrefix,pagename,version,imgId,dimension,newrating) {
156 var actionImg = imgId+'Action';
157 var top = document.getElementById('rateit-widget-top');
158 var nusers = numusers[imgId];
159 var old_rating = rating[imgId];
160 var sum1 = avg[imgId] * nusers;
162 if (newrating == 'X') {
163 deleteRating(actionImg,pagename,dimension);
165 if (old_rating && old_rating > 0) {
167 new_avg = (sum1 - old_rating) / (nusers - 1);
175 if (new_avg.toString() != "NaN" && idTop == imgId) {
176 avg[imgId] = new_avg;
177 top.childNodes[0].innerHTML = sprintfRating(msg_rating_votes, new_avg, nusers-1);
181 displayRating2(imgId,imgPrefix,0,0,1);
183 submitRating(actionImg,pagename,version,dimension,newrating);
185 if (old_rating && (old_rating > 0)) {
186 new_avg = (sum1 + newrating - old_rating) / nusers;
188 new_avg = (sum1 + newrating) / (nusers + 1);
191 if (new_avg.toString() != "NaN" && idTop == imgId) {
192 avg[imgId] = new_avg;
193 if (newrating != rating[imgId]) {
194 top.childNodes[0].innerHTML = sprintfRating(msg_rating_votes, new_avg, numusers[imgId]);
197 } else if (top && idTop == imgId) {
198 top.childNodes[0].innerHTML = sprintfRating(msg_rating_votes, newrating, 1);
199 avg[imgId] = newrating;
202 rating[imgId] = newrating;
203 displayRating2(imgId,imgPrefix,newrating,0,1);
206 function submitRating(actionImg,page,version,dimension,newrating) {
207 //TODO: GET => PUT request
208 // currently ratings are changed with side-effect, but GET should be side-effect free.
209 var myRand = Math.round(Math.random()*(1000000));
210 var imgSrc = WikiURL(page) + 'version=' + version + '&action=' + rateit_action + '&mode=add&rating=' + newrating + '&dimension=' + dimension + '&nocache=1&nopurge=1&rand=' + myRand;
211 document.getElementById(actionImg).title = msg_thanks;
212 document.getElementById(actionImg).src = imgSrc;
214 function deleteRating(actionImg, page, dimension) {
215 //TODO: GET => DELETE request
216 // currently ratings are changed with side-effect, but GET should be side-effect free.
217 var myRand = Math.round(Math.random()*(1000000));
218 var imgSrc = WikiURL(page) + 'action=' + rateit_action + '&mode=delete&dimension=' + dimension + '&nocache=1&nopurge=1&rand=' + myRand;
219 document.getElementById(actionImg).title = msg_rating_deleted;
220 document.getElementById(actionImg).src = imgSrc;