comparison forums/static/js/forums.js @ 581:ee87ea74d46b

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