| 1 |
jQuery( function( $ ) { |
| 2 |
|
| 3 |
$( '.smart-cf-meta-box' ).each( function( i, e ) { |
| 4 |
var wrapper = $( e ); |
| 5 |
var btn_add_repeat_group = wrapper.find( '.btn-add-repeat-group' ); |
| 6 |
var btn_remove_repeat_group = wrapper.find( '.btn-remove-repeat-group' ); |
| 7 |
var table_class = '.smart-cf-meta-box-table'; |
| 8 |
var cnt = wrapper.find( table_class ).length; |
| 9 |
|
| 10 |
/** |
| 11 |
* グループ追加ボタン |
| 12 |
*/ |
| 13 |
btn_add_repeat_group.click( function( e ) { |
| 14 |
cnt ++; |
| 15 |
var parent = $( this ).parents( '.smart-cf-meta-box-repeat-tables' ); |
| 16 |
add_repeat_group( $( this ) ); |
| 17 |
} ); |
| 18 |
|
| 19 |
/** |
| 20 |
* グループ削除ボタン |
| 21 |
*/ |
| 22 |
btn_remove_repeat_group.click( function() { |
| 23 |
var table = $( this ).parents( table_class ); |
| 24 |
table.fadeOut( 'fast', function() { |
| 25 |
$( this ).remove(); |
| 26 |
} ); |
| 27 |
var tables = $( this ).parents( '.smart-cf-meta-box-repeat-tables' ); |
| 28 |
if ( tables.find( table_class ).length === 2 ) { |
| 29 |
cnt ++; |
| 30 |
add_repeat_group( $( this ) ); |
| 31 |
} |
| 32 |
} ); |
| 33 |
|
| 34 |
function add_repeat_group( button ) { |
| 35 |
var tables = button.parents( '.smart-cf-meta-box-repeat-tables' ); |
| 36 |
var table = tables.find( table_class ).first(); |
| 37 |
var clone = table.clone( true, true ).hide(); |
| 38 |
|
| 39 |
clone.find( 'input, select, textarea' ).each( function( i, e ) { |
| 40 |
var name = $( this ).attr( 'name' ); |
| 41 |
if ( name ) { |
| 42 |
$( this ).attr( 'name', |
| 43 |
name.replace( |
| 44 |
/^(smart-custom-fields\[.+?\])\[\]/, |
| 45 |
'$1[' + cnt + ']' |
| 46 |
) |
| 47 |
); |
| 48 |
$( this ).removeAttr( 'disabled' ); |
| 49 |
} |
| 50 |
} ); |
| 51 |
|
| 52 |
clone.find( '.smart-cf-datetime_picker' ).addClass( 'add' ); |
| 53 |
|
| 54 |
button.parent().after( clone.fadeIn( 'fast' ) ); |
| 55 |
button.trigger( 'smart-cf-after-add-group', { button: button, clone: clone} ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* 画像アップローダー |
| 60 |
*/ |
| 61 |
wrapper.find( '.btn-add-image' ).click( function( e ) { |
| 62 |
e.preventDefault(); |
| 63 |
var custom_uploader_image; |
| 64 |
var upload_button = $( this ); |
| 65 |
if ( custom_uploader_image ) { |
| 66 |
custom_uploader_image.open(); |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
wp.media.view.Modal.prototype.on( 'ready', function(){ |
| 71 |
$( 'select.attachment-filters' ) |
| 72 |
.find( '[value="uploaded"]' ) |
| 73 |
.attr( 'selected', true ) |
| 74 |
.parent() |
| 75 |
.trigger( 'change' ); |
| 76 |
} ); |
| 77 |
|
| 78 |
custom_uploader_image = wp.media( { |
| 79 |
button : { |
| 80 |
text: smart_cf_uploader.image_uploader_title |
| 81 |
}, |
| 82 |
states: [ |
| 83 |
new wp.media.controller.Library({ |
| 84 |
title : smart_cf_uploader.image_uploader_title, |
| 85 |
library : wp.media.query( { type: 'image' } ), |
| 86 |
multiple : false, |
| 87 |
filterable: 'uploaded' |
| 88 |
}) |
| 89 |
] |
| 90 |
} ); |
| 91 |
|
| 92 |
custom_uploader_image.on( 'select', function() { |
| 93 |
var images = custom_uploader_image.state().get( 'selection' ); |
| 94 |
images.each( function( file ){ |
| 95 |
var attachment = file.toJSON(); |
| 96 |
var sizes = file.get('sizes'); |
| 97 |
var image_area = upload_button.parent().find( '.smart-cf-upload-image' ); |
| 98 |
var sizename = image_area.data('size'); |
| 99 |
var img = sizes[ sizename ] || sizes.full; |
| 100 |
var alt_attr = attachment.title || ''; |
| 101 |
image_area.find( 'img' ).remove(); |
| 102 |
image_area.prepend( |
| 103 |
$( '<img>' ).attr( { |
| 104 |
src: img.url, |
| 105 |
alt: alt_attr |
| 106 |
} ) |
| 107 |
); |
| 108 |
image_area.removeClass( 'hide' ); |
| 109 |
upload_button.parent().find( 'input[type="hidden"]' ).val( attachment.id ); |
| 110 |
} ); |
| 111 |
} ); |
| 112 |
|
| 113 |
custom_uploader_image.open(); |
| 114 |
} ); |
| 115 |
|
| 116 |
/** |
| 117 |
* 画像削除ボタン |
| 118 |
*/ |
| 119 |
wrapper.find( '.smart-cf-upload-image' ).hover( function() { |
| 120 |
$( this ).find( '.btn-remove-image' ).fadeIn( 'fast', function() { |
| 121 |
$( this ).removeClass( 'hide' ); |
| 122 |
} ); |
| 123 |
}, function() { |
| 124 |
$( this ).find( '.btn-remove-image' ).fadeOut( 'fast', function() { |
| 125 |
$( this ).addClass( 'hide' ); |
| 126 |
} ); |
| 127 |
} ); |
| 128 |
wrapper.find( '.btn-remove-image' ).click( function() { |
| 129 |
$( this ).parent().find( 'img' ).remove(); |
| 130 |
$( this ).parent().siblings( 'input[type="hidden"]' ).val( '' ); |
| 131 |
$( this ).parent().addClass( 'hide' ); |
| 132 |
} ); |
| 133 |
|
| 134 |
/** |
| 135 |
* ファイルアップローダー |
| 136 |
*/ |
| 137 |
wrapper.find( '.btn-add-file' ).click( function( e ) { |
| 138 |
e.preventDefault(); |
| 139 |
var custom_uploader_file; |
| 140 |
var upload_button = $( this ); |
| 141 |
if ( custom_uploader_file ) { |
| 142 |
custom_uploader_file.open(); |
| 143 |
return; |
| 144 |
} |
| 145 |
custom_uploader_file = wp.media( { |
| 146 |
title : smart_cf_uploader.file_uploader_title, |
| 147 |
button: { |
| 148 |
text: smart_cf_uploader.file_uploader_title |
| 149 |
}, |
| 150 |
multiple: false |
| 151 |
} ); |
| 152 |
|
| 153 |
custom_uploader_file.on( 'select', function() { |
| 154 |
var images = custom_uploader_file.state().get( 'selection' ); |
| 155 |
images.each( function( file ){ |
| 156 |
var attachment = file.toJSON(); |
| 157 |
var image_area = upload_button.parent().find( '.smart-cf-upload-file' ); |
| 158 |
var alt_attr = attachment.title || ''; |
| 159 |
image_area.find( 'a' ).remove(); |
| 160 |
image_area.prepend( |
| 161 |
$( '<a>' ) |
| 162 |
.attr( { |
| 163 |
href: attachment.url, |
| 164 |
target: '_blank', |
| 165 |
rel: 'noopener noreferrer' |
| 166 |
} ) |
| 167 |
.append( |
| 168 |
$( '<img>' ).attr( { |
| 169 |
src: attachment.icon, |
| 170 |
alt: alt_attr |
| 171 |
} ) |
| 172 |
) |
| 173 |
.append( |
| 174 |
$( '<span>' ).text( attachment.filename ) |
| 175 |
) |
| 176 |
); |
| 177 |
image_area.removeClass( 'hide' ); |
| 178 |
upload_button.parent().find( 'input[type="hidden"]' ).val( attachment.id ); |
| 179 |
} ); |
| 180 |
} ); |
| 181 |
|
| 182 |
custom_uploader_file.open(); |
| 183 |
} ); |
| 184 |
|
| 185 |
/** |
| 186 |
* ファイル削除ボタン |
| 187 |
*/ |
| 188 |
wrapper.find( '.smart-cf-upload-file' ).hover( function() { |
| 189 |
$( this ).find( '.btn-remove-file' ).fadeIn( 'fast', function() { |
| 190 |
$( this ).removeClass( 'hide' ); |
| 191 |
} ); |
| 192 |
}, function() { |
| 193 |
$( this ).find( '.btn-remove-file' ).fadeOut( 'fast', function() { |
| 194 |
$( this ).addClass( 'hide' ); |
| 195 |
} ); |
| 196 |
} ); |
| 197 |
wrapper.find( '.btn-remove-file' ).click( function() { |
| 198 |
$( this ).parent().find( 'img' ).remove(); |
| 199 |
$( this ).parent().siblings( 'input[type="hidden"]' ).val( '' ); |
| 200 |
$( this ).parent().addClass( 'hide' ); |
| 201 |
} ); |
| 202 |
|
| 203 |
/** |
| 204 |
* sortable |
| 205 |
*/ |
| 206 |
wrapper.find( '.smart-cf-meta-box-repeat-tables' ).sortable( { |
| 207 |
handle: '.smart-cf-icon-handle', |
| 208 |
items : '> .smart-cf-meta-box-table:not( :first-child )', |
| 209 |
start : function( e, ui ) { |
| 210 |
$( this ).trigger( 'smart-cf-repeat-table-sortable-start', ui.item ); |
| 211 |
}, |
| 212 |
stop : function( e, ui ) { |
| 213 |
$( this ).trigger( 'smart-cf-repeat-table-sortable-stop', ui.item ); |
| 214 |
}, |
| 215 |
} ); |
| 216 |
|
| 217 |
} ); |
| 218 |
} ); |
| 219 |
|