Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / resources / src / jquery / jquery.expandableField.js
blobf9db72f91e258d1bc4d4ab3042026ad822f7ed1c
1 /**
2  * This plugin provides functionality to expand a text box on focus to double it's current width
3  *
4  * Usage:
5  *
6  * Set options:
7  *              $('#textbox').expandableField( { option1: value1, option2: value2 } );
8  *              $('#textbox').expandableField( option, value );
9  * Get option:
10  *              value = $('#textbox').expandableField( option );
11  * Initialize:
12  *              $('#textbox').expandableField();
13  *
14  * Options:
15  *
16  */
17 ( function ( $ ) {
19         $.expandableField = {
20                 /**
21                  * Expand the field, make the callback
22                  */
23                 expandField: function ( e, context ) {
24                         context.config.beforeExpand.call( context.data.$field, context );
25                         context.data.$field
26                                 .animate( { width: context.data.expandedWidth }, 'fast', function () {
27                                         context.config.afterExpand.call( this, context );
28                                 } );
29                 },
30                 /**
31                  * Condense the field, make the callback
32                  */
33                 condenseField: function ( e, context ) {
34                         context.config.beforeCondense.call( context.data.$field, context );
35                         context.data.$field
36                                 .animate( { width: context.data.condensedWidth }, 'fast', function () {
37                                         context.config.afterCondense.call( this, context );
38                                 } );
39                 },
40                 /**
41                  * Sets the value of a property, and updates the widget accordingly
42                  *
43                  * @param {Object} context
44                  * @param {string} property Name of property
45                  * @param {Mixed} value Value to set property with
46                  */
47                 configure: function ( context, property, value ) {
48                         // TODO: Validate creation using fallback values
49                         context.config[ property ] = value;
50                 }
52         };
54         $.fn.expandableField = function () {
56                 // Multi-context fields
57                 var returnValue,
58                         args = arguments;
60                 $( this ).each( function () {
61                         var key, context, timeout;
63                         /* Construction / Loading */
65                         context = $( this ).data( 'expandableField-context' );
67                         // TODO: Do we need to check both null and undefined?
68                         if ( context === undefined || context === null ) {
69                                 context = {
70                                         config: {
71                                                 // callback function for before collapse
72                                                 beforeCondense: function () {},
74                                                 // callback function for before expand
75                                                 beforeExpand: function () {},
77                                                 // callback function for after collapse
78                                                 afterCondense: function () {},
80                                                 // callback function for after expand
81                                                 afterExpand: function () {},
83                                                 // Whether the field should expand to the left or the right -- defaults to left
84                                                 expandToLeft: true
85                                         }
86                                 };
87                         }
89                         /* API */
90                         // Handle various calling styles
91                         if ( args.length > 0 ) {
92                                 if ( typeof args[ 0 ] === 'object' ) {
93                                         // Apply set of properties
94                                         for ( key in args[ 0 ] ) {
95                                                 $.expandableField.configure( context, key, args[ 0 ][ key ] );
96                                         }
97                                 } else if ( typeof args[ 0 ] === 'string' ) {
98                                         if ( args.length > 1 ) {
99                                                 // Set property values
100                                                 $.expandableField.configure( context, args[ 0 ], args[ 1 ] );
102                                         // TODO: Do we need to check both null and undefined?
103                                         } else if ( returnValue === null || returnValue === undefined ) {
104                                                 // Get property values, but don't give access to internal data - returns only the first
105                                                 returnValue = ( args[ 0 ] in context.config ? undefined : context.config[ args[ 0 ] ] );
106                                         }
107                                 }
108                         }
110                         /* Initialization */
112                         if ( context.data === undefined ) {
113                                 context.data = {
114                                         // The width of the field in it's condensed state
115                                         condensedWidth: $( this ).width(),
117                                         // The width of the field in it's expanded state
118                                         expandedWidth: $( this ).width() * 2,
120                                         // Reference to the field
121                                         $field: $( this )
122                                 };
124                                 $( this )
125                                         .addClass( 'expandableField' )
126                                         .focus( function ( e ) {
127                                                 clearTimeout( timeout );
128                                                 $.expandableField.expandField( e, context );
129                                         } )
130                                         .blur( function ( e ) {
131                                                 timeout = setTimeout( function () {
132                                                         $.expandableField.condenseField( e, context );
133                                                 }, 250 );
134                                         } );
135                         }
136                         // Store the context for next time
137                         $( this ).data( 'expandableField-context', context );
138                 } );
139                 return returnValue !== undefined ? returnValue : $( this );
140         };
142 }( jQuery ) );