diff --git a/assets/js/image-shortcake-admin.js b/assets/js/image-shortcake-admin.js index cf0ae64..8095b58 100644 --- a/assets/js/image-shortcake-admin.js +++ b/assets/js/image-shortcake-admin.js @@ -2,13 +2,43 @@ var ImageShortcake = { listeners: { + /** + * Callback for the "attachment" attribute field. + * + * When selecting an attachment for an [img], try to populate image + * shortcode attribute fields (alt, caption, description,etc.) from + * the attachment data. + */ + attachment: function( changed, collection, shortcode ) { + if ( typeof changed.value === 'undefined' ) { + return; + } + + var attachment = sui.views.editAttributeFieldAttachment.getFromCache( changed.value ); + + if ( attachment ) { + + var attrView = sui.views.editAttributeField, + altField = attrView.getField( collection, 'alt' ), + captionField = attrView.getField( collection, 'caption' ); + + if ( ! altField.getValue() && attachment.alt ) { + altField.$el.find('[name="alt"]').val( attachment.alt ); + } + + if ( ! captionField.getValue() && attachment.caption ) { + captionField.$el.find('[name="caption"]').val( attachment.caption ); + } + } + }, + /** * Callback for the "linkto" attribute field. * * Display the "Custom Link" field if and only if the "linkto" field is "custom" */ linkto: function( changed, collection, shortcode ) { - var customLinkField = _.find( collection, function( viewModel ) { return 'url' === viewModel.model.get('attr'); } ); + var customLinkField = sui.views.editAttributeField.getField( collection, 'url' ); if ( changed.value === 'custom' ) { customLinkField.$el.show() @@ -27,6 +57,7 @@ var ImageShortcake = { */ if ( typeof wp.shortcake !== 'undefined' && typeof wp.shortcake.hooks !== 'undefined' ) { - wp.shortcake.hooks.addAction( 'img.linkto', ImageShortcake.listeners.linkto ); + wp.shortcake.hooks.addAction( 'img.attachment', ImageShortcake.listeners.attachment ); + wp.shortcake.hooks.addAction( 'img.linkto', ImageShortcake.listeners.linkto ); }