From: Roan Kattouw Date: Wed, 9 Mar 2011 14:27:13 +0000 (+0000) Subject: Fix r83586 (drag and drop support for placeholder plugin) to work in IE, whose drag... X-Git-Tag: 1.31.0-rc.0~31543 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=00c5c39e24b25e92bf405e58bb573ffcd6fc5f5f;p=lhc%2Fweb%2Fwiklou.git Fix r83586 (drag and drop support for placeholder plugin) to work in IE, whose drag&drop API is surprisingly similar to Firefox's but with slight differences --- diff --git a/resources/jquery/jquery.placeholder.js b/resources/jquery/jquery.placeholder.js index 16c715a04e..1bb69f0046 100644 --- a/resources/jquery/jquery.placeholder.js +++ b/resources/jquery/jquery.placeholder.js @@ -41,9 +41,26 @@ $.fn.placeholder = function() { // already focused when the events were bound .bind( 'focus drop keydown paste', function( e ) { if ( $input.hasClass( 'placeholder' ) ) { - // Support for drag&drop in Firefox if ( e.type == 'drop' && e.originalEvent.dataTransfer ) { - this.value = e.originalEvent.dataTransfer.getData( 'text/plain' ); + // Support for drag&drop. Instead of inserting the dropped + // text somewhere in the middle of the placeholder string, + // we want to set the contents of the search box to the + // dropped text. + + // IE wants getData( 'text' ) but Firefox wants getData( 'text/plain' ) + // Firefox fails gracefully with an empty string, IE barfs with an error + try { + // Try the Firefox way + this.value = e.originalEvent.dataTransfer.getData( 'text/plain' ); + } catch ( exception ) { + // Got an exception, so use the IE way + this.value = e.originalEvent.dataTransfer.getData( 'text' ); + } + + // On Firefox, drop fires after the dropped text has been inserted, + // but on IE it fires before. If we don't prevent the default action, + // IE will insert the dropped text twice. + e.preventDefault(); } else { this.value = ''; }