src/cm/media/js/client/c_icomments.js
changeset 0 40c8f766c9b8
child 111 76a68d59ee3d
equal deleted inserted replaced
-1:000000000000 0:40c8f766c9b8
       
     1 // gConf
       
     2 IComments = function() {
       
     3 	// this class manages Comments interface (fetched comments <--> IComment.commentId != null)
       
     4 
       
     5 	this._c = [] ; // IComments instances   // commentId == null means is connected to no comment anymore
       
     6 
       
     7 	this._a = [] ; // IComments animations to run
       
     8 
       
     9 	this._nbEndedAnim = 0 ;
       
    10 
       
    11 	this._topActiveCommentDbId = null ; // active 
       
    12 }
       
    13 
       
    14 IComments.prototype = {
       
    15 		init : function (container) {
       
    16 			for (var i = 0 ; i < gConf['iCommentsInitAlloc']; i++) { 
       
    17 				this._c.push(new IComment()) ;
       
    18 			}
       
    19 		},
       
    20 
       
    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() ;
       
    44 
       
    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 		},
       
    57 
       
    58 		_getInvisibleChildren : function (commentId) {
       
    59 			return CY.Array.filter(this._getChildren(commentId), function(iComment) { return (!iComment.isVisible()) ; }) ;
       
    60 		},
       
    61 
       
    62 		// REFRESH (readreplies link etc ?..)
       
    63 		refresh : function (commentId) {
       
    64 			
       
    65 			var iComment = this.getIComment(commentId) ;
       
    66 
       
    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 		},
       
    73 
       
    74 		remove : function (commentId) {
       
    75 			this._remove(this._getChildren(commentId)) ;
       
    76 		},
       
    77 
       
    78 		close : function (commentId) {
       
    79 			CY.Array.each(this._getChildren(commentId), function (iComment) { iComment.hide() ; }) ;
       
    80 		},
       
    81 
       
    82 		open : function (commentId) {
       
    83 			CY.Array.each(this._getChildren(commentId), function (iComment) { iComment.show() ; }) ;
       
    84 		},
       
    85 
       
    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 		},
       
   104 
       
   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 		},
       
   133 
       
   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 		},
       
   142 
       
   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 		},
       
   151 
       
   152 		setAnimationToPositions : function (y) {
       
   153 			this._initAnimations();
       
   154 			CY.log(gPrefs.get('comments','threadpad')) ;
       
   155 			var lpad = (gPrefs.get('comments','threadpad') == '1') ? 15 : 0 ; // gIThreadPad ... TODO 'configurize'
       
   156 
       
   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'] ;
       
   163 
       
   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 // ANIMATION FUNCTIONS		
       
   176 		_initAnimations : function () {
       
   177 			this._a = [] ;
       
   178 			this._nbEndedAnim = 0 ; 		
       
   179 		},
       
   180 		
       
   181 		runAnimations : function () {
       
   182 			if (this._a.length == 0) // will occur when closing last displayed comment
       
   183 				gSync.resetAutoContinue("animationRun") ;
       
   184 			else 
       
   185 				CY.each(this._a, function (animation) { animation.run() ; }) ;
       
   186 		},
       
   187 		
       
   188 		whenAnimationsEnd : function () {
       
   189 			gSync.resume() ; 		
       
   190 		},
       
   191 		
       
   192 		animationsEnded : function () {
       
   193 			return ((this._a.length == 0) || (this._a.length == this._nbEndedAnim)) ; 		
       
   194 		},
       
   195 		
       
   196 		signalAnimationEnd : function () {
       
   197 			this._nbEndedAnim++ ;					
       
   198 		},
       
   199 		
       
   200 // ACTIVE RELATED FUNCTIONS		
       
   201 		// returns true only for the top iComment
       
   202 		isTopActive : function(commentDbId) {
       
   203 			return ((commentDbId != null) && (this._topActiveCommentDbId == commentDbId)) ;
       
   204 		},
       
   205 		
       
   206 		isAnyActive : function() {
       
   207 			return (this._topActiveCommentDbId != null) ;
       
   208 		},
       
   209 		
       
   210 		//warning : calling this function "focus" would make IE get mad
       
   211 		activate : function(commentDbId) {
       
   212 			
       
   213 			if (this._topActiveCommentDbId != null) {// then deactivate current 
       
   214 				this.deactivate() ;
       
   215 			}
       
   216 
       
   217 			// activate whole top parent thread
       
   218 			var comment = gDb.getComment(commentDbId) ;
       
   219 			var comment_path = gDb.getPath(comment) ;
       
   220 			var topParent = comment_path[comment_path.length - 1] ;
       
   221 			
       
   222 			var iComments = this._getChildren(topParent.id) ;
       
   223 			CY.Array.each(iComments, function(iComment){iComment.activate();}) ;
       
   224 
       
   225 			this._topActiveCommentDbId = topParent.id ;
       
   226 
       
   227 			// update browser index
       
   228 			if (gLayout.isInFrame()) {
       
   229 				var indxDict = gDb.browsingIndex(this._topActiveCommentDbId) ;
       
   230 				parent.$("#browse_by option").each(function() {
       
   231 					var rank = 1 + indxDict[this.value] ;
       
   232 					parent.$("#c_browse_indx_"+this.value).html(''+ rank) ;
       
   233 				}) ;
       
   234 			}
       
   235 			
       
   236 			showScope(topParent.id) ;
       
   237 		},
       
   238 		
       
   239 		deactivate : function() {
       
   240 			if (this._topActiveCommentDbId != null) {
       
   241 				parent.$("#browse_by option").each(function() {
       
   242 					parent.$("#c_browse_indx_"+this.value).html('-') ;
       
   243 				}) ;
       
   244 
       
   245 				// scopes
       
   246 				hideScopeAnyway() ;
       
   247 				
       
   248 				var iComments = this._getChildren(this._topActiveCommentDbId) ;
       
   249 				CY.Array.each(iComments, function(iComment){iComment.deactivate();}) ;
       
   250 
       
   251 				this._topActiveCommentDbId = null ;
       
   252 			}
       
   253 		},
       
   254 
       
   255 		// active next IComment among the displayed ones (try below and if couldn't try above)
       
   256 		// model based
       
   257 		activateVisibleNext : function() {
       
   258 			
       
   259 			if (this._topActiveCommentDbId != null) {
       
   260 
       
   261 				for (var j = 0 ; j < 2 ; j++) {
       
   262 					
       
   263 					var start = (j==0) ? 0 : this._c.length - 1 ; 
       
   264 					
       
   265 					var b = false ;
       
   266 					
       
   267 					for (var i = start ; (i >= 0) && i <= (this._c.length - 1) ;) {
       
   268 						var iComment = this._c[i] ;
       
   269 						
       
   270 						if (iComment.commentId != null && iComment.isVisible()) {
       
   271 							b = b || (gDb.isChild(iComment.commentId,this._topActiveCommentDbId)) ; 
       
   272 							if (b && (!gDb.isChild(iComment.commentId,this._topActiveCommentDbId))) { // found the one below that doesn't have topActive as parent
       
   273 								this.activate(iComment.commentId) ;
       
   274 								return true ;
       
   275 							}
       
   276 						}
       
   277 						i = (j == 0) ? i + 1 : i - 1 ;
       
   278 					}
       
   279 				}
       
   280 			}
       
   281 			return false ;
       
   282 		},
       
   283 		
       
   284 		browse : function(order, whereto) {
       
   285 			var wt = whereto ;
       
   286 			if ((whereto == "prev") && !this.isAnyActive()) {
       
   287 				wt = "last" ;
       
   288 			}
       
   289 			if ((whereto == "next") && !this.isAnyActive()) {
       
   290 				wt = "first" ;
       
   291 			}
       
   292 			return gDb.browse(order, wt, this._topActiveCommentDbId) ;
       
   293 		}
       
   294 		
       
   295 }