Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
285:8fd4984d5c3b | 286:72fd300685d5 |
---|---|
1 $(document).ready(function() { | 1 $(document).ready(function() { |
2 var postText = $('#id_body'); | 2 var postText = $('#id_body'); |
3 var postButton = $('#forums-reply-post'); | 3 var postButton = $('#forums-reply-post'); |
4 postButton.click(function () { | 4 postButton.click(function () { |
5 var text = $.trim(postText.val()); | 5 var text = $.trim(postText.val()); |
6 if (text.length == 0) { | |
7 alert('Please enter some text.'); | |
8 return false; | |
9 } | |
10 $(this).attr('disabled', 'disabled').val('Posting reply...'); | 6 $(this).attr('disabled', 'disabled').val('Posting reply...'); |
11 | 7 |
12 var attachments = new Array() | 8 var attachments = new Array() |
13 $('#attachment div input').each(function(index) { | 9 $('#attachment div input').each(function(index) { |
14 attachments[index] = $(this).val(); | 10 attachments[index] = $(this).val(); |
100 { | 96 { |
101 $('#attachment div').remove(); | 97 $('#attachment div').remove(); |
102 $('#attach-another').remove(); | 98 $('#attach-another').remove(); |
103 } | 99 } |
104 | 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="/media/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 | |
105 function initAttachments() | 127 function initAttachments() |
106 { | 128 { |
107 clearAttachments(); | 129 clearAttachments(); |
108 | 130 |
109 var post_input = $('#attachment input'); | 131 var post_input = $('#id_post_id'); |
132 var attachments = $("#forums_post_form input:hidden[name='attachment']"); | |
110 if (post_input.length == 1) | 133 if (post_input.length == 1) |
111 { | 134 { |
112 post_id = post_input.val(); | 135 post_id = post_input.val(); |
113 post_input.replaceWith('<img src="/media/icons/ajax_busy.gif" alt="Busy" />'); | 136 vidDiv.prepend('<img src="/media/icons/ajax_busy.gif" alt="Busy" />'); |
114 $.ajax({ | 137 $.ajax({ |
115 url: '/forums/fetch_attachments/', | 138 url: '/forums/fetch_attachments/', |
116 type: 'GET', | 139 type: 'GET', |
117 data: { | 140 data: { |
118 pid : post_id | 141 pid : post_id |
119 }, | 142 }, |
120 dataType: 'json', | 143 dataType: 'json', |
121 success: function (data, textStatus) { | 144 success: processEmbeds, |
122 $('#attachment img').remove(); | |
123 $.each(data, function(index, value) { | |
124 var html = '<div id="video-' + index + '">' + value.html + | |
125 '<span class="link">' + | |
126 '<img src="/media/icons/television_delete.png" alt="Remove" /> ' + | |
127 '<a href="#">Remove</a></span>' + | |
128 '<input type="hidden" name="attachment" value="' + value.id + '" />'; | |
129 '</div>'; | |
130 vidDiv.append(html); | |
131 $('#video-' + index + ' a').click(function() { | |
132 $('#video-' + index).remove(); | |
133 relabelAttachLink(); | |
134 return false; | |
135 }); | |
136 }); | |
137 vid = data.length; | |
138 $('#video-' + (vid-1)).after('<a id="attach-another" href="#">Attach another video</a>'); | |
139 $('#attach-another').click(function() { | |
140 addVideo(); | |
141 relabelAttachLink(); | |
142 return false; | |
143 }); | |
144 }, | |
145 error: function (xhr, textStatus, ex) { | 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="/media/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(); | |
146 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + | 171 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + |
147 xhr.responseText); | 172 xhr.responseText); |
148 } | 173 } |
149 }); | 174 }); |
150 } | 175 } |
212 }); | 237 }); |
213 | 238 |
214 var vidText = $('#' + id + ' input'); | 239 var vidText = $('#' + id + ' input'); |
215 | 240 |
216 $('#' + id + ' button').click(function() { | 241 $('#' + id + ' button').click(function() { |
242 var button = $(this); | |
243 button.attr('disabled', 'disabled'); | |
217 $.ajax({ | 244 $.ajax({ |
218 url: '/oembed/fetch/', | 245 url: '/oembed/fetch/', |
219 type: 'POST', | 246 type: 'POST', |
220 data: { | 247 data: { |
221 q : vidText.val() | 248 q : vidText.val() |
237 }); | 264 }); |
238 }, | 265 }, |
239 error: function (xhr, textStatus, ex) { | 266 error: function (xhr, textStatus, ex) { |
240 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + | 267 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + |
241 xhr.responseText); | 268 xhr.responseText); |
269 button.removeAttr('disabled'); | |
242 } | 270 } |
243 }); | 271 }); |
244 }); | 272 }); |
245 | 273 |
246 if (vid == 0) | 274 if (vid == 0) |