/[suikacvs]/markup/html/html5/spec-ja/ja-script.js
Suika

Diff of /markup/html/html5/spec-ja/ja-script.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by wakaba, Sun Oct 26 06:50:10 2008 UTC revision 1.4 by wakaba, Sun Oct 26 10:10:54 2008 UTC
# Line 1  Line 1 
1    window.addEventListener ('load', function () {
2      createJaHashIndex ();
3    
4      var req = new XMLHttpRequest ();
5      req.open ('GET', 'edit2/updates.json', true);
6      req.onreadystatechange = function () {
7        if (req.readyState == 4) {
8          if (req.status >= 200 && req.status < 400) {
9            applyJaUpdates (eval ('(' + req.responseText + ')'));
10          }
11        }
12      }; // onreadystatechange
13      req.send (null);
14    }, false); // window.onload
15    
16  document.documentElement.ondblclick = function (event) {  document.documentElement.ondblclick = function (event) {
17    var container = getEJContainer (event.target);    var container = getEJContainer (event.target);
18    if (container && !container.jaForm) {    if (container && !container.jaForm) {
# Line 8  document.documentElement.ondblclick = fu Line 23  document.documentElement.ondblclick = fu
23    return true;    return true;
24  }; // document.documentElement.ondblclick  }; // document.documentElement.ondblclick
25    
26    function createJaHashIndex () {
27      var hash = [];
28      var realHash = [];
29    
30      var all = document.getElementsByTagName ('*');
31      var allL = all.length;
32      for (var i = 0; i < allL; i++) {
33        var el = all[i];
34        var hashValue = el.getAttribute ('data-ja-hash');
35        if (hashValue) {
36          if (!hash[hashValue]) hash[hashValue] = [];
37          hash[hashValue].push (el);
38    
39          hashValue = el.getAttribute ('data-ja-real-hash');
40          if (!realHash[hashValue]) realHash[hashValue] = [];
41          realHash[hashValue].push (el);
42        }
43      }
44    
45      document.jaHashes = hash;
46      document.jaRealHashes = realHash;
47    } // createJaHashIndex
48    
49  function getEJContainer (n) {  function getEJContainer (n) {
50    while (n) {    while (n) {
51      if (n.hasAttribute && n.hasAttribute ('data-ja-hash')) {      if (n.hasAttribute && n.hasAttribute ('data-ja-hash')) {
# Line 19  function getEJContainer (n) { Line 57  function getEJContainer (n) {
57  } // getEJContainer  } // getEJContainer
58    
59  function insertJaForm (targetNode) {  function insertJaForm (targetNode) {
60    var form = document.createElement ('form');    var form = document.createElement ('div');
61    form.action = 'javascript:void (0)';    form.className = 'ja-edit-form';
62    
63    form.innerHTML = "<p><label>Original text:<br><textarea name=en></textarea></label><br>(<label><input name=pattern type=checkbox> Pattern</label>)<p><label>Translated text:<br><textarea name=ja></textarea></label><p><label>Tags:<br><textarea name=tags></textarea></label><p><button type=submit class=ja-save-button>Save</button> <button type=button class=ja-close-button>Close</button>";    form.innerHTML = "<p><label>Original text:<br><textarea name=en></textarea></label><br>\
64          (<label><input name=pattern type=checkbox> Pattern</label><span> <button type=button>Load original text</button></span>)\
65          <p><label>Translated text:<br><textarea name=ja></textarea></label><p><label>Tags:<br><textarea name=tags></textarea></label><p><button type=submit class=ja-save-button>Save and close</button> <button type=button class=ja-close-button>Close</button> (<output>Text not loaded yet</output>)";
66        
67    form.onchange = function () {    form.onchange = function () {
68      form.jaFormChanged = true;      form.jaFormChanged = true;
# Line 32  function insertJaForm (targetNode) { Line 72  function insertJaForm (targetNode) {
72      form.jaFormChanged = false;      form.jaFormChanged = false;
73    }; // form.onsubmit    }; // form.onsubmit
74    
75    form.getElementsByTagName ('button')[0].disabled = true; // Submit    if (!targetNode.hasAttribute ('data-ja-real-hash')) {
76        form.getElementsByTagName ('span')[0].setAttribute ('hidden', '');
77      }
78    
79      form.getElementsByTagName ('button')[0].onclick = function () {
80        if (!form.jaFormChanged ||
81            confirm ('Changes are discarded.')) {
82          loadTextByHash (form, targetNode.getAttribute ('data-ja-real-hash'));
83        }
84      }; // "Load real text".onclick
85    
86      form.getElementsByTagName ('button')[1].disabled = true; // Submit
87    form.getElementsByTagName ('button')[1].onclick = function () {    form.getElementsByTagName ('button')[1].onclick = function () {
88        if (!form.jaFormChanged) {
89          form.getElementsByTagName ('output')[0].firstChild.data
90              = 'Text not modified.';
91          return;
92        }
93    
94        var v = '';
95    
96        var ta = form.getElementsByTagName ('textarea');
97        v += 'en=' + encodeURIComponent (ta[0].value);
98        v += '&ja=' + encodeURIComponent (ta[1].value);
99        v += '&tags=' + encodeURIComponent (ta[2].value);
100    
101        var isPattern = form.getElementsByTagName ('input')[0].checked;
102        if (isPattern) {
103          v += '&pattern=on';
104        }
105    
106        var req = new XMLHttpRequest ();
107        req.open ('POST', 'edit2/', true);
108        req.onreadystatechange = function () {
109          if (req.readyState == 4) {
110            form.getElementsByTagName ('output')[0].firstChild.data
111                = 'Save: ' + req.status + ' ' + req.statusText;
112            if (req.status >= 200 && req.status < 400) {
113              targetNode.jaForm = null;
114              form.parentNode.removeChild (form);
115              applyJaUpdates (eval ('(' + req.responseText + ')'));
116            }
117          }
118        }; // onreadystatechange
119        req.setRequestHeader
120            ('Content-Type', 'application/x-www-form-urlencoded');
121        req.send (v);
122      }; // "Save and close".onclick
123    
124      form.getElementsByTagName ('button')[2].onclick = function () {
125      if (!form.jaFormChanged ||      if (!form.jaFormChanged ||
126          confirm ('Changes are discarded.')) {          confirm ('Changes are discarded.')) {
127        targetNode.jaForm = null;        targetNode.jaForm = null;
# Line 43  function insertJaForm (targetNode) { Line 130  function insertJaForm (targetNode) {
130    }; // "Close".onclick    }; // "Close".onclick
131    
132    targetNode.jaForm = form;    targetNode.jaForm = form;
133      insertInterfaceElement (form, targetNode);
134    
135    var hash = targetNode.getAttribute ('data-ja-hash');    var hash = targetNode.getAttribute ('data-ja-hash');
136      loadTextByHash (form, hash);
137    } // insertJaForm
138    
139    function loadTextByHash (form, hash) {
140    var req = new XMLHttpRequest ();    var req = new XMLHttpRequest ();
141    req.open ('GET', 'edit2/' + hash + '.json', true);    req.open ('GET', 'edit2/' + hash + '.json', true);
142    req.onreadystatechange = function () {    req.onreadystatechange = function () {
143      if (req.readyState == 4) {      if (req.readyState == 4) {
144          form.getElementsByTagName ('output')[0].firstChild.data
145              = 'Load: ' + req.status + ' ' + req.statusText;
146        if (req.status >= 200 && req.status < 400) {        if (req.status >= 200 && req.status < 400) {
147          var entry = eval ("(" + req.responseText + ")");          var entry = eval ("(" + req.responseText + ")");
148    
149          form.action = 'edit2/';          form.getElementsByTagName ('button')[1].disabled = false; // Submit
         form.method = 'post';  
         form.acceptCharset = 'utf-8';  
   
         form.getElementsByTagName ('button')[0].disabled = false;  
150    
151          form.getElementsByTagName ('textarea')[0].value = entry.en;          form.getElementsByTagName ('textarea')[0].value = entry.en;
152          form.getElementsByTagName ('textarea')[1].value = entry.ja || '';          form.getElementsByTagName ('textarea')[1].value = entry.ja || '';
153          form.getElementsByTagName ('textarea')[2].value = entry.tags.join ("\n");          form.getElementsByTagName ('textarea')[2].value = entry.tags.join ("\n");
154          form.getElementsByTagName ('input')[0].checked = entry.isPattern;          form.getElementsByTagName ('input')[0].checked = entry.isPattern;
155    
156          insertInterfaceElement (form, targetNode);          form.jaFormChanged = false;
157        }        }
158      }      }
159    }; // onreadystatechange    }; // onreadystatechange
160    req.send (null);    req.send (null);
161  } // insertJaForm  } // loadTextByHash
162    
163  function insertInterfaceElement (insertedElement, contextElement) {  function insertInterfaceElement (insertedElement, contextElement) {
164    var parent = contextElement.parentNode;    var parent = contextElement.parentNode;
# Line 86  function insertInterfaceElement (inserte Line 176  function insertInterfaceElement (inserte
176    }    }
177    
178    parent.insertBefore (insertedElement, prev.nextSibling);    parent.insertBefore (insertedElement, prev.nextSibling);
 } // insertInterfaceElement  
179    } // insertInterfaceElement
180    
181    function applyJaUpdates (updates) {
182    document.newEntries =updates;
183      for (var hash in updates) {
184        if (document.jaHashes[hash]) {
185          for (var i in document.jaHashes[hash]) {
186            applyJaEntry (document.jaHashes[hash][i], updates[hash]);
187          }
188        }
189        if (document.jaRealHashes[hash]) {
190          for (var i in document.jaRealHashes[hash]) {
191            applyJaEntry (document.jaRealHashes[hash][i], updates[hash]);
192          }
193        }
194      }
195    } // applyJaUpdates
196    
197    function applyJaEntry (targetNode, entry) {
198      if (entry.isPattern) {
199        targetNode.className += ' ja-not-patched';
200      } else {
201        var en;
202        var cn = targetNode.childNodes;
203        for (var i = 0; i < cn.length; i++) {
204          var n = cn[i];
205          if (n.className && n.className.match (/\bja-translation\b/)) {
206            var prefix;
207            if (n.firstChild && n.firstChild.className == 'secno') {
208              prefix = n.firstChild;
209            }
210            n.innerHTML = formatJaText (entry.ja);
211            if (prefix) n.insertBefore (prefix, n.firstChild);
212            targetNode.className += ' ja-patched';
213            return;
214          } else if (n.className && n.className.match (/\ben-original\b/)) {
215            en = n;
216          }
217        }
218      
219        var span = document.createElement ('span');
220        span.className = 'ja-translation';
221        var prefix;
222        if (en.firstChild && en.firstChild.className == 'secno') {
223          prefix = en.firstChild.cloneNode (true);
224        }
225        span.innerHTML = formatJaText (entry.ja);
226        if (prefix) span.insertBefore (prefix, span.firstChild);
227        targetNode.appendChild (span);
228        targetNode.className
229            = targetNode.className.replace
230                (/\bno-ja-translation\b/, 'has-ja-translation ja-patched');
231      }
232    } // applyJaEntry
233    
234    function formatJaText (s) {
235      return s.replace
236          (/\[\[([A-Z ]+):([^]]+)\]\]/,
237           function (a, b, c) {
238             return "<em class=rfc2119 title=\"" + b + "\">" + c + "</em>";
239           });
240    } // formatJaText

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.4

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24