image-widget
Last commit date
lang
15 years ago
views
14 years ago
image-widget.js
14 years ago
image-widget.php
14 years ago
readme.txt
14 years ago
screenshot-1.png
14 years ago
screenshot-2.png
14 years ago
screenshot-3.png
14 years ago
image-widget.js
198 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 | function imgHandler(event) { |
| 93 | event.preventDefault(); |
| 94 | window.send_to_editor = image_widget_send_to_editor; |
| 95 | tb_show("Add an Image", event.target.href, false); |
| 96 | } |
| 97 | |
| 98 | $(document).ready(function() { |
| 99 | // Use new style event handling since $.fn.live() will be deprecated |
| 100 | if ( typeof $.fn.on !== 'undefined' ) { |
| 101 | $("#wpbody").on("click", ".thickbox-image-widget", imgHandler); |
| 102 | } |
| 103 | else { |
| 104 | $("a.thickbox-image-widget").live('click', imgHandler); |
| 105 | } |
| 106 | |
| 107 | // Modify thickbox link to fit window. Adapted from wp-admin\js\media-upload.dev.js. |
| 108 | $('a.thickbox-image-widget').each( function() { |
| 109 | var href = $(this).attr('href'), width = $(window).width(), H = $(window).height(), W = ( 720 < width ) ? 720 : width; |
| 110 | if ( ! href ) return; |
| 111 | href = href.replace(/&width=[0-9]+/g, ''); |
| 112 | href = href.replace(/&height=[0-9]+/g, ''); |
| 113 | $(this).attr( 'href', href + '&width=' + ( W - 80 ) + '&height=' + ( H - 85 ) ); |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | })(jQuery); |
| 118 | |
| 119 | /* Fix browser upload */ |
| 120 | |
| 121 | jQuery(document).ready(function() { |
| 122 | |
| 123 | jQuery('form#image-form').submit(function(){ |
| 124 | var wp_ref = jQuery("input[name='_wp_http_referer']").val(); |
| 125 | // _wp_http_referer only contains the widget_sp_image if the |
| 126 | // previous action was pressing the add image link in an Image Widget |
| 127 | // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/indexOf |
| 128 | if( wp_ref.indexOf('widget_sp_image') != -1 ) { |
| 129 | var parsed_url = parse_url(wp_ref); |
| 130 | var nw_action_url = jQuery('form#image-form').attr('action'); |
| 131 | |
| 132 | // make sure the widget_sp_image is not part of the form action url |
| 133 | // so we will add it to fix the context |
| 134 | if( nw_action_url.indexOf('widget_sp_image') == -1 ) { |
| 135 | nw_action_url = nw_action_url + '&' + parsed_url.query; |
| 136 | jQuery('form#image-form').attr('action', nw_action_url); |
| 137 | } |
| 138 | } |
| 139 | return true; |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | |
| 144 | /* |
| 145 | * Thanks to http://github.com/kvz/phpjs/raw/master/functions/url/parse_url.js |
| 146 | */ |
| 147 | function parse_url (str, component) { |
| 148 | // http://kevin.vanzonneveld.net |
| 149 | // + original by: Steven Levithan (http://blog.stevenlevithan.com) |
| 150 | // + reimplemented by: Brett Zamir (http://brett-zamir.me) |
| 151 | // + input by: Lorenzo Pisani |
| 152 | // + input by: Tony |
| 153 | // + improved by: Brett Zamir (http://brett-zamir.me) |
| 154 | // % note: Based on http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js |
| 155 | // % note: blog post at http://blog.stevenlevithan.com/archives/parseuri |
| 156 | // % note: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js |
| 157 | // % note: Does not replace invalid characters with '_' as in PHP, nor does it return false with |
| 158 | // % note: a seriously malformed URL. |
| 159 | // % note: Besides function name, is essentially the same as parseUri as well as our allowing |
| 160 | // % note: an extra slash after the scheme/protocol (to allow file:/// as in PHP) |
| 161 | // * example 1: parse_url('http://username:password@hostname/path?arg=value#anchor'); |
| 162 | // * returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'} |
| 163 | var key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', |
| 164 | 'relative', 'path', 'directory', 'file', 'query', 'fragment'], |
| 165 | ini = (this.php_js && this.php_js.ini) || {}, |
| 166 | mode = (ini['phpjs.parse_url.mode'] && |
| 167 | ini['phpjs.parse_url.mode'].local_value) || 'php', |
| 168 | parser = { |
| 169 | php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, |
| 170 | strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, |
| 171 | loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this) |
| 172 | }; |
| 173 | |
| 174 | var m = parser[mode].exec(str), |
| 175 | uri = {}, |
| 176 | i = 14; |
| 177 | while (i--) { |
| 178 | if (m[i]) { |
| 179 | uri[key[i]] = m[i]; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (component) { |
| 184 | return uri[component.replace('PHP_URL_', '').toLowerCase()]; |
| 185 | } |
| 186 | if (mode !== 'php') { |
| 187 | var name = (ini['phpjs.parse_url.queryKey'] && |
| 188 | ini['phpjs.parse_url.queryKey'].local_value) || 'queryKey'; |
| 189 | parser = /(?:^|&)([^&=]*)=?([^&]*)/g; |
| 190 | uri[name] = {}; |
| 191 | uri[key[12]].replace(parser, function ($0, $1, $2) { |
| 192 | if ($1) {uri[name][$1] = $2;} |
| 193 | }); |
| 194 | } |
| 195 | delete uri.source; |
| 196 | return uri; |
| 197 | } |
| 198 | /* /wp-admin/media-upload.php?type=image&widget_id=widget_sp_image-11& */ |