additional_plugins/serendipity_event_livecomment/serendipity_event_livecomment.js-def
2011-12-13 12:29:05 +01:00

187 lines
6.2 KiB
Plaintext
Executable file

/* Based on the liveComment plugin, which has this copyright note: */
/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License"); |
// | you may not use this file except in compliance with the License. |
// | You may obtain a copy of the License at |
// | http://www.apache.org/licenses/LICENSE-2.0 |
// | Unless required by applicable law or agreed to in writing, software |
// | distributed under the License is distributed on an "AS IS" BASIS, |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
// | implied. See the License for the specific language governing |
// | permissions and limitations under the License. |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch> |
// +----------------------------------------------------------------------+
*/
var liveCommentReq = false;
var liveCommentLast = "";
var liveCommentIsIE = false;
var liveCommentWarn = false;
var lc_t = false;
function lc_addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function liveAttach(tel, myevent, liveCommentKeyPress) {
if (navigator.userAgent.indexOf("Safari") > 0) {
tel.addEventListener(myevent, liveCommentKeyPress,false);
} else if (navigator.product == "Gecko") {
tel.addEventListener(myevent, liveCommentKeyPress,false);
} else {
tel.attachEvent('on' + myevent, liveCommentKeyPress);
liveCommentIsIE = true;
}
}
function liveCommentInit() {
if (!document.getElementById('serendipity_replyTo')) {
// alert('Unsupported Template for LiveComment Preview, missing #serendipity_replyTo');
return false;
}
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
liveCommentReq = new XMLHttpRequest();
}
myevent = 'keyup';
tel = document.getElementById('serendipity_replyTo');
pel = document.getElementById('serendipity_preview');
liveAttach(tel, myevent, liveCommentKeyPress);
liveAttach(pel, 'click', liveCommentPreview);
tel.style.color = 'green';
liveCommentStart();
}
function liveCommentPreview(event) {
if (!liveCommentIsIE) { event.preventDefault(); }
liveCommentDoSearch();
return false;
}
function liveGetSource() {
rid = document.getElementById('serendipity_replyTo').value;
//alert('RID: ' + rid);
if (rid == 0 || liveCommentWarn) {
return false;
}
sourceel = document.getElementById('serendipityCommentForm');
if (!sourceel) {
sourceel = document.getElementById('serendipity_comment_CommentForm');
}
if (!sourceel && !liveCommentWarn) {
alert('This template does not have #serendipityCommentForm. Live Comment Previewing disabled.');
liveCommentWarn = true;
return false;
}
return sourceel;
}
function liveCommentKeyPress(event) {
sourceel = liveGetSource();
if (!sourceel) return false;
fields = sourceel.cloneNode(true);
sourceel.id = 'serendipityCommentFormOld';
store_text = document.getElementById('serendipity_commentform_comment').value;
store_replyTo = document.getElementById('serendipity_replyTo').value;
liveAttach(fields, 'click', liveCommentPreview);
iNode = document.getElementById('serendipity_replyform_' + rid);
iNode.parentNode.appendChild(fields);
pNode = sourceel.parentNode;
pNode.removeChild(sourceel);
document.getElementById('serendipity_commentform_comment').value = store_text;
document.getElementById('serendipity_replyTo').value = store_replyTo;
return true;
}
function liveCommentStart() {
if (lc_t) {
window.clearTimeout(lc_t);
}
lc_t = window.setTimeout("liveCommentDoSearch()",200);
}
function liveCommentDoSearch() {
if (liveCommentLast == document.getElementById('serendipity_commentform_comment').value) {
liveCommentStart();
return true;
}
if (liveCommentReq && liveCommentReq.readyState < 4) {
liveCommentReq.abort();
}
if (window.XMLHttpRequest) {
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
liveCommentReq = new ActiveXObject("Microsoft.XMLHTTP");
}
liveCommentReq.onreadystatechange= liveCommentProcessReqChange;
liveCommentReq.open("POST", lcbase);
liveCommentReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=' + lcchar);
liveCommentReq.send('data=' + document.getElementById('serendipity_commentform_comment').value);
liveCommentLast = document.getElementById('serendipity_commentform_comment').value;
}
function liveCommentProcessReqChange() {
if (liveCommentReq.readyState == 4) {
rid = document.getElementById('serendipity_replyTo').value;
if (rid < 1) {
rid = 0;
}
var res = document.getElementById("serendipity_replyform_" + rid);
if (!res) {
sourceel = document.getElementById('serendipityCommentForm');
if (!sourceel) {
sourceel = document.getElementById('serendipity_comment_CommentForm');
}
if (!sourceel) return false;
oldFeed = document.getElementById('serendipityPreviewFeedback');
if (oldFeed) {
poldFeed = oldFeed.parentNode;
poldFeed.removeChild(oldFeed);
}
res = sourceel.cloneNode(true);
res.id = 'serendipityPreviewFeedback';
res.innerHTML = '';
sourceel.parentNode.insertBefore(res, sourceel);
}
res.innerHTML = liveCommentReq.responseText;
liveCommentStart();
}
}
lc_addLoadEvent(liveCommentInit);