Changeset 14032 in project


Ignore:
Timestamp:
04/01/09 11:12:52 (11 years ago)
Author:
azul
Message:

Lots of tweaks: Use the Svnwiki JS library, improve the commit msgs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release/3/svnwiki-rating/trunk/svnwiki-rating.scm

    r12862 r14032  
    1212        ((script type "text/javascript" src (format #f "~A/xsvnwiki-helper/rating/rating" static-url)))))))
    1313
    14 (svnwiki-extension-define 'html-head-data 'inline-edit html-head-data)
     14(svnwiki-extension-define 'html-head-data 'rating html-head-data)
    1515
    1616(define (create-rating-helper env)
    1717  (let-from-environment env (path-out program)
    18     (when (and path-out (string=? program "post-commit"))
    19       (assert (and 'create-rating-helper (string? path-out)))
    20       (write-file-with-tmp
    21         (svnwiki-make-pathname (list "xsvnwiki-helper" "rating") "rating")
    22         "text/javascript"
    23         path-out
    24         (string->stream
    25           *rating-js-code*)))))
     18    (assert (and 'create-rating-helper (string? path-out)))
     19    (write-file-with-tmp
     20      (svnwiki-make-pathname (list "xsvnwiki-helper" "rating") "rating")
     21      "text/javascript"
     22      path-out
     23      *rating-js-code*)))
    2624
    2725(define (rating-render-bottom-span env)
     
    3836            ((span id span-id class "render-bottom-span render-bottom-span-rating") "Loading...")
    3937            ((script type "text/javascript")
    40              (format #f "<!--~%rating.url = \"~A\";~%if (typeof(rating.updateStats) == 'function') { rating.loadXml('~A', function (x) { rating.updateStats(x, '~A'); }); }~%-->"
     38             (format #f "<!--~%svnwikiRating.url = \"~A\";~%if (typeof(svnwikiRating.updateStats) == 'function') { svnwikiJavascript.loadXml('~A', function (x) { svnwikiRating.updateStats(x, '~A'); }); }~%-->"
    4139                     (or (get-props-parents-first "rating:application-url" path-in path) ; Deprecated
    4240                         (get-props-parents-first "svnwiki:rating:application-url" path-in path)
     
    6159        env
    6260        (html-stream
    63           ((a href (format #f "javascript:rating.vote(~A, \"~A\", \"~A\");" direction path id)
     61          ((a href (format #f "javascript:svnwikiRating.vote(~A, \"~A\", \"~A\");" direction path id)
    6462              class (format #f "render-bottom-span render-bottom-span-rating-~A" name))
    6563           (if image
     
    6967(define *rating-js-code*
    7068#<<EOF
    71 if (typeof(rating) != "object") {
    72 var rating = new Object();
    73 
    74 // We use this to avoid the GC from collecting objects from loadXml before
    75 // they have actually loaded their documents and processed them:
    76 
    77 rating.requests = new Array();
    78 
    79 rating.loadXmlDone = function (xmlDoc, process)
    80 {
    81   if (xmlDoc.readyState == 4) { // if "loaded"
    82     if (xmlDoc.status == 200) { // if "OK"
    83       process(xmlDoc.responseXML); // pass DOM
    84     } else if (xmlDoc.status >= 400) {
    85       alert("There was a problem (status = "+xmlDoc.status+")\nretrieving the XML data:\n" + xmlDoc.statusText);
    86     }
    87     rating.removeRequest(xmlDoc);
    88   }
     69var svnwikiRating = new Object();
     70
     71svnwikiRating.vote = function ( direction, file, id ) {
     72  var content = document.getElementById(id);
     73  svnwikiJavascript.cleanContent(content);
     74  content.appendChild(svnwikiJavascript.makeElement('i', document.createTextNode("Updating...")));
     75  svnwikiJavascript.loadXml(svnwikiRating.url + file + "?action=extension&extension=rating&vote=" + direction, function (x) { svnwikiRating.updateStats(x, id); });
    8976}
    9077
    91 rating.loadXml = function (href, process)
    92 {
    93   var xmlDoc;
    94   var registerstatechange = true;
    95 
    96   if (window.XMLHttpRequest && !(window.ActiveXObject)) {
    97     try {
    98       xmlDoc = new XMLHttpRequest();
    99       xmlDoc.onreadystatechange = function () { rating.loadXmlDone(xmlDoc, process); };
    100       xmlDoc.open("GET", href, true);
    101       xmlDoc.send("");
    102     }
    103     catch(e) {
    104       xmlDoc = false;
    105     }
    106   }
    107 
    108   if (!xmlDoc && document.implementation && document.implementation.createDocument) {
    109     xmlDoc = document.implementation.createDocument("", "", null);
    110     xmlDoc.onload = function () { rating.removeRequest(xmlDoc); process(xmlDoc) };
    111     xmlDoc.load(href);
    112   }
    113 
    114   if (!xmlDoc && window.ActiveXObject) {
    115     try {
    116       xmlDoc = new ActiveXObject("Msxml2.XMLHTTP");
    117     }
    118     catch(e) {
    119       try {
    120         xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
    121       }
    122       catch(e) {
    123         xmlDoc = false;
    124       }
    125     }
    126     if (xmlDoc) {
    127       xmlDoc.onreadystatechange = function() { rating.loadXmlDone(xmlDoc, process); }
    128       xmlDoc.load(href);
    129     }
    130   }
    131 
    132   if (!xmlDoc) {
    133     alert('Your browser can\'t handle this script');
    134     return;
    135   }
    136 
    137   rating.requests.push(xmlDoc);
    138 }
    139 
    140 rating.removeRequest = function (element)
    141 {
    142   var rd, wr;
    143   for (rd = 0, wr = 0; rd < rating.requests.length; rd ++)
    144     if (rating.requests[rd] != element)
    145       rating.requests[wr ++] = rating.requests[rd];
    146   while (wr < rating.requests.len)
    147     rating.requests.pop();
    148 }
    149 
    150 rating.cleanContent = function (element)
    151 {
    152   while (element.hasChildNodes())
    153     element.removeChild(element.firstChild);
    154 }
    155 
    156 rating.makeLink = function ( target, content )
    157 {
    158   var link = makeElement('A', content);
    159   link.setAttribute('href', target);
    160   return link;
    161 }
    162 
    163 rating.makeElement = function (type, content)
    164 {
    165   var elem = document.createElement(type);
    166   if (content)
    167     elem.appendChild(content);
    168   return elem;
    169 }
    170 
    171 rating.vote = function ( direction, file, id ) {
     78svnwikiRating.updateStats = function ( xmlDoc, id ) {
    17279  var content = document.getElementById(id);
    173   rating.cleanContent(content);
    174   content.appendChild(rating.makeElement('i', document.createTextNode("Updating...")));
    175   rating.loadXml(rating.url + file + "?action=extension&extension=rating&vote=" + direction, function (x) { rating.updateStats(x, id); });
    176 }
    177 
    178 rating.updateStats = function ( xmlDoc, id ) {
    179   var content = document.getElementById(id);
    180   rating.cleanContent(content);
     80  svnwikiJavascript.cleanContent(content);
    18181  var data = xmlDoc.getElementsByTagName('data');
    18282  if (data && data[0]) {
    18383    content.appendChild(document.createTextNode('Rating: '));
    184     content.appendChild(rating.makeElement('b', document.createTextNode(data[0].getAttribute('score') || data[0].getAttribute('rating'))));
     84    content.appendChild(svnwikiJavascript.makeElement('b', document.createTextNode(data[0].getAttribute('score') || data[0].getAttribute('rating'))));
    18585    votes = data[0].getAttribute('votes');
    18686    if (votes) {
     
    19090    }
    19191  }
    192 }
    19392}
    19493EOF
     
    378277          (svn-add (svnwiki-make-pathname path-in real-path)))
    379278
    380         (svn-commit path-in user password (format #f "Registering vote~%")))
     279        (svn-commit path-in user password
     280                    (receive (score vote)
     281                             (rating-compute env)
     282                      (format #f "Registering ~A vote: ~A (~A)~%"
     283                              (or (stream->number (user-input 'vote stream-null)) 1)
     284                              score
     285                              vote))))
    381286
    382287      (->stream-char
Note: See TracChangeset for help on using the changeset viewer.