$.fn.CommentEditor = function(options) { var OPT; OPT = $.extend({ url: "https://www.depechemode.sk/?ACT=95", comment_body: '.comment_body', showEditor: '.edit_link', hideEditor: '.cancel_edit', saveComment: '.submit_edit', closeComment: '.mod_link' }, options); var view_elements = [OPT.comment_body, OPT.showEditor, OPT.closeComment].join(','), edit_elements = '.editCommentBox', csrf_token = 'e5e8b0df70d04585378411918c8b68b168b17ffa'; return this.each(function() { var id = this.id.replace('comment_', ''), parent = $(this); parent.find(OPT.showEditor).click(function() { showEditor(id); return false; }); parent.find(OPT.hideEditor).click(function() { hideEditor(id); return false; }); parent.find(OPT.saveComment).click(function() { saveComment(id); return false; }); parent.find(OPT.closeComment).click(function() { closeComment(id); return false; }); }); function showEditor(id) { $("#comment_"+id) .find(view_elements).hide().end() .find(edit_elements).show().end(); } function hideEditor(id) { $("#comment_"+id) .find(view_elements).show().end() .find(edit_elements).hide(); } function closeComment(id) { var data = {status: "close", comment_id: id, csrf_token: csrf_token}; $.post(OPT.url, data, function (res) { if (res.error) { return $.error('Could not moderate comment.'); } $('#comment_' + id).hide(); }); } function saveComment(id) { var content = $("#comment_"+id).find('.editCommentBox'+' textarea').val(), data = {comment: content, comment_id: id, csrf_token: csrf_token}; $.post(OPT.url, data, function (res) { if (res.error) { hideEditor(id); return $.error('Could not save comment.'); } $("#comment_"+id).find('.comment_body').html(res.comment); hideEditor(id); }); } }; $(function() { $('.comment').CommentEditor(); });