src/cm/media/js/site/c_text_view_comments.js
changeset 341 053551f213fb
parent 340 9e2b9e568e42
child 421 3ddbfa64f596
equal deleted inserted replaced
340:9e2b9e568e42 341:053551f213fb
     5 gIComments = null;
     5 gIComments = null;
     6 gSync = null;
     6 gSync = null;
     7 gGETValues = null;
     7 gGETValues = null;
     8 
     8 
     9 gConf = {'iCommentLeftPadding':4, 
     9 gConf = {'iCommentLeftPadding':4, 
    10 		'iCommentThreadPadding':12,
    10     'iCommentThreadPadding':12,
    11         'defaultCommentFormat':'markdown',
    11         'defaultCommentFormat':'markdown',
    12         'sliderFixedMin':.9,
    12         'sliderFixedMin':.9,
    13         'sliderFixedMax':.1,
    13         'sliderFixedMax':.1,
    14         'iCommentsInitAlloc':2,
    14         'iCommentsInitAlloc':2,
    15         'defaultPrefs':{'text':{'style':'text-modern-style'}, 'user' : {'name' : '','email' : ''},'general': {'animduration' : '0.4'},'comments': {'threadpad' : '1'},'layout': {'comments_col_width' : '25'  /* out of 100 */}}
    15         'defaultPrefs':{'text':{'style':'text-modern-style'}, 'user' : {'name' : '','email' : ''},'general': {'animduration' : '0.4'},'comments': {'threadpad' : '1'},'layout': {'comments_col_width' : '25'  /* out of 100 */}}
    18 //available text styles in c-text-styles.css
    18 //available text styles in c-text-styles.css
    19 //classname(key) : will be added to #textcontainer, name(value) : the name to display in style dropdown choice"
    19 //classname(key) : will be added to #textcontainer, name(value) : the name to display in style dropdown choice"
    20 gTextStyles = {'text-modern-style':gettext('modern'), 'text-classic-style':gettext('classic'), 'text-code-style':gettext('code')};
    20 gTextStyles = {'text-modern-style':gettext('modern'), 'text-classic-style':gettext('classic'), 'text-code-style':gettext('code')};
    21 
    21 
    22 YUI( {
    22 YUI( {
    23 	base :sv_media_url + "/js/lib/yui/" + c_yui_base + "/build/",
    23   base :sv_media_url + "/js/lib/yui/" + c_yui_base + "/build/",
    24 	// filter: '{% if CLIENT_DEBUG %}debug{% else %}raw{% endif %}',
    24   // filter: '{% if CLIENT_DEBUG %}debug{% else %}raw{% endif %}',
    25 	// filter :'raw',
    25   // filter :'raw',
    26 	timeout :10000
    26   timeout :10000
    27 }).use(
    27 }).use(
    28 	    "text-modern-style",
    28       "text-modern-style",
    29 		"cookie",
    29     "cookie",
    30 		"json",
    30     "json",
    31 		"overlay",
    31     "overlay",
    32 		"io-form",
    32     "io-form",
    33 		"async-queue",
    33     "async-queue",
    34 		"event-mouseenter",		
    34     "event-mouseenter",   
    35 		"anim",
    35     "anim",
    36 		"collection",
    36     "collection",
    37 		function(Y) {
    37     function(Y) {
    38 	        CY = Y;
    38           CY = Y;
    39 	        
    39           
    40             gPrefs = new Preferences() ;
    40             gPrefs = new Preferences() ;
    41             gPrefs.init() ;
    41             gPrefs.init() ;
    42             
    42             
    43             gLayout = new Layout() ;
    43             gLayout = new Layout() ;
    44             gLayout.init() ;
    44             gLayout.init() ;
    45 
    45 
    46     		if (sv_withComments) {
    46         if (sv_withComments) {
    47 	            gDb = new Db() ;
    47               gDb = new Db() ;
    48 	            gDb.init() ;
    48               gDb.init() ;
    49 	            
    49               
    50 	            gIComments = new IComments() ;	            	            
    50               gIComments = new IComments() ;                            
    51 	            gIComments.init() ;
    51               gIComments.init() ;
    52     		}
    52         }
    53 
    53 
    54             gSync = new Sync() ;
    54             gSync = new Sync() ;
    55             gSync.init() ;
    55             gSync.init() ;
    56 
    56 
    57 			CY.on("domready", onDomReady, this); 
    57       CY.on("domready", onDomReady, this); 
    58 
    58 
    59 		});
    59     });
    60 
    60 
    61 _reinit = function(filterRes) {
    61 _reinit = function(filterRes) {
    62 	gIComments.hide();
    62   gIComments.hide();
    63 	gDb.initComments(filterRes['commentIds']);
    63   gDb.initComments(filterRes['commentIds']);
    64 
    64 
    65 	unpaintAllComments();
    65   unpaintAllComments();
    66 	renderCommentScopes();
    66   renderCommentScopes();
    67 	
    67   
    68 	updateFilterResultsCount(filterRes['nbDiscussions'], filterRes['nbComments'], filterRes['nbReplies']);
    68   updateFilterResultsCount(filterRes['nbDiscussions'], filterRes['nbComments'], filterRes['nbReplies']);
    69 };
    69 };
    70 
    70 
    71 reinit = function(filterGETValues) {
    71 reinit = function(filterGETValues) {
    72 	var filterRes = gDb.computeFilterResults(filterGETValues);
    72   var filterRes = gDb.computeFilterResults(filterGETValues);
    73 	_reinit(filterRes);
    73   _reinit(filterRes);
    74 };
    74 };
    75 
    75 
    76 hideAll = function() {
    76 hideAll = function() {
    77 	_reinit({'commentIds':[],'nbDiscussions':0, 'nbComments':0, 'nbReplies':0});
    77   _reinit({'commentIds':[],'nbDiscussions':0, 'nbComments':0, 'nbReplies':0});
    78 };
    78 };
    79 
    79 
    80 updateFilterResultsCount = function(nbDiscussions, nbComments, nbReplies) {
    80 updateFilterResultsCount = function(nbDiscussions, nbComments, nbReplies) {
    81 	var r = gDb.getCommentsAndRepliesCounts(true);
    81   var r = gDb.getCommentsAndRepliesCounts(true);
    82 	var nbAllComments = r[0], nbAllReplies = r[1];
    82   var nbAllComments = r[0], nbAllReplies = r[1];
    83 	var detailedResults = (nbComments != 0 || nbReplies != 0) && (nbAllComments != nbComments || nbAllReplies != nbReplies) ;
    83   var detailedResults = (nbComments != 0 || nbReplies != 0) && (nbAllComments != nbComments || nbAllReplies != nbReplies) ;
    84 	if (gLayout.isInFrame()) {
    84   if (gLayout.isInFrame()) {
    85 		parent.f_updateFilterCountDetailed(detailedResults) ;
    85     parent.f_updateFilterCountDetailed(detailedResults) ;
    86 		parent.f_updateFilterCountResult(nbDiscussions, nbComments, nbReplies, nbAllComments, nbAllReplies);
    86     parent.f_updateFilterCountResult(nbDiscussions, nbComments, nbReplies, nbAllComments, nbAllReplies);
    87 	}
    87   }
    88 };
    88 };
    89 
    89 
    90 updateResetFilterResultsCount = function() {
    90 updateResetFilterResultsCount = function() {
    91 	var counts = gDb.getCommentsAndRepliesCounts(false)
    91   var counts = gDb.getCommentsAndRepliesCounts(false)
    92 	var nbComments = counts[0], nbReplies = counts[1];
    92   var nbComments = counts[0], nbReplies = counts[1];
    93 
    93 
    94 	var nbDiscussions = nbComments;
    94   var nbDiscussions = nbComments;
    95 	updateFilterResultsCount(nbDiscussions, nbComments, nbReplies);
    95   updateFilterResultsCount(nbDiscussions, nbComments, nbReplies);
    96 };
    96 };
    97 
    97 
    98 // TODO MOVE
    98 // TODO MOVE
    99 renderCommentScopes = function() {
    99 renderCommentScopes = function() {
   100 	for (var i = 0 ; i < gDb.comments.length ; i++) {
   100   for (var i = 0 ; i < gDb.comments.length ; i++) {
   101 		var comment = gDb.comments[i] ;
   101     var comment = gDb.comments[i] ;
   102 		paintCommentScope(comment) ;
   102     paintCommentScope(comment) ;
   103 	}
   103   }
   104 } ;
   104 } ;
   105 
   105 
   106 onTextMouseUp = function(e) {
   106 onTextMouseUp = function(e) {
   107 	if (readyForAction()) {
   107   if (readyForAction()) {
   108 		var selection = getSelectionInfo() ;
   108     var selection = getSelectionInfo() ;
   109 		if (selection != null) {
   109     if (selection != null) {
   110 			updateICommentFormSelection(selection) ;
   110       updateICommentFormSelection(selection) ;
   111 			if (gEditICommentHost != null) {
   111       if (gEditICommentHost != null) {
   112 				var modifyScope = CY.get("#"+gEdit['ids']['changeScopeInputId']+" input").get('checked') ;
   112         var modifyScope = CY.get("#"+gEdit['ids']['changeScopeInputId']+" input").get('checked') ;
   113 				if (modifyScope) {
   113         if (modifyScope) {
   114 					gEditICommentHost.scrollIntoView() ;
   114           gEditICommentHost.scrollIntoView() ;
   115 				}
   115         }
   116 			}
   116       }
   117 		}
   117     }
   118 		else {
   118     else {
   119 			var node = e.target ;
   119       var node = e.target ;
   120 			if (node.hasClass('c-c')) {
   120       if (node.hasClass('c-c')) {
   121 				var elt = CY.Node.getDOMNode(node) ;
   121         var elt = CY.Node.getDOMNode(node) ;
   122 				var commentIds = getCommentIdsFromClasses(elt) ;
   122         var commentIds = getCommentIdsFromClasses(elt) ;
   123 				if (commentIds.length > 0) {
   123         if (commentIds.length > 0) {
   124 					checkForOpenedDialog(null, function() { // will only check for reply
   124           checkForOpenedDialog(null, function() { // will only check for reply
   125 						gSync.showComments(commentIds, [e.pageX, e.pageY], false) ;
   125             gSync.showComments(commentIds, [e.pageX, e.pageY], false) ;
   126 					}) ;
   126           }) ;
   127 				}
   127         }
   128 			} 
   128       } 
   129 		}
   129     }
   130 //// FIRST UPDATE SELECTION IF ANY AND SCROLL TO EDITION IF ANY		
   130 //// FIRST UPDATE SELECTION IF ANY AND SCROLL TO EDITION IF ANY   
   131 //		if (isICommentFormVisible() || gEditICommentHost != null) {
   131 //    if (isICommentFormVisible() || gEditICommentHost != null) {
   132 //			CY.log(selection) ;
   132 //      CY.log(selection) ;
   133 //			updateICommentFormSelection(selection) ;
   133 //      updateICommentFormSelection(selection) ;
   134 //			
   134 //      
   135 //			if (gEditICommentHost != null) {
   135 //      if (gEditICommentHost != null) {
   136 //				var modifyScope = CY.get("#"+gEdit['ids']['changeScopeInputId']+" input").get('checked') ;
   136 //        var modifyScope = CY.get("#"+gEdit['ids']['changeScopeInputId']+" input").get('checked') ;
   137 //				if (modifyScope) {
   137 //        if (modifyScope) {
   138 //					gEditICommentHost.scrollIntoView() ;
   138 //          gEditICommentHost.scrollIntoView() ;
   139 //					didSomething = true ;
   139 //          didSomething = true ;
   140 //				}
   140 //        }
   141 //			}
   141 //      }
   142 //			if (isICommentFormVisible())
   142 //      if (isICommentFormVisible())
   143 //			didSomething = true ;
   143 //      didSomething = true ;
   144 //		}
   144 //    }
   145 //		else {
   145 //    else {
   146 //			checkForOpenedDialog(null, function() { // will only check for reply
   146 //      checkForOpenedDialog(null, function() { // will only check for reply
   147 //					var node = e.target ;
   147 //          var node = e.target ;
   148 //					if (node.hasClass('c-c')) {
   148 //          if (node.hasClass('c-c')) {
   149 //						var elt = CY.Node.getDOMNode(node) ;
   149 //            var elt = CY.Node.getDOMNode(node) ;
   150 //						var commentIds = getCommentIdsFromClasses(elt) ;
   150 //            var commentIds = getCommentIdsFromClasses(elt) ;
   151 //						if (commentIds.length > 0) {
   151 //            if (commentIds.length > 0) {
   152 //							//hideOverlay(gICommentForm) ;
   152 //              //hideOverlay(gICommentForm) ;
   153 //							// gIComments.hide() ;20080814 moved it to gSync._showComments
   153 //              // gIComments.hide() ;20080814 moved it to gSync._showComments
   154 //							gSync.showComments(commentIds, [e.pageX, e.pageY]) ;
   154 //              gSync.showComments(commentIds, [e.pageX, e.pageY]) ;
   155 //						}
   155 //            }
   156 //					}
   156 //          }
   157 //				}) ;
   157 //        }) ;
   158 //		}
   158 //    }
   159 	}
   159   }
   160 } ;
   160 } ;
   161 
   161 
   162 gLastScrollTime = null ;
   162 gLastScrollTime = null ;
   163 checkForAlignement = function () {
   163 checkForAlignement = function () {
   164 	var now = (new Date()).getTime() ;
   164   var now = (new Date()).getTime() ;
   165 	if ((gLastScrollTime != null) && (now - gLastScrollTime) > 200) {
   165   if ((gLastScrollTime != null) && (now - gLastScrollTime) > 200) {
   166 		positionICommentForm() ;			
   166     positionICommentForm() ;      
   167 		gLastScrollTime = null ;
   167     gLastScrollTime = null ;
   168 	}
   168   }
   169 };
   169 };
   170 
   170 
   171 onFrameScroll = function () {
   171 onFrameScroll = function () {
   172 	gLastScrollTime = (new Date()).getTime() ;
   172   gLastScrollTime = (new Date()).getTime() ;
   173 };
   173 };
   174 
   174 
   175 browse = function(order, whereto) {
   175 browse = function(order, whereto) {
   176 	gSync.browse(order, whereto) ; 
   176   gSync.browse(order, whereto) ; 
   177 };
   177 };
   178 
   178 
   179 initialConnect = function() {
   179 initialConnect = function() {
   180 	CY.on("mouseup", onTextMouseUp, "#textcontainer");
   180   CY.on("mouseup", onTextMouseUp, "#textcontainer");
   181 	gTimer = CY.Lang.later(200, this, checkForAlignement, [], true) ;
   181   gTimer = CY.Lang.later(200, this, checkForAlignement, [], true) ;
   182 	CY.on('scroll', onFrameScroll, window, this, true);
   182   CY.on('scroll', onFrameScroll, window, this, true);
   183 	CY.on('resize', onFrameScroll, window, this, true);
   183   CY.on('resize', onFrameScroll, window, this, true);
   184 };
   184 };
   185 
   185 
   186 preventLinksInText = function() {
   186 preventLinksInText = function() {
   187 	var interceptLink = function(e) {
   187   var interceptLink = function(e) {
   188 		var a = e.target;
   188     var a = e.target;
   189 		var href = null;
   189     var href = null;
   190 		while (a != null && href == null) {
   190     while (a != null && href == null) {
   191 			a = a.get('parentNode');
   191       a = a.get('parentNode');
   192 			href = a.get("href");
   192       href = a.get("href");
   193 		}
   193     }
   194 		
   194     
   195 		if (a != null && href != null) {
   195     if (a != null && href != null) {
   196 			//alert(window.location) ;
   196       //alert(window.location) ;
   197 			var clean_window_location = window.location.href ;
   197       var clean_window_location = window.location.href ;
   198 			
   198       
   199 			var ind = clean_window_location.indexOf('#') ;
   199       var ind = clean_window_location.indexOf('#') ;
   200 			if (ind != -1)
   200       if (ind != -1)
   201 				clean_window_location = clean_window_location.substring(0, ind) ;
   201         clean_window_location = clean_window_location.substring(0, ind) ;
   202 			
   202       
   203 			if (href.indexOf(clean_window_location) == -1 ) {
   203       if (href.indexOf(clean_window_location) == -1 ) {
   204 				window.open(a.get("href"));
   204         window.open(a.get("href"));
   205 				e.preventDefault(); ;
   205         e.preventDefault(); ;
   206 			}
   206       }
   207 		}
   207     }
   208 	} ;
   208   } ;
   209 
   209 
   210 	CY.all("#textcontainer a").on("click", interceptLink);
   210   CY.all("#textcontainer a").on("click", interceptLink);
   211 };
   211 };
   212 
   212 
   213 onDomReady = function(arg1) {
   213 onDomReady = function(arg1) {
   214 	preventLinksInText();
   214   preventLinksInText();
   215 	var q1 = new CY.AsyncQueue(); //  
   215   var q1 = new CY.AsyncQueue(); //  
   216 	// doesn't behave like the doc says :
   216   // doesn't behave like the doc says :
   217 	// no timeout -> seems in blocking mode
   217   // no timeout -> seems in blocking mode
   218 	q1.add( {
   218   q1.add( {
   219 		fn : function() {
   219     fn : function() {
   220 
   220 
   221 			if (gLayout.isInComentSite()) {
   221       if (gLayout.isInComentSite()) {
   222 				parent.toInitialSize();
   222         parent.toInitialSize();
   223 			}
   223       }
   224 			if (sv_withComments) {
   224       if (sv_withComments) {
   225 				instanciateICommentForm();
   225         instanciateICommentForm();
   226 			}
   226       }
   227 		},
   227     },
   228 		timeout :5
   228     timeout :5
   229 	}, {
   229   }, {
   230 		fn : function() {
   230     fn : function() {
   231 
   231 
   232 			gGETValues = CY.JSON.parse(sv_get_params);
   232       gGETValues = CY.JSON.parse(sv_get_params);
   233 			
   233       
   234 			CY.get('#maincontainer').setStyle('display', 'block');
   234       CY.get('#maincontainer').setStyle('display', 'block');
   235 			CY.get('#textcontainer').setStyle('display', 'block');
   235       CY.get('#textcontainer').setStyle('display', 'block');
   236 			
   236       
   237 			var val = (sv_withComments) ? gPrefs.get('layout','comments_col_width') : 0 ; 
   237       var val = (sv_withComments) ? gPrefs.get('layout','comments_col_width') : 0 ; 
   238 			var colWidth = sliderValToPx(val) ;
   238       var colWidth = sliderValToPx(val) ;
   239 			gLayout.setLeftColumnWidth(colWidth) ;
   239       gLayout.setLeftColumnWidth(colWidth) ;
   240 			
   240       
   241 			if (gLayout.isInFrame()) {
   241       if (gLayout.isInFrame()) {
   242 				parent.f_initFrame();
   242         parent.f_initFrame();
   243 				parent.f_layoutFrames();
   243         parent.f_layoutFrames();
   244 
   244 
   245 				if (sv_withComments) {
   245         if (sv_withComments) {
   246 					parent.f_fillTopToolbar() ;
   246           parent.f_fillTopToolbar() ;
   247 					if (hasPerm("can_create_comment"))  	
   247           if (hasPerm("can_create_comment"))    
   248 						parent.$("#add_comment_btn").removeClass('initially_hidden');
   248             parent.$("#add_comment_btn").removeClass('initially_hidden');
   249 					//parent.enqueueLoadingMsg(); // no more loading message ? TODO
   249           //parent.enqueueLoadingMsg(); // no more loading message ? TODO
   250 					
   250           
   251 					parent.f_fillFilterTab() ;
   251           parent.f_fillFilterTab() ;
   252 					parent.f_fillPreferencesTab() ;
   252           parent.f_fillPreferencesTab() ;
   253 					
   253           
   254 //					parent.f_fillBrowseSection() ;
   254 //          parent.f_fillBrowseSection() ;
   255 					
   255           
   256 					var firstTimeFilterData = CY.JSON.parse(sv_filter_data);
   256           var firstTimeFilterData = CY.JSON.parse(sv_filter_data);
   257 					parent.f_updateFilterData(firstTimeFilterData);
   257           parent.f_updateFilterData(firstTimeFilterData);
   258 					
   258           
   259 					parent.f_setFilterValue(gGETValues) ;					
   259           parent.f_setFilterValue(gGETValues) ;         
   260 				}
   260         }
   261 				parent.f_fillTextPreferencesTab() ;
   261         parent.f_fillTextPreferencesTab() ;
   262 			}
   262       }
   263 			
   263       
   264 			if (gLayout.isInComentSite()) 
   264       if (gLayout.isInComentSite()) 
   265 				parent.$("#c_fullscreen_btn").show();
   265         parent.$("#c_fullscreen_btn").show();
   266 			else {
   266       else {
   267 				parent.$("#c_fullscreen_btn").hide();
   267         parent.$("#c_fullscreen_btn").hide();
   268 			}
   268       }
   269 
   269 
   270 		},
   270     },
   271 		timeout :5
   271     timeout :5
   272 	}, {
   272   }, {
   273 		fn : function() {
   273     fn : function() {
   274 			if (sv_withComments) {
   274       if (sv_withComments) {
   275 				
   275         
   276 				reinit(gGETValues);
   276         reinit(gGETValues);
   277 				initialConnect();
   277         initialConnect();
   278 			}
   278       }
   279 		},
   279     },
   280 		timeout :5
   280     timeout :5
   281 	}, {
   281   }, {
   282 		fn : function() {
   282     fn : function() {
   283 			if (gLayout.isInFrame()) {
   283       if (gLayout.isInFrame()) {
   284 				parent.f_interfaceUnfreeze() ;
   284         parent.f_interfaceUnfreeze() ;
   285 				parent.f_removeLoadingMsg();
   285         parent.f_removeLoadingMsg();
   286 			}
   286       }
   287 			// if there is a comment id in the url display it
   287       // if there is a comment id in the url display it
   288 			if ("comment_id_key" in gGETValues) {
   288       if ("comment_id_key" in gGETValues) {
   289 				var id_key = gGETValues["comment_id_key"] ;
   289         var id_key = gGETValues["comment_id_key"] ;
   290 				var comment = gDb.getCommentByIdKey(id_key) ;
   290         var comment = gDb.getCommentByIdKey(id_key) ;
   291 				if (comment != null) {
   291         if (comment != null) {
   292 					var path = gDb.getPath(comment) ;
   292           var path = gDb.getPath(comment) ;
   293 					var topParentComment = path[path.length - 1] ;
   293           var topParentComment = path[path.length - 1] ;
   294           var focusComment = gDb.getCommentByIdKey(id_key);
   294           var focusComment = gDb.getCommentByIdKey(id_key);
   295           // if comment_op=reply, show reply form
   295           // if comment_op=reply, show reply form
   296           if ("comment_op" in gGETValues && gGETValues["comment_op"] == 'reply') {
   296           if ("comment_op" in gGETValues && gGETValues["comment_op"] == 'reply') {
   297             gSync.showFocusSingleComment(topParentComment, focusComment, true) ;
   297             gSync.showFocusSingleComment(topParentComment, focusComment, true) ;
   298           }
   298           }
   299           else {
   299           else {
   300             gSync.showFocusSingleComment(topParentComment, focusComment, false) ;
   300             gSync.showFocusSingleComment(topParentComment, focusComment, false) ;
   301           }
   301           }
   302 				}
   302         }
   303 			}
   303       }
   304 
   304 
   305 			// if comment_auto_display: show all comments
   305       // if comment_auto_display: show all comments
   306 			if ("comments_auto_display" in gGETValues) {
   306       if ("comments_auto_display" in gGETValues) {
   307 				gSync.showAllComments();
   307         gSync.showAllComments();
   308 			}
   308       }
   309 			
   309       
   310 //			else {
   310 //      else {
   311 //			gSync.showAllComments() ;// show all
   311 //      gSync.showAllComments() ;// show all
   312 //			}
   312 //      }
   313 		}
   313     }
   314 	});
   314   });
   315 	q1.run();
   315   q1.run();
   316 };
   316 };
   317 
   317