/** * Tribe Image Widget Javascript * @author Modern Tribe, Inc. * Copyright 2013. */ jQuery(document).ready(function($){ imageWidget = { // Call this from the upload button to initiate the upload frame. uploader : function( widget_id, widget_id_string ) { var frame = wp.media({ title : TribeImageWidget.frame_title, multiple : false, library : { type : 'image' }, button : { text : TribeImageWidget.button_title } }); // Handle results from media manager. frame.on('close',function( ) { var attachments = frame.state().get('selection').toJSON(); imageWidget.render( widget_id, widget_id_string, attachments[0] ); }); frame.open(); return false; }, // Output Image preview and populate widget form. render : function( widget_id, widget_id_string, attachment ) { var $attachment_id = $( document.getElementById( widget_id_string + 'attachment_id' ) ); var $image_url = $( document.getElementById( widget_id_string + 'imageurl' ) ); $("#" + widget_id_string + 'preview').html(imageWidget.imgHTML( attachment )); $("#" + widget_id_string + 'fields').slideDown(); // update the attachment id if it has changed if ( $attachment_id.val() !== attachment.id ) { $attachment_id.val( attachment.id ).trigger( 'change' ); } // update the url if it has changed if ( $image_url.val() !== attachment.url ) { $image_url.val( attachment.url ).trigger( 'change' ); } $("#" + widget_id_string + 'aspect_ratio').val(attachment.width/attachment.height); $("#" + widget_id_string + 'width').val(attachment.width); $("#" + widget_id_string + 'height').val(attachment.height); $("#" + widget_id_string + 'size').val('full'); $("#" + widget_id_string + 'custom_size_selector').slideDown(); imageWidget.toggleSizes( widget_id, widget_id_string ); imageWidget.updateInputIfEmpty( widget_id_string, 'title', attachment.title ); imageWidget.updateInputIfEmpty( widget_id_string, 'alt', attachment.alt ); imageWidget.updateInputIfEmpty( widget_id_string, 'description', attachment.description ); // attempt to populate 'description' with caption if description is blank. imageWidget.updateInputIfEmpty( widget_id_string, 'description', attachment.caption ); }, // Update input fields if it is empty updateInputIfEmpty : function( widget_id_string, name, value ) { var field = $("#" + widget_id_string + name); if ( field.val() == '' ) { field.val(value); } }, // Render html for the image. imgHTML : function( attachment ) { var img_html = '