##// 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 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 var blinkDelay = 250;
35 34
36 35 var nodeToAnimate = node;
37 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 41 function updateThread() {
43 42 if (loading) {
44 43 return;
45 44 }
46 45
47 46 loading = true;
48 47
49 48 var threadPosts = $('div.thread').children('.post');
50 49
51 50 var lastPost = threadPosts.last();
52 51 var threadId = threadPosts.first().attr('id');
53 52
54 53 var diffUrl = '/api/diff_thread/' + threadId + '/' + lastUpdateTime + '/';
55 54 $.getJSON(diffUrl)
56 55 .success(function(data) {
57 56 var bottom = isPageBottom();
58 57
59 58 var addedPosts = data.added;
60 59 for (var i = 0; i < addedPosts.length; i++) {
61 60 var postText = addedPosts[i];
62 61
63 62 var post = $(postText);
64 63 post.appendTo(lastPost.parent());
65 64 addRefLinkPreview(post[0]);
66 65
67 66 lastPost = post;
68 67 blink(post);
69 68 }
70 69
71 70 var updatedPosts = data.updated;
72 71 for (var i = 0; i < updatedPosts.length; i++) {
73 72 var postText = updatedPosts[i];
74 73
75 74 var post = $(postText);
76 75 var postId = post.attr('id');
77 76
78 77 var oldPost = $('div.thread').children('.post[id=' + postId + ']');
79 78
80 79 oldPost.replaceWith(post);
81 80 addRefLinkPreview(post[0]);
82 81
83 82 blink(post);
84 83 }
85 84
86 85 // TODO Process deleted posts
87 86
88 87 lastUpdateTime = data.last_update;
89 88 loading = false;
90 89
91 90 if (bottom) {
92 91 var $target = $('html,body');
93 92 $target.animate({scrollTop: $target.height()}, 1000);
94 93 }
95 94
96 95 $('#reply-count').text(getReplyCount());
97 96 $('#image-count').text(getImageCount());
98 97
99 98 updateBumplimitProgress(data.added.length);
100 99 updatePostBumpableStatus();
101 100
102 101 if (data.added.length + data.updated.length > 0) {
103 102 showNewPostsTitle(data.added.length);
104 103 }
105 104 })
106 105 .error(function(data) {
107 106 // TODO Show error message that server is unavailable?
108 107
109 108 loading = false;
110 109 });
111 110 }
112 111
113 112 function isPageBottom() {
114 113 var scroll = $(window).scrollTop() / ($(document).height()
115 114 - $(window).height())
116 115
117 116 return scroll == 1
118 117 }
119 118
120 119 function initAutoupdate() {
121 120 loading = false;
122 121
123 122 lastUpdateTime = $('.metapanel').attr('data-last-update');
124 123
125 124 setInterval(updateThread, THREAD_UPDATE_DELAY);
126 125 }
127 126
128 127 function getReplyCount() {
129 128 return $('.thread').children('.post').length
130 129 }
131 130
132 131 function getImageCount() {
133 132 return $('.thread').find('img').length
134 133 }
135 134
136 135 /**
137 136 * Update bumplimit progress bar
138 137 */
139 138 function updateBumplimitProgress(postDelta) {
140 139 var progressBar = $('#bumplimit_progress');
141 140 if (progressBar) {
142 141 var postsToLimitElement = $('#left_to_limit');
143 142
144 143 var oldPostsToLimit = parseInt(postsToLimitElement.text());
145 144 var postCount = getReplyCount();
146 145 var bumplimit = postCount - postDelta + oldPostsToLimit;
147 146
148 147 var newPostsToLimit = bumplimit - postCount;
149 148 if (newPostsToLimit <= 0) {
150 149 $('.bar-bg').remove();
151 150 } else {
152 151 postsToLimitElement.text(newPostsToLimit);
153 152 progressBar.width((100 - postCount / bumplimit * 100.0) + '%');
154 153 }
155 154 }
156 155 }
157 156
158 157 /**
159 158 * If the bumplimit is reached, add dead_post class to all posts
160 159 */
161 160 function updatePostBumpableStatus() {
162 161 var postCount = getReplyCount();
163 162 var postsToLimitElement = $('#left_to_limit');
164 163 var postsToLimit = parseInt(postsToLimitElement.text());
165 164
166 165 if (postsToLimit <= 0) {
167 166 $('.thread').find('.post').addClass('dead_post');
168 167 }
169 168 }
170 169
171 170 var documentOriginalTitle = '';
172 171 /**
173 172 * Show 'new posts' text in the title if the document is not visible to a user
174 173 */
175 174 function showNewPostsTitle(newPostCount) {
176 175 if (document.hidden) {
177 176 if (documentOriginalTitle === '') {
178 177 documentOriginalTitle = document.title;
179 178 }
180 179 unreadPosts = unreadPosts + newPostCount;
181 180 document.title = '[' + unreadPosts + '] ' + documentOriginalTitle;
182 181
183 182 document.addEventListener('visibilitychange', function() {
184 183 if (documentOriginalTitle !== '') {
185 184 document.title = documentOriginalTitle;
186 185 documentOriginalTitle = '';
187 186 unreadPosts = 0;
188 187 }
189 188
190 189 document.removeEventListener('visibilitychange', null);
191 190 });
192 191 }
193 192 }
194 193
195 194 $(document).ready(function(){
196 195 initAutoupdate();
197 196 });
General Comments 0
You need to be logged in to leave comments. Login now