image-widget.deprecated.js
13 years ago
image-widget.deprecated.upload-fixer.js
13 years ago
image-widget.js
13 years ago
image-widget.js
107 lines
| 1 | /** |
| 2 | * Tribe Image Widget Javascript |
| 3 | * @author Modern Tribe, Inc. |
| 4 | * Copyright 2013. |
| 5 | */ |
| 6 | jQuery(document).ready(function($){ |
| 7 | |
| 8 | imageWidget = { |
| 9 | |
| 10 | // Call this from the upload button to initiate the upload frame. |
| 11 | uploader : function( widget_id, widget_id_string ) { |
| 12 | |
| 13 | var frame = wp.media({ |
| 14 | title : TribeImageWidget.frame_title, |
| 15 | multiple : false, |
| 16 | library : { type : 'image' }, |
| 17 | button : { text : TribeImageWidget.button_title } |
| 18 | }); |
| 19 | |
| 20 | // Handle results from media manager. |
| 21 | frame.on('close',function( ) { |
| 22 | var attachments = frame.state().get('selection').toJSON(); |
| 23 | imageWidget.render( widget_id, widget_id_string, attachments[0] ); |
| 24 | }); |
| 25 | |
| 26 | frame.open(); |
| 27 | return false; |
| 28 | }, |
| 29 | |
| 30 | // Output Image preview and populate widget form. |
| 31 | render : function( widget_id, widget_id_string, attachment ) { |
| 32 | |
| 33 | $("#" + widget_id_string + 'preview').html(imageWidget.imgHTML( attachment )); |
| 34 | |
| 35 | $("#" + widget_id_string + 'fields').slideDown(); |
| 36 | |
| 37 | $("#" + widget_id_string + 'attachment_id').val(attachment.id); |
| 38 | $("#" + widget_id_string + 'imageurl').val(attachment.url); |
| 39 | $("#" + widget_id_string + 'aspect_ratio').val(attachment.width/attachment.height); |
| 40 | $("#" + widget_id_string + 'width').val(attachment.width); |
| 41 | $("#" + widget_id_string + 'height').val(attachment.height); |
| 42 | |
| 43 | $("#" + widget_id_string + 'size').val('full'); |
| 44 | $("#" + widget_id_string + 'custom_size_selector').slideDown(); |
| 45 | imageWidget.toggleSizes( widget_id, widget_id_string ); |
| 46 | |
| 47 | imageWidget.updateInputIfEmpty( widget_id_string, 'title', attachment.title ); |
| 48 | imageWidget.updateInputIfEmpty( widget_id_string, 'alt', attachment.alt ); |
| 49 | imageWidget.updateInputIfEmpty( widget_id_string, 'description', attachment.description ); |
| 50 | // attempt to populate 'description' with caption if description is blank. |
| 51 | imageWidget.updateInputIfEmpty( widget_id_string, 'description', attachment.caption ); |
| 52 | }, |
| 53 | |
| 54 | // Update input fields if it is empty |
| 55 | updateInputIfEmpty : function( widget_id_string, name, value ) { |
| 56 | var field = $("#" + widget_id_string + name); |
| 57 | if ( field.val() == '' ) { |
| 58 | field.val(value); |
| 59 | } |
| 60 | }, |
| 61 | |
| 62 | // Render html for the image. |
| 63 | imgHTML : function( attachment ) { |
| 64 | var img_html = '<img src="' + attachment.url + '" '; |
| 65 | img_html += 'width="' + attachment.width + '" '; |
| 66 | img_html += 'height="' + attachment.height + '" '; |
| 67 | if ( attachment.alt != '' ) { |
| 68 | img_html += 'alt="' + attachment.alt + '" '; |
| 69 | } |
| 70 | img_html += '/>'; |
| 71 | return img_html; |
| 72 | }, |
| 73 | |
| 74 | // Hide or display the WordPress image size fields depending on if 'Custom' is selected. |
| 75 | toggleSizes : function( widget_id, widget_id_string ) { |
| 76 | if ( $( '#' + widget_id_string + 'size' ).val() == 'tribe_image_widget_custom' ) { |
| 77 | $( '#' + widget_id_string + 'custom_size_fields').slideDown(); |
| 78 | } else { |
| 79 | $( '#' + widget_id_string + 'custom_size_fields').slideUp(); |
| 80 | } |
| 81 | }, |
| 82 | |
| 83 | // Update the image height based on the image width. |
| 84 | changeImgWidth : function( widget_id, widget_id_string ) { |
| 85 | var aspectRatio = $("#" + widget_id_string + 'aspect_ratio').val(); |
| 86 | if ( aspectRatio ) { |
| 87 | var width = $( '#' + widget_id_string + 'width' ).val(); |
| 88 | var height = Math.round( width / aspectRatio ); |
| 89 | $( '#' + widget_id_string + 'height' ).val(height); |
| 90 | //imageWidget.changeImgSize( widget_id, widget_id_string, width, height ); |
| 91 | } |
| 92 | }, |
| 93 | |
| 94 | // Update the image width based on the image height. |
| 95 | changeImgHeight : function( widget_id, widget_id_string ) { |
| 96 | var aspectRatio = $("#" + widget_id_string + 'aspect_ratio').val(); |
| 97 | if ( aspectRatio ) { |
| 98 | var height = $( '#' + widget_id_string + 'height' ).val(); |
| 99 | var width = Math.round( height * aspectRatio ); |
| 100 | $( '#' + widget_id_string + 'width' ).val(width); |
| 101 | //imageWidget.changeImgSize( widget_id, widget_id_string, width, height ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | }; |
| 106 | |
| 107 | }); |