| 1 |
jQuery( function( $ ) { |
| 2 |
|
| 3 |
var table_class = 'tr'; |
| 4 |
|
| 5 |
/** |
| 6 |
* 初期化 |
| 7 |
* click_count はロードボタンを押すごとに加算。 |
| 8 |
* 検索ボックスが変更されるたびに 0 にリセットすること。 |
| 9 |
*/ |
| 10 |
$( '.smart-cf-meta-box .load-relation-post-types' ).closest( table_class ) |
| 11 |
.data( 'click_count', 0 ) |
| 12 |
.data( 'search_timer', null ) |
| 13 |
.data( 'recent_search_query', '' ); |
| 14 |
|
| 15 |
/** |
| 16 |
* 検索ボタン |
| 17 |
*/ |
| 18 |
$( document ).on( 'keyup', '.smart-cf-meta-box .search-input-post-types', function() { |
| 19 |
var parent = $( this ).closest( table_class ); |
| 20 |
var search_timer = parent.data( 'search_timer' ); |
| 21 |
clearTimeout( search_timer ); |
| 22 |
|
| 23 |
parent.data( 'click_count', 0 ); |
| 24 |
parent.find( '.smart-cf-relation-children-select ul li' ).remove(); |
| 25 |
|
| 26 |
var search_query = $( this ).val(); |
| 27 |
parent.data( 'recent_search_query', search_query ); |
| 28 |
parent.data( 'search_timer', setTimeout( function() { |
| 29 |
get_posts( { s: search_query }, parent ); |
| 30 |
}, 2000 ) ); |
| 31 |
} ); |
| 32 |
|
| 33 |
/** |
| 34 |
* 読み込みボタン |
| 35 |
*/ |
| 36 |
$( document ).on( 'click', '.smart-cf-meta-box .load-relation-post-types', function() { |
| 37 |
var parent = $( this ).closest( table_class ); |
| 38 |
var click_count = parent.data( 'click_count' ); |
| 39 |
click_count ++; |
| 40 |
parent.data( 'click_count', click_count ); |
| 41 |
var search_query = parent.data( 'recent_search_query' ); |
| 42 |
if ( search_query ) { |
| 43 |
get_posts( { s: search_query }, parent ); |
| 44 |
} else { |
| 45 |
get_posts( {}, parent ); |
| 46 |
} |
| 47 |
} ); |
| 48 |
|
| 49 |
/** |
| 50 |
* クエリ |
| 51 |
*/ |
| 52 |
function get_posts( args, table ) { |
| 53 |
var click_count = table.data( 'click_count' ); |
| 54 |
var post_types = table.find( '.smart-cf-relation-left' ).data( 'post-types' ); |
| 55 |
var btn_load = table.find( '.load-relation-post-types' ); |
| 56 |
var btn_load_text = btn_load.text(); |
| 57 |
btn_load.text( 'Now loading...' ); |
| 58 |
|
| 59 |
var nameEl = document.getElementById('smart-cf-field-name-data'); |
| 60 |
var name = ''; |
| 61 |
if (nameEl) { |
| 62 |
name = nameEl.getAttribute('data-js'); |
| 63 |
} |
| 64 |
|
| 65 |
args = $.extend( args, { |
| 66 |
action : smart_cf_relation_post_types.action, |
| 67 |
nonce : smart_cf_relation_post_types.nonce, |
| 68 |
click_count: click_count, |
| 69 |
post_types : post_types, |
| 70 |
field_name: name |
| 71 |
} ); |
| 72 |
$.post( |
| 73 |
smart_cf_relation_post_types.endpoint, |
| 74 |
args, |
| 75 |
function( response ) { |
| 76 |
btn_load.addClass( 'hide' ); |
| 77 |
$( response ).each( function( i, e ) { |
| 78 |
table.find( '.smart-cf-relation-children-select ul' ).append( |
| 79 |
$( '<li />' ) |
| 80 |
.attr( 'data-id', this.ID ) |
| 81 |
.attr( 'data-status', this.post_status ) |
| 82 |
.text( this.post_title ) |
| 83 |
); |
| 84 |
} ); |
| 85 |
|
| 86 |
btn_load.text( btn_load_text ); |
| 87 |
btn_load.removeClass( 'hide' ); |
| 88 |
} |
| 89 |
); |
| 90 |
return false; |
| 91 |
} |
| 92 |
} ); |
| 93 |
|