if(typeof(jQuery) === "function") { (function($){ var u = new EPOS_FORM.Util(), zipAddress, _this; zipAddress = { ajaxErrorMessage:"接続できませんでした。おそれ入りますが、しばらくしてから再度お試し下さい", // ▼▼▼【修正1】スクロールを実行するか判定するためのフラグを追加 ▼▼▼ shouldScrollAfterClose: false, init:function(){ var html, urlPrefix; _this = this; // _this に zipAddress オブジェクトを格納 _this.urlPrefix = ""; $(_this.load); }, load:function(){ var ifReady; ifReady = true; _this.$zip2AddBtn = $(".js-searchAddressBtn"); _this.$add2ZipBtn = $(".js-searchZipBtn"); _this.$zipInput = $("#tbox_house_zip").eq(0); $.each([_this.$zip2AddBtn, _this.$add2ZipBtn, _this.$zipInput],function(){ if(this.length < 1 ) { ifReady = false; return false; } }); if(!ifReady){ return true; } // 「住所検索」「住所から検索」ボタンの既存クリックイベント再定義 _this.$zipInput.unbind("keyup").bind("keyup", function (e) { if(_this.$zipInput.val().length === 7){ _this.doZip2Address(e, _this.$zipInput.val()); } }); _this.$zip2AddBtn.removeAttr("onclick","").unbind("click").bind("click", function (e) { _this.doZip2Address(e, _this.$zipInput.val()); }); _this.$add2ZipBtn.removeAttr("onclick","").unbind("click").bind("click", function (e) { $.colorbox({ className:"cb_layout01", inline: true, opacity:0.5, width:"90%", height:"80%", href: $(_this.selector.modalOuter) }); $(_this.selector.modalInner).prepend('
読み込み中
'); _this.viewAddressInput(e); }); // モーダルのベース要素を生成 $([ '
', '
', '', '
', '
', '
', '
' ].join('')).appendTo($("body")); _this.selector = {}; _this.selector.modalOuter = ".cb_page_zipaddress"; _this.selector.modalInner = ".cb_main_zipaddress"; // ▼▼▼【修正2】モーダルが閉じた後の処理を、フラグチェックを含めてここに集約 ▼▼▼ // これにより、他のモーダルを閉じた際には意図しないスクロールが発生しなくなります。 $(document).bind('cbox_closed', function(){ $(_this.selector.modalInner).empty(); // 住所選択からのクローズの場合(フラグがtrue)のみスクロールを実行 if (_this.shouldScrollAfterClose) { // フラグをすぐにリセットして、次回以降のクローズでは実行させない _this.shouldScrollAfterClose = false; var position = $("#entry-customer-address").find(".entry-panel").offset().top; var speed = 300; var easing = "swing"; $("html, body").animate({ scrollTop: position }, speed, easing); setTimeout(function(){ $("#tbox_house_address2_kanji").focus().addClass("highlight"); },300); $("#tbox_house_address2_kana").addClass("highlight"); } }); // ▼▼▼【新規追加】画面読み込み時に値があれば再表示処理を呼び出す ▼▼▼ var initialZip = _this.$zipInput.val(); var searchedFlag = $("#tbox_zip_search_check").is(':checked'); if (initialZip && initialZip.length === 7 && searchedFlag) { _this.repopulateDisplay(initialZip); } // ▲▲▲【新規追加】画面読み込み時に値があれば再表示処理を呼び出す ▲▲▲ }, // ▼▼▼【新規追加】再表示処理の関数を新設 ▼▼▼ repopulateDisplay: function(zip) { var suppAddrKanji = $("#tbox_house_address2_kanji").val(); // 1. 先に確定している情報(郵便番号、番地)を表示する $("#addressReflect01").text(zip.replace(/(\d{3})(\d{4})/, "$1-$2")); $("#addressReflect03").text(suppAddrKanji); // 2. 郵便番号から住所の全体情報(都道府県など)をAjaxで再取得 $.ajax({ type:"GET", datatype:"xml", url : _this.urlPrefix + "/memberservice/pc/webservice/common/addresslist/byzip/" + zip, success:function (xml) { if($("addresslist_response result_code", xml).text() === "1" ){ // 住所が見つからない場合は、最低限の情報で表示 $("#address_kanji1 .readonly_box").text('住所を再検索してください'); $("#addressReflect02").text('(不明な住所)'); } else { // 3. 取得した情報で表示を更新する var pref = $('input[name="houseAddr.prefecNameKanji"]').val() || ''; var city = $('input[name="houseAddr.cityNameKanji"]').val() || ''; var town = $('input[name="houseAddr.townNameKanji"]').val() || ''; var section = $('input[name="houseAddr.formlSectionNameKanji"]').val() || ''; var sectionKanji = $('input#tbox_house_address2_kanji').val() || ''; // 連結 var fullAddress = pref + city + town + section; // #address_kanji1 .readonly_box にセット if (fullAddress) { $('#address_kanji1 .readonly_box').text(fullAddress); $("#addressReflect02").text(fullAddress); $("#addressReflect03").text(sectionKanji); } } // 4. 住所表示エリアを表示状態にする $("#entry-customer-address").fadeIn(); $("#entry-customer-address-kana").fadeIn(); // 5. 完了状態を示すクラスを付与 $('#anc05').addClass('input-complete'); $('#anc06').addClass('input-complete'); }, error:function () { console.error("Failed to re-fetch address data on page load."); // エラー時もエリアは表示しておく $("#entry-customer-address").fadeIn(); $("#entry-customer-address-kana").fadeIn(); } }); }, doZip2Address:function(e, zip, ifPart){ var errorText, sendZip; $.colorbox({ className:"cb_layout01", inline: true, opacity:0.5, width:"90%", height:"80%", href: $(_this.selector.modalOuter) }); $(_this.selector.modalInner).prepend('
読み込み中
'); errorText = ""; if (zip == "" || zip === "例)1234567") { errorText = "郵便番号を入力してください"; zip = ""; } else { if (!zip.match(/[^0-9]/g)) { if (zip.length != 7) { errorText = "郵便番号を正しく入力してください"; } } else { errorText = "郵便番号には数値を入力してください"; } } if(errorText != ""){ _this.viewZipInput(zip, errorText); } else { // 郵便番号の始めの3桁で再検索する場合 if(ifPart){ sendZip = String(zip).slice(0, 3); } else { sendZip = String(zip); } $.ajax({ type:"GET", datatype:"xml", url : _this.urlPrefix + "/memberservice/pc/webservice/common/addresslist/byzip/" + sendZip, success:function (xml) { if($("addresslist_response result_code", xml).text() === "1" ){ _this.viewZipInput(sendZip, "入力された郵便番号に該当する住所は存在しません。", true); } else { _this.viewAddressSelect(xml, sendZip); } }, error:function () { _this.viewZipInput(sendZip, _this.ajaxErrorMessage); } }); } e.preventDefault(); return false; }, viewZipInput:function(zip, errorText, ifPartZip){ var html; html = [ '

住所検索結果不一致

', '

', errorText, '

', '
', '', '
' ]; if(zip.length === 7 && ifPartZip === true){ html.push( '

おつとめ先の郵便番号が特定企業などに振られている個別郵便番号をお使いの場合は、同じ地区の郵便番号に変換しますので(郵便番号の始めの3桁で再検索します)、「再検索する」ボタンを押してください。

', '

再検索する

' ); } $(_this.selector.modalInner).fadeOut(function () { $(this).empty().append(html.join("")).fadeIn(function () { $.colorbox.resize({ width:"90%", height:"80%" }); }); $("#modalZip2AddBtn").bind("click", function (e) { return _this.doZip2Address(e, $("#modalZipInput").val()); }); if(zip.length === 7){ $("#modalPartZip2AddBtn").bind("click", function (e) { return _this.doZip2Address(e, $("#modalZipInput").val(), true); }); } }); }, viewAddressSelect:function(xml, zip){ var html; html =[ '

住所選択以下よりお選びください

', ( typeof(zip) === "undefined" ? "" : '

〒' + zip + '

'), '
', '', '
' ); $(_this.selector.modalInner).fadeOut(function () { $(this).empty().append(html.join("")).fadeIn(function () { $.colorbox.resize({ width:"90%", height:"80%" }); $("#modalAddressList li a").eq(0).focus(); }); $("#modalAddressList li a").bind("click", _this.setAddress); }); }, viewAddressInput:function(e, errorText, prefCode, address){ if(typeof(prefCode) === "undefined"){ prefCode = ""; } if(typeof(address) === "undefined"){ address = ""; } $.ajax({ type:"GET", datatype:"xml", url : _this.urlPrefix + "/memberservice/pc/webservice/common/prefecturelist", success:function (xml) { var html; html = [ '

住所からの検索

' ]; if($("prefecturelist_response.result_code", xml).text() === "1" ){ html.push('

通信エラー

'); } else { html.push( '

', errorText, '

', '
', '
都道府県を選択してください。
', '
', '', '
', '
', '
', '
市区郡町村・町名を入力してください。
', '
', '', '

例)千代田区丸の内
足柄下郡箱根町湯本

', '
', '
', '

検索

' ); } $(_this.selector.modalInner).fadeOut(function () { $(this).empty().append(html.join("")).fadeIn(function () { $.colorbox.resize({ width:"90%", height:"80%" }); }); $("#doAddress2Zip").bind("click", function (e) { _this.doAddress2Zip(e, $("#modalPrefSel").val(), $("#modalAddressInput").val()); }); }); }, error:function () { var html; html = [ '

住所からの検索

' ]; html.push('

',_this.ajaxErrorMessage,'

'); $(_this.selector.modalInner).fadeOut(function () { $(this).empty().append(html.join("")).fadeIn(function () { $.colorbox.resize({ width:"90%", height:"80%" }); }); $("#doAddress2Zip").bind("click", function (e) { _this.doAddress2Zip(e, $("#modalPrefSel").val(), $("#modalAddressInput").val()); }); }); } }); e.preventDefault(); return false; }, doAddress2Zip:function(e, prefCode, address){ var errorText, xml; errorText = ""; if(prefCode === ""){ errorText += "都道府県を選択してください。
"; } if(address === ""){ errorText += "市区町村を入力してください。
"; } if(errorText !== ""){ _this.viewAddressInput(e, errorText, prefCode, address); } else { // 住所取得リクエストのXML形式ボディ準備 xml = $.parseXML([ '', '', '', String(prefCode), '', '', String(address), '', '' ].join("")); $.ajax({ type:"POST", processData: false, contentType: "application/xml; charset=UTF-8", data: xml, datatype:"xml", url : _this.urlPrefix + "/memberservice/pc/webservice/common/addresslist/byaddr", success:function (xml) { if($("addresslist_response result_code", xml).text() === "1" ){ errorText = '入力された住所に該当する郵便番号は存在しません。
'; _this.viewAddressInput(e, errorText, prefCode, address); } else { _this.viewAddressSelect(xml); } }, error:function (xml) { _this.viewAddressInput(e, _this.ajaxErrorMessage, prefCode, address); } }); } e.preventDefault(); return false; }, setAddress:function () { var values, keys, data, html, labelAddressKanji, labelAddressKana, targetPositions; values = $(this).attr("title").split(":"); // 選択住所のvalue属性をパースし、画面内の住所情報書換えに備えてdataオブジェクトへ展開 // valueの例↓ // 13114009001:1640001:東京都:中野区:中野:中野:1:1丁目:トウキヨウト:ナカノク:ナカノ:ナカノ:1:1−:13 keys =[ "addrCode","addrCodeZipcode","prefecNameKanji","cityNameKanji","townNameKanji","formlTownNameKanji","sectionNameKanji","formlSectionNameKanji","prefecNameKana","cityNameKana","townNameKana","formlTownNameKana","sectionNameKana","formlSectionNameKana","prefecCode"]; data = {}; $.each(values, function (idx, value) { var key; key = keys[idx]; data[key] = value; }); // 住所と住所カナの行を挿入 labelAddressKanji = ""; $.each([2,3,4,7],function (i, idx) { var key = keys[idx]; labelAddressKanji += data[key]; }); labelAddressKana = ""; $.each([8,9,10,13],function (i, idx) { var key = keys[idx]; labelAddressKana += data[key]; }); $("#address_kanji1 .readonly_box").text(labelAddressKanji); $("#addressReflect02").text(labelAddressKanji); $("#address_kana1 .readonly_box").text(labelAddressKana); $("#tbox_house_address2_kanji").val("").parent().parent().removeClass("input-inputted"); $("#tbox_house_address2_kana").val("").parent().parent().removeClass("input-inputted"); $("#tbox_zip_search_check").prop("checked", true).change(); $("#entry-customer-address").fadeIn(); $("#entry-customer-address-kana").fadeIn(); // 送信値/隠し送信値更新 _this.$zipInput.val(data["addrCodeZipcode"]).parent().parent().addClass("input-inputted input-visited").removeClass("input-error"); $("#addressReflect01").text($("#tbox_house_zip").val().replace(/(\d{3})(\d{4})/,"$1-$2")); $.each(data, function (key, value) { $("input[type=hidden][name='houseAddr." + key + "']").val(value); }); // ▼▼▼【修正3】スクロール実行のフラグを立ててからモーダルを閉じる ▼▼▼ _this.shouldScrollAfterClose = true; $.colorbox.close(); // ▼▼▼【修正4】ここにあったスクロール処理はload()内のcbox_closedイベントに移動したため削除 ▼▼▼ } }; // ▼▼▼【修正5】グローバルスコープでの_this設定を削除し、init()内で実行するように変更 ▼▼▼ zipAddress.init(); }(jQuery)) };