"New page" button for creating new pages interactively.
[cocanwiki.git] / html / _js / editor.js
1 /* Javascript for OCAMLWIKI.
2  * Copyright (C) 2004 Merjis Ltd.
3  * $Id: editor.js,v 1.1 2004/09/07 10:14:09 rich Exp $
4  */
5
6 var delay = 1000                // Delay in milliseconds before updating.
7
8 function update_preview (content_id, preview_id)
9 {
10   // Updating is quite expensive, so only update after a period of apparent
11   // inactivity.
12   var preview = document.getElementById (preview_id);
13   if (preview.timer) clearTimeout (preview.timer);
14   preview.timer =
15     setTimeout ("update_preview_now ('" + content_id + "', '" +
16                 preview_id + "')", delay);
17 }
18
19 function update_preview_now (content_id, preview_id)
20 {
21   // Remove the timer.
22   var preview = document.getElementById (preview_id);
23   if (preview.timer) preview.timer = null;
24
25   // Get the Wiki-markup content from the content textarea.
26   var content = document.getElementById (content_id).value;
27
28   // Send the Wiki-markup to the server to be turned into XHTML.
29   var http = document.all ?
30     new ActiveXObject ('Microsoft.XMLHTTP') : new XMLHttpRequest ();
31   if (http)
32     {
33       http.open ('POST', '/_bin/preview.cmo', false);
34       http.setRequestHeader ('Content-Type',
35                              'application/x-www-form-urlencoded');
36       http.send ('content=' + encodeURIComponent (content));
37
38       var xhtml = http.responseText;
39
40       // Next line fails with my copy of IE if the text contains a
41       // link (ie. <a href...>).  It is unclear why.  The error is:
42       // "Unknown runtime error"
43       preview.innerHTML = xhtml;
44     }
45 }