kruncher

ExpressionEngine AJAX Comment Form

Dec 10th, 2012
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Based on: http://gristmill.github.com/jquery-popbox/
  2. //
  3. // Special thanks to Tom Davies
  4. // http://expressionengine.stackexchange.com/questions/1378/how-to-create-front-end-ajax-handler-which-validates-and-adds-comment-to-content
  5. //
  6. $(function() {
  7.  
  8.     function hideBox(speed) {
  9.         $('.box').fadeOut(speed || 'fast');
  10.         $('.popbox-button').removeClass('active');
  11.     }
  12.  
  13.     function showBox(pop) {
  14.         pop = $(pop);
  15.         box = pop.parent().find('.box');
  16.  
  17.         box.find('.arrow, .arrow-border').css({
  18.             'left': 45
  19.         });
  20.  
  21.         if (box.css('display') !== 'block') {
  22.             pop.addClass('active');
  23.             box.css({
  24.                 'display': 'block',
  25.                 'top': 5
  26.             });
  27.         }
  28.         else
  29.             hideBox();
  30.     }
  31.  
  32.     var actionUrl = $('#leave-comment-placeholder').data('action');
  33.     var form;
  34.  
  35.     // Hide comment popup box when escape key is pressed or document
  36.     // was clicked somewhere.
  37.     $(document).bind('keyup', function(event) {
  38.         if (event.keyCode == 27)
  39.             hideBox();
  40.     });
  41.     $(document).bind('click', function(event){
  42.         if (!$(event.target).closest('.popbox').length)
  43.             hideBox();
  44.     });
  45.  
  46.     // Replace placeholder element with "Leave Comment" button
  47.     $('#leave-comment-placeholder').replaceWith(
  48.         '<div id="comment-popbox" class="popbox">' +
  49.             '<button id="leave-comment-button" class="ui-pink-button popbox-button">Leave Comment</button>' +
  50.             '<div class="collapse">' +
  51.                 '<div class="box">' +
  52.                     '<div class="arrow"></div>' +
  53.                     '<div class="arrow-border"></div>' +
  54.                     '<div class="message-body"></div>' +
  55.                     '<div class="popbox-content"></div>' +
  56.                 '</div>' +
  57.             '</div>' +
  58.         '</div>'
  59.     );
  60.  
  61.     // Occurs when comment form has been submitted successfully
  62.     function onCommentFormSuccess(responseHtml) {
  63.         // Was an error message returned?
  64.         if (responseHtml.match(/<title>Error<\/title>/)) {
  65.             // Add error prompt.
  66.             responseHtml = responseHtml.match(/<div[^]+div>/)[0];
  67.             if (!responseHtml)
  68.                 responseHtml = '<div class="message-body">An unknown error occurred when submitting comment.</div>';
  69.  
  70.             responseHtml = $(responseHtml);
  71.             responseHtml.find('p.link').remove();
  72.             $('#comment-popbox .message-body')
  73.                 .replaceWith(responseHtml);
  74.         }
  75.         else {
  76.             // Comment was posted, display success message!
  77.             $('#comment-popbox .message-body')
  78.                 .html('<h2>Thank you!</h3><p>Your comment has been submitted for moderation.</p>');
  79.  
  80.             // Reload comment submission form next time button is clicked!
  81.             form.remove();
  82.             form = null;
  83.  
  84.             setTimeout(function() {
  85.                 hideBox('slow');
  86.             }, 2000);
  87.         }
  88.     }
  89.  
  90.     // Occurs when comment form has been loaded
  91.     function onCommentFormLoaded(formHtml) {
  92.         var popboxContent = $('#comment-popbox .popbox-content');
  93.         popboxContent.html(formHtml);
  94.         form = popboxContent.find('form');
  95.         form.submit(function(e) {
  96.             e.preventDefault();
  97.             e.stopPropagation();
  98.  
  99.             $.ajax({
  100.                 type: 'POST',
  101.                 url: form.attr('action'),
  102.                 data: form.serialize(),
  103.                 success: onCommentFormSuccess
  104.             });
  105.         });
  106.         showBox('#leave-comment-button');
  107.     }
  108.  
  109.     $('#leave-comment-button')
  110.         .click(function() {
  111.             // Do not reload comment form if it has already been loaded!
  112.             if (!form) {
  113.                 // Make button stick down
  114.                 $(this).addClass('active');
  115.                 // Remove previous status message
  116.                 $('#comment-popbox .message-body').html('');
  117.  
  118.                 // Load comment form using AJAX
  119.                 $.ajax({
  120.                     url: actionUrl,
  121.                     success: onCommentFormLoaded
  122.                 })
  123.             }
  124.             else {
  125.                 showBox($(this));
  126.             }
  127.         })
  128.  
  129. });
Advertisement
Add Comment
Please, Sign In to add comment