vendor: 21,946 bytes, lines 1-809
1/*jslint browser:true, devel:true*/ 2/*jslint unparam:true*/ 3/*global window, $, jQuery*/ 4 5/** 6 * jquery.jpostal.js ver2.7 7 * 8 * Copyright 2014, Aoki Makoto, Ninton G.K. http://www.ninton.co.jp 9 * 10 * Released under the MIT license - http://en.wikipedia.org/wiki/MIT_License 11 * 12 * Requirements 13 * jquery.js 14 */ 15var Jpostal = {}; 16 17Jpostal.Database = function () { 18 "use strict"; 19 20 this.address = []; // database cache 21 this.map = {}; 22 this.url = { 23 'http' : '//jpostal-1006.appspot.com/json/', 24 'https' : '//jpostal-1006.appspot.com/json/' 25 }; 26}; 27 28Jpostal.Database.prototype.find = function (i_postcode) { 29 "use strict"; 30 31 var address = []; 32 33 this.address.forEach(function (eachAddress) { 34 if (eachAddress[0] === '_' + i_postcode) { 35 address = eachAddress; 36 } 37 }); 38 39 return address; 40}; 41 42Jpostal.Database.prototype.get = function (i_postcode) { 43 "use strict"; 44 45 // -------------------------------------------------- 46 // i_postcode find() find() result 47 // 1234567 123 48 // -------------------------------------------------- 49 // 1 - - defaults 50 // 12 - - defaults 51 // 123 - Y find( '123' ) 52 // 123 - N defaults 53 // 1234 - Y find( '123' ) 54 // 1234 - N defaults 55 // 1234567 Y - find( '1234567' ) 56 // 1234567 N Y find( '123' ) 57 // 1234567 N N defaults 58 // -------------------------------------------------- 59 var defaults = ['', '', '', '', '', '', '', '', ''], 60 address, 61 head3; 62 63 switch (i_postcode.length) { 64 case 3: 65 case 4: 66 case 5: 67 case 6: 68 head3 = i_postcode.substr(0, 3); 69 address = this.find(head3); 70 address = $.extend(defaults, address); 71 break; 72 73 case 7: 74 address = this.find(i_postcode); 75 if (address.length === 0) { 76 head3 = i_postcode.substr(0, 3); 77 address = this.find(head3); 78 } 79 address = $.extend(defaults, address); 80 break; 81 82 default: 83 address = defaults; 84 break; 85 } 86 87 return address; 88}; 89 90Jpostal.Database.prototype.getUrl = function (i_head3) { 91 "use strict"; 92 93 var url = ''; 94 95 switch (this.getProtocol()) { 96 case 'http:': 97 url = this.url.http; 98 break; 99 100 case 'https:': 101 url = this.url.https; 102 break; 103 } 104 url = url + i_head3 + '.json'; 105 106 try { 107 url = url + '?referer=' + encodeURIComponent(window.location.href); 108 } catch (e) {} 109 110 return url; 111}; 112 113Jpostal.Database.prototype.request = function (i_postcode, i_callback) { 114 "use strict"; 115 116 var head3, 117 url, 118 options; 119 120 head3 = i_postcode.substr(0, 3); 121 122 if (i_postcode.length <= 2 || this.getStatus(head3) !== 'none' || head3.match(/\D/)) { 123 return false; 124 } 125 this.setStatus(head3, 'waiting'); 126 127 url = this.getUrl(head3); 128 129 options = { 130 async : true, 131 dataType : 'jsonp', 132 jsonpCallback : 'jQuery_jpostal_callback', 133 type : 'GET', 134 url : url, 135 success : function () { // function(i_data, i_dataType 136 i_callback(); 137 }, 138 timeout : 5000 // msec 139 }; 140 this.ajax(options); 141 return true; 142}; 143 144Jpostal.Database.prototype.ajax = function (options) { 145 "use strict"; 146 147 $.ajax(options); 148}; 149 150Jpostal.Database.prototype.save = function (i_data) { 151 "use strict"; 152 153 var that = this; 154 155 i_data.forEach(function (rcd) { 156 var postcode = rcd[0]; 157 158 if (that.map[postcode] === undefined) { 159 that.address.push(rcd); 160 that.map[postcode] = {state : 'complete', time : 0}; 161 } else if (that.map[postcode].state === 'waiting') { 162 that.address.push(rcd); 163 that.map[postcode].state = 'complete'; 164 } 165 }); 166}; 167 168Jpostal.Database.prototype.getStatus = function (i_postcode) { 169 "use strict"; 170 171 // -------------------------------------------------- 172 // # ['_001'] ..state .time result 173 // -------------------------------------------------- 174 // 1 =undefined - - none 175 // 2 !=undefined 'complete' - complete 176 // 3 !=undefined 'waiting' <5sec waiting 177 // 4 !=undefined 'waiting' >=5sec none 178 // -------------------------------------------------- 179 var st = '', 180 postcode = '_' + i_postcode, 181 t_ms; 182 183 if (this.map[postcode] === undefined) { 184 // # 1 185 st = 'none'; 186 187 } else if ('complete' === this.map[postcode].state) { 188 // # 2 189 st = 'complete'; 190 191 } else { 192 t_ms = this.getTime() - this.map[postcode].time; 193 if (t_ms < 5000) { 194 // # 3 195 st = 'waiting'; 196 197 } else { 198 // # 4 199 st = 'none'; 200 } 201 } 202 203 return st; 204}; 205 206Jpostal.Database.prototype.setStatus = function (i_postcode) { 207 "use strict"; 208 209 var postcode = '_' + i_postcode; 210 211 if (this.map[postcode] === undefined) { 212 this.map[postcode] = { 213 state : 'waiting', 214 time : 0 215 }; 216 } 217 218 this.map[postcode].time = this.getTime(); 219}; 220 221Jpostal.Database.prototype.getProtocol = function () { 222 "use strict"; 223 224 return window.location.protocol; 225}; 226 227Jpostal.Database.prototype.getTime = function () { 228 "use strict"; 229 230 return (new Date()).getTime(); 231}; 232 233(function () { 234 "use strict"; 235 236 var instance; 237 238 Jpostal.Database.getInstance = function () { 239 if (instance === undefined) { 240 instance = new Jpostal.Database(); 241 } 242 return instance; 243 }; 244}()); 245 246Jpostal.Jpostal = function (i_JposDb) { 247 "use strict"; 248 249 this.address = ''; 250 this.jposDb = i_JposDb; 251 this.options = {}; 252 this.postcode = ''; 253 this.minLen = 3; 254}; 255 256Jpostal.Jpostal.prototype.displayAddress = function () { 257 "use strict"; 258 259 var that = this; 260 261 if (this.postcode === '000info') { 262 this.address[2] += ' ' + this.getScriptSrc(); 263 } 264 265 Object.keys(this.options.address).forEach(function (key) { 266 var format = that.options.address[key], 267 value = that.formatAddress(format, that.address); 268 269 if (that.isSelectTagForPrefecture(key, format)) { 270 that.setSelectTagForPrefecture(key, value); 271 } else { 272 $(key).val(value); 273 that.trigger(key); 274 } 275 }); 276}; 277 278Jpostal.Jpostal.prototype.isSelectTagForPrefecture = function (i_key, i_fmt) { 279 "use strict"; 280 281 // é½éåºçã®SELECTã¿ã°ãï¼ 282 var f; 283 284 switch (i_fmt) { 285 case '%3': 286 case '%p': 287 case '%prefecture': 288 if ($(i_key).get(0).tagName.toUpperCase() === 'SELECT') { 289 f = true; 290 } else { 291 f = false; 292 } 293 break; 294 295 default: 296 f = false; 297 break; 298 } 299 return f; 300}; 301 302Jpostal.Jpostal.prototype.setSelectTagForPrefecture = function (i_key, i_value) { 303 "use strict"; 304 305 var value, 306 el; 307 308 // é½éåºçã®SELECTã¿ã° 309 // ã±ã¼ã¹1: <option value="æ±äº¬é½">æ±äº¬é½</option> 310 $(i_key).val(i_value); 311 if ($(i_key).val() === i_value) { 312 this.trigger(i_key); 313 return; 314 } 315 316 // ã±ã¼ã¹2: valueãæ°å¤(èªæ²»ä½ã³ã¼ãã®å ´åãå¤ã) 317 // ããã¹ãããåæµ·éããå«ããã©ããã§å¤æãã 318 // <option value="01">åæµ·é(01)</option> 319 // <option value="1">1.åæµ·é</option> 320 value = ''; 321 el = $(i_key)[0]; 322 Object.keys(el.options).forEach(function (i) { 323 var p = String(el.options[i].text).indexOf(i_value); 324 if (0 <= p) { 325 value = el.options[i].value; 326 } 327 }); 328 329 if (value !== '') { 330 $(i_key).val(value); 331 this.trigger(i_key); 332 } 333 334}; 335 336Jpostal.Jpostal.prototype.trigger = function (i_key) { 337 "use strict"; 338 339 if (this.options.trigger === undefined || this.options.trigger[i_key] === undefined || this.options.trigger[i_key] === false) { 340 return; 341 } 342 $(i_key).trigger("change"); 343}; 344 345Jpostal.Jpostal.prototype.formatAddress = function (i_fmt, i_address) { 346 "use strict"; 347 348 var s = i_fmt, 349 that = this; 350 351 s = s.replace(/%3|%p|%prefecture/, i_address[1]); 352 s = s.replace(/%4|%c|%city/, i_address[2]); 353 s = s.replace(/%5|%t|%town/, i_address[3]); 354 s = s.replace(/%6|%a|%address/, i_address[4]); 355 s = s.replace(/%7|%n|%name/, i_address[5]); 356 357 s = s.replace(/%8/, i_address[6]); 358 s = s.replace(/%9/, i_address[7]); 359 s = s.replace(/%10/, i_address[8]); 360 361 s = s.replace(/%([ASHKV]+)8/, function (match, p1) { 362 return that.mb_convert_kana(i_address[6], p1); 363 }); 364 s = s.replace(/%([ASHKV]+)9/, function (match, p1) { 365 return that.mb_convert_kana(i_address[7], p1); 366 }); 367 s = s.replace(/%([ASHKV]+)10/, function (match, p1) { 368 return that.mb_convert_kana(i_address[8], p1); 369 }); 370 371 return s; 372}; 373 374Jpostal.Jpostal.prototype.mb_convert_kana = function (i_str, i_option) { 375 "use strict"; 376 377 var str = i_str, 378 i, 379 o, 380 funcs; 381 382 function tr(i_str, map) { 383 var reg = new RegExp("(" + Object.keys(map).join("|") + ")", "g"); 384 385 return i_str.replace(reg, function (s) { 386 return map[s]; 387 }); 388 } 389 390 funcs = { 391 A: function (i_str) { 392 var reg = /[A-Za-z0-9!#\$%&\(\)\*\+,\-\.\/:;<=>\?@\[\]\^_`\{\|\}]/g, 393 s; 394 395 s = i_str.replace(reg, function (s) { 396 return String.fromCharCode(s.charCodeAt(0) + 65248); 397 }); 398 399 return s; 400 }, 401 S: function (i_str) { 402 return i_str.replace(/\u0020/g, '\u3000'); 403 }, 404 H: function (i_str) { 405 var map = { 406 "ï½±": "ã", 407 "ï½²": "ã", 408 "ï½³": "ã", 409 "ï½´": "ã", 410 "ï½µ": "ã", 411 "ï½¶": "ã", 412 "ï½·": "ã", 413 "ク": "ã", 414 "ï½¹": "ã", 415 "コ": "ã", 416 "ï½»": "ã", 417 "ï½¼": "ã", 418 "ï½½": "ã", 419 "ï½¾": "ã", 420 "ソ": "ã", 421 "ï¾": "ã", 422 "ï¾": "ã¡", 423 "ï¾": "ã¤", 424 "ï¾": "ã¦", 425 "ï¾": "ã¨", 426 "ï¾ ": "ãª", 427 "ï¾": "ã«", 428 "ï¾": "ã¬", 429 "ï¾": "ã", 430 "ï¾": "ã®", 431 "ï¾": "ã¯", 432 "ï¾": "ã²", 433 "ï¾": "ãµ", 434 "ï¾": "ã¸", 435 "ï¾": "ã»", 436 "ï¾": "ã¾", 437 "ï¾": "ã¿", 438 "ï¾": "ã", 439 "ï¾": "ã", 440 "ï¾": "ã", 441 "ï¾": "ã", 442 "ï¾": "ã", 443 "ï¾": "ã", 444 "ï¾": "ã", 445 "ï¾": "ã", 446 "ï¾": "ã", 447 "ï¾": "ã", 448 "ï¾": "ã", 449 "ï¾": "ã", 450 "ヲ": "ã", 451 "ï¾": "ã", 452 "ï½§": "ã", 453 "ィ": "ã", 454 "ゥ": "ã ", 455 "ェ": "ã", 456 "ォ": "ã", 457 "ッ": "ã£", 458 "ャ": "ã", 459 "ï½": "ã ", 460 "ï½®": "ã", 461 "。": "ã", 462 "、": "ã", 463 "ï½°": "ã¼", 464 "ï½¢": "ã", 465 "ï½£": "ã", 466 "ï½¥": "ã»", 467 "ï¾": "ã", 468 "ï¾": "ã" 469 }; 470 return tr(i_str, map); 471 }, 472 K: function (i_str) { 473 var map = { 474 "ï½±": "ã¢", 475 "ï½²": "ã¤", 476 "ï½³": "ã¦", 477 "ï½´": "ã¨", 478 "ï½µ": "ãª", 479 "ï½¶": "ã«", 480 "ï½·": "ã", 481 "ク": "ã¯", 482 "ï½¹": "ã±", 483 "コ": "ã³", 484 "ï½»": "ãµ", 485 "ï½¼": "ã·", 486 "ï½½": "ã¹", 487 "ï½¾": "ã»", 488 "ソ": "ã½", 489 "ï¾": "ã¿", 490 "ï¾": "ã", 491 "ï¾": "ã", 492 "ï¾": "ã", 493 "ï¾": "ã", 494 "ï¾ ": "ã", 495 "ï¾": "ã", 496 "ï¾": "ã", 497 "ï¾": "ã", 498 "ï¾": "ã", 499 "ï¾": "ã", 500 "ï¾": "ã", 501 "ï¾": "ã", 502 "ï¾": "ã", 503 "ï¾": "ã", 504 "ï¾": "ã", 505 "ï¾": "ã", 506 "ï¾": "ã ", 507 "ï¾": "ã¡", 508 "ï¾": "ã¢", 509 "ï¾": "ã¤", 510 "ï¾": "ã¦", 511 "ï¾": "ã¨", 512 "ï¾": "ã©", 513 "ï¾": "ãª", 514 "ï¾": "ã«", 515 "ï¾": "ã¬", 516 "ï¾": "ã", 517 "ï¾": "ã¯", 518 "ヲ": "ã²", 519 "ï¾": "ã³", 520 "ï½§": "ã¡", 521 "ィ": "ã£", 522 "ゥ": "ã¥", 523 "ェ": "ã§", 524 "ォ": "ã©", 525 "ッ": "ã", 526 "ャ": "ã£", 527 "ï½": "ã¥", 528 "ï½®": "ã§", 529 "。": "ã", 530 "、": "ã", 531 "ï½°": "ã¼", 532 "ï½¢": "ã", 533 "ï½£": "ã", 534 "ï½¥": "ã»", 535 "ï¾": "ã", 536 "ï¾": "ã" 537 }; 538 return tr(i_str, map); 539 }, 540 V: function (i_str) { 541 var map = { 542 "ãã": "ã", 543 "ãã": "ã", 544 "ãã": "ã", 545 "ãã": "ã", 546 "ãã": "ã", 547 "ãã": "ã", 548 "ãã": "ã", 549 "ãã": "ã", 550 "ãã": "ã", 551 "ãã": "ã", 552 "ãã": "ã ", 553 "ã¡ã": "ã¢", 554 "ã¤ã": "ã¥", 555 "ã¦ã": "ã§", 556 "ã¨ã": "ã©", 557 "ã¯ã": "ã°", 558 "ã²ã": "ã³", 559 "ãµã": "ã¶", 560 "ã¸ã": "ã¹", 561 "ã»ã": "ã¼", 562 "ã¯ã": "ã±", 563 "ã²ã": "ã´", 564 "ãµã": "ã·", 565 "ã¸ã": "ãº", 566 "ã»ã": "ã½", 567 568 "ã«ã": "ã¬", 569 "ãã": "ã®", 570 "ã¯ã": "ã°", 571 "ã±ã": "ã²", 572 "ã³ã": "ã´", 573 "ãµã": "ã¶", 574 "ã·ã": "ã¸", 575 "ã¹ã": "ãº", 576 "ã»ã": "ã¼", 577 "ã½ã": "ã¾", 578 "ã¿ã": "ã", 579 "ãã": "ã", 580 "ãã": "ã ", 581 "ãã": "ã", 582 "ãã": "ã", 583 "ãã": "ã", 584 "ãã": "ã", 585 "ãã": "ã", 586 "ãã": "ã", 587 "ãã": "ã", 588 "ãã": "ã", 589 "ãã": "ã", 590 "ãã": "ã", 591 "ãã": "ã", 592 "ãã": "ã" 593 }; 594 return tr(i_str, map); 595 } 596 }; 597 598 for (i = 0; i < i_option.length; i += 1) { 599 o = i_option[i]; 600 str = funcs[o](str); 601 } 602 603 return str; 604}; 605 606Jpostal.Jpostal.prototype.getScriptSrc = function () { 607 "use strict"; 608 609 var src = '', 610 el_arr, 611 i, 612 n, 613 el_src; 614 615 el_arr = document.getElementsByTagName('script'); 616 n = el_arr.length; 617 for (i = 0; i < n; i += 1) { 618 el_src = el_arr[i].src; 619 if (0 <= el_src.indexOf("jquery.jpostal.js")) { 620 src = el_src; 621 break; 622 } 623 } 624 625 return src; 626}; 627 628Jpostal.Jpostal.prototype.init = function (i_options) { 629 "use strict"; 630 631 if (i_options.postcode === undefined) { 632 throw new Error('postcode undefined'); 633 } 634 if (i_options.address === undefined) { 635 throw new Error('address undefined'); 636 } 637 638 this.options.postcode = []; 639 if (typeof i_options.postcode === 'string') { 640 this.options.postcode.push(i_options.postcode); 641 } else { 642 this.options.postcode = i_options.postcode; 643 } 644 645 this.options.address = i_options.address; 646 647 if (i_options.url !== undefined) { 648 this.jposDb.url = i_options.url; 649 } 650 651 this.options.trigger = {}; 652 if (i_options.trigger !== undefined) { 653 this.options.trigger = i_options.trigger; 654 } 655}; 656 657Jpostal.Jpostal.prototype.main = function () { 658 "use strict"; 659 660 var that, 661 f; 662 663 this.scanPostcode(); 664 if (this.postcode.length < this.minLen) { 665 // git hub issue #4: éµä¾¿çªå·æ¬ã0ï½2æåã®ã¨ãã使æ¬ã空æ¬ã«ãããå ¥åå 容ãç¶æãã¦ã»ãã 666 return; 667 } 668 669 that = this; 670 f = this.jposDb.request(this.postcode, function () { 671 that.callback(); 672 }); 673 if (!f) { 674 this.callback(); 675 } 676}; 677 678Jpostal.Jpostal.prototype.callback = function () { 679 "use strict"; 680 681 this.address = this.jposDb.get(this.postcode); 682 this.displayAddress(); 683}; 684 685Jpostal.Jpostal.prototype.scanPostcode = function () { 686 "use strict"; 687 688 var s = '', 689 s3, 690 s4; 691 692 switch (this.options.postcode.length) { 693 case 0: 694 break; 695 696 case 1: 697 // github issue #8: 1ã¤ç®ã空æ¬ã2ã¤ç®ãã001ãã¨ãã¦ããã001ãã¨ãã¦åæµ·éæå¹å¸ã表示ãã¦ãã¾ã 698 // ---------------------------------------- 699 // case postcode result 700 // ---------------------------------------- 701 // 1 '' '' 702 // 1 12 '' 703 // 2 123 123 704 // 2 123- 123 705 // 2 123-4 123 706 // 3 123-4567 1234567 707 // 2 1234 123 708 // 4 1234567 1234567 709 // ---------------------------------------- 710 s = String($(this.options.postcode[0]).val()); 711 if (0 <= s.search(/^([0-9]{3})([0-9A-Za-z]{4})/)) { 712 // case 4 713 s = s.substr(0, 7); 714 } else if (0 <= s.search(/^([0-9]{3})-([0-9A-Za-z]{4})/)) { 715 // case 3 716 s = s.substr(0, 3) + s.substr(4, 4); 717 } else if (0 <= s.search(/^([0-9]{3})/)) { 718 // case 2 719 s = s.substr(0, 3); 720 } else { 721 // case 1 722 s = ''; 723 } 724 break; 725 726 case 2: 727 // github issue #8: 1ã¤ç®ã空æ¬ã2ã¤ç®ãã001ãã¨ãã¦ããã001ãã¨ãã¦åæµ·éæå¹å¸ã表示ãã¦ãã¾ã 728 // ---------------------------------------- 729 // case post1 post2 result 730 // ---------------------------------------- 731 // 1 '' --- '' 732 // 1 12 --- '' 733 // 2 123 '' 123 734 // 2 123 4 123 735 // 3 123 4567 1234567 736 // ---------------------------------------- 737 s3 = String($(this.options.postcode[0]).val()); 738 s4 = String($(this.options.postcode[1]).val()); 739 if (0 <= s3.search(/^[0-9]{3}$/)) { 740 if (0 <= s4.search(/^[0-9A-Za-z]{4}$/)) { 741 // case 3 742 s = s3 + s4; 743 } else { 744 // case 2 745 s = s3; 746 } 747 } else { 748 // case 1 749 s = ''; 750 } 751 break; 752 } 753 754 this.postcode = s; 755}; 756 757// MEMO: For the following reason, JposDb was put on the global scope, not local scope. 758// --------------------------------------------------------------------- 759// data file callback JposDb scope 760// --------------------------------------------------------------------- 761// 001.js JposDb.save global scope 762// 001.js.php $_GET['callback'] local scopde for function($){} 763// --------------------------------------------------------------------- 764window.jQuery_jpostal_callback = function (i_data) { 765 "use strict"; 766 767 Jpostal.Database.getInstance().save(i_data); 768}; 769 770 771(function (factory) { 772 "use strict"; 773 774 if (typeof module === "object" && typeof module.exports === "object") { 775 module.exports = factory(require("jquery"), window, document); 776 } else { 777 factory(jQuery, window, document); 778 } 779//}(function ($, window, document, undefined) { 780}(function ($) { 781 "use strict"; 782 783 $.fn.jpostal = function (i_options) { 784 var Jpos, 785 selector; 786 787 Jpos = new Jpostal.Jpostal(Jpostal.Database.getInstance()); 788 Jpos.init(i_options); 789 790 if (typeof i_options.click === 'string' && i_options.click !== '') { 791 $(i_options.click).bind('click', function () { 792 Jpos.main(); 793 }); 794 } else { 795 selector = Jpos.options.postcode[0]; 796 $(selector).bind('keyup change', function () { 797 Jpos.main(); 798 }); 799 800 if (1 <= Jpos.options.postcode.length) { 801 selector = Jpos.options.postcode[1]; 802 $(selector).bind('keyup change', function () { 803 Jpos.main(); 804 }); 805 } 806 } 807 }; 808 809}));
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.