/* ================================================================ * JsonZip Copyright (c) 2006-2008 Kawasaki Yusuke Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ================================================================ */ if ( typeof(JsonpZip) == 'undefined' ) JsonpZip = {}; if ( typeof(JsonpZip.JSONP_BASE) == 'undefined' ) { JsonpZip.JSONP_BASE = 'http://jsonp-hosting.googlecode.com/svn/trunk/jsonpzip/jsonp/'; } JsonpZip.VERSION = '0.01'; JsonpZip.Base = function () { return this; }; /* ********************************************************* */ JsonpZip.Base.prototype.list_to_hash = function ( list, kcol, vcol ) { var hash = []; for( var i=0; i[$col] == $test } @list ) JsonpZip.Base.prototype.grep_multiple = function ( list, col, test ) { var out = []; for( var i=0; i[$col] == $test } @list )[0] JsonpZip.Base.prototype.grep_first = function ( list, col, test ) { for( var i=0; i[$col] =~ /^$test/ } @list )[0] JsonpZip.Base.prototype.match_first = function ( list, col, test ) { var ret; var max = -1; for( var i=0; i max ) { ret = list[i]; max = len; } } } return ret; } // 配列の各カラムに名前を振る JsonpZip.Base.prototype.map_title = function ( list, title ) { var out = []; for( var i=0; i=this.default_length; i-- ) { if ( ! opts[i] ) continue; opts[i].parentNode.removeChild( opts[i] ); } if ( ! listtxt ) return; for( var i=0; i 0 ) { pos1 += PREFIX.length; var column = rel.substr( pos1, pos2-pos1 ); var input = new JsonpZip.Element( elem, column, onchange ); if ( ! input ) continue; this.input[column] = input; cnt ++; if ( column == 'pref' ) this.init_preflist(); if ( column == 'city' ) this.init_citylist(); if ( column == 'area' ) this.init_arealist(); } } if ( ! cnt ) return; return this; }; JsonpZip.Form.prototype = new JsonpZip.Base(); JsonpZip.Form.prototype.init_preflist = function ( chain ) { if ( ! this.input.pref.type_select ) return; if ( this.input.pref.default_length > 1 ) return; var self = this; var next = function ( list ) { var txtlist = self.map_column( list, 'pref' ); self.input.pref.init_options( txtlist, txtlist ); if ( chain ) chain(); }; this.jsonpzip.get_preflist( next ); } JsonpZip.Form.prototype.init_citylist = function ( chain ) { if ( ! this.input.city.type_select ) return; if ( this.input.city.default_length > 1 ) return; var self = this; var next = function ( list ) { var txtlist = self.map_column( list, 'city' ); self.input.city.init_options( txtlist, txtlist ); if ( chain ) chain(); }; var pref = this.input.pref.get_value(); if ( ! pref ) return next( [] ); this.jsonpzip.get_citylist_by_pref( next, pref ); } JsonpZip.Form.prototype.init_arealist = function ( chain ) { if ( ! this.input.area.type_select ) return; if ( this.input.area.default_length > 1 ) return; var self = this; var next = function ( list ) { self.input.area.init_options( list, list ); if ( chain ) chain(); }; var addr = this.input.city.get_value(); if ( ! addr ) return next( [] ); if ( this.input.pref ) addr = this.input.pref.get_value() + addr; this.jsonpzip.get_arealist_by_addr( next, addr ); } // フォームに入力されている郵便番号を取得 JsonpZip.Form.prototype.read_zipcd = function () { var zip7 = ""; if ( this.input.zip7 ) { zip7 = this.input.zip7.get_value(); } else if ( this.input.zip3 && this.input.zip4 ) { zip7 = this.input.zip3.get_value() + this.input.zip4.get_value(); } zip7 = zip7.replace( /0/g, "0" ).replace( /1/g, "1" ).replace( /2/g, "2" ); zip7 = zip7.replace( /3/g, "3" ).replace( /4/g, "4" ).replace( /5/g, "5" ); zip7 = zip7.replace( /6/g, "6" ).replace( /7/g, "7" ).replace( /8/g, "8" ); zip7 = zip7.replace( /9/g, "9" ); zip7 = zip7.replace( /[^0-9]/g, "" ); return zip7; }; // 指定された郵便番号をフォームに入力 JsonpZip.Form.prototype.write_zipcd = function ( zip7 ) { var zip3 = zip7.substr( 0, 3 ); var zip4 = zip7.substr( 3, 4 ); if ( this.input.zip7 ) { var maxlen = this.input.zip7.elem.getAttribute( 'maxlength' ); if ( maxlen == 8 ) zip7 = zip3 + '-' + zip4; this.input.zip7.set_value( zip7 ); } else if ( this.input.zip3 && this.input.zip4 ) { this.input.zip3.set_value( zip3 ); this.input.zip4.set_value( zip4 ); } }; // フォームに入力されている住所を取得 JsonpZip.Form.prototype.read_addr = function () { var addr = ""; if ( this.input.pref ) { addr = this.input.pref.get_value(); } if ( this.input.addr ) { addr += this.input.addr.get_value(); } else { if ( this.input.city ) addr += this.input.city.get_value(); if ( this.input.area ) addr += this.input.area.get_value(); if ( this.input.strt ) addr += this.input.strt.get_value(); } return addr; }; // 指定された住所オブジェクトから住所フォームを入力 JsonpZip.Form.prototype.write_addr = function ( data ) { if ( this.input.pref ) { this.input.pref.set_value( data.pref ); } if ( this.input.addr ) { var addr = data.city + data.area + data.strt; if ( ! this.input.pref ) addr = data.pref + addr; this.input.addr.set_value( addr ); // this.input.addr.elem.focus(); } else { var self = this; var next3 = function () { if ( self.input.strt ) { self.input.strt.set_value( data.strt ); // self.input.strt.elem.focus(); } else { // self.input.area.elem.focus(); } }; var next2 = function () { if ( self.input.area ) { self.write_area( data.area, next3 ); } }; if ( this.input.city ) { this.write_city( data.city, next2 ); } } }; JsonpZip.Form.prototype.write_strt = function ( strt, chain ) { if ( ! this.input.strt ) return; this.input.strt.set_value( strt ); if( chain ) chain(); }; JsonpZip.Form.prototype.write_area = function ( area, chain ) { if ( ! this.input.area ) return; var self = this; var next = function () { self.input.area.set_value( area ); if( chain ) chain(); }; if ( this.input.area.type_select ) { this.init_arealist( next ); } else { next(); } }; JsonpZip.Form.prototype.write_city = function ( city, chain ) { if ( ! this.input.city ) return; var self = this; var next = function () { self.input.city.set_value( city ); if( chain ) chain(); }; if ( this.input.city.type_select ) { this.init_citylist( next ); } else { next(); } }; JsonpZip.Form.prototype.onChange = function ( input, chain ) { var self = this; // 都道府県が変更になり、かつ市区町村がプルダウン形式の場合、 // 市区町村のプルダウンを更新してから、再度 onChange を呼び直す if ( input.column == 'pref' && this.input.city ) { if ( this.input.city.type_select ) { var next5 = function () { self.onChange( self.input.city, chain ); }; return this.init_citylist( next5 ); } } // 市区町村が変更になり、かつ大字町域がプルダウン形式の場合、 // 大字町域のプルダウンを更新してから、再度 onChange を呼び直す if ( input.column == 'city' && this.input.area ) { if ( this.input.area.type_select ) { var next4 = function ( list ) { self.onChange( self.input.area, chain ); }; return this.init_arealist( next4 ); } } var zip7 = this.read_zipcd(); if ( ! zip7 ) zip7 = ""; var addr = this.read_addr(); if ( ! addr ) addr = ""; // 更新のあったカラム種別が zip3,zip4,zip7 の場合 // 郵便番号 ⇒ 住所自動入力 if ( input.column.substr(0,3) == 'zip' ) { // 最終回と同じ(変更がない)場合は無視する if ( zip7 == this.last_zipenter ) return; this.last_zipenter = zip7; // 郵便番号が7桁でない場合は無視する if ( zip7.length != 7 ) return; // 住所が一部でも手入力されていれば無視する var text = ""; if ( this.input.pref && this.input.pref.type_text ) text += this.input.pref.get_value(); if ( this.input.city && this.input.city.type_text ) text += this.input.city.get_value(); if ( this.input.area && this.input.area.type_text ) text += this.input.area.get_value(); if ( this.input.strt && this.input.strt.type_text ) text += this.input.strt.get_value(); if ( this.input.addr && this.input.addr.type_text ) text += this.input.addr.get_value(); if ( text.length > 0 ) return; // 住所を取得する var next1 = function ( data ) { var newaddr = data.pref + data.city + data.area + data.strt; self.write_addr( data ); if ( chain ) chain(); }; return this.jsonpzip.get_addr_by_zipcd( next1, zip7 ); } // 更新のあったカラム種別が zip3,zip4,zip7 以外の場合 // 住所 ⇒ 郵便番号自動入力モード else { // 既に郵便番号が入力済かつ、手動で更新されている場合は無視する if ( zip7.length == 7 && zip7 != this.last_zipauto ) return; // 住所が3文字以下(未入力 or 都道府県名のみ)の場合は無視する if ( ! addr ) return; if ( addr.length < 4 ) return; // 最終回と同じ(変更がない)場合は無視する if ( addr == this.last_addr ) return; this.last_addr = addr; // 郵便番号を取得する var next2 = function ( list ) { if ( list.length != 1 ) return; var newzip7 = list[0]; self.last_zipauto = newzip7; self.write_zipcd( newzip7 ); if ( chain ) chain(); }; return this.jsonpzip.get_ziplist_by_addr( next2, addr ); } }; /* ********************************************************* */ JsonpZip.Page = function () { // this.list = []; this.form = {}; var list = document.getElementsByTagName( 'form' ); for( var i=0; i は、JsonpZip.page.form.XXXX でアクセス可能 if ( list[i].name ) { this.form[list[i].name] = temp; } if ( list[i].id ) { this.form[list[i].id] = temp; } } // if ( ! this.list.length ) return; return this; }; JsonpZip.Page.prototype = new JsonpZip.Base(); /* ********************************************************* */ JsonpZip.master.callback({index:'preflist',ver:'1.1',data: [[1,"北海道"],[2,"青森県"],[3,"岩手県"],[4,"宮城県"],[5,"秋田県"],[6,"山形県"],[7,"福島県"],[8,"茨城県"],[9,"栃木県"],[10,"群馬県"],[11,"埼玉県"],[12,"千葉県"],[13,"東京都"],[14,"神奈川県"],[15,"新潟県"],[16,"富山県"],[17,"石川県"],[18,"福井県"],[19,"山梨県"],[20,"長野県"],[21,"岐阜県"],[22,"静岡県"],[23,"愛知県"],[24,"三重県"],[25,"滋賀県"],[26,"京都府"],[27,"大阪府"],[28,"兵庫県"],[29,"奈良県"],[30,"和歌山県"],[31,"鳥取県"],[32,"島根県"],[33,"岡山県"],[34,"広島県"],[35,"山口県"],[36,"徳島県"],[37,"香川県"],[38,"愛媛県"],[39,"高知県"],[40,"福岡県"],[41,"佐賀県"],[42,"長崎県"],[43,"熊本県"],[44,"大分県"],[45,"宮崎県"],[46,"鹿児島県"],[47,"沖縄県"]] }); /* jsonpzip-init.js **************************************** */ new function () { var init = function () { JsonpZip.page = new JsonpZip.Page(); }; if ( window.jQuery && jQuery.fn && jQuery.fn.ready ) { jQuery.fn.ready( init ); } else { setTimeout( init, 0 ); } } /* ********************************************************* */