commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / bower_components / jquery / src / attributes / val.js
1 define([
2 "../core",
3 "./support",
4 "../core/init"
5 ], function( jQuery, support ) {
6
7 var rreturn = /\r/g;
8
9 jQuery.fn.extend({
10 val: function( value ) {
11 var hooks, ret, isFunction,
12 elem = this[0];
13
14 if ( !arguments.length ) {
15 if ( elem ) {
16 hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ];
17
18 if ( hooks && "get" in hooks && (ret = hooks.get( elem, "value" )) !== undefined ) {
19 return ret;
20 }
21
22 ret = elem.value;
23
24 return typeof ret === "string" ?
25 // handle most common string cases
26 ret.replace(rreturn, "") :
27 // handle cases where value is null/undef or number
28 ret == null ? "" : ret;
29 }
30
31 return;
32 }
33
34 isFunction = jQuery.isFunction( value );
35
36 return this.each(function( i ) {
37 var val;
38
39 if ( this.nodeType !== 1 ) {
40 return;
41 }
42
43 if ( isFunction ) {
44 val = value.call( this, i, jQuery( this ).val() );
45 } else {
46 val = value;
47 }
48
49 // Treat null/undefined as ""; convert numbers to string
50 if ( val == null ) {
51 val = "";
52 } else if ( typeof val === "number" ) {
53 val += "";
54 } else if ( jQuery.isArray( val ) ) {
55 val = jQuery.map( val, function( value ) {
56 return value == null ? "" : value + "";
57 });
58 }
59
60 hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ];
61
62 // If set returns undefined, fall back to normal setting
63 if ( !hooks || !("set" in hooks) || hooks.set( this, val, "value" ) === undefined ) {
64 this.value = val;
65 }
66 });
67 }
68 });
69
70 jQuery.extend({
71 valHooks: {
72 option: {
73 get: function( elem ) {
74 var val = jQuery.find.attr( elem, "value" );
75 return val != null ?
76 val :
77 // Support: IE10-11+
78 // option.text throws exceptions (#14686, #14858)
79 jQuery.trim( jQuery.text( elem ) );
80 }
81 },
82 select: {
83 get: function( elem ) {
84 var value, option,
85 options = elem.options,
86 index = elem.selectedIndex,
87 one = elem.type === "select-one" || index < 0,
88 values = one ? null : [],
89 max = one ? index + 1 : options.length,
90 i = index < 0 ?
91 max :
92 one ? index : 0;
93
94 // Loop through all the selected options
95 for ( ; i < max; i++ ) {
96 option = options[ i ];
97
98 // oldIE doesn't update selected after form reset (#2551)
99 if ( ( option.selected || i === index ) &&
100 // Don't return options that are disabled or in a disabled optgroup
101 ( support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null ) &&
102 ( !option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" ) ) ) {
103
104 // Get the specific value for the option
105 value = jQuery( option ).val();
106
107 // We don't need an array for one selects
108 if ( one ) {
109 return value;
110 }
111
112 // Multi-Selects return an array
113 values.push( value );
114 }
115 }
116
117 return values;
118 },
119
120 set: function( elem, value ) {
121 var optionSet, option,
122 options = elem.options,
123 values = jQuery.makeArray( value ),
124 i = options.length;
125
126 while ( i-- ) {
127 option = options[ i ];
128
129 if ( jQuery.inArray( jQuery.valHooks.option.get( option ), values ) >= 0 ) {
130
131 // Support: IE6
132 // When new option element is added to select box we need to
133 // force reflow of newly added node in order to workaround delay
134 // of initialization properties
135 try {
136 option.selected = optionSet = true;
137
138 } catch ( _ ) {
139
140 // Will be executed only in IE6
141 option.scrollHeight;
142 }
143
144 } else {
145 option.selected = false;
146 }
147 }
148
149 // Force browsers to behave consistently when non-matching value is set
150 if ( !optionSet ) {
151 elem.selectedIndex = -1;
152 }
153
154 return options;
155 }
156 }
157 }
158 });
159
160 // Radios and checkboxes getter/setter
161 jQuery.each([ "radio", "checkbox" ], function() {
162 jQuery.valHooks[ this ] = {
163 set: function( elem, value ) {
164 if ( jQuery.isArray( value ) ) {
165 return ( elem.checked = jQuery.inArray( jQuery(elem).val(), value ) >= 0 );
166 }
167 }
168 };
169 if ( !support.checkOn ) {
170 jQuery.valHooks[ this ].get = function( elem ) {
171 // Support: Webkit
172 // "" is returned instead of "on" if a value isn't specified
173 return elem.getAttribute("value") === null ? "on" : elem.value;
174 };
175 }
176 });
177
178 });