OSDN Git Service

WebSocket support 1t phase
[keitairc/keitairc.git] / data / public / webkit.js
1 /*
2  -*- mode: javascript; coding: utf-8 -*-
3  $Id$
4
5  Copyright (c) 2010 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
6  This program is covered by the GNU General Public License 2
7 */
8
9 function noreferrer(a){
10     if (!$(a).hasClass('href_replaced')) {
11         var url = $(a).attr('href');
12         var html = '<html><head><script type="text/javascript">'
13             + 'document.write(\'<meta http-equiv="refresh" content="0;url='+url+'">\');'
14             + '<'+'/script></head><body></body></html>';
15         $(a).attr('href', 'data:text/html;charset=utf-8,'+encodeURIComponent(html)).addClass('href_replaced');
16     }
17 }
18
19 function restore_original(a){
20     $(a).attr('href', $(a).attr('o_href')).removeClass('href_replaced');
21 }
22
23
24 /* だいぶカオスってきたので整理しないとダメだな こりゃ(わら */
25 jQuery(document).ready(function($) {
26     /* 画像の preload */
27     var preload_image = new Array('right_arrow.png', 'stripe.png',
28                                   'twg_iphone_toolbar_icons/icon_arrow_left.png',
29                                   'twg_iphone_toolbar_icons/icon_circle_arrow_right.png',
30                                   'twg_iphone_toolbar_icons/icon_blog.png',
31                                   'twg_iphone_toolbar_icons/icon_users.png',
32                                   'twg_iphone_toolbar_icons/icon_post.png',
33                                   'twg_iphone_toolbar_icons/icon_flag.png',
34                                   'twg_iphone_toolbar_icons/icon_cancel.png',
35                                   'ajax-loader.gif');
36
37     for (i = 0; i < preload_image.length; i++) {
38         $('#preload').append('<img src="' + web_root + preload_image[i] + '" />');
39     }
40
41     /* animation end event がハンドリングできるか判定 */
42     var animation_end = false;
43     var el = document.createElement('div');
44     if ('WebkitTransform' in el.style) {
45         animation_end = 'webkitAnimationEnd';
46     }
47     el = null;
48
49     var reconnect = false;
50
51     var map_load = true;
52     var myScroll;
53     var ws = false;
54
55     $(window).bind('orientationchange', function(){
56         adjust_height();
57         adjust_map();
58         setTimeout(scrollTo, 300, 0, 1);
59     });
60
61     /* 初期ページデータのロード */
62     $('#index').load(web_root + session_id + '/index', reinit);
63
64     function adjust_map() {
65         if ($('#map_canvas').length != 0) {
66             var pos = $(document).height() - 20;
67             setTimeout(scrollTo, 300, 0, 1);
68             $('#map_canvas').height(pos);
69         }
70     }
71
72     function inner_reload(url, anim, param) {
73         scrollTo(0, 0);
74         $('#loading').css('display', 'block').height($(document).height());
75         $('body').append($('<div id="'+url.replace('/','_')+'"></div>').attr('animation', anim).load(web_root + session_id + '/' + url, param, reinit));
76
77         if (url.match(/all\/[0-9]+/) && !ws) {
78             ws = new WebSocket('ws://' + location.host + '/' + session_id + '/push');
79             ws.addEventListener("open",
80                                 function () {
81                                     alert('open');
82                                 },false);
83             ws.addEventListener("close",
84                                 function () {
85                                     alert('close');
86                                 },false);
87             ws.addEventListener("message",
88                                 function (e) {
89                                     alert(e.data);
90                                 },false);
91         } else {
92             ws.close();
93             ws = false;
94         }
95
96         return false;
97     }
98
99     function format_date(unixtime) {
100         var d = new Date(unixtime * 1000);
101         var h = String(d.getHours());
102         if (h.length < 2) h = '0' + h;
103         var m = String(d.getMinutes());
104         if (m.length < 2) m = '0' + m;
105         return h + ':' + m;
106     }
107
108     function adjust_height() {
109         $('.scroll_wrap').each(function() {
110             $(this).height(0);
111         });
112         var hi = $('.current:first').height();
113         $('.toolbar, .edit, .info').each(function() {
114             hi -= $(this).attr('offsetHeight');
115         });
116         $('.scroll_wrap').each(function() {
117             $(this).height(hi);
118         });
119     }
120
121     function reinit(resp, status, xhr) {
122         if (status != 'success' || typeof resp == 'undefined' || resp == '' || xhr.status != 200) {
123             var reload_url = window.location.protocol + '//' + window.location.host + web_root;
124             window.location.replace(reload_url);
125             return false;
126         }
127
128         var from = $('.current');
129         var to = $(this);
130         var animation = to.attr('animation');
131
132         if (animation_end != false && animation && animation != '')  {
133             to.addClass(animation + ' in current').one(animation_end, _animation_callback);
134             from.addClass(animation + ' out');
135         } else {
136             to.addClass('current');
137             _animation_callback();
138         }
139         $('#loading').css('display', 'none').height(0);
140
141         function _animation_callback() {
142             from.remove();
143             to.removeClass('in reverse slide');
144             adjust_height();
145             setTimeout(scrollTo, 300, 0, 1);
146
147             $('.internal').one('click', function() {
148                 var anim = ($(this).hasClass('no_anime') ? '' : $(this).hasClass('reverse') ? 'slide reverse' : 'slide');
149                 inner_reload($(this).attr('rel'), anim);
150                 return false;
151             });
152
153             $('.internal_form').bind('submit', function () {
154                 $('#m').blur();
155                 inner_reload($(this).attr('action'), '', $(this).serializeArray());
156                 return false;
157             });
158
159             $('.menu_button').bind('click', function() {
160                 $('.floaty').toggle();
161                 $('#send_location').forcus();
162                 return false;
163             });
164
165             if (!navigator.geolocation) {
166                 $('#post_location').css({'display': 'none'});
167             }
168
169             if ($('#map_canvas').length != 0) {
170                 adjust_map();
171                 if (map_load) {
172                     map_load = false
173                     $.getScript(web_root + 'geopost-keitairc.js', function() {
174                         append_location();
175                     });
176                 } else {
177                     append_location();
178                 }
179             }
180
181             if ($('#scroll').length != 0) {
182                 if (typeof myScroll != 'undefined' && myScroll != null) {
183                     myScroll.element.removeEventListener('touchstart', myScroll);
184                     myScroll.element.removeEventListener('touchmove', myScroll);
185                     myScroll.element.removeEventListener('touchend', myScroll);
186                     myScroll.element.removeEventListener('DOMSubtreeModified', myScroll);
187                     window.removeEventListener('orientationchange', myScroll);
188                     myScroll = null;
189                 }
190                 myScroll = new iScroll('scroll');
191                 var sc = $('#scroll');
192                 var mark = $('#reload_mark');
193                 if (mark.length != 0) {
194                     sc.bind('touchmove', function() {
195                         if (myScroll.y > 30) {
196                             mark.attr('reload', 'on').show();
197                         }
198                     });
199                     sc.bind('touchend', function() {
200                         mark.hide();
201                         if (mark.attr('reload') == 'on') {
202                             mark.removeAttr('reload');
203                             inner_reload(mark.attr('rel'), '');
204                         }
205                     });
206                 }
207                 $('.toolbar h1').bind('click', function() {
208                     myScroll.scrollTo(0,0);
209                 });
210             }
211         }
212     }
213 });