annotate forums/static/js/forums.js @ 661:15dbe0ccda95

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