annotate forums/static/js/forums.js @ 1001:c6c3ba5cf6eb

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