comparison gpp/forums/static/js/forums.js @ 312:88b2b9cb8c1f

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