scratch – Blame information for rev 58

Subversion Repositories:
Rev:
Rev Author Line No. Line
58 office 1 define( [
2 "../core",
3 "../core/stripAndCollapse",
4 "./support",
5 "../core/init"
6 ], function( jQuery, stripAndCollapse, support ) {
7  
8 "use strict";
9  
10 var rreturn = /\r/g;
11  
12 jQuery.fn.extend( {
13 val: function( value ) {
14 var hooks, ret, isFunction,
15 elem = this[ 0 ];
16  
17 if ( !arguments.length ) {
18 if ( elem ) {
19 hooks = jQuery.valHooks[ elem.type ] ||
20 jQuery.valHooks[ elem.nodeName.toLowerCase() ];
21  
22 if ( hooks &&
23 "get" in hooks &&
24 ( ret = hooks.get( elem, "value" ) ) !== undefined
25 ) {
26 return ret;
27 }
28  
29 ret = elem.value;
30  
31 // Handle most common string cases
32 if ( typeof ret === "string" ) {
33 return ret.replace( rreturn, "" );
34 }
35  
36 // Handle cases where value is null/undef or number
37 return ret == null ? "" : ret;
38 }
39  
40 return;
41 }
42  
43 isFunction = jQuery.isFunction( value );
44  
45 return this.each( function( i ) {
46 var val;
47  
48 if ( this.nodeType !== 1 ) {
49 return;
50 }
51  
52 if ( isFunction ) {
53 val = value.call( this, i, jQuery( this ).val() );
54 } else {
55 val = value;
56 }
57  
58 // Treat null/undefined as ""; convert numbers to string
59 if ( val == null ) {
60 val = "";
61  
62 } else if ( typeof val === "number" ) {
63 val += "";
64  
65 } else if ( jQuery.isArray( val ) ) {
66 val = jQuery.map( val, function( value ) {
67 return value == null ? "" : value + "";
68 } );
69 }
70  
71 hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ];
72  
73 // If set returns undefined, fall back to normal setting
74 if ( !hooks || !( "set" in hooks ) || hooks.set( this, val, "value" ) === undefined ) {
75 this.value = val;
76 }
77 } );
78 }
79 } );
80  
81 jQuery.extend( {
82 valHooks: {
83 option: {
84 get: function( elem ) {
85  
86 var val = jQuery.find.attr( elem, "value" );
87 return val != null ?
88 val :
89  
90 // Support: IE <=10 - 11 only
91 // option.text throws exceptions (#14686, #14858)
92 // Strip and collapse whitespace
93 // https://html.spec.whatwg.org/#strip-and-collapse-whitespace
94 stripAndCollapse( jQuery.text( elem ) );
95 }
96 },
97 select: {
98 get: function( elem ) {
99 var value, option, i,
100 options = elem.options,
101 index = elem.selectedIndex,
102 one = elem.type === "select-one",
103 values = one ? null : [],
104 max = one ? index + 1 : options.length;
105  
106 if ( index < 0 ) {
107 i = max;
108  
109 } else {
110 i = one ? index : 0;
111 }
112  
113 // Loop through all the selected options
114 for ( ; i < max; i++ ) {
115 option = options[ i ];
116  
117 // Support: IE <=9 only
118 // IE8-9 doesn't update selected after form reset (#2551)
119 if ( ( option.selected || i === index ) &&
120  
121 // Don't return options that are disabled or in a disabled optgroup
122 !option.disabled &&
123 ( !option.parentNode.disabled ||
124 !jQuery.nodeName( option.parentNode, "optgroup" ) ) ) {
125  
126 // Get the specific value for the option
127 value = jQuery( option ).val();
128  
129 // We don't need an array for one selects
130 if ( one ) {
131 return value;
132 }
133  
134 // Multi-Selects return an array
135 values.push( value );
136 }
137 }
138  
139 return values;
140 },
141  
142 set: function( elem, value ) {
143 var optionSet, option,
144 options = elem.options,
145 values = jQuery.makeArray( value ),
146 i = options.length;
147  
148 while ( i-- ) {
149 option = options[ i ];
150  
151 /* eslint-disable no-cond-assign */
152  
153 if ( option.selected =
154 jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1
155 ) {
156 optionSet = true;
157 }
158  
159 /* eslint-enable no-cond-assign */
160 }
161  
162 // Force browsers to behave consistently when non-matching value is set
163 if ( !optionSet ) {
164 elem.selectedIndex = -1;
165 }
166 return values;
167 }
168 }
169 }
170 } );
171  
172 // Radios and checkboxes getter/setter
173 jQuery.each( [ "radio", "checkbox" ], function() {
174 jQuery.valHooks[ this ] = {
175 set: function( elem, value ) {
176 if ( jQuery.isArray( value ) ) {
177 return ( elem.checked = jQuery.inArray( jQuery( elem ).val(), value ) > -1 );
178 }
179 }
180 };
181 if ( !support.checkOn ) {
182 jQuery.valHooks[ this ].get = function( elem ) {
183 return elem.getAttribute( "value" ) === null ? "on" : elem.value;
184 };
185 }
186 } );
187  
188 } );