| author | gibus |
| Tue, 16 Jul 2013 14:29:46 +0200 | |
| changeset 525 | 89ef5ed3c48b |
| parent 341 | 053551f213fb |
| permissions | -rw-r--r-- |
| 0 | 1 |
// gConf |
2 |
IComments = function() { |
|
| 341 | 3 |
// this class manages Comments interface (fetched comments <--> IComment.commentId != null) |
| 0 | 4 |
|
| 341 | 5 |
this._c = [] ; // IComments instances // commentId == null means is connected to no comment anymore |
| 0 | 6 |
|
| 341 | 7 |
this._a = [] ; // IComments animations to run |
| 0 | 8 |
|
| 341 | 9 |
this._nbEndedAnim = 0 ; |
| 0 | 10 |
|
| 341 | 11 |
this._topActiveCommentDbId = null ; // active |
| 0 | 12 |
} |
13 |
||
14 |
IComments.prototype = { |
|
| 341 | 15 |
init : function (container) { |
16 |
for (var i = 0 ; i < gConf['iCommentsInitAlloc']; i++) { |
|
17 |
this._c.push(new IComment()) ; |
|
18 |
} |
|
19 |
}, |
|
| 0 | 20 |
|
| 341 | 21 |
getIComment : function (commentId) { |
22 |
return CY.Array.find(this._c, function(iComment) { return (iComment.isfetched() && iComment.commentId == commentId) ;}) ; |
|
23 |
}, |
|
24 |
|
|
25 |
insertAfter : function (previousComment, comment) { |
|
26 |
var _cids = CY.Array.map(this._c, function(iComment) { return iComment.commentId ; }) ; |
|
27 |
var index = CY.Array.indexOf(_cids, previousComment.id) ; |
|
28 |
if (index != -1) { |
|
29 |
this._c.splice(index + 1, 0, new IComment()) ; // will append when index + 1 == array length |
|
30 |
this._c[index + 1].fetch(comment) ; |
|
31 |
return this._c[index + 1] ; |
|
32 |
} |
|
33 |
return null ; |
|
34 |
}, |
|
35 |
|
|
36 |
_remove : function (iComments) { |
|
37 |
var toRemoveIds = CY.Array.map(iComments, function(comment) { return comment.commentId ; }) ; |
|
38 |
|
|
39 |
for (var i = 0 ; i < this._c.length ; i++) { // starting at the bottom to be sure that last remove will be iComment |
|
40 |
var iComment2 = this._c[i] ; |
|
41 |
if (iComment2.isfetched() && CY.Array.indexOf(toRemoveIds, iComment2.commentId) != -1) { |
|
42 |
|
|
43 |
iComment2.unfetch() ; |
|
| 0 | 44 |
|
| 341 | 45 |
this._c.push(this._c.splice(i, 1)[0]) ; |
46 |
|
|
47 |
i-- ; |
|
48 |
} |
|
49 |
} |
|
50 |
}, |
|
51 |
|
|
52 |
// all children, comment's IComment included ! |
|
53 |
// model based (cf. gDb) |
|
54 |
_getChildren : function (commentId) { |
|
55 |
return CY.Array.filter(this._c, function(iComment) { return (iComment.isfetched() && gDb.isChild(iComment.commentId,commentId)) ; }) ; |
|
56 |
}, |
|
| 0 | 57 |
|
| 341 | 58 |
_getInvisibleChildren : function (commentId) { |
59 |
return CY.Array.filter(this._getChildren(commentId), function(iComment) { return (!iComment.isVisible()) ; }) ; |
|
60 |
}, |
|
| 0 | 61 |
|
| 341 | 62 |
// REFRESH (readreplies link etc ?..) |
63 |
refresh : function (commentId) { |
|
64 |
|
|
65 |
var iComment = this.getIComment(commentId) ; |
|
| 0 | 66 |
|
| 341 | 67 |
var invisibleChildrenIComments = this._getInvisibleChildren(commentId) ; |
68 |
if (invisibleChildrenIComments.length > 0) //parentIComment is supposed to be visible |
|
69 |
iComment.showReadRepliesLnk() ; |
|
70 |
else |
|
71 |
iComment.hideReadRepliesLnk() ; |
|
72 |
}, |
|
| 0 | 73 |
|
| 341 | 74 |
remove : function (commentId) { |
75 |
this._remove(this._getChildren(commentId)) ; |
|
76 |
}, |
|
| 0 | 77 |
|
| 341 | 78 |
close : function (commentId) { |
79 |
CY.Array.each(this._getChildren(commentId), function (iComment) { iComment.hide() ; }) ; |
|
80 |
}, |
|
| 0 | 81 |
|
| 341 | 82 |
open : function (commentId) { |
83 |
CY.Array.each(this._getChildren(commentId), function (iComment) { iComment.show() ; }) ; |
|
84 |
}, |
|
| 0 | 85 |
|
| 341 | 86 |
fetch : function (comments) { |
87 |
// fill |
|
88 |
for (var i = 0 ; i < comments.length; i++) { |
|
89 |
if (i == this._c.length) |
|
90 |
this._c.push(new IComment()) ; |
|
91 |
|
|
92 |
this._c[i].fetch(comments[i]) ; |
|
93 |
} |
|
94 |
|
|
95 |
// nullify others |
|
96 |
for (var i = comments.length ; i < this._c.length ; i++) { |
|
97 |
this._c[i].unfetch() ; |
|
98 |
} |
|
99 |
}, |
|
100 |
|
|
101 |
setPosition : function (xy) { |
|
102 |
CY.each(this._c, function (iComment) { iComment.setPosition(xy) ; }) ; |
|
103 |
}, |
|
| 0 | 104 |
|
| 341 | 105 |
show : function () { |
106 |
CY.each(this._c, function (iComment) { |
|
107 |
if (iComment.isfetched()) { |
|
108 |
iComment.show(); |
|
109 |
}}) ; |
|
110 |
}, |
|
111 |
|
|
112 |
hide : function () { |
|
113 |
this.deactivate(); // to prevent a chain of activate / deactivate while hiding IComments one by one |
|
114 |
CY.each(this._c, function (iComment) { if (iComment.commentId != null) iComment.hide(); }) ; |
|
115 |
}, |
|
116 |
|
|
117 |
setWidth : function (colWidth) { |
|
118 |
var nextY = null ; |
|
119 |
for (var i = 0 ; i < this._c.length; i++) { |
|
120 |
var iComment = this._c[i] ; |
|
121 |
iComment.setWidth(colWidth) ; |
|
122 |
|
|
123 |
if (iComment.commentId != null && iComment.isVisible()) { |
|
124 |
var xy = iComment.getPosition() ; |
|
125 |
if (nextY == null) |
|
126 |
nextY = xy[1] ; |
|
127 |
xy[1] = nextY ; |
|
128 |
iComment.setPosition(xy) ; |
|
129 |
nextY += iComment.getHeight() ; |
|
130 |
} |
|
131 |
} |
|
132 |
}, |
|
| 0 | 133 |
|
| 341 | 134 |
getTopPosition:function() { |
135 |
for (var i = 0 ; i < this._c.length; i++) { |
|
136 |
var iComment = this._c[i] ; |
|
137 |
if (iComment.commentId != null && iComment.isVisible()) |
|
138 |
return iComment.getPosition() ; |
|
139 |
} |
|
140 |
return null |
|
141 |
}, |
|
| 0 | 142 |
|
| 341 | 143 |
getPosition:function(id) { |
144 |
for (var i = 0 ; i < this._c.length; i++) { |
|
145 |
var iComment = this._c[i] ; |
|
146 |
if (iComment.commentId == id && iComment.isVisible()) |
|
147 |
return iComment.getPosition() ; |
|
148 |
} |
|
149 |
return null ; |
|
150 |
}, |
|
| 0 | 151 |
|
| 341 | 152 |
setAnimationToPositions : function (y) { |
153 |
this._initAnimations(); |
|
154 |
//CY.log(gPrefs.get('comments','threadpad')) ; |
|
155 |
var lpad = (gPrefs.get('comments','threadpad') == '1') ? gConf['iCommentThreadPadding'] : 0 ; // gIThreadPad ... TODO 'configurize' |
|
| 0 | 156 |
|
| 341 | 157 |
var nextY = y ; |
158 |
for (var i = 0 ; i < this._c.length;i++) { |
|
159 |
var iComment = this._c[i] ; |
|
160 |
if (iComment.isfetched && iComment.isVisible()) { |
|
161 |
var comment_path = gDb.getPath(gDb.getComment(iComment.commentId)) ; |
|
162 |
var iCommentX = ((comment_path.length - 1) * lpad) + gConf['iCommentLeftPadding'] ; |
|
| 0 | 163 |
|
| 341 | 164 |
if (nextY == null) { |
165 |
var xy = iComment.getPosition() ; |
|
166 |
nextY = xy[1] ; |
|
167 |
} |
|
168 |
|
|
169 |
this._a.push(iComment.setAnimationToPosition([iCommentX, nextY])) ; |
|
170 |
nextY += iComment.getHeight() ; |
|
171 |
} |
|
172 |
} |
|
173 |
}, |
|
174 |
|
|
175 |
setAnimationToPositionsAndFocus : function (y, focusCommentId, reply) { |
|
176 |
this._initAnimations(); |
|
177 |
var lpad = (gPrefs.get('comments','threadpad') == '1') ? gConf['iCommentThreadPadding'] : 0 ; |
|
|
329
00df963f91fb
When comment_id_key is passed as URL parameter, focus on this comment, instead of the top comment of the related thread.
gibus
parents:
217
diff
changeset
|
178 |
|
| 341 | 179 |
var nextY = y ; |
180 |
for (var i = 0 ; i < this._c.length;i++) { |
|
181 |
var iComment = this._c[i] ; |
|
182 |
if (iComment.isfetched && iComment.isVisible()) { |
|
183 |
var comment_path = gDb.getPath(gDb.getComment(iComment.commentId)) ; |
|
184 |
var iCommentX = ((comment_path.length - 1) * lpad) + gConf['iCommentLeftPadding'] ; |
|
|
329
00df963f91fb
When comment_id_key is passed as URL parameter, focus on this comment, instead of the top comment of the related thread.
gibus
parents:
217
diff
changeset
|
185 |
|
| 341 | 186 |
if (nextY == null) { |
187 |
var xy = iComment.getPosition() ; |
|
188 |
nextY = xy[1] ; |
|
189 |
} |
|
190 |
|
|
|
331
7ddb9fa9c3f2
focus on comment specified as url parameter *even if it is not the last comment of a thread*
gibus
parents:
329
diff
changeset
|
191 |
if (iComment.commentId >= focusCommentId) |
| 341 | 192 |
this._a.push(iComment.setAnimationToPosition([iCommentX, nextY], focusCommentId, reply)) ; |
|
329
00df963f91fb
When comment_id_key is passed as URL parameter, focus on this comment, instead of the top comment of the related thread.
gibus
parents:
217
diff
changeset
|
193 |
else |
| 341 | 194 |
this._a.push(iComment.setAnimationToPosition([iCommentX, nextY])) ; |
195 |
nextY += iComment.getHeight() ; |
|
196 |
} |
|
197 |
} |
|
198 |
}, |
|
199 |
|
|
200 |
// ANIMATION FUNCTIONS |
|
201 |
_initAnimations : function () { |
|
202 |
this._a = [] ; |
|
203 |
this._nbEndedAnim = 0 ; |
|
204 |
}, |
|
205 |
|
|
206 |
runAnimations : function () { |
|
207 |
if (this._a.length == 0) // will occur when closing last displayed comment |
|
208 |
gSync.resetAutoContinue("animationRun") ; |
|
209 |
else |
|
210 |
CY.each(this._a, function (animation) { animation.run() ; }) ; |
|
211 |
}, |
|
212 |
|
|
213 |
whenAnimationsEnd : function () { |
|
214 |
gSync.resume() ; |
|
215 |
}, |
|
216 |
|
|
217 |
whenAnimationsEndFocus : function () { |
|
|
329
00df963f91fb
When comment_id_key is passed as URL parameter, focus on this comment, instead of the top comment of the related thread.
gibus
parents:
217
diff
changeset
|
218 |
gGETValues = CY.JSON.parse(sv_get_params); |
|
00df963f91fb
When comment_id_key is passed as URL parameter, focus on this comment, instead of the top comment of the related thread.
gibus
parents:
217
diff
changeset
|
219 |
if ("comment_id_key" in gGETValues) { |
|
00df963f91fb
When comment_id_key is passed as URL parameter, focus on this comment, instead of the top comment of the related thread.
gibus
parents:
217
diff
changeset
|
220 |
var id_key = gGETValues["comment_id_key"]; |
|
00df963f91fb
When comment_id_key is passed as URL parameter, focus on this comment, instead of the top comment of the related thread.
gibus
parents:
217
diff
changeset
|
221 |
var focusComment = gDb.getCommentByIdKey(id_key); |
|
00df963f91fb
When comment_id_key is passed as URL parameter, focus on this comment, instead of the top comment of the related thread.
gibus
parents:
217
diff
changeset
|
222 |
if (focusComment != null) |
|
00df963f91fb
When comment_id_key is passed as URL parameter, focus on this comment, instead of the top comment of the related thread.
gibus
parents:
217
diff
changeset
|
223 |
gIComments.getIComment(focusComment.id).overlay.focus(); |
|
00df963f91fb
When comment_id_key is passed as URL parameter, focus on this comment, instead of the top comment of the related thread.
gibus
parents:
217
diff
changeset
|
224 |
} |
| 341 | 225 |
gSync.resume(); |
226 |
}, |
|
227 |
|
|
228 |
whenAnimationsEndReply : function () { |
|
|
337
614669e0e313
Add url parameter (comment_op=reply) to open automatically reply to comment form
gibus
parents:
331
diff
changeset
|
229 |
gGETValues = CY.JSON.parse(sv_get_params); |
|
614669e0e313
Add url parameter (comment_op=reply) to open automatically reply to comment form
gibus
parents:
331
diff
changeset
|
230 |
if ("comment_id_key" in gGETValues) { |
|
614669e0e313
Add url parameter (comment_op=reply) to open automatically reply to comment form
gibus
parents:
331
diff
changeset
|
231 |
var id_key = gGETValues["comment_id_key"]; |
|
614669e0e313
Add url parameter (comment_op=reply) to open automatically reply to comment form
gibus
parents:
331
diff
changeset
|
232 |
var focusComment = gDb.getCommentByIdKey(id_key); |
|
614669e0e313
Add url parameter (comment_op=reply) to open automatically reply to comment form
gibus
parents:
331
diff
changeset
|
233 |
if (focusComment != null) { |
|
614669e0e313
Add url parameter (comment_op=reply) to open automatically reply to comment form
gibus
parents:
331
diff
changeset
|
234 |
gSync.showReplyForm(gIComments.getIComment(focusComment.id)); |
|
614669e0e313
Add url parameter (comment_op=reply) to open automatically reply to comment form
gibus
parents:
331
diff
changeset
|
235 |
} |
|
614669e0e313
Add url parameter (comment_op=reply) to open automatically reply to comment form
gibus
parents:
331
diff
changeset
|
236 |
} |
| 341 | 237 |
gSync.resume(); |
238 |
}, |
|
239 |
|
|
240 |
animationsEnded : function () { |
|
241 |
return ((this._a.length == 0) || (this._a.length == this._nbEndedAnim)) ; |
|
242 |
}, |
|
243 |
|
|
244 |
signalAnimationEnd : function () { |
|
245 |
this._nbEndedAnim++ ; |
|
246 |
}, |
|
247 |
|
|
248 |
// ACTIVE RELATED FUNCTIONS |
|
249 |
// returns true only for the top iComment |
|
250 |
isTopActive : function(commentDbId) { |
|
251 |
return ((commentDbId != null) && (this._topActiveCommentDbId == commentDbId)) ; |
|
252 |
}, |
|
253 |
|
|
254 |
isAnyActive : function() { |
|
255 |
return (this._topActiveCommentDbId != null) ; |
|
256 |
}, |
|
257 |
|
|
258 |
//warning : calling this function "focus" would make IE get mad |
|
259 |
activate : function(commentDbId) { |
|
260 |
|
|
261 |
if (this._topActiveCommentDbId != null) {// then deactivate current |
|
262 |
this.deactivate() ; |
|
263 |
} |
|
| 0 | 264 |
|
| 341 | 265 |
// activate whole top parent thread |
266 |
var comment = gDb.getComment(commentDbId) ; |
|
267 |
var comment_path = gDb.getPath(comment) ; |
|
268 |
var topParent = comment_path[comment_path.length - 1] ; |
|
269 |
|
|
270 |
var iComments = this._getChildren(topParent.id) ; |
|
271 |
CY.Array.each(iComments, function(iComment){iComment.activate();}) ; |
|
| 0 | 272 |
|
| 341 | 273 |
this._topActiveCommentDbId = topParent.id ; |
| 0 | 274 |
|
| 341 | 275 |
// update browser index |
276 |
if (gLayout.isInFrame()) { |
|
277 |
var indxDict = gDb.browsingIndex(this._topActiveCommentDbId) ; |
|
278 |
parent.$("#browse_by option").each(function() { |
|
279 |
var rank = 1 + indxDict[this.value] ; |
|
280 |
parent.$("#c_browse_indx_"+this.value).html(''+ rank) ; |
|
281 |
}) ; |
|
282 |
} |
|
283 |
|
|
284 |
showScope(topParent.id) ; |
|
285 |
}, |
|
286 |
|
|
287 |
deactivate : function() { |
|
288 |
if (this._topActiveCommentDbId != null) { |
|
289 |
parent.$("#browse_by option").each(function() { |
|
290 |
parent.$("#c_browse_indx_"+this.value).html('-') ; |
|
291 |
}) ; |
|
| 0 | 292 |
|
| 341 | 293 |
// scopes |
294 |
hideScopeAnyway() ; |
|
295 |
|
|
296 |
var iComments = this._getChildren(this._topActiveCommentDbId) ; |
|
297 |
CY.Array.each(iComments, function(iComment){iComment.deactivate();}) ; |
|
| 0 | 298 |
|
| 341 | 299 |
this._topActiveCommentDbId = null ; |
300 |
} |
|
301 |
}, |
|
| 0 | 302 |
|
| 341 | 303 |
// active next IComment among the displayed ones (try below and if couldn't try above) |
304 |
// model based |
|
305 |
activateVisibleNext : function() { |
|
306 |
|
|
307 |
if (this._topActiveCommentDbId != null) { |
|
| 0 | 308 |
|
| 341 | 309 |
for (var j = 0 ; j < 2 ; j++) { |
310 |
|
|
311 |
var start = (j==0) ? 0 : this._c.length - 1 ; |
|
312 |
|
|
313 |
var b = false ; |
|
314 |
|
|
315 |
for (var i = start ; (i >= 0) && i <= (this._c.length - 1) ;) { |
|
316 |
var iComment = this._c[i] ; |
|
317 |
|
|
318 |
if (iComment.commentId != null && iComment.isVisible()) { |
|
319 |
b = b || (gDb.isChild(iComment.commentId,this._topActiveCommentDbId)) ; |
|
320 |
if (b && (!gDb.isChild(iComment.commentId,this._topActiveCommentDbId))) { // found the one below that doesn't have topActive as parent |
|
321 |
this.activate(iComment.commentId) ; |
|
322 |
return true ; |
|
323 |
} |
|
324 |
} |
|
325 |
i = (j == 0) ? i + 1 : i - 1 ; |
|
326 |
} |
|
327 |
} |
|
328 |
} |
|
329 |
return false ; |
|
330 |
}, |
|
331 |
|
|
332 |
browse : function(order, whereto) { |
|
333 |
var wt = whereto ; |
|
334 |
if ((whereto == "prev") && !this.isAnyActive()) { |
|
335 |
wt = "last" ; |
|
336 |
} |
|
337 |
if ((whereto == "next") && !this.isAnyActive()) { |
|
338 |
wt = "first" ; |
|
339 |
} |
|
340 |
return gDb.browse(order, wt, this._topActiveCommentDbId) ; |
|
341 |
} |
|
342 |
|
|
| 0 | 343 |
} |