diff options
Diffstat (limited to 'web/scripts3.js')
-rw-r--r-- | web/scripts3.js | 138 |
1 files changed, 110 insertions, 28 deletions
diff --git a/web/scripts3.js b/web/scripts3.js index 9cb72a98..3d35b441 100644 --- a/web/scripts3.js +++ b/web/scripts3.js @@ -1,43 +1,123 @@ -/* var ws=null; +var pageTitle; function initWS() { - ws = new WebSocket("wss://ws.juick.com/?hash="+hash); - ws.onopen = function() { - $('#sidepanel').addClass('online'); - }; - ws.onclose = function() { - $('#sidepanel').removeClass('online'); - ws=null; - initWS(); - }; - ws.onmessage = function(msg) { - if(msg.data==' ') { - ws.send(' '); + if(typeof(pageMID)!="undefined" && pageMID>0) { + var url; + if(typeof(juickDebug)!="undefined") { + url="wss://ws.juick.com/_replies"; } else { - try { - var jsonMsg=$.parseJSON(msg.data); - if(jsonMsg.rid>0) { - //incomingComment(jsonMsg); - } else if(jsonMsg.mid>0) { - //incomingPost(jsonMsg); - } else { - //incomingPM(jsonMsg); + url="wss://ws.juick.com/"+pageMID; + } + if(typeof(hash)!="undefined" && hash) { + url+="?hash="+hash; + } + + ws = new WebSocket(url); + ws.onopen = function() { + console.log('online'); + if($('#wsthread').length==0) { + var d=$('<div id="wsthread"/>'); + d.on('click',onclickNextReply); + d.appendTo("body"); + pageTitle=document.title; + } + }; + ws.onclose = function() { + console.log('offline'); + ws=null; + setTimeout(function() { + initWS(); + },2000); + }; + ws.onmessage = function(msg) { + if(msg.data==' ') { + ws.send(' '); + } else { + try { + var jsonMsg=$.parseJSON(msg.data); + console.log('data: '+msg.data); + wsIncomingReply(jsonMsg); + } catch(err) { + console.log(err); } - } catch(err) { - console.log(err); } - } - }; - setInterval(wsSendKeepAlive, 90000); + }; + setInterval(wsSendKeepAlive, 90000); + } } function wsSendKeepAlive() { - if(ws!=null) { + if(ws) { ws.send(' '); } } - */ + +function wsShutdown() { + if(ws) { + ws.onclose=function(){}; + ws.close(); + } +} + +function wsIncomingReply(msg) { + var p; + if(msg.replyto>0) { + p=$('#'+msg.replyto); + if(p.length==0) { + p=null; + } + } + + var li=$('<li class="msg reply-new" id="'+msg.rid+'" onclick="onclickNewReply(this)" onmouseover="onclickNewReply(this)">'); + li.html('<div class="msg-avatar"><a href="/'+msg.user.uname+'/"><img src="//i.juick.com/a/'+msg.user.uid+'.png"/></a></div>'+ + '<div class="msg-cont">'+ + '<div class="msg-menu"><a href="#" onclick="showMessageLinksDialog('+msg.mid+','+msg.rid+'); return false"></a></div>'+ + '<div class="msg-header"><a href="/'+msg.user.uname+'/">@'+msg.user.uname+'</a>:</div>'+ + '<div class="msg-ts"><a href="/'+msg.mid+'#'+msg.rid+'" title="'+msg.timestamp+' GMT">'+msg.timestamp+'</a></div>'+ + '<div class="msg-txt">'+msg.body+'</div>'+ + '<div class="msg-links"><a href="#" onclick="return showCommentForm('+msg.mid+','+msg.rid+')">Ответить</a></div>'+ + '<div class="msg-comment" style="display: none"></div>'+ + '</div>'); + + if(p) { + li.css('margin-left',parseInt(p.css('margin-left'))+20+'px'); + p.after(li); + } else { + $('#replies').append(li); + } + + updateRepliesCounter(); +} + +function onclickNewReply(e) { + var li=$(e); + li.removeClass('reply-new'); + li.off('click'); + li.off('mouseover'); + updateRepliesCounter(); +} + +function onclickNextReply() { + var li=$('#replies>li.reply-new:first'); + if(li.length) { + li.removeClass('reply-new'); + li.off('click'); + li.get(0).scrollIntoView(); + updateRepliesCounter(); + } +} + +function updateRepliesCounter() { + var replies=$('#replies>li.reply-new').length; + if(replies>0) { + $('#wsthread').text(replies).css('display','block'); + document.title='['+replies+'] '+pageTitle; + } else { + $('#wsthread').css('display','none'); + document.title=pageTitle; + } +} /******************************************************************************/ /******************************************************************************/ @@ -592,4 +672,6 @@ $(document).ready(function() { unfoldReply(); $(window).bind('hashchange',unfoldPostForm); $(window).bind('hashchange',unfoldReply); + + $(window).on('beforeunload',wsShutdown); }); |