##// END OF EJS Templates
Fixed clearing image after posting.
neko259 -
r682:c9e03aa8 default
parent child Browse files
Show More
@@ -1,260 +1,265 b''
1 1 /*
2 2 @licstart The following is the entire license notice for the
3 3 JavaScript code in this page.
4 4
5 5
6 6 Copyright (C) 2013 neko259
7 7
8 8 The JavaScript code in this page is free software: you can
9 9 redistribute it and/or modify it under the terms of the GNU
10 10 General Public License (GNU GPL) as published by the Free Software
11 11 Foundation, either version 3 of the License, or (at your option)
12 12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15 15
16 16 As additional permission under GNU GPL version 3 section 7, you
17 17 may distribute non-source (e.g., minimized or compacted) forms of
18 18 that code without the copy of the GNU GPL normally required by
19 19 section 4, provided you include this license notice and a URL
20 20 through which recipients can access the Corresponding Source.
21 21
22 22 @licend The above is the entire license notice
23 23 for the JavaScript code in this page.
24 24 */
25 25
26 26 var THREAD_UPDATE_DELAY = 10000;
27 27
28 28 var loading = false;
29 29 var lastUpdateTime = null;
30 30 var unreadPosts = 0
31 31
32 32 function blink(node) {
33 33 var blinkCount = 2;
34 34
35 35 var nodeToAnimate = node;
36 36 for (var i = 0; i < blinkCount; i++) {
37 37 nodeToAnimate = nodeToAnimate.fadeTo('fast', 0.5).fadeTo('fast', 1.0);
38 38 }
39 39 }
40 40
41 41 function updateThread() {
42 42 if (loading) {
43 43 return;
44 44 }
45 45
46 46 loading = true;
47 47
48 48 var threadPosts = $('div.thread').children('.post');
49 49
50 50 var lastPost = threadPosts.last();
51 51 var threadId = threadPosts.first().attr('id');
52 52
53 53 var diffUrl = '/api/diff_thread/' + threadId + '/' + lastUpdateTime + '/';
54 54 $.getJSON(diffUrl)
55 55 .success(function(data) {
56 56 var bottom = isPageBottom();
57 57
58 58 var lastUpdate = '';
59 59
60 60 var addedPosts = data.added;
61 61 for (var i = 0; i < addedPosts.length; i++) {
62 62 var postText = addedPosts[i];
63 63
64 64 var post = $(postText);
65 65
66 66 if (lastUpdate === '') {
67 67 lastUpdate = post.find('.pub_time').text();
68 68 }
69 69
70 70 post.appendTo(lastPost.parent());
71 71 addRefLinkPreview(post[0]);
72 72
73 73 lastPost = post;
74 74 blink(post);
75 75 }
76 76
77 77 var updatedPosts = data.updated;
78 78 for (var i = 0; i < updatedPosts.length; i++) {
79 79 var postText = updatedPosts[i];
80 80
81 81 var post = $(postText);
82 82
83 83 if (lastUpdate === '') {
84 84 lastUpdate = post.find('.pub_time').text();
85 85 }
86 86
87 87 var postId = post.attr('id');
88 88
89 89 var oldPost = $('div.thread').children('.post[id=' + postId + ']');
90 90
91 91 oldPost.replaceWith(post);
92 92 addRefLinkPreview(post[0]);
93 93
94 94 blink(post);
95 95 }
96 96
97 97 // TODO Process deleted posts
98 98
99 99 lastUpdateTime = data.last_update;
100 100 loading = false;
101 101
102 102 if (bottom) {
103 103 var $target = $('html,body');
104 104 $target.animate({scrollTop: $target.height()}, 1000);
105 105 }
106 106
107 107 var hasPostChanges = (updatedPosts.length > 0)
108 108 || (addedPosts.length > 0);
109 109 if (hasPostChanges) {
110 110 updateMetadataPanel(lastUpdate);
111 111 }
112 112
113 113 updateBumplimitProgress(data.added.length);
114 114
115 115 if (data.added.length + data.updated.length > 0) {
116 116 showNewPostsTitle(data.added.length);
117 117 }
118 118 })
119 119 .error(function(data) {
120 120 // TODO Show error message that server is unavailable?
121 121
122 122 loading = false;
123 123 });
124 124 }
125 125
126 126 function isPageBottom() {
127 127 var scroll = $(window).scrollTop() / ($(document).height()
128 128 - $(window).height())
129 129
130 130 return scroll == 1
131 131 }
132 132
133 133 function initAutoupdate() {
134 134 loading = false;
135 135
136 136 lastUpdateTime = $('.metapanel').attr('data-last-update');
137 137
138 138 setInterval(updateThread, THREAD_UPDATE_DELAY);
139 139 }
140 140
141 141 function getReplyCount() {
142 142 return $('.thread').children('.post').length
143 143 }
144 144
145 145 function getImageCount() {
146 146 return $('.thread').find('img').length
147 147 }
148 148
149 149 function updateMetadataPanel(lastUpdate) {
150 150 var replyCountField = $('#reply-count');
151 151 var imageCountField = $('#image-count');
152 152
153 153 replyCountField.text(getReplyCount());
154 154 imageCountField.text(getImageCount());
155 155
156 156 if (lastUpdate !== '') {
157 157 var lastUpdateField = $('#last-update');
158 158 lastUpdateField.text(lastUpdate);
159 159 blink(lastUpdateField);
160 160 }
161 161
162 162 blink(replyCountField);
163 163 blink(imageCountField);
164 164 }
165 165
166 166 /**
167 167 * Update bumplimit progress bar
168 168 */
169 169 function updateBumplimitProgress(postDelta) {
170 170 var progressBar = $('#bumplimit_progress');
171 171 if (progressBar) {
172 172 var postsToLimitElement = $('#left_to_limit');
173 173
174 174 var oldPostsToLimit = parseInt(postsToLimitElement.text());
175 175 var postCount = getReplyCount();
176 176 var bumplimit = postCount - postDelta + oldPostsToLimit;
177 177
178 178 var newPostsToLimit = bumplimit - postCount;
179 179 if (newPostsToLimit <= 0) {
180 180 $('.bar-bg').remove();
181 181 $('.thread').children('.post').addClass('dead_post');
182 182 } else {
183 183 postsToLimitElement.text(newPostsToLimit);
184 184 progressBar.width((100 - postCount / bumplimit * 100.0) + '%');
185 185 }
186 186 }
187 187 }
188 188
189 189 var documentOriginalTitle = '';
190 190 /**
191 191 * Show 'new posts' text in the title if the document is not visible to a user
192 192 */
193 193 function showNewPostsTitle(newPostCount) {
194 194 if (document.hidden) {
195 195 if (documentOriginalTitle === '') {
196 196 documentOriginalTitle = document.title;
197 197 }
198 198 unreadPosts = unreadPosts + newPostCount;
199 199 document.title = '[' + unreadPosts + '] ' + documentOriginalTitle;
200 200
201 201 document.addEventListener('visibilitychange', function() {
202 202 if (documentOriginalTitle !== '') {
203 203 document.title = documentOriginalTitle;
204 204 documentOriginalTitle = '';
205 205 unreadPosts = 0;
206 206 }
207 207
208 208 document.removeEventListener('visibilitychange', null);
209 209 });
210 210 }
211 211 }
212 212
213 213 /**
214 214 * Clear all entered values in the form fields
215 215 */
216 216 function resetForm(form) {
217 217 form.find('input:text, input:password, input:file, select, textarea').val('');
218 218 form.find('input:radio, input:checkbox')
219 219 .removeAttr('checked').removeAttr('selected');
220 $('#file_wrap').find('#file-thumb').remove();
220 $('.file_wrap').find('.file-thumb').remove();
221 221 }
222 222
223
224 $(document).ready(function(){
225 initAutoupdate();
226
227 // Post form data over AJAX
228 var threadId = $('div.thread').children('.post').first().attr('id');;
229
230 var form = $('#form');
231 var options = {
232 success: updateOnPost,
233 url: '/api/add_post/' + threadId + '/',
234 };
235
236 form.ajaxForm(options);
237
238 function updateOnPost(response, statusText, xhr, $form) {
223 /**
224 * When the form is posted, this method will be run as a callback
225 */
226 function updateOnPost(response, statusText, xhr, form) {
239 227 var json = $.parseJSON(response);
240 228 var status = json.status;
241 229
242 230 form.children('.form-errors').remove();
243 231
244 232 if (status === 'ok') {
245 233 resetForm(form);
246 234 updateThread();
247 235 } else {
248 236 var errors = json.errors;
249 237 for (var i = 0; i < errors.length; i++) {
250 238 var fieldErrors = errors[i];
251 239
252 240 var error = fieldErrors.errors;
253 241
254 242 var errorList = $('<div class="form-errors">' + error
255 243 + '<div>');
256 244 errorList.appendTo(form);
257 245 }
258 246 }
259 247 }
248
249 $(document).ready(function(){
250 initAutoupdate();
251
252 // Post form data over AJAX
253 var threadId = $('div.thread').children('.post').first().attr('id');;
254
255 var form = $('#form');
256
257 var options = {
258 success: updateOnPost,
259 url: '/api/add_post/' + threadId + '/'
260 };
261
262 form.ajaxForm(options);
263
264 resetForm(form);
260 265 });
General Comments 0
You need to be logged in to leave comments. Login now