forked from lidiya86/Facebook-for-Adobe-AIR
/
CommentsRenderer.mxml
executable file
·367 lines (302 loc) · 13 KB
/
CommentsRenderer.mxml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<!--
Copyright Facebook Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- This VBox acts as a simple "List" of CommentRenderers
There's exactly one of these in every UFI
Unfortunately this class is fairly complex, because we manage
whether or not we're "revealed" and then we mark our
count on every new data set to determine whether we go
to CommentCache to get more comments. -->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:util="fbair.util.display.*"
xmlns:fb="fb.display.*"
xmlns:gui="fbair.gui.*"
creationComplete="creationComplete(event)" >
<!-- When we first expand we show this while fetching all comments -->
<gui:LoadingIndicator width="100%"
id="loadingIndicator"
visible="false"
text="Loading Comments" />
<!-- Link to show when we have TONS of comments -->
<util:AnimatedCanvas width="100%"
id="allCommentsContainer"
visible="false"
animateOut="true"
animateOnce="true">
<!-- Sentence of how many people have commented on this -->
<util:LinkLabel id="allCommentsText"
styleName="allCommentsText"
text="{'See all ' + data.comments.count + ' comments'}"
url="{data.permalink}" />
</util:AnimatedCanvas>
<!-- This holds the CommentRenderers themselves -->
<util:AnimatedCanvas id="commentsBoxContainer"
width="100%"
animateIn="false"
animateOnce="true" >
<mx:VBox id="commentsBox"
styleName="commentsBox"
width="100%"
removed="rendererRemoved(event)" />
</util:AnimatedCanvas>
<mx:Script><![CDATA[
import fb.FBEvent;
import fb.util.Output;
import fb.util.StringUtil;
import fbair.gc.Depot;
import fbair.nile.renderers.CommentRenderer;
import fbair.nile.renderers.UFI;
import fbair.util.CommentCache;
import fbair.util.HashArray
import mx.binding.utils.BindingUtils;
import mx.binding.utils.ChangeWatcher;
import mx.events.FlexEvent;
// Max number we want to show before we permalink
private static const MaxComments:int = 6;
// Tracks whether we've been revealed and are getting ALL comments
public var revealed:Boolean = false;
// Tracks if this renderer is getting it's very first data or not
private var firstPull:Boolean = true;
// Mutex on whether we're currently requesting comments
private var fetchingComments:Boolean = false;
// We iterate this variable to have the timestamp of
// the latest comments thus far made on this story
// This allows us to only fetch "new" comments each time.
private var latestUpdate:int = 0;
// Watches when our count has changed, to refetch
private var countWatcher:ChangeWatcher;
// Current count
private var commentCount:int = -1;
// comments[0] => Newest
// comments[comments.length-1] = Oldest
// All the CommentRenderers, keyed by id
private var commentsHash:HashArray = new HashArray();
// List of comment-ids we have absolutely deleted
// We hold on to make sure a quick refresh doesn't revive them.
private var zombies:Object = new Object();
// List of comment-ids we just created
// We hold on once to make sure a quick refresh doesn't delete.
private var immunes:Object = new Object();
// On creation, we subscribe to comment remove events
private function creationComplete(event:FlexEvent):void {
addEventListener(CommentCache.COMMENT_REMOVED,
myCommentRemoved);
}
// When we remove old items from the commentsrenderer
// we want to put them in the depot
private function rendererRemoved(event:Event):void {
if (event.target is CommentRenderer)
Depot.put(event.target);
}
// Marks an id as immune to the next deletion round
public function markImmune(commentData:Object):void {
immunes[commentData.id] = true;
}
// Creates new CommentRenderer and adds to display
// commentData => {fromid:int, text:String}
public function addComment(commentData:Object):void {
if (!revealed || commentsHash.hasKey(commentData.id)) return;
// Pass on our can_remove var
commentData.can_remove = data.comments.can_remove;
// Create renderer
var renderer:CommentRenderer = Depot.get(CommentRenderer);
renderer.data = commentData;
renderer.animateIn = !firstPull;
// Determine position of comment based on timestamp
var entry_position:int = 0;
while(entry_position < commentsHash.length &&
Number(commentsHash.getAt(entry_position).data.time) <
Number(renderer.data.time))
entry_position++;
// Add comment renderer to the stage
if (entry_position == commentsHash.length) {
commentsBox.addChild(renderer);
} else {
var commentIndex:int = commentsBox.getChildIndex(
commentsHash.getAt(entry_position));
commentsBox.addChildAt(renderer, commentIndex);
}
// remember this comment
commentsHash.insertAt(entry_position, commentData.id, renderer);
// show the comment box since there certainly are comments
commentsBoxContainer.visible = true;
}
// Called when we're revealing the commentsrenderer
public function reveal():void {
if (revealed) return;
revealed = true;
// Ok we need to check how many stories we have, and if we need more
if (data.comments.count <= 3) {
loadingIndicator.visible = false;
commentsBoxContainer.animateIn = false;
populateComments(data.comments.comment_list);
} else {
commentsBoxContainer.visible = false;
commentsBoxContainer.animateIn = true;
// If data is already in the cache, then just show it
if (CommentCache.hasFreshComments(data.post_id, data.comments.count))
commentsFetched(new FBEvent(CommentCache.COMMENTS_FETCHED,
CommentCache.commentCache[data.post_id]));
// Fetch data, and show loader
else {
loadingIndicator.immediateVisible = true;
commentsBoxContainer.addEventListener(AnimatedCanvas.TWEEN_COMPLETE,
hideLoadingIndicator);
fetchComments();
}
}
// Now lets bind to the count to see when to refetch
commentCount = data.comments.count;
if (countWatcher) countWatcher.unwatch();
countWatcher = BindingUtils.bindSetter(updateCommentCount,
data, ["comments", "count"]);
}
private function updateCommentCount(new_count:int):void {
// If we have more than max, then display link
allCommentsContainer.visible = (new_count > MaxComments);
// If we didn't really change our number, then screw it
// If we're doing stuff RIGHT NOW, then nevermind, also
if (commentCount == new_count || fetchingComments) return;
commentCount = new_count;
// If count is 0, then screw it
if (new_count <= 0) return;
// If <= 3, then we already own it
if (new_count <= 3) populateComments(data.comments.comment_list);
else {
// Perhaps they were already fetched on another filter tab
if (CommentCache.hasFreshComments(data.post_id, new_count))
commentsFetched(new FBEvent(CommentCache.COMMENTS_FETCHED,
CommentCache.commentCache[data.post_id]));
// Fetch more!
else
fetchComments();
}
}
// Called when a comment has been deleted by the user inside ourselves
private function myCommentRemoved(event:FBEvent):void {
var commentData:Object = event.target.data;
commentData.post_id = data.post_id;
CommentCache.removeComment(commentData);
}
// Called when we want to get some comments, yo
private function fetchComments():void {
if (!revealed || fetchingComments) return;
fetchingComments = true;
CommentCache.getComments(data.post_id, latestUpdate)
.addEventListener(CommentCache.COMMENTS_FETCHED, commentsFetched);
}
// Comments incoming from fql!
private function commentsFetched(event:FBEvent):void {
fetchingComments = false;
if (!revealed) return;
populateComments(event.data);
}
// Populate our comments with given array
private function populateComments(newComments:*):void {
// If comments isn't an Array, then there are no comments
if (!(newComments is Array)) newComments = new Array();
var commentsArray:Array = newComments.slice(0, MaxComments);
// Some more comments may have happened during this period of fetching
// so update our data.comments.count
commentCount = Math.max(data.comments.count, commentsArray.length);
data.comments.count = commentCount;
// If we have any zombies, they need to be counted out
removeZombies(commentsArray);
// Add our comments
for each (var newComment:Object in commentsArray)
addComment(newComment);
// Remove extraneous comments
removeAllCommentsExcept(commentsArray);
// Remove overflow, if any.
removeExcessComments();
// Mark latest update
markLatestUpdate();
// Not the first anymore
firstPull = false;
}
// We remove any stories above our max here
public function removeExcessComments():void {
while (commentsHash.length > MaxComments)
removeCommentAtIndex(0);
}
// Mark latest update
public function markLatestUpdate():void {
latestUpdate = (commentsHash.length == 0 ? 0 :
commentsHash.getAt(0).data.time);
}
// We call this to narrow our comment view down to the group
// provided by server. A way to find deletes. basically.
private function removeAllCommentsExcept(theseComments:Array):void {
// Order posts by id to easily discover any deletions
var postsByID:Object = new Object();
for each (var newComment:Object in theseComments)
postsByID[newComment.id] = newComment;
// Now if we have any comments that don't exist
// in this update, we're gonna remove them
for (var i:int = 0; i < commentsHash.length; i++)
if (!postsByID[commentsHash.getAt(i).data.id]) {
if (immunes[commentsHash.getAt(i).data.id])
immunes[commentsHash.getAt(i).data.id] = false;
else
removeCommentAtIndex(i--);
}
}
// Remove a comment deleted locally
public function removeLocalComment(comment_id:String):void {
Output.assert(commentsHash.hasKey(comment_id),
"Removing comment: " + comment_id + " that isn't in: " + this);
// Remove by index
removeCommentAtIndex(commentsHash.indexAtKey(comment_id), true);
// Decrement counts manually
commentCount = data.comments.count - 1;
data.comments.count = commentCount;
}
// Function to remove a comment, by index
public function removeCommentAtIndex(i:int, zombie:Boolean = false):void {
var deadComment:CommentRenderer = commentsHash.removeIndex(i)[0];
if (zombie) zombies[deadComment.data.id] = true;
deadComment.remove();
}
// We call this to remove any zombies from an array
private function removeZombies(newComments:Array):int {
var removed:int = 0;
for (var i:int = 0; i < newComments.length; i++)
if (zombies[newComments[i].id]) {
newComments.splice(i--, 1);
removed++;
}
return removed;
}
// Hide loading indicator when done revealing
private function hideLoadingIndicator(event:Event):void {
commentsBoxContainer.removeEventListener(AnimatedCanvas.TWEEN_COMPLETE,
hideLoadingIndicator);
loadingIndicator.visible = false;
}
// Clean up our renderers and prepare us for reuse
public function destroy():void {
revealed = false;
firstPull = true;
latestUpdate = 0;
commentCount = -1;
for (var i:int = 0; i < commentsHash.length; i++)
removeCommentAtIndex(i--);
if (countWatcher) countWatcher.unwatch();
commentsHash = new HashArray();
zombies = new Object();
immunes = new Object();
// Put this back in the non revealed state
commentsBoxContainer.visible = false;
commentsBoxContainer.animateIn = false;
}
]]></mx:Script>
</mx:VBox>