OSDN Git Service

websocket based messaging support
[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 has_ws = ('WebSocket' in window);
50     var ws = false;
51
52     var map_load = true;
53     var myScroll;
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 _ws_ping() {
65         if (has_ws && ws) {
66             ws.send('ABC');
67             alert('send ABC');
68         }
69         setTimeout(_ws_ping, 10000);
70     }
71
72     /* _ws_ping(); */
73
74     function adjust_map() {
75         if ($('#map_canvas').length != 0) {
76             var pos = $(document).height() - 20;
77             setTimeout(scrollTo, 300, 0, 1);
78             $('#map_canvas').height(pos);
79         }
80     }
81
82
83     function adjust_height() {
84         $('.scroll_wrap').each(function() {
85             $(this).height(0);
86         });
87         var hi = $('.current:first').height();
88         $('.toolbar, .edit, .info').each(function() {
89             hi -= $(this).attr('offsetHeight');
90         });
91         $('.scroll_wrap').each(function() {
92             $(this).height(hi);
93         });
94     }
95
96     function reinit(resp, status, xhr) {
97         if (status != 'success' || typeof resp == 'undefined' || resp == '' || xhr.status != 200) {
98             var reload_url = window.location.protocol + '//' + window.location.host + web_root;
99             window.location.replace(reload_url);
100             return false;
101         }
102
103         var from = $('.current');
104         var to = $(this);
105         var animation = to.attr('animation');
106
107         if (animation_end != false && animation && animation != '')  {
108             to.addClass(animation + ' in current').one(animation_end, _animation_callback);
109             from.addClass(animation + ' out');
110         } else {
111             to.addClass('current');
112             _animation_callback();
113         }
114         $('#loading').css('display', 'none').height(0);
115
116         function inner_reload(url, anim, param) {
117             scrollTo(0, 0);
118             $('#loading').css('display', 'block').height($(document).height());
119             $('body').append($('<div id="'+url.replace('/','_')+'"></div>').attr('animation', anim).load(web_root + session_id + '/' + url, param, reinit));
120
121             if (ws) {
122                 ws.close();
123                 ws = false;
124             }
125
126             if (has_ws && url.match(/all\/([0-9]+)/)) {
127                 var cid = RegExp.$1;
128                 ws = new WebSocket('ws://' + location.host + '/' + session_id + '/push/' + cid);
129                 ws.addEventListener("open",
130                                     function () {
131                                         /* alert('open'); */
132                                     },false);
133                 ws.addEventListener("close",
134                                     function () {
135                                         /* alert('close') */
136                                     }, false);
137                 ws.addEventListener("message",
138                                     function (e) {
139                                         var data = JSON.parse(e.data);
140                                         $('div.scroll_wrap ul.edgetoedge li.sep').after(data.formatted);
141                                     },false);
142             }
143
144             return false;
145         }
146
147         function _animation_callback() {
148             from.remove();
149             to.removeClass('in reverse slide');
150             adjust_height();
151             setTimeout(scrollTo, 300, 0, 1);
152
153             $('.internal').one('click', function() {
154                 var anim = ($(this).hasClass('no_anime') ? '' : $(this).hasClass('reverse') ? 'slide reverse' : 'slide');
155                 inner_reload($(this).attr('rel'), anim);
156                 return false;
157             });
158
159             $('.internal_form').bind('submit', function () {
160                 $('#m').blur();
161                 if (has_ws && ws) {
162                     ws.send(JSON.stringify({url: $(this).attr('action'),
163                                             param: $(this).serializeArray()}));
164                     $('#m', this).val('');
165                     $("input[name='stamp']", this).val(parseInt((new Date)/1000));
166                     $('li.messagenew').addClass('message').removeClass('messagenew');
167                 } else {
168                     inner_reload($(this).attr('action'), '', $(this).serializeArray());
169                 }
170                 return false;
171             });
172
173             $('.menu_button').bind('click', function() {
174                 $('.floaty').toggle();
175                 $('#send_location').forcus();
176                 return false;
177             });
178
179             if (!navigator.geolocation) {
180                 $('#post_location').css({'display': 'none'});
181             }
182
183             if ($('#map_canvas').length != 0) {
184                 adjust_map();
185                 if (map_load) {
186                     map_load = false
187                     $.getScript(web_root + 'geopost-keitairc.js', function() {
188                         append_location();
189                     });
190                 } else {
191                     append_location();
192                 }
193             }
194
195             if ($('#scroll').length != 0) {
196                 if (typeof myScroll != 'undefined' && myScroll != null) {
197                     myScroll.element.removeEventListener('touchstart', myScroll);
198                     myScroll.element.removeEventListener('touchmove', myScroll);
199                     myScroll.element.removeEventListener('touchend', myScroll);
200                     myScroll.element.removeEventListener('DOMSubtreeModified', myScroll);
201                     window.removeEventListener('orientationchange', myScroll);
202                     myScroll = null;
203                 }
204                 myScroll = new iScroll('scroll');
205                 var sc = $('#scroll');
206                 var mark = $('#reload_mark');
207                 if (mark.length != 0) {
208                     sc.bind('touchmove', function() {
209                         if (myScroll.y > 30) {
210                             mark.attr('reload', 'on').show();
211                         }
212                     });
213                     sc.bind('touchend', function() {
214                         mark.hide();
215                         if (mark.attr('reload') == 'on') {
216                             mark.removeAttr('reload');
217                             inner_reload(mark.attr('rel'), '');
218                         }
219                     });
220                 }
221                 $('.toolbar h1').bind('click', function() {
222                     myScroll.scrollTo(0,0);
223                 });
224             }
225         }
226     }
227 });