htmlTableUi.js 11.8 KB
Newer Older
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
function htmltableUiToggleDiv(myId){
	// Create the selectors
	var tbody = "#"+myId+" div.body";
	var theader = "#"+myId+" div.header";
	var hideButton = "#"+myId+" #hidecontrol";
	var showButton = "#"+myId+" #showcontrol";
	
	// Verify display property (CSS) of tbody
	var display = $(tbody).css("display");

	if(display=="none"){
		$(tbody).show("slide",{ direction: "up" }, 200);
		$(theader).removeClass("ui-corner-bottom");
		$(hideButton).show();
		$(showButton).hide();
	}else{
		$(tbody).hide("slide",{ direction: "up" }, 200);
		$(theader).addClass("ui-corner-bottom");
		$(hideButton).hide();
		$(showButton).show();
	}
}

function htmltableUiToggleMode(myId){
	var buttonSelector = "#"+myId+" div.header table.header-table tr td.header-mode div.header-mode-button";
	// If in edit mode, change the rows
	if($(buttonSelector).hasClass("editmode")){
		htmltableUiView(myId);
	}else{
		htmltableUiEdit(myId);
	}
}

function htmltableUiEdit(myId){
	var buttonSelector = "#"+myId+" div.header table.header-table tr td.header-mode div.header-mode-button";
	var rowSelector = "#"+myId+" div.body table tbody tr";
	// Enter Edit Mode
	$(rowSelector).addClass("editrow");
	$(buttonSelector).addClass("editmode");
}

function htmltableUiView(myId){
	var buttonSelector = "#"+myId+" div.header table.header-table tr td.header-mode div.header-mode-button";
	var rowSelector = "#"+myId+" div.body table tbody tr";
	var formSelector = "#"+myId+" div.form";

	$(rowSelector).removeClass("editrow");
	$(buttonSelector).removeClass("editmode");
	$(formSelector).hide();
	htmltableUi_globalLastPos=null;
}


/*
 * Function to return the 
 * column headers.
 * Returns an array with all the 
 * column headers.
 */
function htmltableUiGetColumns(myId){
	// Get the column's headers
	var columnsSelector = "#"+myId+" div.body table.body-table tr.body-header th div.column-name span"
	var columns = new Array();
	$(columnsSelector).each(function(index){
		columns[index] = $(this).text();
	});	
	return columns;
}

/*
 * Global Variables
 * 
 * These are variables needed to store globally
 * some important parameters such as:
 * htmltableUi_globalId - This is the id of the widget been
 * used.
 * htmltableUi_globalRow - This is the row being clicked
 * html_globalFormData - Data extracted from the form.
 */
//Global Variables needed for row manipulation
var htmltableUi_globalId=null;
var htmltableUi_globalRow=null;
var htmltableUi_globalFormData=null;
var htmltableUi_globalLastPos=null;

/*
 * htmltableShowUiForm
 *  
 * This function is used to show the
 * edit form when a row is clicked. It has the following 
 * arguments:
 * myId - This is the Id of the particular widget that is being manipulated
 * myRow - This is the element that was clicked
 * formTitle - The title to be shown in the form.
 */
