annotate media/js/forums.js @ 286:72fd300685d5

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