( 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( '