function htmltableShowUiForm(myId,myRow,formTitle){
	// Widget Reference
	var widgetSelector = "#"+myId;
	var divSelector = "#edit-form";//+myId+" div.form";
	var titleSelector = "#edit-form-header-title";
	var formSelector = "#table-form";//+myId+"-form";
	var rowSelector = "#"+myId+" div.body table tbody tr";
	
	// Close and Destroy form if already
	// opened.
	htmltableCloseUiForm();
	
	// Check if row is in edit mode
	if($(rowSelector).hasClass("editrow")){
		// Set the global variable with the clicked row
		htmltableUi_globalId = myId;
		htmltableUi_globalRow = myRow;

		// Set Form Title
		//$(divSelector+" .ui-widget-header").text(formTitle);
		$(titleSelector).text(formTitle);
		
		// Show the form
		var widgetLeft = $(widgetSelector).offset().left;
		var widgetTop = $(widgetSelector).position().top;
		var widgetWidth = $(widgetSelector).outerWidth();
		var widgetHeight = $(widgetSelector).outerHeight();
		if(htmltableUi_globalLastPos == null){
			$(divSelector).css('top',widgetTop);
			$(divSelector).css('left',widgetLeft+widgetWidth/4);
		}else{
			$(divSelector).css('top',htmltableUi_globalLastPos.top);
			$(divSelector).css('left',htmltableUi_globalLastPos.left);
		}
		$(divSelector).show("slide",{ direction: "up" }, 100);
		
		var columns = htmltableUiGetColumns(myId);
		
		// Get the html elements of the row that was clicked
		// and create the form
		var myString = "";
		$(myRow).children().each(function(index) {
			var openDiv = "<div class='edit-table-row'><table><tbody><tr>";
			var label = "<td><label for='"+columns[index]+"'>"+columns[index]+"</label></td>";
			var input = "<td><input type='text' name='"+columns[index]+"' class='text' value='"+$(this).text()+"'></input></td>";
			myString = myString + openDiv + label + input + "</tr></tbody></table></div>";
		});
		var changeButton = "<button type='button' class='ui-button ui-button-text-only ui-widget ui-state-default ui-corner-all' onclick='htmltableUiModifyRow(htmltableUi_globalRow);'>Change</button>";
		var closeButton = "<button type='button' class='ui-button  ui-button-text-only ui-widget ui-state-default ui-corner-all' onclick='htmltableCloseUiForm();'>Close</button>";
		var submitButton = "<input type='submit' class='ui-button  ui-button-text-only ui-widget ui-state-default ui-corner-all' value='submit' class='button'></input>";
		$(formSelector).append(myString+changeButton+closeButton);
		$(formSelector+" button").button();
		var position = $(rowSelector).offset();
		$(divSelector).offset(position);
		$(divSelector).draggable();
	}
}

/*
 * htmltableUiModifyRow
 * 
 * This function changes the table row
 * using the data in the form.
 * It uses one parameter:
 * myRow - Object representing the tr element
 * that was clicked.
 */
function htmltableUiModifyRow(myRow){
	htmltableUi_globalFormData = new Array();
	
	// Get all the form entries
	$("#table-form input.text").each(function(index){
		htmltableUi_globalFormData[index] = $(this).val();
	});
	
	// Write to the table
	$(myRow).children().each(function(index) {
		$(this).text(htmltableUi_globalFormData[index]);
	});
	$(myRow).addClass("modified");
	
	htmltableUi_globalFormData=null;
	
	// Show send button
	var sendButton = "#"+htmltableUi_globalId+" div.header table.header-table tr td.header-mode div.header-send-button";
	$(sendButton).show();
	
	//Update Table Sorter Plugin
	$("#"+htmltableUi_globalId+"-body-table").trigger("update");
}

/*
 * htmltableCloseUiForm
 * 
 * This function cleanups the widget
 * First it hides the Form's div container
 * then it removes all form elements from the form
 * Then it resets all the global variables
 */
function htmltableCloseUiForm(){
	$("#edit-form").hide();	
	$("#table-form").children().remove();
	htmltableUi_globalId=null;
	htmltableUi_globalRow=null;
	htmltableUi_globalFormData=null;
}


/*
 * htmltableExportCsv
 * 
 * This function sends the whole table to 
 * a controller action using an AJAX http request
 * Is the responsibility of the controller action to
 * create the CSV file with the data sent through this
 * function.
 */
function htmltableExportCsv(myId,url){
	var rowSelector = "#"+myId+" div.body table tbody tr";
	var titleSelector = "#"+myId+" div.header table.header-table div.header-container span.title";
	var extraSelector = "#"+myId+" div.header table.header-table div.header-container span.extra";
	var subtitleSelector = "#"+myId+" div.body table.body-table thead tr.body-subtitle span.subtitle"
	var footerSelector = "#"+myId+" div.body table.body-table tfoot tr.body-footer td span.footer";

	var rows = new Object();
	var columns = htmltableUiGetColumns(myId);
	
	rows['id'] = myId;
	rows['title'] = $(titleSelector).text();
	rows['subtitle'] = $(subtitleSelector).text();
	rows['extra'] = $(extraSelector).text();
	rows['columns'] = columns;
	
	//<---- GET ALL ROWS ---->
	$.each($(rowSelector),function(key,value){
		var rowKey = "row-"+key;
		var myElements = new Object();

		$(value).children().each(function(index){
			myElements[columns[index]] = $(this).text();
		});
		
		//...and add them as key-value pairs
		//...in an object called rows
		rows[rowKey] = myElements;
		myElements = null;
	});
	
	rows['footer'] = $(footerSelector).text();
	rows['timezone'] = get_time_zone_offset();
	
	//<---- SEND XHR ----->
	var jqXHR = $.ajax({
		url: url,
		type: "POST",
		data: $.param(rows),
		beforeSend: function(xhr,settings){
			$("#"+myId+"-send-button")
				.addClass("ajaxLoading");
		},
		success: function(fileUrl,status,xhr){
			// Open the file URL
			window.open(fileUrl);
		},
		error: function(xhr,errorText,errorObj){
			alert(errorText);
		},
		complete: function(xhr,status){
			$(rowSelector).removeClass("modified");
			$("#"+myId+"-send-button")
				.removeClass("ajaxLoading")
				.hide();
		}
	});
	
	htmltableUiView(myId);
	htmltableCloseUiForm();
}


