aboutsummaryrefslogtreecommitdiffstats
path: root/test/delegatetest.html
blob: 53efe54ce524bd112e6e9f48f0be8039112a51fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<html>
<head>
<title>Event Delegation Tests</title>
<script src="jquery.js"></script>
<style>
table {
	border-collapse: collapse;
	empty-cells: show;
}
th {
	text-align: left;
}
thead td {
	width: 11%;
}
tbody td {
	background: #fed;
}
th, td {
	border: 1px solid #bbb;
}
</style>
</head>
<body>
<h2>Delegate Tests (<span id="fileversion">x</span>)</h2>

<table id="changes">
<thead>
	<tr>
		<th>
			Controls:
		</th>
		<td id="select-one">
			<select>
				<option value='one1'>one1</option>
				<option value='one2'>one2</option>
				<option value='one3'>one3</option>
			</select>
			<select>
				<option value='two1'>two1</option>
				<option value='two2' selected="selected">two2</option>
				<option value='two3'>two3</option>
			</select>
		</td>
		<td id="select-mult">
			<select multiple="multiple">
				<option value='multi1'>multi1</option>
				<option value='multi2'>multi2</option>
				<option value='multi3'>multi3</option>
			</select>
		</td>
		<td id="checkbox">
			<input type="checkbox" name="mycheckbox" id="check1"/>
			<label for="check1">check1</label><br/>
			<input type="checkbox" name="mycheckbox" id="check2"/>
			<label for="check2">check2</label><br />
			<input type="checkbox" name="mycheckbox" id="check3" disabled="disabled"/>
			<label for="check3">check3</label>
		</td>
		<td id="radio">
			<input type="radio" name="myradio" id="radio1"/>
			<label for="radio1">Radio1</label><br/>
			<input type="radio" name="myradio" id="radio2"/>
			<label for="radio2">Radio2</label><br />
			<input type="radio" name="myradio" id="radio3" disabled="disabled"/>
			<label for="radio3">Radio3</label>
		</td>
		<td id="file">
			<input class="file_test" id="file1" type="file"/>
		</td>
		<td id="text">
			<input class='test' value='' id='input' size='10' />
			<input class='test' value='test' id='input2' size='10' readonly="readonly" />
		</td>
		<td id="textarea">
			<textarea rows='2'></textarea>
		</td>
		<td id="button">
			<button name="mybutton1" id="button1">Button</button><br />
			<button name="mybutton2" id="button2"><span>Button w/ child</span></button><br />
			<button name="mybutton3" id="button3" disabled="disabled">Button Disabled</button><br />
			<button name="mybutton4" id="button4" disabled="disabled"><span disabled="disabled">Button, child Disabled</span></button><br />
		</td>
	</tr>
</thead>
<tbody>
</tbody>
</table>
<p>NOTE: Only IE supports propertychange, beforeactivate, beforedeactivate; buttons do not support change events.</p>

<h2>Submit Tests</h2>
<table>
	<tr>
		<td>
			Submit each:
		</td>
		<td>
			<form action="" id="text_submit">
				<input class='test' type='text' value='Key Return To Submit'/>
			</form>
		</td>
		<td>
			<form action="" id="password_submit">
				<input class='test' type='password' value=''/>
			</form>
		</td>
		<td>
			<form action="" id="submit_submit">
				<input type='submit' value="Click Me To Submit" />
			</form>
		</td>
		<td>$(document).bind('submit')</td>
	</tr>
	<tr>
		<td>Results:</td>
		<td id='textSubmit' class="red">TEXT</td>
		<td id='passwordSubmit' class="red">PASSWORD</td>
		<td id='submitSubmit' class="red">BUTTON</td>
		<td id='boundSubmit' class="red">DOCUMENT</td>
	</tr>
</table>

	<form id="autosub"><input type=submit name=subme /></form>

<script type='text/javascript'>

$( "#fileversion" ).text( $.fn.jquery );

// Try an auto-submit, it should only fire once
$( function() {
	var triggered = false;
	$( "#autosub input" ).trigger( "keypress" );
	$( "body" ).on( "submit", "#autosub", function( e ) {
		e.preventDefault();
		e.stopPropagation();
		if ( triggered ) {
			alert( "autosubmit FAIL" );
		}
		triggered = true;
	} );
	$( "#autosub" ).submit().remove();
} );

// Events we want to track in row-order
var events = "bind-change live-change onX-change bind-propertychange live-beforeactivate live-focusin bind-focus live-beforedeactivate live-focusout bind-blur live-click live-keydown".split( " " ),
	counter = 0;
blinker = function( event ) {
	if ( !counter ) {
		$( "#changes tbody td" ).text( "" );
	}
	var $el = event.data,
		prev = $el.text();
	prev = prev ? prev + " | " : "";
	return $el
		.text( prev + ++counter + " " + ( this.value.replace( /^on$/, "" ) || this.id || this.checked || "" ) )
		.css( "backgroundColor", "#0f0" )
		.delay( 800 )
		.queue( function( next ) {
			$el.css( "backgroundColor", "#afa" );
			--counter;
			next();
		} );
};

for ( var i = 0; i < events.length; i++ ) {
	var m = events[ i ].split( "-" ),
		api = m[ 0 ],
		type = m[ 1 ],
		$row = $( "<tr><th>" + type + " " + api + "</th></tr>" );

	$( "#changes thead td" ).each( function() {
		var id = "#" + this.id,
			$cell = $( "<td></td>" );
		if ( api == "onX" ) {
			$( this ).find( "input, button, select, textarea" ).each( function() {
				this[ "on" + type ] = function( e ) {
 e = $.event.fix( e || event ); e.data = $cell; blinker.call( this, e );
};
			} );
		} else if ( api == "bind" ) {
			$( this ).find( "input, button, select, textarea" ).bind( type, $cell, blinker );
		} else {
			$( id + " input," + id + " button," + id + " select," + id + " textarea" ).live( type, $cell, blinker );
		}
		$row.append( $cell );
	} );
	$( "#changes tbody" ).append( $row );
}

jQuery.fn.blink = function() {
	return this
		.css( "backgroundColor", "green" )
		.text( ( parseInt( this.text(), 10 ) || 0 ) + 1 )
		.delay( 700 ).queue( function( next ) {
			jQuery( this ).css( "backgroundColor", "#afa" );
			next();
		} );
};

jQuery.fn.addSubmitTest = function( id, prevent ) {
	return this.live( "submit", function( e ) {
		if ( prevent ) {
			e.preventDefault();
		}
		jQuery( id ).blink();
	} );
};

$( "#text_submit" ).addSubmitTest( "#textSubmit", true );
$( "#password_submit" ).addSubmitTest( "#passwordSubmit", true );
$( "#submit_submit" ).addSubmitTest( "#submitSubmit", true );
$( "#prog_submit" ).addSubmitTest( "#submitSubmit", true );
$( document ).bind( "submit", function() {
	jQuery( "#boundSubmit" ).blink();
} );

</script>
</body>
</html>