$(function () { 'use strict'; if( !$('#main').hasClass('step2') ) return; var debug = location.hostname !== 'stworld.jp'; var PC = ( (window.innerWidth || document.documentElement.clientWidth) > 640 ); var base_path = location.pathname.replace(/\/+$/, ''); var uri_cities = base_path.replace(/.*\//, ''); var uri_city_codes = uri_cities.toUpperCase().split('_'); var max_city_word = base_path.replace(/\/[^/]+$/, '').replace(/.*\//, ''); var max_city = {two: 2, three: 3, four: 4, five: 5}[max_city_word]; var $contact = $('#main .contact'); $('#cities_wrapper li').each(function () { var code = $(this).data('code'); var code_part = code.split('-'); if ($.inArray(code_part[2], uri_city_codes) >= 0) { // URLに含まれる都市は消す $(this).remove(); } else { // リンク先URLセット var $link = $(this).children('a'); if (uri_city_codes.length < max_city - 1) { // リンク先は都市追加ページ var url = base_path + '_' + code_part[2].toLowerCase() + '#tab_wrapper'; $link.attr('href', url); } else { // リンク先はツアー検索結果 base_path = PC ? 'http://stworld.jp/tour_search_f2/#/' : 'http://i.stworld.jp/tour/searchresult'; var query = '?' + makeQueryString(stwTourSearchQuery(SELECTED_CITY_CODES.concat(code))); $link.attr('href', base_path + query); // リンク先にツアーがあるかどうか調べる debug && console.log(code,'ツアーがあるかAPIで調べます'); var url = API_TOUR_SEARCH_DEPARTURES + query; $.get(url, function (deps) { if (!stwDepartureExist(deps) && !$link.parent().hasClass('disable')) { $link.removeAttr('href').parent().addClass('disable').append($contact.clone()); debug && console.log(code,'ツアーなし'); } else{ debug && console.log(code,'ツアーあり'); } }); } } }); // ステップ.03~ if( max_city > 3 ){ if( PC ) $('#step_wrapper > .step > .num').text( uri_city_codes.length ); else $('#step_wrapper_sp > .step > .num').text( uri_city_codes.length ); } // 次の都市を検査 if (uri_city_codes.length < max_city - 1) { // 都市コード組み合わせ全パターン取得、都市の選択肢毎にツアーがあるかどうかAPI検査 // 選択肢の都市コードが含まれる組み合わせが1つでもツアーあれば他の組み合わせは検査不要 // APIコールができるだけ少なくなるよう考慮する $(window).load(function() { if (debug) { var count = 0; var start = (new Date).getTime(); } $.get(API_CITIES_COMBINATIONS + max_city_word, function (combinations) { debug && console.log('ぜんぶで', combinations.length, '通りの組み合わせがあります'); // 全組み合わせの中から指定の都市コード(複数)を持つ組み合わせを抽出 var combinations_filter = function (codes) { var filterd = []; for (var i = combinations.length; i--;) { if (ArrayContain(codes, combinations[i])) { filterd.unshift(combinations[i]); } } return filterd; }; // ツアー検索結果 var results = {}; // 都市の選択肢毎に $('#cities_wrapper li').each(function () { var $link = $(this); var codes = SELECTED_CITY_CODES.concat($link.data('code')); // 都市コードを含む周遊ツアーがあるかどうかAPI検査 var result; $link.on('toursearch', function () { result = null; // 自身のコードを含む組み合わせについて $.each(combinations_filter(codes), function () { var combination = this; var combination_codes = combination.join(','); if (combination_codes in results) { // API検査開始済み result = results[combination_codes]; if (result.ajax) { // API検査中、終了 return false; } else if (result.enable) { // ツアーあり、終了 return false; } else if (result.disable) { //debug && console.log(combination_codes,'ツアーなかったので次の組み合わせを調べます'); return true; } debug && console.log(combination_codes,'ツアーがあるかないかわかりません'); return true; } debug && console.log(combination_codes,'ツアーがあるかAPIで調べます'); result = {}; (function (result) { var query = '?' + makeQueryString(stwTourSearchQuery(combination)); var url = API_TOUR_SEARCH_DEPARTURES + query; result.ajax = $.ajax({ url: url, success: function (deps) { // API結果保存 if (stwDepartureExist(deps)) result.enable = true; // ツアーあり else result.disable = true; // ツアーなし }, complete: function () { result.ajax = null; if (debug) { var time = (new Date).getTime() - start; console.log(++count + '回目API完了 ' + (time / 1000) + ' s (' + query + ')'); } // 全体再チェック $('#cities_wrapper li').trigger('toursearch'); } }); })(result); results[combination_codes] = result; return false; }); if (result) { if (result.ajax) { debug && console.log(codes.join(','),'API検査中...'); } else if (result.enable) { debug && console.log(codes.join(','),'ツアーあり'); } else if (result.disable) { debug && console.log(codes.join(','),'ツアーなし'); if( !$link.hasClass('disable') ){ $link.addClass('disable').append($contact.clone()).children('a').removeAttr('href'); } } } }).trigger('toursearch'); }); }); }); } });