image-widget.deprecated.js
13 years ago
image-widget.deprecated.upload-fixer.js
13 years ago
image-widget.js
13 years ago
image-widget.deprecated.js
123 lines
| 1 | var imageWidget; |
| 2 | |
| 3 | (function($){ |
| 4 | |
| 5 | imageWidget = { |
| 6 | |
| 7 | sendToEditor : function(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 | changeImgWidth : function(instance) { |
| 43 | var width = $( '#widget-'+instance+'-width' ).val(); |
| 44 | var height = Math.round(width / imageWidget.imgRatio(instance)); |
| 45 | imageWidget.changeImgSize(instance,width,height); |
| 46 | }, |
| 47 | |
| 48 | changeImgHeight : function(instance) { |
| 49 | var height = $( '#widget-'+instance+'-height' ).val(); |
| 50 | var width = Math.round(height * imageWidget.imgRatio(instance)); |
| 51 | imageWidget.changeImgSize(instance,width,height); |
| 52 | }, |
| 53 | |
| 54 | imgRatio : function(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 | changeImgSize : function(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 | changeImgAlign : function(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 | imgHandler : function(event) { |
| 93 | event.preventDefault(); |
| 94 | window.send_to_editor = imageWidget.sendToEditor; |
| 95 | tb_show("Add an Image", event.target.href, false); |
| 96 | }, |
| 97 | |
| 98 | setActiveWidget : function(instance_id) { |
| 99 | self.IW_instance = instance_id; |
| 100 | } |
| 101 | |
| 102 | }; |
| 103 | |
| 104 | $(document).ready(function() { |
| 105 | // Use new style event handling since $.fn.live() will be deprecated |
| 106 | if ( typeof $.fn.on !== 'undefined' ) { |
| 107 | $("#wpbody").on("click", ".thickbox-image-widget", imageWidget.imgHandler); |
| 108 | } |
| 109 | else { |
| 110 | $("a.thickbox-image-widget").live('click', imageWidget.imgHandler); |
| 111 | } |
| 112 | |
| 113 | // Modify thickbox link to fit window. Adapted from wp-admin\js\media-upload.dev.js. |
| 114 | $('a.thickbox-image-widget').each( function() { |
| 115 | var href = $(this).attr('href'), width = $(window).width(), H = $(window).height(), W = ( 720 < width ) ? 720 : width; |
| 116 | if ( ! href ) return; |
| 117 | href = href.replace(/&width=[0-9]+/g, ''); |
| 118 | href = href.replace(/&height=[0-9]+/g, ''); |
| 119 | $(this).attr( 'href', href + '&width=' + ( W - 80 ) + '&height=' + ( H - 85 ) ); |
| 120 | }); |
| 121 | }); |
| 122 | |
| 123 | })(jQuery); |