PageSourceSearch

https://printrider.jp/wp-content/themes/insatu/js/jquery.jpostal.js?ver=3.5-bp20260323

js printrider.jp collected 2026-10-07 23:02:30 UTC 21,946 bytes, 809 lines download raw bytes

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.