var D = MochiKit.DOM; function submitCommentForm(form) { var container = form.containerDiv; var selElem = container.selectedElement; if (selElem) { unshowCommented(selElem); } var postBody = formQueryString(form); var action = form.getAttribute('action'); action += '?_=t'; d = doSimplePost(action, postBody); d.addCallbacks(function (data) { if (data.responseText == 'XXX') { unshowForm(container, selElem); return; } deselectElement(selElem); if (container.editingComment) { if (container.editingComment.parentNode) { D.removeElement(container.editingComment); } container.editingComment = null; } if (data.responseText == '__DELETE__') { unshowForm(container); return; } container.innerHTML = data.responseText; }, function (error) { container.innerHTML = error.req.responseText; D.addElementClass(container, 'commentary-error'); }); return false; } function submitFormInPlace(button) { var form = button.form; var postBody = formQueryString(form, ['inplace'], ['t']); var action = form.getAttribute('action'); action += '?_=t'; /* Start with a throb... */ var effect = new Effect.Highlight( form.containerDiv, {startcolor: '#ffff88', endcolor: '#ff8888', restorecolor: '#ffff88', duration: 1000}); d = doSimplePost(action, postBody); d.addCallbacks(function (data) { effect.cancel(); /* Finish with green... */ new Effect.Highlight( form.containerDiv, {startcolor: '#88ff88', endcolor: '#ffff88', restorecolor: '#ffff88'}); if (form.elements.position_id) { var old = form.elements.position_id; old.setAttribute('name', 'comment_id'); form.elements.comment_id = old; old.value = data.responseText; form.setAttribute('action', commentary_edit_uri); } return false; }, function (error) { var container = form.containerDiv; var errorContainer = D.DIV({'class': 'commentary-error'}); errorContainer.innerHTML = error.req.responseText; container.appendChild(errorContainer); }); return false; } function createCommentForm(position_id, inline) { var form, position, page, username, email; form = $('commentary-form').cloneNode(true); form.id = null; fixupForm(form); var formDiv = setupCommentForm(form, inline); position = form.elements.position_id; page = form.elements.page; position.value = position_id; page.value = commentary_this_page; return formDiv; } function setupCommentForm(form, inline) { var divClass = 'commentary-comment'; if (inline) { divClass += ' commentary-inline'; } var formDiv = D.DIV({'class': divClass, 'comment-meta': '1'}); formDiv.appendChild(form); form.containerDiv = formDiv; formDiv.form = form; username = form.elements.username; email = form.elements.email; defaultLabel(username, 'Your name', 'username'); defaultLabel(email, 'Your email', 'email'); return formDiv; } function cancelSubmitForm(button) { var container = button.form.containerDiv; _cancelSubmitForm(container); } function _cancelSubmitForm(container) { var selElem = container.selectedElement; if (selElem) { for (var i=0; i<_elementsCommentedOn.length; i++) { if (_elementsCommentedOn[i] && _elementsCommentedOn[i].element == selElem) { _elementsCommentedOn[i] = null; } } unshowCommented(container.selectedElement); D.removeElementClass( selElem, 'commentary-selected-element'); } unshowForm(container, selElem); return false; } _elementsCommentedOn = []; //cancelSubmitForm = MochiKit.Logging.catchException(cancelSubmitForm); function commentOnElement(el) { for (var i=0; i<_elementsCommentedOn.length; i++) { if (_elementsCommentedOn[i] && el == _elementsCommentedOn[i].element) { var formDiv = _elementsCommentedOn[i].formDiv; if (formDiv.form.elements.comment.value) { highlightCommented(el); } else { cancelSubmitForm(formDiv); _elementsCommentedOn[i] = null; } return; } } var expr = dumbpath.makeExpression(el); var formDiv = createCommentForm(expr); formDiv.selectedElement = el; showCommented(el); D.addElementClass(formDiv, 'commentary-inline'); showForm(formDiv, el); _elementsCommentedOn.push({element: el, formDiv: formDiv}); try { formDiv.form.elements.comment.focus(); } catch (e) { /* We can get an exception here because the visibility of the textarea hasn't registered yet. */ } } function fixupForm(form) { tags = ['select', 'input', 'textarea', 'button']; for (var i=0; i