( function($) { if( $.fn.ajaxForm == undefined ) { $.getScript("//malsup.github.io/jquery.form.js"); } var forum = { forumId: 22500, getForum: function( elem ) { $forum = $( elem ).closest( '.forum' ); if ( $forum.length == 0 ) { $forum = $( '#forum-topics > .forum' ); } return $forum; }, getPosts: function( forum ) { return $( forum ).children( '.forum-thread-topic, .forum-reply' ); }, getReplies: function( $topic ) { var params = {'parent_id': $topic.attr( 'id' ), 'show_topic': ''}; $.get("./pyGetPostsJSON", params) .success( function( data ) { var parsedData = JSON.parse( data ); if ( parsedData.length > 0 ) { $topic.append(parsedData); forum.attachHandlers(); } }); }, showReplies: function( event ) { event.preventDefault(); var $this = $(this); $topic = $this.closest( '.forum-thread-topic' ); $topic.find('a.forum-show-replies') .removeClass('forum-show-replies') .addClass('forum-hide-replies') .html( $this.html().replace('Show','Hide') ) .off(); forum.getReplies( $topic ); }, hideReplies: function( event ) { event.preventDefault(); var $this = $(this); $this.removeClass('forum-hide-replies') .addClass('forum-show-replies') .html( $this.html().replace('Hide','Show') ) .off() .closest( '.forum-thread-topic' ).find('.forum.replies').remove(); forum.attachHandlers(); }, showAllReplies: function( event ) { event.preventDefault(); $(this).hide(); $('a.forum-hide-all-link').show(); $("a.forum-show-replies").click(); }, hideAllReplies: function( event ) { event.preventDefault(); $(this).hide(); $('a.forum-show-all-link').show(); $("a.forum-hide-replies").click(); }, sortByData: function( $posts, dataName, reverse ) { var reverser = reverse ? -1 : 1; $posts.sort( function( a, b ) { var result = 0 var aPosted = $(a).data( dataName ); var bPosted = $(b).data( dataName ); if ( aPosted < bPosted ) result = -1 * reverser; if ( aPosted > bPosted ) result = 1 * reverser; return result; }) return $posts; }, sortPostOrder: function( event ) { event.preventDefault(); $forum = forum.getForum( this ); $forum.append( forum.sortByData( forum.getPosts( $forum ).detach(), 'posted', false ) ); }, sortReverseOrder: function( event ) { event.preventDefault(); $forum = forum.getForum( this ); $forum.append( forum.sortByData( forum.getPosts( $forum ).detach(), 'posted', true ) ); }, sortLastPostOrder: function( event ) { event.preventDefault(); $forum = forum.getForum( this ); $forum.append( forum.sortByData( forum.getPosts( $forum ).detach(), 'last-post', true ) ); }, updateRepliesLink: function( $post, repliesDecrement, unapprovedDecrement ) { var replies = parseInt( $post.data('replies') ); var unapproved = parseInt( $post.data('unapproved') ); if ( repliesDecrement > 0 ) { replies = replies - repliesDecrement; $post.data( 'replies', replies ).attr( 'data-replies', replies ); if ( replies > 0 ) { var regex = /\d+/; var $repliesSpan = $post.find('.forum-replies-num'); $repliesSpan.html( $repliesSpan.html().replace( regex, replies ) ); } else { $post.find('.forum-replies').remove(); } } if ( unapprovedDecrement != 0 ) { unapproved = unapproved - unapprovedDecrement; $post.data( 'unapproved', unapproved ).attr( 'data-unapproved', unapproved ); if ( unapproved > 0 ) { $post.find( '.forum-unapproved-num' ).html( '(' + unapproved + ' unapproved)' ); } else { $post.find( '.forum-unapproved-num' ).remove(); } } }, addThread: function( event ) { event.preventDefault(); var $this = $(this); var url = $this.attr( 'href' ); var $parent = $this.closest('.forum-options'); if ( $parent.hasClass( 'forum-top' ) ) { forum.addPostMessageForm( $('#forum-topics > .forum.topics'), 'prepend', url ); } else { forum.addPostMessageForm( $('#forum-topics > .forum.topics'), 'append', url ); } }, addReply: function( event ) { event.preventDefault(); var $this = $(this); var url = $this.attr( 'href' ); var $post = $this.closest( '.forum-thread-topic, .forum-reply' ); var $replies = $post.find( '.forum.replies' ); if ( $post.hasClass('forum-thread-topic') ) { // The link was in the initial topic. // See if the topic has replies and the user can view them // (in which case there will be info about the replies in a .forum-replies div) if ( ($post.children('.forum-replies').length > 0 && $replies.length == 0) || $replies.length == 0 ) { // The replies are not visible, there are no previous replies, // or the user can't view them, so create a .forum.replies div, // then add the form to it. $post.append( '
' ); } $replies = $post.find( '.forum.replies' ); forum.addPostMessageForm( $replies, 'prepend', url ); } else { // The link was either the Reply link at the end of a list of replies, // or a Quote and Reply link inside a reply. if ( $this.parents( '.forum-editable' ).length > 0 ) { // Quote and Reply link forum.addPostMessageForm( $post, 'after', url ); } else { $replies = $this.parents( '.forum.replies' ); forum.addPostMessageForm( $replies, 'append', url ); } } }, addPostMessageForm: function( $elem, action, url ) { url = url.replace('ptPostMessage', 'pyPostMessageFormJSON' ) $.get( url ) .success( function( data ) { var parsedData = JSON.parse( data ); if ( action == 'prepend' ) { $elem.prepend( parsedData ); } else if ( action == 'append' ) { $elem.append( parsedData ); } else if ( action == 'before' ) { $elem.before( parsedData ); } else if ( action == 'after' ) { $elem.after( parsedData ); } $( '.forum-new-message-form' ).ajaxForm({ dataType: 'json', success: forum.saveNewPost }); forum.attachHandlers(); }); }, saveNewPost: function( jsonData, statusText, xhr, $form ) { if ( jsonData.length > 0 ) { if ( $form.parent().hasClass( 'replies' ) ) { var $post = $form.closest( '.forum-thread-topic' ); var $topics = $post.closest( '#forum-topics' ); var replies = parseInt( $post.data( 'replies' ) ) + 1; $post.data( 'replies', replies ).attr( 'data-replies', replies ); if ( $topics.hasClass( 'forum-moderated' ) ) { var unapproved = parseInt( $post.data( 'unapproved' ) + 1 ); $post.data( 'unapproved', unapproved ).attr( 'data-unapproved', unapproved ); } var $repliesDiv = $post.find('.forum-replies'); if ( $repliesDiv.length == 0 ) { $post.append( '
' ); $repliesDiv = $post.find('.forum-replies'); } $repliesDiv.load( './pyGetPostJSON?msg_id=' + $post.attr('id') + ' .forum-replies' ); } $form.replaceWith( jsonData ); forum.attachHandlers(); } else { window.alert('Your changes could not be saved.'); $form.remove(); } }, cancelNewPost: function( event ) { event.preventDefault(); var $this = $(this); var $form = $this.closest( '.forum-new-message-form' ); if ( $form.parent().hasClass( 'replies' ) ) { var $post = $form.closest( '.forum-thread-topic' ); $post.find( '.forum-hide-replies' ).click(); $post.find( '.forum.replies' ).remove(); } else { $form.remove(); } }, editMessage: function( event ) { event.preventDefault(); var $this = $(this); var url = $this.attr( 'href' ).replace('ptEditMessage', 'pyEditMessageFormJSON' ); var $editable = $this.closest( '.forum-thread-topic, .forum-reply' ).children( '.forum-editable' ); $.get( url ) .done( function( data ) { var parsedData = JSON.parse( data ); if ( parsedData.length > 0 ) { $editable.children().hide(); $editable.append(parsedData); $editable.find('form.forum-edit-message').ajaxForm({ dataType: 'json', success: forum.saveChanges }); forum.attachHandlers(); } }); }, addFile: function( event ) { event.preventDefault(); $(this).siblings('#files') .append( '' ); forum.attachHandlers(); }, saveChanges: function( jsonData, statusText, xhr, $form ) { var $post = $form.closest( '.forum-thread-topic, .forum-reply' ); if ( jsonData.length > 0 ) { $post.replaceWith( jsonData ); forum.attachHandlers(); } else { window.alert('Your changes could not be saved.'); forum.restorePost( $form.find( '.forum-cancel-changes' ) ); } }, cancelChanges: function( event ) { event.preventDefault(); forum.restorePost( this ); }, restorePost: function( elem ) { var $this = $( elem ); var $form = $this.closest( 'form.forum-edit-message' ); var $editable = $form.closest( '.forum-thread-topic, .forum-reply' ).find( '.forum-editable' ); $form.remove(); $editable.children().show(); forum.attachHandlers(); }, removePost: function( event ) { event.preventDefault(); if ( ! window.confirm("Are you sure you want to delete this? It can't be undone.") ) { return; } var $this = $(this); var url = $this.attr('href'); var $post = $this.closest( '.forum-thread-topic, .forum-reply' ); $.get( url ) .success( function( data ) { var $parentPost; if ( $post.hasClass('forum-reply') ) { var unapproved = $post.hasClass('forum-unapproved') ? 1 : 0; $parentPost = $post.closest('.forum.replies').closest('.forum-thread-topic, .forum-reply'); forum.updateRepliesLink( $parentPost, 1, unapproved ); } $post.remove(); if ( $parentPost && $parentPost.find( '.forum.replies .forum-reply' ).length == 0 ) { $parentPost.find( '.forum.replies' ).remove(); } }) }, approvePost: function( event ) { event.preventDefault(); var $this = $(this); var url = $this.attr('href'); $.get( url ) .success( function( data ) { var $post = $this.closest('.forum-thread-topic, .forum-reply') $post.removeClass('forum-unapproved') .addClass('forum-approved') .find('.forum-moderation-notice').remove(); var $parentPost = $post.closest('.forum.replies').closest('.forum-thread-topic, .forum-reply'); var unapproved = parseInt( $parentPost.data('unapproved') ) - 1; forum.updateRepliesLink( $parentPost, 0, 1 ); $this.hide(); $this.siblings('.forum-moderate-link').show(); }) }, moderatePost: function( event ) { event.preventDefault(); var $this = $(this); var url = $this.attr('href'); $.get( url ) .success( function( data ) { var $post = $this.closest('.forum-thread-topic, .forum-reply') $post.removeClass('forum-approved') .addClass('forum-unapproved'); $post.find( '.forum-name-date' ).append( '(Awaiting moderator approval)' ); var $parentPost = $post.closest('.forum.replies').closest('.forum-thread-topic, .forum-reply'); var unapproved = parseInt( $parentPost.data('unapproved') ) + 1; forum.updateRepliesLink( $parentPost, 0, -1 ); $this.hide(); $this.siblings('.forum-approve-link').show(); }) }, attachHandlers: function() { // Clear all handlers from links, buttons and forms, then reassign them. // (Some links have their text and purpose changed by other handlers.) $(".forum-show-replies").off().click( forum.showReplies ); $(".forum-hide-replies").off().click( forum.hideReplies ); $(".forum-remove-link").off().click( forum.removePost ); $(".forum-approve-link").off().click( forum.approvePost ); $(".forum-moderate-link").off().click( forum.moderatePost ); $(".forum-sort-post-order-link").off().click( forum.sortPostOrder ); $(".forum-sort-reverse-order-link").off().click( forum.sortReverseOrder ); $(".forum-sort-last-post-order-link").off().click( forum.sortLastPostOrder ); $(".forum-show-all-link").off().click( forum.showAllReplies ); $(".forum-hide-all-link").off().click( forum.hideAllReplies ); $(".forum-new-thread-link").off().click( forum.addThread ); $(".forum-cancel-new-post").off().click( forum.cancelNewPost ); $(".forum-reply-link, .forum-quote-link").off().click( forum.addReply ); $(".forum-edit-link").off().click( forum.editMessage ); $(".forum-add-file-link").off().click( forum.addFile ); $(".forum-cancel-changes").off().click( forum.cancelChanges ); $( '.forum-new-message-form' ).off().ajaxForm({ dataType: 'json', success: forum.saveNewPost }); $('form.forum-edit-message').off().ajaxForm({ dataType: 'json', success: forum.saveChanges }); } }; forum.attachHandlers(); })(jQuery);