##// END OF EJS Templates
Made blink effect on thread update more soft
neko259 -
r481:61c787a5 default
parent child Browse files
Show More
@@ -1,197 +1,196 b''
1 /*
1 /*
2 @licstart The following is the entire license notice for the
2 @licstart The following is the entire license notice for the
3 JavaScript code in this page.
3 JavaScript code in this page.
4
4
5
5
6 Copyright (C) 2013 neko259
6 Copyright (C) 2013 neko259
7
7
8 The JavaScript code in this page is free software: you can
8 The JavaScript code in this page is free software: you can
9 redistribute it and/or modify it under the terms of the GNU
9 redistribute it and/or modify it under the terms of the GNU
10 General Public License (GNU GPL) as published by the Free Software
10 General Public License (GNU GPL) as published by the Free Software
11 Foundation, either version 3 of the License, or (at your option)
11 Foundation, either version 3 of the License, or (at your option)
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 without even the implied warranty of MERCHANTABILITY or FITNESS
13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15
15
16 As additional permission under GNU GPL version 3 section 7, you
16 As additional permission under GNU GPL version 3 section 7, you
17 may distribute non-source (e.g., minimized or compacted) forms of
17 may distribute non-source (e.g., minimized or compacted) forms of
18 that code without the copy of the GNU GPL normally required by
18 that code without the copy of the GNU GPL normally required by
19 section 4, provided you include this license notice and a URL
19 section 4, provided you include this license notice and a URL
20 through which recipients can access the Corresponding Source.
20 through which recipients can access the Corresponding Source.
21
21
22 @licend The above is the entire license notice
22 @licend The above is the entire license notice
23 for the JavaScript code in this page.
23 for the JavaScript code in this page.
24 */
24 */
25
25
26 var THREAD_UPDATE_DELAY = 10000;
26 var THREAD_UPDATE_DELAY = 10000;
27
27
28 var loading = false;
28 var loading = false;
29 var lastUpdateTime = null;
29 var lastUpdateTime = null;
30 var unreadPosts = 0
30 var unreadPosts = 0
31
31
32 function blink(node) {
32 function blink(node) {
33 var blinkCount = 2;
33 var blinkCount = 2;
34 var blinkDelay = 250;
35
34
36 var nodeToAnimate = node;
35 var nodeToAnimate = node;
37 for (var i = 0; i < blinkCount; i++) {
36 for (var i = 0; i < blinkCount; i++) {
38 nodeToAnimate = nodeToAnimate.fadeOut(blinkDelay).fadeIn(blinkDelay);
37 nodeToAnimate = nodeToAnimate.fadeTo('fast', 0.5).fadeTo('fast', 1.0);
39 }
38 }
40 }
39 }
41
40
42 function updateThread() {
41 function updateThread() {
43 if (loading) {
42 if (loading) {
44 return;
43 return;
45 }
44 }
46
45
47 loading = true;
46 loading = true;
48
47
49 var threadPosts = $('div.thread').children('.post');
48 var threadPosts = $('div.thread').children('.post');
50
49
51 var lastPost = threadPosts.last();
50 var lastPost = threadPosts.last();
52 var threadId = threadPosts.first().attr('id');
51 var threadId = threadPosts.first().attr('id');
53
52
54 var diffUrl = '/api/diff_thread/' + threadId + '/' + lastUpdateTime + '/';
53 var diffUrl = '/api/diff_thread/' + threadId + '/' + lastUpdateTime + '/';
55 $.getJSON(diffUrl)
54 $.getJSON(diffUrl)
56 .success(function(data) {
55 .success(function(data) {
57 var bottom = isPageBottom();
56 var bottom = isPageBottom();
58
57
59 var addedPosts = data.added;
58 var addedPosts = data.added;
60 for (var i = 0; i < addedPosts.length; i++) {
59 for (var i = 0; i < addedPosts.length; i++) {
61 var postText = addedPosts[i];
60 var postText = addedPosts[i];
62
61
63 var post = $(postText);
62 var post = $(postText);
64 post.appendTo(lastPost.parent());
63 post.appendTo(lastPost.parent());
65 addRefLinkPreview(post[0]);
64 addRefLinkPreview(post[0]);
66
65
67 lastPost = post;
66 lastPost = post;
68 blink(post);
67 blink(post);
69 }
68 }
70
69
71 var updatedPosts = data.updated;
70 var updatedPosts = data.updated;
72 for (var i = 0; i < updatedPosts.length; i++) {
71 for (var i = 0; i < updatedPosts.length; i++) {
73 var postText = updatedPosts[i];
72 var postText = updatedPosts[i];
74
73
75 var post = $(postText);
74 var post = $(postText);
76 var postId = post.attr('id');
75 var postId = post.attr('id');
77
76
78 var oldPost = $('div.thread').children('.post[id=' + postId + ']');
77 var oldPost = $('div.thread').children('.post[id=' + postId + ']');
79
78
80 oldPost.replaceWith(post);
79 oldPost.replaceWith(post);
81 addRefLinkPreview(post[0]);
80 addRefLinkPreview(post[0]);
82
81
83 blink(post);
82 blink(post);
84 }
83 }
85
84
86 // TODO Process deleted posts
85 // TODO Process deleted posts
87
86
88 lastUpdateTime = data.last_update;
87 lastUpdateTime = data.last_update;
89 loading = false;
88 loading = false;
90
89
91 if (bottom) {
90 if (bottom) {
92 var $target = $('html,body');
91 var $target = $('html,body');
93 $target.animate({scrollTop: $target.height()}, 1000);
92 $target.animate({scrollTop: $target.height()}, 1000);
94 }
93 }
95
94
96 $('#reply-count').text(getReplyCount());
95 $('#reply-count').text(getReplyCount());
97 $('#image-count').text(getImageCount());
96 $('#image-count').text(getImageCount());
98
97
99 updateBumplimitProgress(data.added.length);
98 updateBumplimitProgress(data.added.length);
100 updatePostBumpableStatus();
99 updatePostBumpableStatus();
101
100
102 if (data.added.length + data.updated.length > 0) {
101 if (data.added.length + data.updated.length > 0) {
103 showNewPostsTitle(data.added.length);
102 showNewPostsTitle(data.added.length);
104 }
103 }
105 })
104 })
106 .error(function(data) {
105 .error(function(data) {
107 // TODO Show error message that server is unavailable?
106 // TODO Show error message that server is unavailable?
108
107
109 loading = false;
108 loading = false;
110 });
109 });
111 }
110 }
112
111
113 function isPageBottom() {
112 function isPageBottom() {
114 var scroll = $(window).scrollTop() / ($(document).height()
113 var scroll = $(window).scrollTop() / ($(document).height()
115 - $(window).height())
114 - $(window).height())
116
115
117 return scroll == 1
116 return scroll == 1
118 }
117 }
119
118
120 function initAutoupdate() {
119 function initAutoupdate() {
121 loading = false;
120 loading = false;
122
121
123 lastUpdateTime = $('.metapanel').attr('data-last-update');
122 lastUpdateTime = $('.metapanel').attr('data-last-update');
124
123
125 setInterval(updateThread, THREAD_UPDATE_DELAY);
124 setInterval(updateThread, THREAD_UPDATE_DELAY);
126 }
125 }
127
126
128 function getReplyCount() {
127 function getReplyCount() {
129 return $('.thread').children('.post').length
128 return $('.thread').children('.post').length
130 }
129 }
131
130
132 function getImageCount() {
131 function getImageCount() {
133 return $('.thread').find('img').length
132 return $('.thread').find('img').length
134 }
133 }
135
134
136 /**
135 /**
137 * Update bumplimit progress bar
136 * Update bumplimit progress bar
138 */
137 */
139 function updateBumplimitProgress(postDelta) {
138 function updateBumplimitProgress(postDelta) {
140 var progressBar = $('#bumplimit_progress');
139 var progressBar = $('#bumplimit_progress');
141 if (progressBar) {
140 if (progressBar) {
142 var postsToLimitElement = $('#left_to_limit');
141 var postsToLimitElement = $('#left_to_limit');
143
142
144 var oldPostsToLimit = parseInt(postsToLimitElement.text());
143 var oldPostsToLimit = parseInt(postsToLimitElement.text());
145 var postCount = getReplyCount();
144 var postCount = getReplyCount();
146 var bumplimit = postCount - postDelta + oldPostsToLimit;
145 var bumplimit = postCount - postDelta + oldPostsToLimit;
147
146
148 var newPostsToLimit = bumplimit - postCount;
147 var newPostsToLimit = bumplimit - postCount;
149 if (newPostsToLimit <= 0) {
148 if (newPostsToLimit <= 0) {
150 $('.bar-bg').remove();
149 $('.bar-bg').remove();
151 } else {
150 } else {
152 postsToLimitElement.text(newPostsToLimit);
151 postsToLimitElement.text(newPostsToLimit);
153 progressBar.width((100 - postCount / bumplimit * 100.0) + '%');
152 progressBar.width((100 - postCount / bumplimit * 100.0) + '%');
154 }
153 }
155 }
154 }
156 }
155 }
157
156
158 /**
157 /**
159 * If the bumplimit is reached, add dead_post class to all posts
158 * If the bumplimit is reached, add dead_post class to all posts
160 */
159 */
161 function updatePostBumpableStatus() {
160 function updatePostBumpableStatus() {
162 var postCount = getReplyCount();
161 var postCount = getReplyCount();
163 var postsToLimitElement = $('#left_to_limit');
162 var postsToLimitElement = $('#left_to_limit');
164 var postsToLimit = parseInt(postsToLimitElement.text());
163 var postsToLimit = parseInt(postsToLimitElement.text());
165
164
166 if (postsToLimit <= 0) {
165 if (postsToLimit <= 0) {
167 $('.thread').find('.post').addClass('dead_post');
166 $('.thread').find('.post').addClass('dead_post');
168 }
167 }
169 }
168 }
170
169
171 var documentOriginalTitle = '';
170 var documentOriginalTitle = '';
172 /**
171 /**
173 * Show 'new posts' text in the title if the document is not visible to a user
172 * Show 'new posts' text in the title if the document is not visible to a user
174 */
173 */
175 function showNewPostsTitle(newPostCount) {
174 function showNewPostsTitle(newPostCount) {
176 if (document.hidden) {
175 if (document.hidden) {
177 if (documentOriginalTitle === '') {
176 if (documentOriginalTitle === '') {
178 documentOriginalTitle = document.title;
177 documentOriginalTitle = document.title;
179 }
178 }
180 unreadPosts = unreadPosts + newPostCount;
179 unreadPosts = unreadPosts + newPostCount;
181 document.title = '[' + unreadPosts + '] ' + documentOriginalTitle;
180 document.title = '[' + unreadPosts + '] ' + documentOriginalTitle;
182
181
183 document.addEventListener('visibilitychange', function() {
182 document.addEventListener('visibilitychange', function() {
184 if (documentOriginalTitle !== '') {
183 if (documentOriginalTitle !== '') {
185 document.title = documentOriginalTitle;
184 document.title = documentOriginalTitle;
186 documentOriginalTitle = '';
185 documentOriginalTitle = '';
187 unreadPosts = 0;
186 unreadPosts = 0;
188 }
187 }
189
188
190 document.removeEventListener('visibilitychange', null);
189 document.removeEventListener('visibilitychange', null);
191 });
190 });
192 }
191 }
193 }
192 }
194
193
195 $(document).ready(function(){
194 $(document).ready(function(){
196 initAutoupdate();
195 initAutoupdate();
197 });
196 });
General Comments 0
You need to be logged in to leave comments. Login now