| 1 |
wakaba |
1.1 |
document.documentElement.ondblclick = function (event) { |
| 2 |
|
|
var container = getEJContainer (event.target); |
| 3 |
|
|
if (container && !container.jaForm) { |
| 4 |
|
|
insertJaForm (container); |
| 5 |
|
|
return false; |
| 6 |
|
|
} |
| 7 |
|
|
|
| 8 |
|
|
return true; |
| 9 |
|
|
}; // document.documentElement.ondblclick |
| 10 |
|
|
|
| 11 |
|
|
function getEJContainer (n) { |
| 12 |
|
|
while (n) { |
| 13 |
|
|
if (n.hasAttribute && n.hasAttribute ('data-ja-hash')) { |
| 14 |
|
|
return n; |
| 15 |
|
|
} |
| 16 |
|
|
n = n.parentNode; |
| 17 |
|
|
} |
| 18 |
|
|
return null; |
| 19 |
|
|
} // getEJContainer |
| 20 |
|
|
|
| 21 |
|
|
function insertJaForm (targetNode) { |
| 22 |
|
|
var form = document.createElement ('form'); |
| 23 |
|
|
form.action = 'javascript:void (0)'; |
| 24 |
|
|
|
| 25 |
|
|
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>"; |
| 26 |
|
|
|
| 27 |
|
|
form.onchange = function () { |
| 28 |
|
|
form.jaFormChanged = true; |
| 29 |
|
|
}; // form.onchange |
| 30 |
|
|
|
| 31 |
|
|
form.onsubmit = function () { |
| 32 |
|
|
form.jaFormChanged = false; |
| 33 |
|
|
}; // form.onsubmit |
| 34 |
|
|
|
| 35 |
|
|
form.getElementsByTagName ('button')[0].disabled = true; // Submit |
| 36 |
|
|
|
| 37 |
|
|
form.getElementsByTagName ('button')[1].onclick = function () { |
| 38 |
|
|
if (!form.jaFormChanged || |
| 39 |
|
|
confirm ('Changes are discarded.')) { |
| 40 |
|
|
targetNode.jaForm = null; |
| 41 |
|
|
form.parentNode.removeChild (form); |
| 42 |
|
|
} |
| 43 |
|
|
}; // "Close".onclick |
| 44 |
|
|
|
| 45 |
|
|
targetNode.jaForm = form; |
| 46 |
|
|
|
| 47 |
|
|
var hash = targetNode.getAttribute ('data-ja-hash'); |
| 48 |
|
|
var req = new XMLHttpRequest (); |
| 49 |
|
|
req.open ('GET', 'edit2/' + hash + '.json', true); |
| 50 |
|
|
req.onreadystatechange = function () { |
| 51 |
|
|
if (req.readyState == 4) { |
| 52 |
|
|
if (req.status >= 200 && req.status < 400) { |
| 53 |
|
|
var entry = eval ("(" + req.responseText + ")"); |
| 54 |
|
|
|
| 55 |
|
|
form.action = 'edit2/'; |
| 56 |
|
|
form.method = 'post'; |
| 57 |
|
|
form.acceptCharset = 'utf-8'; |
| 58 |
|
|
|
| 59 |
|
|
form.getElementsByTagName ('button')[0].disabled = false; |
| 60 |
|
|
|
| 61 |
|
|
form.getElementsByTagName ('textarea')[0].value = entry.en; |
| 62 |
|
|
form.getElementsByTagName ('textarea')[1].value = entry.ja || ''; |
| 63 |
|
|
form.getElementsByTagName ('textarea')[2].value = entry.tags.join ("\n"); |
| 64 |
|
|
form.getElementsByTagName ('input')[0].checked = entry.isPattern; |
| 65 |
|
|
|
| 66 |
|
|
insertInterfaceElement (form, targetNode); |
| 67 |
|
|
} |
| 68 |
|
|
} |
| 69 |
|
|
}; // onreadystatechange |
| 70 |
|
|
req.send (null); |
| 71 |
|
|
} // insertJaForm |
| 72 |
|
|
|
| 73 |
|
|
function insertInterfaceElement (insertedElement, contextElement) { |
| 74 |
|
|
var parent = contextElement.parentNode; |
| 75 |
|
|
var prev = contextElement; |
| 76 |
|
|
while (true) { |
| 77 |
|
|
if (!parent.parentNode) break; |
| 78 |
|
|
var ln = parent.nodeName.toLowerCase (); |
| 79 |
|
|
if (ln == 'tr' || ln == 'tbody' || ln == 'thead' || ln == 'tfoot' || |
| 80 |
|
|
ln == 'table' || ln == 'pre') { |
| 81 |
|
|
prev = parent; |
| 82 |
|
|
parent = parent.parentNode; |
| 83 |
|
|
} else { |
| 84 |
|
|
break; |
| 85 |
|
|
} |
| 86 |
|
|
} |
| 87 |
|
|
|
| 88 |
|
|
parent.insertBefore (insertedElement, prev.nextSibling); |
| 89 |
|
|
} // insertInterfaceElement |