source: project/release/3/svnwiki-progress/trunk/svnwiki-progress.scm @ 12533

Last change on this file since 12533 was 12533, checked in by azul, 11 years ago

Importing svnwiki extensions.

  • Property svn:keywords set to id
File size: 8.7 KB
Line 
1; $id$
2;
3; License: GPL-3
4
5(declare (export))
6(use svnwiki-extensions-support srfi-40 html-stream stream-ext srfi-1)
7
8(define *progress-js-code*
9  #<<EOJS
10if (typeof(svnwikiProgress) != "object") {
11
12var svnwikiProgress = new Object();
13
14// We use this to avoid the GC from collecting objects from loadXml before
15// they have actually loaded their documents and processed them:
16
17svnwikiProgress.requests = new Array();
18
19svnwikiProgress.loadXml = function (href, process)
20{
21  var xmlDoc;
22  var len = svnwikiProgress.requests.length;
23
24  if (document.implementation && document.implementation.createDocument)
25    {
26      xmlDoc = document.implementation.createDocument("", "", null);
27      xmlDoc.onload = function () { svnwikiProgress.removeRequest(xmlDoc); process(xmlDoc) };
28    }
29  else if (window.ActiveXObject)
30    {
31      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
32      xmlDoc.onreadystatechange = function () { if (xmlDoc.readyState == 4) { svnwikiProgress.removeRequest(xmlDoc); process(xmlDoc); } };
33    }
34  else
35    {
36      alert('Your browser can\'t handle this script');
37      return;
38    }
39
40  svnwikiProgress.requests.push(xmlDoc);
41  xmlDoc.load(href);
42}
43
44svnwikiProgress.removeRequest = function (element)
45{
46  var rd, wr;
47  for (rd = 0, wr = 0; rd < svnwikiProgress.requests.length; rd ++)
48    if (svnwikiProgress.requests[rd] != element)
49      svnwikiProgress.requests[wr ++] = svnwikiProgress.requests[rd];
50  while (wr < svnwikiProgress.requests.len)
51    svnwikiProgress.requests.pop();
52}
53
54svnwikiProgress.cleanContent = function (element)
55{
56  while (element.hasChildNodes())
57    element.removeChild(element.firstChild);
58}
59
60svnwikiProgress.makeLink = function ( target, content )
61{
62  var link = svnwikiProgress.makeElement('A', content);
63  link.setAttribute('href', target);
64  return link;
65}
66
67svnwikiProgress.makeElement = function ( type, content )
68{
69  var elem = document.createElement(type);
70  if (content)
71    elem.appendChild(content);
72  return elem;
73}
74
75svnwikiProgress.init = function ( expectedRev, target, staticURL )
76{
77  svnwikiProgress.loading = true;
78  svnwikiProgress.delay = 1000;
79  svnwikiProgress.expectedRev = expectedRev;
80  svnwikiProgress.position = 0;
81  svnwikiProgress.actualTime = 0;
82  // Append some random string at the end of the URL to defeat caches:
83  svnwikiProgress.staticURL = staticURL + "?" + expectedRev;
84
85  svnwikiProgress.progressWidget = document.getElementById(target);
86
87  for (var j = 0; j <= 8; ++j) {
88    if (j == 0 || j == 3 || j == 6) {
89      var span = svnwikiProgress.makeElement('span');
90      span.setAttribute('class', 'progress-break');
91      svnwikiProgress.progressWidget.appendChild(span);
92    }
93    var span = svnwikiProgress.makeElement('span');
94    span.setAttribute('class', 'progress-entry');
95    span.setAttribute('id', 'progress-id-' + j);
96    svnwikiProgress.progressWidget.appendChild(span);
97  }
98
99  svnwikiProgress.statusBar = svnwikiProgress.makeElement('p');
100  svnwikiProgress.progressWidget.appendChild(svnwikiProgress.statusBar);
101  svnwikiProgress.advance("Checking status...");
102  svnwikiProgress.checkProgress();
103}
104
105svnwikiProgress.advance = function ( text )
106{
107  svnwikiProgress.showWaitText(text);
108
109  if (!svnwikiProgress.loading) {
110    svnwikiProgress.progressWidget.style.display = "none";
111    return;
112  }
113
114  var colors = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
115  var chars = [ "0", "2", "4", "6", "8", "a", "c", "e" ];
116
117  svnwikiProgress.position = (svnwikiProgress.position + 1) % colors.length;
118
119  var current = chars.length;
120
121  for (var j = 0; j < 8; j ++) {
122    colors[(svnwikiProgress.position + j) % colors.length] = current;
123    current --;
124  }
125
126  for (var j = 0; j < colors.length; j ++) {
127    var position = document.getElementById("progress-id-" + j);
128    if (!position)
129      alert("Failed for" + j + ":" + colors.length);
130    position.setAttribute('style', 'background: #' + chars[colors[j]] + chars[colors[j]] + 'f;');
131  }
132}
133
134svnwikiProgress.showWaitText = function ( text )
135{
136  svnwikiProgress.cleanContent(svnwikiProgress.statusBar);
137  svnwikiProgress.statusBar.appendChild(document.createTextNode(svnwikiProgress.loading ? text : 'Done'));
138}
139
140svnwikiProgress.redirectStart = function ( )
141{
142  svnwikiProgress.cleanContent(svnwikiProgress.showTime);
143  if (svnwikiProgress.redirectTime == 0) {
144    svnwikiProgress.showTime.appendChild(document.createTextNode('You are now being redirected to '));
145    svnwikiProgress.showTime.appendChild(svnwikiProgress.makeLink(svnwikiProgress.staticURL, document.createTextNode('the resulting page')));
146    svnwikiProgress.showTime.appendChild(document.createTextNode('.'));
147    window.location = svnwikiProgress.staticURL;
148    return;
149  }
150
151  svnwikiProgress.showTime.appendChild(document.createTextNode('You will be redirected to '));
152  svnwikiProgress.showTime.appendChild(svnwikiProgress.makeLink(svnwikiProgress.staticURL, document.createTextNode('the resulting page')));
153  svnwikiProgress.showTime.appendChild(document.createTextNode(' in ' + svnwikiProgress.redirectTime + ' ' + (svnwikiProgress.redirectTime == 1 ? 'second' : 'seconds') + '.'));
154  setTimeout("svnwikiProgress.redirectStart();", 1000);
155  svnwikiProgress.redirectTime --;
156}
157
158svnwikiProgress.registerProgress = function ( xmlDoc )
159{
160  if (svnwikiProgress.expectedRev > -1
161      && xmlDoc.getElementsByTagName('rev')[0].childNodes[0].nodeValue >= svnwikiProgress.expectedRev) {
162    svnwikiProgress.loading = false;
163  }
164
165  svnwikiProgress.advance(xmlDoc.getElementsByTagName('desc')[0].childNodes[0].nodeValue);
166
167  if (!svnwikiProgress.loading) {
168    svnwikiProgress.redirectTime = 5;
169    var report = document.getElementById('commit-results');
170    if (report) {
171      svnwikiProgress.cleanContent(report);
172      report.appendChild(svnwikiProgress.makeElement('p', svnwikiProgress.makeElement('b', document.createTextNode('Your changes have been saved.'))));
173      svnwikiProgress.showTime = svnwikiProgress.makeElement('p');
174      report.appendChild(svnwikiProgress.showTime);
175      svnwikiProgress.redirectStart();
176    }
177    return;
178  }
179
180  var actualTime = xmlDoc.getElementsByTagName('time')[0].childNodes[0].nodeValue;
181  if (actualTime != svnwikiProgress.actualTime) {
182    svnwikiProgress.actualTime = actualTime;
183    svnwikiProgress.delay = 1000;
184  } else if (svnwikiProgress.delay < 120000) {
185    svnwikiProgress.delay *= 1.5;
186  }
187
188  setTimeout("svnwikiProgress.checkProgress()", svnwikiProgress.delay);
189}
190
191svnwikiProgress.checkProgress = function ( )
192{
193  svnwikiProgress.loadXml('?action=extension&extension=progress&format=xml', svnwikiProgress.registerProgress);
194}
195
196}
197EOJS
198)
199
200(define (progress-code-write env)
201  (let-from-environment env (path-out program)
202    (when (and path-out (string=? program "post-commit"))
203      (assert (and 'progress-code-write (string? path-out)))
204      (unless (directory? (svnwiki-make-pathname path-out "xsvnwiki-helper"))
205        (create-directory (svnwiki-make-pathname path-out "xsvnwiki-helper")))
206      (unless (directory? (svnwiki-make-pathname (list path-out "xsvnwiki-helper") "progress"))
207        (create-directory (svnwiki-make-pathname (list path-out "xsvnwiki-helper") "progress")))
208      (write-file-with-tmp
209        (svnwiki-make-pathname (list "xsvnwiki-helper" "progress") "progress")
210        "text/javascript"
211        path-out
212        (string->stream
213          *progress-js-code*)))))
214
215(define (show-progress-xml env)
216  (let-from-environment env (path-out)
217    (->stream-char
218      "Content-type: application/xml\n\n"
219      (port->stream
220        (open-input-file
221          (svnwiki-make-pathname (list path-out "xsvnwiki-helper") "progress-report" "xml"))))))
222
223(define (progress-commit-dynamic env)
224  (let-from-environment env (user-input)
225    (case (stream->symbol (user-input 'format stream-null))
226      ((xml) (show-progress-xml env))
227      (else
228        (->stream-char
229          "Content-type: text/html\n\n"
230          (html-stream
231            (html
232              (body
233                (progress-commit-code (environment env ((commit-rev -1))))))))))))
234
235(define (progress-commit-code env)
236  (let-from-environment env (static-url commit-rev path)
237    (html-stream
238      ((div id "progress-commit-confirmation"))
239      ((script type "text/javascript" src (format #f "~A/xsvnwiki-helper/progress/progress" static-url)))
240      ((script type "text/javascript")
241       (format #f "<!--~%if (typeof(svnwikiProgress) == 'object' && typeof(svnwikiProgress.init) == 'function') { svnwikiProgress.init(~A, 'progress-commit-confirmation', '~A/~A'); }~%-->"
242               commit-rev
243               static-url
244               path)))))
245
246(define (progress-commit-confirmation env)
247  (svnwiki-commit-handler-info
248    env
249    (progress-commit-code env)))
250
251(svnwiki-extension-define 'commit-handler 'progress progress-commit-confirmation)
252(svnwiki-extension-define 'start-update-notify 'progress progress-code-write)
253(svnwiki-extension-define 'dynamic 'progress progress-commit-dynamic)
Note: See TracBrowser for help on using the repository browser.