OSDN Git Service

replace pinstripes.png
[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
54     $(window).bind('orientationchange', function(){
55         adjust_height();
56         adjust_map();
57         setTimeout(scrollTo, 300, 0, 0);
58     });
59
60     /* 初期ページデータのロード */
61     $('#index').load(web_root + session_id + '/index', reinit);
62
63     function adjust_map() {
64         if ($('#map_canvas').length != 0) {
65             var pos = $(document).height() - 20;
66             setTimeout(scrollTo, 300, 0, 0);
67             $('#map_canvas').height(pos);
68         }
69     }
70
71     function inner_reload(url, anim, param) {
72         scrollTo(0, 0);
73         $('#loading').css('display', 'block').height($(document).height());
74         $('body').append($('<div id="'+url.replace('/','_')+'"></div>').attr('animation', anim).load(web_root + session_id + '/' + url, param, reinit));
75         return false;
76     }
77
78     function format_date(unixtime) {
79         var d = new Date(unixtime * 1000);
80         var h = String(d.getHours());
81         if (h.length < 2) h = '0' + h;
82         var m = String(d.getMinutes());
83         if (m.length < 2) m = '0' + m;
84         return h + ':' + m;
85     }
86
87     function adjust_height() {
88         $('.scroll_wrap').each(function() {
89             $(this).height(0);
90         });
91         var hi = $('.current:first').height();
92         $('.toolbar, .edit, .info').each(function() {
93             hi -= $(this).attr('offsetHeight');
94         });
95         $('.scroll_wrap').each(function() {
96             $(this).height(hi);
97         });
98     }
99
100     function reinit(resp, status, xhr) {
101         if (status != 'success' || typeof resp == 'undefined' || resp == '' || xhr.status != 200) {
102             var reload_url = window.location.protocol + '//' + window.location.host + web_root;
103             window.location.replace(reload_url);
104             return false;
105         }
106
107         var from = $('.current');
108         var to = $(this);
109         var animation = to.attr('animation');
110
111         if (animation_end != false && animation && animation != '')  {
112             to.addClass(animation + ' in current').one(animation_end, _animation_callback);
113             from.addClass(animation + ' out');
114         } else {
115             to.addClass('current');
116             _animation_callback();
117         }
118         $('#loading').css('display', 'none').height(0);
119
120         function _animation_callback() {
121             from.remove();
122             to.removeClass('in reverse slide');
123             adjust_height();
124             setTimeout(scrollTo, 300, 0, 0);
125
126             $('.internal').one('click', function() {
127                 var anim = ($(this).hasClass('no_anime') ? '' : $(this).hasClass('reverse') ? 'slide reverse' : 'slide');
128                 inner_reload($(this).attr('rel'), anim);
129                 return false;
130             });
131
132             $('.internal_form').bind('submit', function () {
133                 $('#m').blur();
134                 inner_reload($(this).attr('action'), '', $(this).serializeArray());
135                 return false;
136             });
137
138             $('.menu_button').bind('click', function() {
139                 $('.floaty').toggle();
140                 $('#send_location').forcus();
141                 return false;
142             });
143
144             if (!navigator.geolocation) {
145                 $('#post_location').css({'display': 'none'});
146             }
147
148             if ($('#map_canvas').length != 0) {
149                 adjust_map();
150                 if (map_load) {
151                     map_load = false
152                     $.getScript(web_root + 'geopost-keitairc.js', function() {
153                         append_location();
154                     });
155                 } else {
156                     append_location();
157                 }
158             }
159
160             if ($('#scroll').length != 0) {
161                 if (typeof myScroll != 'undefined' && myScroll != null) {
162                     myScroll.element.removeEventListener('touchstart', myScroll);
163                     myScroll.element.removeEventListener('touchmove', myScroll);
164                     myScroll.element.removeEventListener('touchend', myScroll);
165                     myScroll.element.removeEventListener('DOMSubtreeModified', myScroll);
166                     window.removeEventListener('orientationchange', myScroll);
167                     myScroll = null;
168                 }
169                 myScroll = new iScroll('scroll');
170                 var sc = $('#scroll');
171                 var mark = $('#reload_mark');
172                 if (mark.length != 0) {
173                     sc.bind('touchmove', function() {
174                         if (myScroll.y > 30) {
175                             mark.attr('reload', 'on').show();
176                         }
177                     });
178                     sc.bind('touchend', function() {
179                         mark.hide();
180                         if (mark.attr('reload') == 'on') {
181                             mark.removeAttr('reload');
182                             inner_reload(mark.attr('rel'), '');
183                         }
184                     });
185                 }
186                 $('.toolbar h1').bind('click', function() {
187                     myScroll.scrollTo(0,0);
188                 });
189             }
190         }
191     }
192 });