/*
 * htmltableUiSend
 * 
 * This function sends the the modified 
 * rows through an AJAX XHTTPRequest. 
 * It uses three parameters:
 * myId - the widget id
 * url - url for the request in a Controller/Action format
 * target - an element where the answer of the request should be
 * directed to
 */
function htmltableUiSend(myId,url,target){
	var modifiedSelector = "#"+myId+" div.body table tbody tr.modified";
	var rowSelector = "#"+myId+" div.body table tbody tr";
	var titleSelector = "#"+myId+" div.header table.header-table div.header-container span.title";
	var extraSelector = "#"+myId+" div.header table.header-table div.header-container span.extra";
	var subtitleSelector = "#"+myId+" div.body table.body-table thead tr.body-subtitle span.subtitle"
	var footerSelector = "#"+myId+" div.body table.body-table tfoot tr.body-footer td span.footer";

	var rows = new Object();
	var columns = htmltableUiGetColumns(myId);
	
	rows['id'] = myId;
	rows['title'] = $(titleSelector).text();
	rows['subtitle'] = $(subtitleSelector).text();
	rows['extra'] = $(extraSelector).text();
	rows['columns'] = columns;
	
	//<---- GET MODIFIED ROWS ---->
	// For each modified row....
	$.each($(modifiedSelector),function(key,value){
		//var rowKey = "htmltable-"+myId+"-row-"+key;
		var rowKey = "row-"+key;
		var myElements = new Object();

		$(value).children().each(function(index){
			myElements[columns[index]] = $(this).text();
		});
		
		//...and add them as key-value pairs
		//...in an object called rows
		rows[rowKey] = myElements;
		myElements = null;
	});
	
	rows['footer'] = $(footerSelector).text();
	rows['timezone'] = get_time_zone_offset();
	
	//<---- SEND XHR ----->
	var jqXHR = $.ajax({
		url: url,
		type: "POST",
		data: $.param(rows),
		beforeSend: function(xhr,settings){
			$("#"+myId+"-send-button")
				.addClass("ajaxLoading");
			//alert(decodeURIComponent($.param(rows)));
		},
		success: function(returnedData,status,xhr){
			alert(returnedData);
			//$(target).text(returnedData);
		},
		error: function(xhr,errorText,errorObj){
			alert(errorText);
		},
		complete: function(xhr,status){
			$(rowSelector).removeClass("modified");
			$("#"+myId+"-send-button")
				.removeClass("ajaxLoading")
				.hide();
		}
	});
	
	htmltableUiView(myId);
	htmltableCloseUiForm();
}


/*
 * htmltableUiSort
 * 
 * This function enables the TableSorter 2.0
 * jQuery plugin on the table for client sided
 * sorting. It has three parameters:
 * myId - the widget's id
 * sortColumn - an index starting from 0 referring
 * to the column to use for sorting initially
 * sortOrder - 0-ASC, 1-DESC
 * 
 */
function htmltableUiSort(myId,sortColumn,sortOrder){
	// Enable table sorter for the table
	var selector = '#'+myId+'-body-table';
	
	$(selector).tablesorter({
		cssHeader: "unsorted",
		sortList: [[sortColumn,sortOrder]]
	});

	// Set the cursor to a hand or pointer over the
	// cells used to sort.
	selector = selector + ' thead tr.body-header th';
	$(selector).css("cursor","pointer");
}

/*
 * get_time_zone_offset
 * 
 * This function provides the GMT timezone difference
 * from the browser to be used in the server.
 * http://josephscott.org/archives/2009/08/detecting-client-side-time-zone-offset-via-javascript/
 */
function get_time_zone_offset( ) {
	var current_date = new Date( );
	var gmt_offset = current_date.getTimezoneOffset( ) / 60;
	return (gmt_offset);
}