byteLimit/byteLength improvements:
[lhc/web/wiklou.git] / resources / jquery / jquery.placeholder.js
index d4c00e5..1bb69f0 100644 (file)
@@ -33,15 +33,37 @@ $.fn.placeholder = function() {
                                if ( this.value === '' ) {
                                        this.value = placeholder;
                                        $input.addClass( 'placeholder' );
-                               } else {
-                                       $input.removeClass( 'placeholder' );
                                }
                        } )
 
                        // Hide on focus
-                       .focus( function() {
+                       // Also listen for other events in case $input was
+                       // already focused when the events were bound
+                       .bind( 'focus drop keydown paste', function( e ) {
                                if ( $input.hasClass( 'placeholder' ) ) {
-                                       this.value = '';
+                                       if ( e.type == 'drop' && e.originalEvent.dataTransfer ) {
+                                               // 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 = '';
+                                       }
                                        $input.removeClass( 'placeholder' );
                                }
                        } );
@@ -62,4 +84,4 @@ $.fn.placeholder = function() {
 
        });
 };
-} )( jQuery );
\ No newline at end of file
+} )( jQuery );