1 //require <jquery.packed.js>
  2 (function(){
  3 
  4 	jQuery(document).ready(function($){
  5 		
  6 		var resultList = $('#result_list');
  7 		var thead = $('thead', resultList);
  8 		var tbody = $('tbody', resultList);
  9 		var headingRow = $('tr', thead);
 10 		if ( typeof(window.xataface) == 'undefined' ) window.xataface = {};
 11 		window.xataface.query = {};
 12 		var queryJson = resultList.attr('data-xataface-query');
 13 		if ( queryJson ) eval('window.xataface.query = '+queryJson+';');
 14 		
 15 		
 16 		var searchRow = $('tr', thead).clone();
 17 		headingRow.addClass('table-headings');
 18 		searchRow.hide();
 19 		$('th', searchRow).html('');
 20 		$('th.searchable-column', searchRow).each(function(){
 21 			$(this).html('<div><input class="column-search-field" name="'+$(this).attr('data-column')+'" type="text"/></div>');
 22 			var fld = $('input', this);
 23 			
 24 			var q = xataface.query;
 25 			if ( typeof(q[$(this).attr('data-column')]) != 'undefined' ){
 26 				fld.val(q[$(this).attr('data-column')]);
 27 			}
 28 		});
 29 		$('th', headingRow).each(function(){
 30 			$('th[data-column="'+$(this).attr('data-column')+'"]', searchRow).each(function(){
 31 				$(this)
 32 						.css('padding', 0)
 33 						.css('margin', 0)
 34 						;
 35 						
 36 					$('div', this).css({
 37 						position: 'relative',
 38 						margin: 0,
 39 						padding: 0,
 40 						width: 'auto',
 41 						height: '1.5em'
 42 						
 43 					});
 44 					var width = $('div', this).width();
 45 					var height = $('div', this).height();
 46 					$('input', this).css({
 47 						position: 'absolute',
 48 						
 49 						padding: 0,
 50 						margin: 0,
 51 						top: 0,
 52 						left: 0,
 53 						right: 0,
 54 						bottom: 0
 55 						
 56 						
 57 					});
 58 			});
 59 		});
 60 		
 61 		$('th.searchable-column', headingRow).each(function(){
 62 			//$(this).prepend('<img class="column-search-button" style="float:right; cursor:pointer" src="'+DATAFACE_URL+'/images/search_icon.gif"/>');
 63 			//var btn = $('.column-search-button', this);
 64 			var th = this;
 65 			var width = $(th).width();
 66 			
 67 			$(th).click(function(){
 68 				if ( searchRow.is(':visible') ){
 69 					searchRow.hide();
 70 					searchRow.css('display','none');
 71 				} else {
 72 					
 73 					//searchRow.fadeIn(500, function(){
 74 					searchRow.show();
 75 					searchRow.css('display','');
 76 					$(searchRow).each(function(){
 77 						//alert('here');
 78 						$('div', this).each(function(){
 79 							var width = $(this).width();
 80 							
 81 							$('input', this).animate({width: width-2}, 500);
 82 						});
 83 						//alert('now');
 84 					});
 85 					//alert('there');
 86 					$('th input', searchRow).each(function(){
 87 						//alert($(this).attr('name'));
 88 						//$(this).width($('a', th).width()*0.75);
 89 						//alert('hello');
 90 						if ( $(this).attr('name') == $(th).attr('data-column') ){
 91 							//alert('world');
 92 							//alert('here');
 93 							$(this).focus();
 94 							$(this).select();
 95 							//alert('universe');
 96 						}
 97 						//alert('god');
 98 					});
 99 				}
100 				
101 			});
102 			
103 			$('a', th).click(function(e){
104 				//alert('about to stop');
105 				e.stopPropagation();
106 				//alert('stopped');
107 			});
108 		});
109 		
110 		$('th.searchable-column input', searchRow).keypress(function(e){
111 			var code = (e.keyCode ? e.keyCode: e.which);
112 			if ( code == 13 ){
113 				submitSearch();
114 				e.preventDefault();
115 			}
116 		});
117 		
118 		//searchRow.hide();
119 		thead.append(searchRow);
120 		
121 		
122 		function submitSearch(){
123 		
124 			var query = xataface.query;
125 			$('th.searchable-column input', searchRow).each(function(){
126 				query[$(this).attr('name')] = $(this).val();
127 			});
128 			
129 			// now let's dismantle the existing query
130 			var search = window.location.search;
131 			if ( !search ) search = '?';
132 			//alert(search.substring(1));
133 			
134 			var terms = search.substring(1).split('&');
135 			var out = [];
136 			var query2 = query;
137 			$.each(terms, function(){
138 				var parts = this.split('=');
139 				if ( parts.length <= 1 ) out.push(this);
140 				if ( typeof(query[decodeURIComponent(parts[0])]) != 'undefined' ){
141 					parts[1] = encodeURIComponent(query[decodeURIComponent(parts[0])]);
142 					delete query2[decodeURIComponent(parts[0])];
143 				}
144 				if ( parts[1] ){
145 					out.push(parts[0]+'='+parts[1]);
146 				}
147 				
148 				
149 				
150 				
151 			});
152 			
153 			$.each(query2, function(k,v){
154 				out.push(encodeURIComponent(k)+'='+encodeURIComponent(v));
155 			});
156 			
157 			search = '?'+out.join('&');
158 			window.location.search = search;
159 			
160 			
161 		
162 		}
163 		
164 		
165 		
166 	
167 	});
168 })();