OSDN Git Service

websocket closed indicate
[keitairc/keitairc.git] / data / public / webkit.js
index 720bc7a..b375f4a 100644 (file)
@@ -46,10 +46,14 @@ jQuery(document).ready(function($) {
     }
     el = null;
 
-    var reconnect = false;
+    var has_ws = ('WebSocket' in window);
+    var ws = false;
+
     var map_load = true;
+    var myScroll;
 
     $(window).bind('orientationchange', function(){
+       adjust_height();
        adjust_map();
        setTimeout(scrollTo, 300, 0, 1);
     });
@@ -57,6 +61,16 @@ jQuery(document).ready(function($) {
     /* 初期ページデータのロード */
     $('#index').load(web_root + session_id + '/index', reinit);
 
+    function _ws_ping() {
+       if (has_ws && ws) {
+           ws.send('ABC');
+           alert('send ABC');
+       }
+       setTimeout(_ws_ping, 10000);
+    }
+
+    /* _ws_ping(); */
+
     function adjust_map() {
        if ($('#map_canvas').length != 0) {
            var pos = $(document).height() - 20;
@@ -65,20 +79,18 @@ jQuery(document).ready(function($) {
        }
     }
 
-    function inner_reload(url, anim, param) {
-       scrollTo(0, 0);
-       $('#loading').css('display', 'block').height($(document).height());
-       $('body').append($('<div id="'+url.replace('/','_')+'"></div>').attr('animation', anim).load(web_root + session_id + '/' + url, param, reinit));
-       return false;
-    }
 
-    function format_date(unixtime) {
-       var d = new Date(unixtime * 1000);
-       var h = String(d.getHours());
-       if (h.length < 2) h = '0' + h;
-       var m = String(d.getMinutes());
-       if (m.length < 2) m = '0' + m;
-       return h + ':' + m;
+    function adjust_height() {
+       $('.scroll_wrap').each(function() {
+           $(this).height(0);
+       });
+       var hi = $('.current:first').height();
+       $('.toolbar, .edit, .info').each(function() {
+           hi -= $(this).attr('offsetHeight');
+       });
+       $('.scroll_wrap').each(function() {
+           $(this).height(hi);
+       });
     }
 
     function reinit(resp, status, xhr) {
@@ -101,9 +113,41 @@ jQuery(document).ready(function($) {
        }
        $('#loading').css('display', 'none').height(0);
 
+       function inner_reload(url, anim, param) {
+           scrollTo(0, 0);
+           $('#loading').css('display', 'block').height($(document).height());
+           $('body').append($('<div id="'+url.replace('/','_')+'"></div>').attr('animation', anim).load(web_root + session_id + '/' + url, param, reinit));
+
+           if (ws) {
+               ws.close();
+               ws = false;
+           }
+
+           if (has_ws && url.match(/all\/([0-9]+)/)) {
+               var cid = RegExp.$1;
+               ws = new WebSocket('ws://' + location.host + '/' + session_id + '/push/' + cid);
+               ws.addEventListener("open",
+                                   function () {
+                                       $('body').removeClass('ws_closed');
+                                   },false);
+               ws.addEventListener("close",
+                                   function () {
+                                       $('body').addClass('ws_closed');
+                                   }, false);
+               ws.addEventListener("message",
+                                   function (e) {
+                                       var data = JSON.parse(e.data);
+                                       $('div.scroll_wrap ul.edgetoedge li.sep').after(data.formatted);
+                                   },false);
+           }
+
+           return false;
+       }
+
        function _animation_callback() {
            from.remove();
            to.removeClass('in reverse slide');
+           adjust_height();
            setTimeout(scrollTo, 300, 0, 1);
 
            $('.internal').one('click', function() {
@@ -114,7 +158,15 @@ jQuery(document).ready(function($) {
 
            $('.internal_form').bind('submit', function () {
                $('#m').blur();
-               inner_reload($(this).attr('action'), '', $(this).serializeArray());
+               if (has_ws && ws) {
+                   ws.send(JSON.stringify({url: $(this).attr('action'),
+                                           param: $(this).serializeArray()}));
+                   $('#m', this).val('');
+                   $("input[name='stamp']", this).val(parseInt((new Date)/1000));
+                   $('li.messagenew').addClass('message').removeClass('messagenew');
+               } else {
+                   inner_reload($(this).attr('action'), '', $(this).serializeArray());
+               }
                return false;
            });
 
@@ -139,6 +191,37 @@ jQuery(document).ready(function($) {
                    append_location();
                }
            }
+
+           if ($('#scroll').length != 0) {
+               if (typeof myScroll != 'undefined' && myScroll != null) {
+                   myScroll.element.removeEventListener('touchstart', myScroll);
+                   myScroll.element.removeEventListener('touchmove', myScroll);
+                   myScroll.element.removeEventListener('touchend', myScroll);
+                   myScroll.element.removeEventListener('DOMSubtreeModified', myScroll);
+                   window.removeEventListener('orientationchange', myScroll);
+                   myScroll = null;
+               }
+               myScroll = new iScroll('scroll');
+               var sc = $('#scroll');
+               var mark = $('#reload_mark');
+               if (mark.length != 0) {
+                   sc.bind('touchmove', function() {
+                       if (myScroll.y > 30) {
+                           mark.attr('reload', 'on').show();
+                       }
+                   });
+                   sc.bind('touchend', function() {
+                       mark.hide();
+                       if (mark.attr('reload') == 'on') {
+                           mark.removeAttr('reload');
+                           inner_reload(mark.attr('rel'), '');
+                       }
+                   });
+               }
+               $('.toolbar h1').bind('click', function() {
+                   myScroll.scrollTo(0,0);
+               });
+           }
        }
     }
 });