annotate gpp/forums/static/js/forums.js @ 334:6805d15cda13

Adding a script I had to write on the fly to filter out posts from the posts csv file that had no parent topics. MyISAM let me get away with that, but InnoDB won't.
author Brian Neal <bgneal@gmail.com>
date Sat, 26 Feb 2011 01:28:22 +0000
parents 88b2b9cb8c1f
children 84bcaff13129
rev   line source
bgneal@312 1 $(document).ready(function() {
bgneal@312 2 var postText = $('#id_body');
bgneal@312 3 var postButton = $('#forums-reply-post');
bgneal@312 4 postButton.click(function () {
bgneal@312 5 var text = $.trim(postText.val());
bgneal@312 6 $(this).attr('disabled', 'disabled').val('Posting reply...');
bgneal@312 7
bgneal@312 8 var attachments = new Array()
bgneal@312 9 $('#attachment div input').each(function(index) {
bgneal@312 10 attachments[index] = $(this).val();
bgneal@312 11 });
bgneal@312 12
bgneal@312 13 $.ajax({
bgneal@312 14 url: '/forums/quick-reply/',
bgneal@312 15 type: 'POST',
bgneal@312 16 data: {
bgneal@312 17 body : postText.val(),
bgneal@312 18 topic_id : $('#id_topic_id').val(),
bgneal@312 19 attachment : attachments
bgneal@312 20 },
bgneal@312 21 traditional: true,
bgneal@312 22 dataType: 'html',
bgneal@312 23 success: function (data, textStatus) {
bgneal@312 24 postText.val('');
bgneal@312 25 $('#forum-topic tr:last').after(data);
bgneal@312 26 var lastTr = $('#forum-topic tr:last');
bgneal@312 27 lastTr.hide();
bgneal@312 28 lastTr.fadeIn(3000);
bgneal@312 29 postButton.removeAttr('disabled').val('Submit Reply');
bgneal@312 30 initAttachments();
bgneal@312 31 },
bgneal@312 32 error: function (xhr, textStatus, ex) {
bgneal@312 33 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312 34 xhr.responseText);
bgneal@312 35 postButton.removeAttr('disabled').val('Submit Reply');
bgneal@312 36 initAttachments();
bgneal@312 37 }
bgneal@312 38 });
bgneal@312 39 return false;
bgneal@312 40 });
bgneal@312 41 $('a.post-flag').click(function () {
bgneal@312 42 var id = this.id;
bgneal@312 43 if (id.match(/fp-(\d+)/)) {
bgneal@312 44 id = RegExp.$1;
bgneal@312 45 if (confirm('Only flag a post if you feel it is spam, abuse, violates site rules, ' +
bgneal@312 46 'or is not appropriate. ' +
bgneal@312 47 'A moderator will be notified and will review the post. ' +
bgneal@312 48 'Are you sure you want to flag this post?')) {
bgneal@312 49 $.ajax({
bgneal@312 50 url: '/forums/flag-post/',
bgneal@312 51 type: 'POST',
bgneal@312 52 data: {id: id},
bgneal@312 53 dataType: 'text',
bgneal@312 54 success: function (response, textStatus) {
bgneal@312 55 alert(response);
bgneal@312 56 },
bgneal@312 57 error: function (xhr, textStatus, ex) {
bgneal@312 58 alert('Oops, an error occurred: ' + xhr.statusText + ' - ' + xhr.responseText);
bgneal@312 59 }
bgneal@312 60 });
bgneal@312 61 }
bgneal@312 62 }
bgneal@312 63 return false;
bgneal@312 64 });
bgneal@312 65 $('a.post-delete').click(function () {
bgneal@312 66 var id = this.id;
bgneal@312 67 if (id.match(/dp-(\d+)/)) {
bgneal@312 68 id = RegExp.$1;
bgneal@312 69 if (confirm('Are you sure you want to delete this post?')) {
bgneal@312 70 $.ajax({
bgneal@312 71 url: '/forums/delete-post/',
bgneal@312 72 type: 'POST',
bgneal@312 73 data: {id: id},
bgneal@312 74 dataType: 'text',
bgneal@312 75 success: function (response, textStatus) {
bgneal@312 76 alert(response);
bgneal@312 77 $('#post-' + id).fadeOut(3000);
bgneal@312 78 },
bgneal@312 79 error: function (xhr, textStatus, ex) {
bgneal@312 80 alert('Oops, an error occurred: ' + xhr.statusText + ' - ' + xhr.responseText);
bgneal@312 81 }
bgneal@312 82 });
bgneal@312 83 }
bgneal@312 84 }
bgneal@312 85 return false;
bgneal@312 86 });
bgneal@312 87 $('#forum-mod-del-topic').click(function () {
bgneal@312 88 return confirm('Are you sure you want to delete this topic?\n' +
bgneal@312 89 'WARNING: all posts will be lost.');
bgneal@312 90 });
bgneal@312 91
bgneal@312 92 var vid = 0;
bgneal@312 93 var vidDiv = $('#attachment');
bgneal@312 94
bgneal@312 95 function clearAttachments()
bgneal@312 96 {
bgneal@312 97 $('#attachment div').remove();
bgneal@312 98 $('#attach-another').remove();
bgneal@312 99 }
bgneal@312 100
bgneal@312 101 function processEmbeds(data, textStatus)
bgneal@312 102 {
bgneal@312 103 vidDiv.find('img').remove();
bgneal@312 104 $.each(data, function(index, value) {
bgneal@312 105 var html = '<div id="video-' + index + '">' + value.html +
bgneal@312 106 '<span class="link">' +
bgneal@312 107 '<img src="/static/icons/television_delete.png" alt="Remove" /> ' +
bgneal@312 108 '<a href="#">Remove</a></span>' +
bgneal@312 109 '<input type="hidden" name="attachment" value="' + value.id + '" />';
bgneal@312 110 '</div>';
bgneal@312 111 vidDiv.append(html);
bgneal@312 112 $('#video-' + index + ' a').click(function() {
bgneal@312 113 $('#video-' + index).remove();
bgneal@312 114 relabelAttachLink();
bgneal@312 115 return false;
bgneal@312 116 });
bgneal@312 117 });
bgneal@312 118 vid = data.length;
bgneal@312 119 $('#video-' + (vid-1)).after('<a id="attach-another" href="#">Attach another video</a>');
bgneal@312 120 $('#attach-another').click(function() {
bgneal@312 121 addVideo();
bgneal@312 122 relabelAttachLink();
bgneal@312 123 return false;
bgneal@312 124 });
bgneal@312 125 }
bgneal@312 126
bgneal@312 127 function initAttachments()
bgneal@312 128 {
bgneal@312 129 clearAttachments();
bgneal@312 130
bgneal@312 131 var post_input = $('#id_post_id');
bgneal@312 132 var attachments = $("#forums_post_form input:hidden[name='attachment']");
bgneal@312 133 if (post_input.length == 1)
bgneal@312 134 {
bgneal@312 135 post_id = post_input.val();
bgneal@312 136 vidDiv.prepend('<img src="/static/icons/ajax_busy.gif" alt="Busy" />');
bgneal@312 137 $.ajax({
bgneal@312 138 url: '/forums/fetch_attachments/',
bgneal@312 139 type: 'GET',
bgneal@312 140 data: {
bgneal@312 141 pid : post_id
bgneal@312 142 },
bgneal@312 143 dataType: 'json',
bgneal@312 144 success: processEmbeds,
bgneal@312 145 error: function (xhr, textStatus, ex) {
bgneal@312 146 vidDiv.find('img').remove();
bgneal@312 147 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312 148 xhr.responseText);
bgneal@312 149 }
bgneal@312 150 });
bgneal@312 151 }
bgneal@312 152 else if (attachments.length > 0)
bgneal@312 153 {
bgneal@312 154 vidDiv.prepend('<img src="/static/icons/ajax_busy.gif" alt="Busy" />');
bgneal@312 155 var embeds = new Array();
bgneal@312 156 attachments.each(function(index) {
bgneal@312 157 embeds[index] = $(this).val();
bgneal@312 158 });
bgneal@312 159 attachments.remove();
bgneal@312 160 $.ajax({
bgneal@312 161 url: '/oembed/fetch_saved/',
bgneal@312 162 type: 'GET',
bgneal@312 163 data: {
bgneal@312 164 embeds: embeds
bgneal@312 165 },
bgneal@312 166 traditional: true,
bgneal@312 167 dataType: 'json',
bgneal@312 168 success: processEmbeds,
bgneal@312 169 error: function (xhr, textStatus, ex) {
bgneal@312 170 vidDiv.find('img').remove();
bgneal@312 171 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312 172 xhr.responseText);
bgneal@312 173 }
bgneal@312 174 });
bgneal@312 175 }
bgneal@312 176 else
bgneal@312 177 {
bgneal@312 178 vid = 0;
bgneal@312 179 var s = '<div id="init-add">' +
bgneal@312 180 '<img src="/static/icons/television_add.png" alt="Add" /> ' +
bgneal@312 181 '<a href="#">Attach Video</a></div>';
bgneal@312 182 vidDiv.prepend(s);
bgneal@312 183 $('#attachment a').click(function () {
bgneal@312 184 $('#init-add').remove();
bgneal@312 185 addVideo();
bgneal@312 186 return false;
bgneal@312 187 });
bgneal@312 188 }
bgneal@312 189 }
bgneal@312 190
bgneal@312 191 function relabelAttachLink()
bgneal@312 192 {
bgneal@312 193 var another = $('#attach-another');
bgneal@312 194 var n = $('#attachment div').length;
bgneal@312 195 if (n == 0)
bgneal@312 196 {
bgneal@312 197 another.html("Attach a video");
bgneal@312 198 }
bgneal@312 199 else
bgneal@312 200 {
bgneal@312 201 another.html("Attach another video");
bgneal@312 202 }
bgneal@312 203 }
bgneal@312 204
bgneal@312 205 function addVideo()
bgneal@312 206 {
bgneal@312 207 var id = "video-" + vid;
bgneal@312 208
bgneal@312 209 var fakeForm = '<div id="' + id + '">' +
bgneal@312 210 '<img src="/static/icons/television_add.png" alt="Attach" class="r" /> ' +
bgneal@312 211 '<input type="text" size="45" class="r" /> <button type="button" class="r">Attach</button> ' +
bgneal@312 212 '<a href="#" class="r">Remove</a><br /></div>';
bgneal@312 213
bgneal@312 214 var n = $('#attachment div').length;
bgneal@312 215
bgneal@312 216 var another = $('#attach-another');
bgneal@312 217 if (n == 0)
bgneal@312 218 {
bgneal@312 219 if (another.length > 0)
bgneal@312 220 {
bgneal@312 221 another.before(fakeForm);
bgneal@312 222 }
bgneal@312 223 else
bgneal@312 224 {
bgneal@312 225 vidDiv.append(fakeForm);
bgneal@312 226 }
bgneal@312 227 }
bgneal@312 228 else
bgneal@312 229 {
bgneal@312 230 $('#attachment div:last').after(fakeForm);
bgneal@312 231 }
bgneal@312 232
bgneal@312 233 $('#' + id + ' a').click(function() {
bgneal@312 234 $('#' + id).remove();
bgneal@312 235 relabelAttachLink();
bgneal@312 236 return false;
bgneal@312 237 });
bgneal@312 238
bgneal@312 239 var vidText = $('#' + id + ' input');
bgneal@312 240
bgneal@312 241 $('#' + id + ' button').click(function() {
bgneal@312 242 var button = $(this);
bgneal@312 243 button.attr('disabled', 'disabled');
bgneal@312 244 $.ajax({
bgneal@312 245 url: '/oembed/fetch/',
bgneal@312 246 type: 'POST',
bgneal@312 247 data: {
bgneal@312 248 q : vidText.val()
bgneal@312 249 },
bgneal@312 250 dataType: 'json',
bgneal@312 251 success: function (data, textStatus) {
bgneal@312 252 $('#' + id + " .r").remove();
bgneal@312 253 var myDiv = $('#' + id);
bgneal@312 254 var html = '<span class="link">' +
bgneal@312 255 '<img src="/static/icons/television_delete.png" alt="Remove" /> ' +
bgneal@312 256 '<a href="#">Remove</a></span>' +
bgneal@312 257 '<input type="hidden" name="attachment" value="' + data.id + '" />';
bgneal@312 258 myDiv.prepend(html);
bgneal@312 259 myDiv.prepend(data.embed);
bgneal@312 260 $('#' + id + ' a').click(function() {
bgneal@312 261 myDiv.remove();
bgneal@312 262 relabelAttachLink();
bgneal@312 263 return false;
bgneal@312 264 });
bgneal@312 265 },
bgneal@312 266 error: function (xhr, textStatus, ex) {
bgneal@312 267 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312 268 xhr.responseText);
bgneal@312 269 button.removeAttr('disabled');
bgneal@312 270 }
bgneal@312 271 });
bgneal@312 272 });
bgneal@312 273
bgneal@312 274 if (vid == 0)
bgneal@312 275 {
bgneal@312 276 $('#video-0').after('<a id="attach-another" href="#">Attach another video</a>');
bgneal@312 277 $('#attach-another').click(function() {
bgneal@312 278 addVideo();
bgneal@312 279 relabelAttachLink();
bgneal@312 280 return false;
bgneal@312 281 });
bgneal@312 282 }
bgneal@312 283 ++vid;
bgneal@312 284 }
bgneal@312 285
bgneal@312 286 initAttachments();
bgneal@312 287 });