PageSourceSearch

https://www.daiwalease.co.jp/cms/content/themes/daiwacorp/js/searchform.js

js daiwalease.co.jp collected 2026-09-24 09:34:45 UTC 15,303 bytes, 450 lines download raw bytes

1var query_string = '';
2var work_type = window.location.pathname.replace('/', '');
3
4// run search
5var jqxhr = null;
6var timeoutID = null;
7
8/**
9 * 検索実装
10 * ajax
11 */
12function run_search(popup, push, element, delay, ready) {
13    if ( 'number' != typeof delay ) {
14      delay = 400;
15    }
16    if (timeoutID) {
17      clearTimeout(timeoutID);
18      timeoutID = null;
19    }
20    if (jqxhr) jqxhr.abort();
21
22	$('#s_zero').hide();
23    $('#s_inquiry').show();
24    $('#work_post ul.list_cms').text('');
25
26    // URLの更新
27	update_url(push);
28
29    // 一覧初期化
30    var $result_list = $('#work_post ul.list_cms');
31    var $invisible_result_list = $('#invisible_work_post ul.list_cms');
32    if ( 0 == $result_list.length ) {
33        $result_list = $('<ul class="list list_cms col3 clearfix"></ul>');
34        $('#work_post').prepend($result_list);
35    }
36    if ( 0 == $invisible_result_list.length ) {
37        $invisible_result_list = $('<ul class="list list_cms col3"></ul>');
38        $('#invisible_work_post').prepend($invisible_result_list);
39    }
40    $result_list.children('li').appendTo($invisible_result_list);
41    
42    // 検索実行
43    var search_params = upd_tb_parseQuery(query_string);
44    search_params.service_cat = (search_params.service_cat ? search_params.service_cat.split(' ') : "")
45
46    function run_ajax() {
47        jqxhr = $.ajax({
48            type: 'POST',
49            url: ajaxurl,
50            data: {
51                action: 'for_search_form_action', //function.php
52                work_type: work_type,
53                posts_per_page: 15,  //TODO 15
54                taxonomy: "service_cat",
55                terms: search_params.service_cat,
56                paged: (search_params.pg ? search_params.pg : 1),
57                keyword: (search_params.keyword ? search_params.keyword : ""),
58				post_status: "publish"
59            },
60            timeout: 8000
61        }).done( function ( response ) {
62            jsonData = JSON.parse( response );
63
64            $('#s_fail').hide();
65            $('.search_found').html('全<span>'+jsonData.num+'</span>件'); //.show();
66            if ( !jsonData.num ) {
67                $('#s_zero').show();
68            }
69            
70            // Pagination設定
71            createHtmlPagination(jsonData);
72            $('.paginate a').click(function() {
73                var page = $(this).data('page');
74                if ( 'number' == typeof page ) {
75                    var $page = $('.works-search-form input[name="pg"]');
76                    $page.val(page);
77                    $('html, body').animate({scrollTop: 0});    //TopへScroll
78                    run_search(false, true, this);
79                    return false;
80                }
81            });
82                
83            // 一覧
84            $.each( jsonData.posts, function( key, post ){
85                createHtmlFromResultList(post, $invisible_result_list, $result_list);
86            });
87            $('#s_inquiry').hide();
88
89        }).fail( function ( response ) {
90            $('#s_fail').show();
91            $('#s_inquiry').hide();
92        });
93    }
94    timeoutID = setTimeout(run_ajax, delay);
95}
96
97/**
98 * 一覧生成
99 * @param {*} data                      データ
100 * @param {*} $invisible_result_list    一時一覧
101 * @param {*} $result_list              一覧
102 */
103function createHtmlFromResultList(data, $invisible_result_list, $result_list) {
104    var data_id = data.post_id;
105    var data_url = data.post_permalink;
106    var data_thumbnail = data.post_thumbnail;
107    var data_title = data.post_title;
108    var data_detail = data.post_content;
109    var data_terms = data.post_terms;
110    var $work = $invisible_result_list.children('#work-' + data_id);
111    if (!$work.length) {
112        $work = $('<li class="item" id="work-' + data_id + '"></li>');
113        if (!data_thumbnail) data_thumbnail = "/images/common/dummy.jpg"; //dummyイメージ追加(20200110)
114        //$work.append('<div class="item_thumb"><a href="' + data_url + '"><img src="' + data_thumbnail + '"></a></div>');
115        $work.append('<a href="' + data_url + '"><div class="item_thumb"><div style="background-image:url(' + data_thumbnail + ')"></div></div></a>');
116        var $item_details = $('<div class="item_details"></div>');
117        var $item_cat = $('<p class="item_cat"></p>');
118        if (data_terms.length) {
119            data_terms.forEach(function (term) {
120                $item_cat.append($('<span>' + term.name + '</span>'));
121            });
122        }
123        $item_details.append($item_cat);
124        $item_details.append('<p class="item_ttl">' + data_title + '</p>');
125        $item_details.append('<p class="item_details">' + data_detail + '</p>');
126        $work.append($item_details);
127    }
128    $work.appendTo($result_list);
129}
130
131/**
132 * Pagination設定
133 */
134function createHtmlPagination(jsonData) {
135    $page_wrap = $('.paginate');
136    $page_wrap.empty();
137    var num_page = jsonData.num_page;
138
139    var urls = url_without_page();
140    var current_page = $('.works-search-form input[name="pg"]').first().val();
141    current_page = ('' == current_page) ? 1 : parseInt(current_page);
142    function get_paginate_object(i) {
143        if (0 == i) {
144            $page_wrap.append($('<span class="abbr">…</span>'));
145        }
146        else if (current_page == i) {
147            if (1 == i) {
148                $page_wrap.append($('<a href="' + urls.wo_pg + '" class="cu" data-page="' + i + '">' + i + '</a>'));
149            }
150            else {
151                $page_wrap.append($('<a href="' + urls.wo_pgnum + i + '"  class="cu" data-page="' + i + '">' + i + '</a>'));
152            }
153        }
154        else {
155            if (1 == i) {
156                $page_wrap.append($('<a href="' + urls.wo_pg + '" data-page="' + i + '">' + i + '</a>'));
157            }
158            else {
159                $page_wrap.append($('<a href="' + urls.wo_pgnum + i + '" data-page="' + i + '">' + i + '</a>'));
160            }
161        }
162    }
163
164    if (1 < num_page) {
165        get_paginate_object(1);
166        if (4 == current_page)
167            get_paginate_object(2);
168        else if (4 < current_page)
169            get_paginate_object(0);
170        for (var i = Math.max(current_page - 1, 2); i <= Math.min(current_page + 1, num_page - 1); i++) {
171            get_paginate_object(i);
172        }
173        if (num_page - 3 == current_page)
174            get_paginate_object(num_page - 1);
175        else if (num_page - 3 > current_page)
176            get_paginate_object(0);
177        get_paginate_object(num_page);
178        $page_wrap.show();
179    }
180    else {
181        //1ページの場合は非表示
182        $page_wrap.hide();
183        // get_paginate_object(1);
184    }
185}
186
187/**
188 *URLの更新
189 * 
190 */
191function update_url(push) {
192    var url = window.location.pathname;
193	var queries = [];
194	var parent_terms = [];
195	var childern_terms = [];
196	var terms = [];
197    var $form = $('.works-search-form').first();
198	var $keywords = $form.find('.keywords input[name="s"]');
199	var $page = $form.find('input[name="pg"]');
200
201	var $terms_parents = $form.find('.conditions select.terms_parents');
202    if ( $terms_parents.length ) {
203		if ( 'none' != $terms_parents.val() ) {
204            parent_terms.push($terms_parents.val());
205		}
206	}
207
208	parent_terms.forEach(function(parent_term) {
209        var child_exists = false;
210        $form.find('.'+parent_term+'-children input').each(function() {
211            if ( $(this).prop('checked') ) {
212                child_exists = true;
213                terms.push($(this).val());
214              }
215		});
216		if (!child_exists) {
217    		terms.push(parent_term);
218		}
219	});
220	if ( terms.length ) {
221		queries.push('service_cat='+terms.join('+'));
222	}
223
224	if ( $keywords.length ) {
225		if ( '' != $keywords.val() ) {
226            $keywords.val($keywords.val().replace(/ /g, ' '));
227            queries.push('keyword=' + encodeURI($keywords.val().replace(/\+/g, '%2B')));
228		}
229	}
230
231	if ( $page.length ) {
232		if ( '' != $page.val() ) {
233            if ( '1' != $page.val() ) {
234                queries.push('pg='+$page.val());
235            }
236		}
237	}
238
239	query_string = queries.join('&');
240	if ( queries.length ) {
241		url += '?'+query_string;
242    }
243    
244	if ( ('pushState' in window.history) && (window.history.pushState !== null) ) {
245		if (push) {
246		    window.history.pushState(query_string, '', url);
247		} else {
248		    window.history.replaceState(query_string, '', url);
249		}
250	}
251}
252
253/**
254 * URLからページを除外
255 */
256function url_without_page() {
257    var without_pg = window.location.search.replace(/&?pg=\d*/, '');
258    var without_pgnum = without_pg;
259    if ( 2 > without_pg.length ) {
260        without_pg = '';
261        without_pgnum = '?pg=';
262    } else {
263        without_pgnum += '&pg=';
264    }
265    without_pg = window.location.pathname + without_pg;
266    without_pgnum = window.location.pathname + without_pgnum;
267    return {wo_pg: without_pg, wo_pgnum: without_pgnum};
268}
269
270
271/**
272 * 子カテゴリリスト更新の更新
273 * @param {*} event 
274 */
275function update_cat_detail(event) {
276    var $parent_terms = $('.works-search-form .conditions select.terms_parents');
277    var $childrens_terms = $('.terms_childrens');
278    var $children_terms = $('.'+$(event.target).val()+'-children');
279
280    $parent_terms.each(function () {
281        $childrens_terms.parent().hide();
282        $childrens_terms.removeClass('active').addClass('inactive');
283    });
284
285    if ( $(event.target).val() ) {
286        $children_terms.removeClass('inactive').addClass('active');
287        $children_terms.parent().show();
288    }
289}
290
291
292/**
293 * クエリに従いフォームを更新
294 *
295 * @param {*} q 
296 */
297function update_form(q) {
298    var query = {};
299    var a = (q[0] === '?' ? q.substr(1) : q).split('&');
300    for (var i = 0; i < a.length; i++) {
301        var b = a[i].split('=');
302        query[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
303    }
304    //MOD #12 url path変更による対応
305    var type = work_type;
306    $('.works-search-form .conditions .terms_childrens input[type="checkbox"]').prop('checked', false);
307    var $keywords = $('.keywords input[name="s"]');
308    var $page = $('.works-search-form input[name="pg"]');
309    $page.val('');
310    $('.works-search-form .conditions select.terms_parents').val('none');
311
312    for (var tax in query) {
313        if (query.hasOwnProperty(tax)) {
314            switch (tax) {
315                case 'service_cat':
316                    query[tax].split('+').forEach(function(term) {
317                        var $option = $('.works-search-form .conditions select.terms_parents option[value="'+term+'"]');
318                        if ( $option.length ) {
319                            $option.addClass('narrow').parent().val(term).addClass('narrow');
320                        } else {
321                            var $child_checkbox = $('.works-search-form .conditions .terms_childrens input[type="checkbox"][name^="'+type+'_'+tax+'"][value="'+term+'"]');
322                            $child_checkbox.prop('checked', true);
323                            var service_cat_val = $child_checkbox.parent().parent().parent().data('parentServiceCat');
324                            $('.works-search-form .conditions select.terms_parents').val(service_cat_val).addClass('narrow').children('option[value="'+service_cat_val+'"]').addClass('narrow');
325                        }
326                    });
327                    break;
328                case 'keyword':
329                    $keywords.val(query[tax].replace(/\+/g, ' ').replace(/%2B/g, '+'));
330                    break;
331                case 'pg':
332                    $page.val(query[tax]);
333                    break;
334                default:
335                    $('select[name="'+work_type+'_'+tax+'"]').val(query[tax]).addClass('narrow').children('option[value="'+query[tax]+'"]').addClass('narrow');
336                }
337        }
338    }
339}
340
341
342
343/**
344 * 検索結果popup
345 * 
346 */
347function show_popup(msg) {
348    var $popup = $('<div class="popup" style="display: none;"></div>').append($('<div></div>').append($('<div></div>').html(msg)));
349    //var $popup = $('<div class="popup" style="display: none;"></div>').append($('<div></div>').html(msg)));
350    $('body').append($popup);
351
352    window.setTimeout(function() {
353        $popup.fadeOut('normal', function () {
354            $popup.remove();
355        });
356    }, 1000);
357
358    $popup.fadeIn();
359}
360
361
362/**
363 * 検索機能
364 */
365$(function(){
366    // state
367    $(window).on('popstate', function(e) {
368        if ( 'string' != typeof e.originalEvent.state ) return;
369        update_form(e.originalEvent.state);
370        $('.works-search-form .conditions select.terms_parents').each(function () {
371            update_cat_detail({target: this});
372        });
373        run_search(false, false, null, 0);
374    });
375
376    // キーワード
377    $('.works-search-form .keywords input[name="s"]').change(function() {
378        var self = this;
379        $('.advanced_search').find('.keywords input[name="s"]').each(function () {
380            if ( self != this) {
381                $(this).val($(self).val());
382            }
383        });
384    });
385    $('.works-search-form').submit(function(event) {
386        run_search(true, true, event.target);
387        return false;
388    });
389  
390    // クエリに従いフォームを更新
391    update_form(window.location.search);
392
393    // 子の表示を更新
394    var $form = $('.works-search-form');
395    $form.find('.conditions select.terms_parents').change(update_cat_detail);
396    $form.first().find('.conditions select.terms_parents').trigger('change');
397
398    //条件が変わったら再検索
399	$('.conditions input, .conditions select').on('change', function(){
400        var self = this;
401        if ( 'input' == $(this).prop('tagName').toLowerCase() ) {
402            $('.advanced_search').find('input[value="'+$(this).val()+'"]').each(function () {
403                if ( self != this) {
404                    $(this).prop('checked', $(self).prop('checked'));
405                }
406            });
407        } else {
408            $(this).children('option').removeClass('narrow');
409            //子チェックボックス初期化
410            $('.works-search-form .conditions .terms_childrens input[type="checkbox"]').prop('checked', false);
411            if ( 'none' == $(this).val() ) {
412                $(this).removeClass('narrow');
413            } else {
414                $(this).addClass('narrow').children('option[value="'+$(this).val()+'"]').addClass('narrow');
415            }
416        }
417    
418        var $page = $('.works-search-form input[name="pg"]');
419        $page.val('');
420        run_search(true, false, this);
421
422    });
423    run_search(false, false, null, 0, true);
424});
425
426
427/**
428 * htdocs\cms\wp-includes\js\thickbox\thickbox.js
429 * パラメタ値取得
430 * 
431 * @param {*} query 
432 */
433function upd_tb_parseQuery ( query ) {
434    var Params = {};
435    if ( ! query ) {return Params;}// return empty object
436    var Pairs = query.split(/[;&]/);
437    for ( var i = 0; i < Pairs.length; i++ ) {
438       var KeyVal = Pairs[i].split('=');
439       if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
440    //    //https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Predefined_Functions/escape_and_unescape_Functions
441    //    var key = unescape( KeyVal[0] );
442    //    var val = unescape( KeyVal[1] );
443       var key = decodeURI( KeyVal[0] );
444       var val = decodeURI( KeyVal[1] );
445       val = val.replace(/\+/g, ' ');
446       Params[key] = val;
447    }
448    return Params;
449 }
450 

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.