| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | <html> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script src="jquery.js"></script> | 
					
						
							|  |  |  | <script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function focus(elem){ | 
					
						
							|  |  |  | 	$('.current.square').removeClass('current') | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 	$(elem) | 
					
						
							|  |  |  | 		.closest('.square') | 
					
						
							|  |  |  | 			.addClass('current') | 
					
						
							|  |  |  | 			.trigger('focus') | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | function clickHandler(){ | 
					
						
							|  |  |  | 	focus(this) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-16 15:45:49 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-16 15:56:22 +04:00
										 |  |  | /* Focus next/prev container element | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * This will shift focus within a container. | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * If we reach the start/end of the container, we will "warp" around it  | 
					
						
							|  |  |  | * and focus an element from the other end. | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | // XXX make this customizable (selector-wise)... | 
					
						
							|  |  |  | function shiftFocus(n){ | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | 	var cur = $('.current.square') | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 	if(n > 0){ | 
					
						
							|  |  |  | 		direction = 'next' | 
					
						
							|  |  |  | 	} else if(n < 0){ | 
					
						
							|  |  |  | 		direction = 'prev' | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2013-04-16 15:45:49 +04:00
										 |  |  | 		return cur | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// trigger the event... | 
					
						
							|  |  |  | 	cur.closest('.container') | 
					
						
							|  |  |  | 		.trigger('shiftfocus', n) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 	// XXX will adding a singular [direction] branch make things faster | 
					
						
							|  |  |  | 	//		for n = +/-1 ??? | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 	var dir = cur[direction+'All']('.square') | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-16 15:56:22 +04:00
										 |  |  | 	// see if we need to warp arund... | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 	if(dir.length < Math.abs(n)){ | 
					
						
							| 
									
										
										
										
											2013-04-16 15:45:49 +04:00
										 |  |  | 		var sq = cur.closest('.container').children('.square') | 
					
						
							|  |  |  | 		var i = sq.index(cur) | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 		return focus($(sq[ n > 0  | 
					
						
							| 
									
										
										
										
											2013-04-16 15:45:49 +04:00
										 |  |  | 								? (i + n) % sq.length  | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 								: sq.length - (Math.abs(i + n) % sq.length) ])) | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | 	// shift the current element... | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 	return focus($(dir[Math.abs(n)-1])) | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-16 15:45:49 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | // these will roll the container... | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | // XXX make this container context-aware... | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | function rollContainer(n){ | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 	var sq = $('.current.square') | 
					
						
							|  |  |  | 				.closest('.container') | 
					
						
							|  |  |  | 					.children('.square') | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 	// right... | 
					
						
							|  |  |  | 	if(n > 0){ | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 		return $(sq.splice(0, n)).appendTo($('.container')) | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 	// left... | 
					
						
							|  |  |  | 	} else if(n < 0){ | 
					
						
							|  |  |  | 		var l = sq.length | 
					
						
							|  |  |  | 		return $(sq.splice(l+n, l)).prependTo($('.container')) | 
					
						
							|  |  |  | 	// 0... | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2013-04-16 15:45:49 +04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | // misc demo stuff... | 
					
						
							|  |  |  | var UPDATE_ELEMENT_TEXT = true | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | function toggleNumberUpdate(){ | 
					
						
							|  |  |  | 	if(UPDATE_ELEMENT_TEXT){ | 
					
						
							|  |  |  | 		UPDATE_ELEMENT_TEXT = false | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		UPDATE_ELEMENT_TEXT = true | 
					
						
							| 
									
										
										
										
											2013-04-16 15:45:49 +04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | // init things... | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | $(function(){ | 
					
						
							|  |  |  | 	$('.container') | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 		// NOTE: this event will get completed BEFORE the element is focused. | 
					
						
							|  |  |  | 		.on('shiftfocus', function(evt, n){ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 			// update squares -- numbers... | 
					
						
							|  |  |  | 			if(UPDATE_ELEMENT_TEXT){ | 
					
						
							|  |  |  | 				var sq = $(this).children('.square') | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 				if(n > 0){ | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 					var s = sq.last().text()*1 + 1 | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 				} else if(n < 0){ | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 					var s = sq.first().text()*1 + n | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					return | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				rollContainer(n).each(function(i, e){ | 
					
						
							|  |  |  | 					$(this).text(i + s) | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 			// roll as-is... | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				rollContainer(n) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	$('.square') | 
					
						
							|  |  |  | 		.click(clickHandler) | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <style> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .container { | 
					
						
							|  |  |  | 	position: relative; | 
					
						
							|  |  |  | 	display: inline-block; | 
					
						
							|  |  |  | 	height: auto; | 
					
						
							|  |  |  | 	min-width: 0px; | 
					
						
							|  |  |  | 	overflow: visible; | 
					
						
							|  |  |  | 	white-space: nowrap; | 
					
						
							|  |  |  | 	font-size: 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	background: gray; | 
					
						
							|  |  |  | 	padding: 10px; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .square { | 
					
						
							|  |  |  | 	position: relative; | 
					
						
							|  |  |  | 	display: inline-block; | 
					
						
							|  |  |  | 	vertical-align: middle; | 
					
						
							|  |  |  | 	text-align;left; | 
					
						
							|  |  |  | 	width: 100px; | 
					
						
							|  |  |  | 	height: 100px; | 
					
						
							|  |  |  | 	background: silver; | 
					
						
							|  |  |  | 	font-size: 12pt; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .current.square { | 
					
						
							|  |  |  | 	background: red; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <body> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-16 16:36:01 +04:00
										 |  |  | <button onclick="shiftFocus(-5)"><<</button> | 
					
						
							|  |  |  | <button onclick="shiftFocus(-1)"><</button> | 
					
						
							|  |  |  | <button onclick="shiftFocus(1)">></button> | 
					
						
							|  |  |  | <button onclick="shiftFocus(5)">>></button> | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | <button onclick="toggleNumberUpdate()">toggle square number update</button> | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 05:31:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-12 17:24:06 +04:00
										 |  |  | <div class="meta-container"> | 
					
						
							|  |  |  | 	<div class="container"> | 
					
						
							|  |  |  | 		<div class="square">1</div> | 
					
						
							|  |  |  | 		<div class="square">2</div> | 
					
						
							|  |  |  | 		<div class="square current">3</div> | 
					
						
							|  |  |  | 		<div class="square">4</div> | 
					
						
							|  |  |  | 		<div class="square">5</div> | 
					
						
							|  |  |  | 		<div class="square">6</div> | 
					
						
							|  |  |  | 		<div class="square">7</div> | 
					
						
							|  |  |  | 	</div> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </body> | 
					
						
							|  |  |  | <!-- vim:set ts=4 sw=4 : --> | 
					
						
							|  |  |  | </html> |