image-widget
Last commit date
lang
15 years ago
views
15 years ago
image-widget.js
15 years ago
image-widget.php
15 years ago
readme.txt
15 years ago
screenshot-1.png
16 years ago
screenshot-2.png
16 years ago
screenshot-3.png
16 years ago
image-widget.js
108 lines
| 1 | (function($){ |
| 2 | |
| 3 | window.set_active_widget = function(instance_id) { |
| 4 | self.IW_instance = instance_id; |
| 5 | } |
| 6 | |
| 7 | function image_widget_send_to_editor(h) { |
| 8 | // ignore content returned from media uploader and use variables passed to window instead |
| 9 | |
| 10 | // store attachment id in hidden field |
| 11 | $( '#widget-'+self.IW_instance+'-image' ).val( self.IW_img_id ); |
| 12 | |
| 13 | // display attachment preview |
| 14 | $( '#display-widget-'+self.IW_instance+'-image' ).html( self.IW_html ); |
| 15 | |
| 16 | // change width & height fields in widget to match image |
| 17 | $( '#widget-'+self.IW_instance+'-width' ).val($( '#display-widget-'+self.IW_instance+'-image img').attr('width')); |
| 18 | $( '#widget-'+self.IW_instance+'-height' ).val($( '#display-widget-'+self.IW_instance+'-image img').attr('height')); |
| 19 | |
| 20 | // set alignment in widget |
| 21 | $( '#widget-'+self.IW_instance+'-align' ).val(self.IW_align); |
| 22 | |
| 23 | // set title in widget |
| 24 | $( '#widget-'+self.IW_instance+'-title' ).val(self.IW_title); |
| 25 | |
| 26 | // set caption in widget |
| 27 | $( '#widget-'+self.IW_instance+'-description' ).val(self.IW_caption); |
| 28 | |
| 29 | // set alt text in widget |
| 30 | $( '#widget-'+self.IW_instance+'-alt' ).val(self.IW_alt); |
| 31 | |
| 32 | // set link in widget |
| 33 | $( '#widget-'+self.IW_instance+'-link' ).val(self.IW_url); |
| 34 | |
| 35 | // close thickbox |
| 36 | tb_remove(); |
| 37 | |
| 38 | // change button text |
| 39 | $('#add_image-widget-'+self.IW_instance+'-image').html($('#add_image-widget-'+self.IW_instance+'-image').html().replace(/Add Image/g, 'Change Image')); |
| 40 | } |
| 41 | |
| 42 | function changeImgWidth(instance) { |
| 43 | var width = $( '#widget-'+instance+'-width' ).val(); |
| 44 | var height = Math.round(width / imgRatio(instance)); |
| 45 | changeImgSize(instance,width,height); |
| 46 | } |
| 47 | |
| 48 | function changeImgHeight(instance) { |
| 49 | var height = $( '#widget-'+instance+'-height' ).val(); |
| 50 | var width = Math.round(height * imgRatio(instance)); |
| 51 | changeImgSize(instance,width,height); |
| 52 | } |
| 53 | |
| 54 | function imgRatio(instance) { |
| 55 | var width_old = $( '#display-widget-'+instance+'-image img').attr('width'); |
| 56 | var height_old = $( '#display-widget-'+instance+'-image img').attr('height'); |
| 57 | var ratio = width_old / height_old; |
| 58 | return ratio; |
| 59 | } |
| 60 | |
| 61 | function changeImgSize(instance,width,height) { |
| 62 | if (isNaN(width) || width < 1) { |
| 63 | $( '#widget-'+instance+'-width' ).val(''); |
| 64 | width = 'none'; |
| 65 | } else { |
| 66 | $( '#widget-'+instance+'-width' ).val(width); |
| 67 | width = width + 'px'; |
| 68 | } |
| 69 | $( '#display-widget-'+instance+'-image img' ).css({ |
| 70 | 'width':width |
| 71 | }); |
| 72 | |
| 73 | if (isNaN(height) || height < 1) { |
| 74 | $( '#widget-'+instance+'-height' ).val(''); |
| 75 | height = 'none'; |
| 76 | } else { |
| 77 | $( '#widget-'+instance+'-height' ).val(height); |
| 78 | height = height + 'px'; |
| 79 | } |
| 80 | $( '#display-widget-'+instance+'-image img' ).css({ |
| 81 | 'height':height |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | function changeImgAlign(instance) { |
| 86 | var align = $( '#widget-'+instance+'-align' ).val(); |
| 87 | $( '#display-widget-'+instance+'-image img' ).attr( |
| 88 | 'class', (align == 'none' ? '' : 'align'+align) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | $(document).ready(function() { |
| 93 | $("#widgets-right").delegate('a.thickbox-image-widget', 'click', function(event) { |
| 94 | event.preventDefault(); |
| 95 | window.send_to_editor = image_widget_send_to_editor; |
| 96 | tb_show("Add an Image", event.target.href, false); |
| 97 | }); |
| 98 | // Modify thickbox link to fit window. Adapted from wp-admin\js\media-upload.dev.js. |
| 99 | $('a.thickbox-image-widget').each( function() { |
| 100 | var href = $(this).attr('href'), width = $(window).width(), H = $(window).height(), W = ( 720 < width ) ? 720 : width; |
| 101 | if ( ! href ) return; |
| 102 | href = href.replace(/&width=[0-9]+/g, ''); |
| 103 | href = href.replace(/&height=[0-9]+/g, ''); |
| 104 | $(this).attr( 'href', href + '&width=' + ( W - 80 ) + '&height=' + ( H - 85 ) ); |
| 105 | }); |
| 106 | }); |
| 107 | |
| 108 | })(jQuery); |