1/** 2 * jquery.jpostal.js ver2.2 3 * 4 * Copyright 2014, Aoki Makoto, Ninton G.K. http://www.ninton.co.jp 5 * 6 * Released under the MIT license - http://en.wikipedia.org/wiki/MIT_License 7 * 8 * Requirements 9 * jquery.js 10 */ 11function JpostalDatabase ( i_options ) { 12 this.address = []; // database cache 13 this.map = { 14 }; 15 this.url = { 16 // 'http' : '//jpostal.googlecode.com/svn/trunk/json/', 17 // 'https' : '//jpostal.googlecode.com/svn/trunk/json/' 18 19 'http' : '//jpostal-1006.appspot.com/json/', 20 'https' : '//jpostal-1006.appspot.com/json/', 21 }; 22 23 this.find = function ( i_postcode ) { 24 var address = []; 25 26 for ( var i = 0; i < this.address.length; ++i ) { 27 if ( this.address[i][0] == '_' + i_postcode ) { 28 address = this.address[i]; 29 } 30 } 31 32 return address; 33 }; 34 35 this.get = function ( i_postcode ) { 36 // -------------------------------------------------- 37 // i_postcode find() find() result 38 // 1234567 123 39 // -------------------------------------------------- 40 // 1 - - defaults 41 // 12 - - defaults 42 // 123 - Y find( '123' ) 43 // 123 - N defaults 44 // 1234 - Y find( '123' ) 45 // 1234 - N defaults 46 // 1234567 Y - find( '1234567' ) 47 // 1234567 N Y find( '123' ) 48 // 1234567 N N defaults 49 // -------------------------------------------------- 50 var defaults = ['', '', '', '', '', '', '', '', '']; 51 var address; 52 var head3; 53 54 switch ( i_postcode.length ) { 55 case 3: 56 case 4: 57 case 5: 58 case 6: 59 head3 = i_postcode.substr(0, 3); 60 address = this.find(head3); 61 address = $.extend( defaults, address ); 62 break; 63 64 case 7: 65 address = this.find(i_postcode); 66 if ( !address ) { 67 head3 = i_postcode.substr(0, 3); 68 address = this.find(head3); 69 } 70 address = $.extend( defaults, address ); 71 break; 72 73 default: 74 address = defaults; 75 break; 76 } 77 78 return address; 79 }; 80 81 this.getUrl = function ( i_head3 ) { 82 var url = ''; 83 84 switch ( window.location.protocol ) { 85 case 'http:': 86 url = this.url['http']; 87 break; 88 89 case 'https:': 90 url = this.url['https']; 91 break; 92 } 93 url = url + i_head3 + '.json'; 94 95 return url; 96 }; 97 98 this.request = function ( i_postcode, i_callback ) { 99 var _this = this; 100 var head3 = i_postcode.substr(0, 3); 101 102 if ( i_postcode.length <= 2 || this.getStatus(head3) != 'none' || head3.match(/[^0-9]/) ) { 103 return false; 104 } 105 this.setStatus(head3, 'waiting'); 106 107 var url = this.getUrl( head3 ); 108 109 var options = { 110 async : false, 111 dataType : 'jsonp', 112 jsonpCallback : 'jQuery_jpostal_callback', 113 type : 'GET', 114 url : url, 115 success : function(i_data, i_dataType) { 116 i_callback(); 117 }, 118 error : function(i_XMLHttpRequest, i_textStatus, i_errorThrown) { 119 }, 120 timeout : 5000 // msec 121 }; 122 $.ajax( options ); 123 return true; 124 }; 125 126 this.save = function ( i_data ) { 127 for ( var i = 0; i < i_data.length; ++i ) { 128 var rcd = i_data[i]; 129 var postcode = rcd[0]; 130 131 if ( typeof this.map[postcode] == 'undefined' ) { 132 this.address.push( rcd ); 133 this.map[postcode] = {state : 'complete', time : 0}; 134 } else if ( this.map[postcode].state == 'waiting' ) { 135 this.address.push( rcd ); 136 this.map[postcode].state = 'complete'; 137 } 138 } 139 }; 140 141 this.getStatus = function ( i_postcode ) { 142 // -------------------------------------------------- 143 // # ['_001'] ..state .time result 144 // -------------------------------------------------- 145 // 1 =undefined - - none 146 // 2 !=undefined 'complete' - complete 147 // 3 !=undefined 'waiting' <5sec waiting 148 // 4 !=undefined 'waiting' >=5sec none 149 // -------------------------------------------------- 150 var st = ''; 151 var postcode = '_' + i_postcode; 152 153 if ( typeof this.map[postcode] == 'undefined' ) { 154 // # 1 155 st = 'none'; 156 157 } else if ( 'complete' == this.map[postcode].state ) { 158 // # 2 159 st = 'complete'; 160 161 } else { 162 var t_ms = (new Date()).getTime() - this.map[postcode].time; 163 if ( t_ms < 5000 ) { 164 // # 3 165 st = 'waiting'; 166 167 } else { 168 // # 4 169 st = 'none'; 170 } 171 } 172 173 return st; 174 }; 175 176 this.setStatus = function ( i_postcode ) { 177 var postcode = '_' + i_postcode; 178 179 if ( typeof this.map[postcode] == 'undefined' ) { 180 this.map[postcode] = { 181 state : 'waiting', 182 time : 0 183 }; 184 } 185 186 this.map[postcode].time = (new Date()).getTime(); 187 }; 188 189} 190 191function Jpostal ( i_JposDb ) { 192 this.address = ''; 193 this.jposDb = i_JposDb; 194 this.options = {}; 195 this.postcode = ''; 196 this.minLen = 3; 197 198 this.displayAddress = function () { 199 if ( this.postcode == '000info') {
200 this.address[2] += ' ' + this.getScriptSrc(); 201 } 202 203 console.log( this.address ); 204 for ( var key in this.options.address ) { 205 var s = this.formatAddress( this.options.address[key], this.address ); 206 $(key).val( s ); 207 } 208 }; 209 210 this.formatAddress = function ( i_fmt, i_address ) { 211 var s = i_fmt; 212 213 s = s.replace( /%3|%p|%prefecture/, i_address[1] ); 214 s = s.replace( /%4|%c|%city/ , i_address[2] ); 215 s = s.replace( /%5|%t|%town/ , i_address[3] ); 216 s = s.replace( /%6|%a|%address/ , i_address[4] ); 217 s = s.replace( /%7|%n|%name/ , i_address[5] ); 218 219 s = s.replace( /%8/ , i_address[6] ); 220 s = s.replace( /%9/ , i_address[7] ); 221 s = s.replace( /%10/ , i_address[8] ); 222 223 return s; 224 }; 225 226 this.getScriptSrc = function () { 227 var src = ''; 228 229 var el_arr = document.getElementsByTagName('script'); 230 for ( var i = 0; i < el_arr.length; ++i ) { 231 if ( 0 <= el_arr[i].src.search(/jquery.jpostal.js/) ) { 232 src = el_arr[i].src; 233 } 234 } 235 236 return src; 237 } 238 239 this.init = function ( i_options ) { 240 241 if ( typeof i_options.postcode == 'undefined' ) { 242 throw new Error('postcode undefined'); 243 } 244 if ( typeof i_options.address == 'undefined' ) { 245 throw new Error('address undefined'); 246 } 247 248 this.options.postcode = []; 249 if ( typeof i_options.postcode == 'string' ) { 250 this.options.postcode.push( i_options.postcode ); 251 } else { 252 this.options.postcode = i_options.postcode; 253 } 254 255 this.options.address = i_options.address; 256 257 if ( typeof i_options.url != 'undefined' ) { 258 this.jposDb.url = i_options.url; 259 } 260 }; 261 262 this.main = function () { 263 this.scanPostcode(); 264 if ( this.postcode.length < this.minLen ) { 265 // git hub issue #4: é©ï½µèソé¡ï½ªè¿ï½·è°ºã»â²0ã»?2èã»ï½å±ã»ç¸ºï½¨ç¸ºéªÂâ½ï½½ä¹åè°ºã»ï½é¨ï½ºè°ºã»â縺å¸â 縲âã»èå¸ã»è³ï½¹ç¹§å ¤ï½¶ï½è¬âï¼ ç¸ºï½¦ç¸ºï½»ç¸ºå±ï¼ 266 return ; 267 } 268 269 var _this = this; 270 var f = this.jposDb.request( this.postcode, function () { 271 _this.callback(); 272 }); 273 if ( !f ) { 274 this.callback(); 275 } 276 }; 277 278 this.callback = function () { 279 this.address = this.jposDb.get( this.postcode ); 280 this.displayAddress(); 281 }; 282 283 this.scanPostcode = function () { 284 var s = ''; 285 286 switch ( this.options.postcode.length ) { 287 case 0: 288 break; 289 290 case 1: 291 // github issue #8: 1縺、é¶ï½®ç¹§å ¤ï½©ï½ºè°ºã»Â?2縺、é¶ï½®ç¹§åµÂ?001縲éªâ縺å±â»ç¹§ã ÂâÂ?001縲éªâ縺å±â»è¹ç²ï½µï½·é©æºæè·æï½¸ã ï½é¦ï½¨éコ縺å±â»ç¸ºå±âªç¸º? 292 // ---------------------------------------- 293 // case postcode result 294 // ---------------------------------------- 295 // 1 '' '' 296 // 1 12 '' 297 // 2 123 123 298 // 2 123- 123 299 // 2 123-4 123 300 // 3 123-4567 1234567 301 // 2 1234 123 302 // 4 1234567 1234567 303 // ---------------------------------------- 304 s = String($(this.options.postcode[0]).val()); 305 if ( 0 <= s.search(/^([0-9]{3})([0-9A-Za-z]{4})/) ) { 306 // case 4 307 s = RegExp.$1 + '' + RegExp.$2; 308 } else if ( 0 <= s.search(/^([0-9]{3})-([0-9A-Za-z]{4})/) ) { 309 // case 3 310 s = RegExp.$1 + '' + RegExp.$2; 311 } else if ( 0 <= s.search(/^([0-9]{3})/) ) { 312 // case 2 313 s = RegExp.$1; 314 } else { 315 // case 1 316 s = ''; 317 } 318 break; 319 320 case 2: 321 // github issue #8: 1縺、é¶ï½®ç¹§å ¤ï½©ï½ºè°ºã»Â?2縺、é¶ï½®ç¹§åµÂ?001縲éªâ縺å±â»ç¹§ã ÂâÂ?001縲éªâ縺å±â»è¹ç²ï½µï½·é©æºæè·æï½¸ã ï½é¦ï½¨éコ縺å±â»ç¸ºå±âªç¸º? 322 // ---------------------------------------- 323 // case post1 post2 result 324 // ---------------------------------------- 325 // 1 '' --- '' 326 // 1 12 --- '' 327 // 2 123 '' 123 328 // 2 123 4 123 329 // 3 123 4567 1234567 330 // ---------------------------------------- 331 var s3 = String($(this.options.postcode[0]).val()); 332 var s4 = String($(this.options.postcode[1]).val()); 333 if ( 0 <= s3.search(/^[0-9]{3}$/) ) { 334 if ( 0 <= s4.search(/^[0-9A-Za-z]{4}$/) ) { 335 // case 3 336 s = s3 + s4; 337 } else { 338 // case 2 339 s = s3; 340 } 341 } else { 342 // case 1 343 s = ''; 344 } 345 break; 346 } 347 348 this.postcode = s; 349 }; 350} 351 352// MEMO: For the following reason, JposDb was put on the global scope, not local scope. 353// --------------------------------------------------------------------- 354// data file callback JposDb scope 355// --------------------------------------------------------------------- 356// 001.js JposDb.save global scope 357// 001.js.php $_GET['callback'] local scopde for function($){} 358// --------------------------------------------------------------------- 359var JposDb = new JpostalDatabase(); 360 361function jQuery_jpostal_callback( i_data ) { 362 JposDb.save( i_data ); 363} 364 365(function($) { 366 $.fn.jpostal = function( i_options ){ 367 var Jpos = new Jpostal( JposDb ); 368 Jpos.init( i_options ); 369
370 if ( typeof i_options.click == 'string' && i_options.click != '' ) { 371 $(i_options.click).bind('click', function(e) { 372 Jpos.main(); 373 }); 374 } else { 375 for ( var i = 0; i < Jpos.options.postcode.length; ++i ) { 376 var selector = Jpos.options.postcode[i]; 377 $(selector).bind('keyup change', function(e) { 378 Jpos.main(); 379 }); 380 } 381 } 382 }; 383})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.