| 1 |
jQuery( function( $ ) { |
| 2 |
|
| 3 |
var table_class = 'tr'; |
| 4 |
|
| 5 |
/** |
| 6 |
* 選択肢 |
| 7 |
*/ |
| 8 |
var choices_li = '.smart-cf-relation-children-select li'; |
| 9 |
$( '.smart-cf-meta-box' ).on( 'click', choices_li, function() { |
| 10 |
var id = $( this ).data( 'id' ); |
| 11 |
var parent = $( this ).closest( table_class ); |
| 12 |
var limit = parent.find( '.smart-cf-relation-left' ).data( 'limit' ); |
| 13 |
|
| 14 |
if ( limit > 0 && limit <= parent.find( '.smart-cf-relation-right li' ).length ) { |
| 15 |
return true; |
| 16 |
} |
| 17 |
|
| 18 |
if ( parent.find( '.smart-cf-relation-right li[data-id="' + id + '"]' ).length !== 0 ) { |
| 19 |
return true; |
| 20 |
} |
| 21 |
|
| 22 |
var clone = $( this ).clone(); |
| 23 |
clone |
| 24 |
.prepend( $( '<span class="smart-cf-icon-handle dashicons dashicons-menu"></span>' ) ) |
| 25 |
.append( $( '<span class="relation-remove">-</span>' ) ); |
| 26 |
parent.find( '.smart-cf-relation-right ul' ).append( clone ); |
| 27 |
update_relation_value( $( this ).closest( 'tr' ) ); |
| 28 |
} ); |
| 29 |
|
| 30 |
/** |
| 31 |
* 選択済み� |
| 32 |
目の削除 |
| 33 |
*/ |
| 34 |
var relation_remove = '.smart-cf-relation-right li .relation-remove'; |
| 35 |
$( '.smart-cf-meta-box' ).on( 'click', relation_remove, function() { |
| 36 |
var tr = $( this ).closest( 'tr' ); |
| 37 |
$( this ).parent().remove(); |
| 38 |
update_relation_value( tr ); |
| 39 |
} ); |
| 40 |
|
| 41 |
/** |
| 42 |
* update_relation_value |
| 43 |
* @param dom tr |
| 44 |
*/ |
| 45 |
function update_relation_value( tr ) { |
| 46 |
var hidden = tr.find( 'input[type="hidden"]' ); |
| 47 |
hidden.each( function( i, e ) { |
| 48 |
if ( i !== 0 ) { |
| 49 |
$( this ).remove(); |
| 50 |
} |
| 51 |
} ); |
| 52 |
tr.find( '.smart-cf-relation-right li' ).each( function( i, e ) { |
| 53 |
var hidden_box = $( this ).closest( table_class ).find( '.smart-cf-relation-children-select' ); |
| 54 |
var id = $( this ).data( 'id' ); |
| 55 |
var clone = hidden.first().clone(); |
| 56 |
var name = clone.attr( 'name' ); |
| 57 |
clone.attr( 'name', name + '[]' ); |
| 58 |
clone.val( id ); |
| 59 |
hidden_box.append( clone ); |
| 60 |
} ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* sortable |
| 65 |
*/ |
| 66 |
$( '.smart-cf-meta-box' ).find( '.smart-cf-relation-right ul' ) |
| 67 |
.on( 'mousedown', function( event ) { |
| 68 |
event.stopPropagation(); |
| 69 |
} ) |
| 70 |
.sortable( { |
| 71 |
handle: '.smart-cf-icon-handle', |
| 72 |
update: function() { |
| 73 |
update_relation_value( $( this ).closest( 'tr' ) ); |
| 74 |
} |
| 75 |
} ); |
| 76 |
|
| 77 |
} ); |
| 78 |
|