Parcourir la source

TableAjax mv vars to state (uniqueCol, colsSorted)

Piotr Labudda il y a 10 ans
Parent
commit
259024e096
1 fichiers modifiés avec 70 ajouts et 71 suppressions
  1. 70 71
      SE/se-lib/TableAjax.php

+ 70 - 71
SE/se-lib/TableAjax.php

@@ -554,19 +554,18 @@ class TableAjax extends ViewAjax {
 		var _mapEditorWrap; // map editor wrapper node
 		var _mapEditorDialog; // map editor jquery ui dialog node
 
-		var _state;// state - to replace variables below (date, ...)
+		var _state = {};// state - to replace variables below (date, ...)
 		var _data; //columns and rows
 		var _currPage = 1; // current page
 		var _totalPages; // total pages
 		var _currSortCol; // current sorting column
 		var _currSortFlip = false; // current sorting direction
 		var _currDpOp; // clicked datetimepicker operator
-		var _filterCols = {}; // array with current filters
+		var _filterCols = {}; // array with current filters /// TODO: mv to _state.filterCols
 		var _filterFields = {}; // array with filter DOM elements
 		var _specialFilters = {}; // array with current special filters
 		var _filterTimeout; // timer for delayed filtering
-		var _uniqueCol; // reference to column with the unique property
-		var _uniqueCols = {}; // array with checked rows
+		var _uniqueCols = {}; // array with checked rows /// TODO: mv to _state.filterCols
 		var _checkToggleChecked = false; // check-all toggle state
 		var _exportFieldsSelect = {};
 
@@ -577,6 +576,7 @@ class TableAjax extends ViewAjax {
 		 */
 		priv.init = function () {
 			_uiNodeCont = priv.options.id;
+			_state = {};// init state
 			priv.initEvents();
 			priv.initialRender();// set up _uiNode$...
 			priv.options.types.string = ((priv.options.types || {}).string || {});
@@ -655,7 +655,6 @@ class TableAjax extends ViewAjax {
 			_mapEditorWrap = $('<div class="mapEditor" style="display:none"></div>').insertAfter(_foot);
 				_mapEditor = $('<div class="mapEditor-map"></div>').appendTo(_mapEditorWrap);
 			_popoverCell = $('<div class="popoverCell" style="display:none"></div>').insertAfter(_foot);
-
 			_foot = _head = _body = undefined;// TODO: refactor
 		}
 
@@ -677,11 +676,6 @@ class TableAjax extends ViewAjax {
 				_uiNode$Table.find('thead').replaceWith(_head);
 			}
 
-			//sort the columns in index order
-			var colsSorted = Object.keys(_data.cols).sort(function (a, b) {
-				return _data.cols[a].index - _data.cols[b].index;
-			});
-
 			//create the header sorting row
 			if (!_headSort) {
 				_head.find('.sort i').tooltip('hide');
@@ -704,7 +698,7 @@ class TableAjax extends ViewAjax {
 				}
 
 				//create the checkall toggle
-				if (_uniqueCol && priv.options.checkboxes) {
+				if (_state.uniqueCol && priv.options.checkboxes) {
 					var checked = _checkToggleChecked ? 'checked' : '';
 					var headCell = $('<th></th>').appendTo(_headSort);
 					var elem = $('<input {0} class="checkToggle" type="checkbox" />'.f(checked)).appendTo(headCell);
@@ -712,8 +706,8 @@ class TableAjax extends ViewAjax {
 				}
 
 				//create the sortable headers
-				for (var i = 0; i < colsSorted.length; i++) {
-					var column = colsSorted[i];
+				for (var i = 0; i < _state.colsSorted.length; i++) {
+					var column = _state.colsSorted[i];
 					var props = _data.cols[column];
 					if (!props.hidden) {
 						var headCell = $('<th class="ta-ordering"></th>').appendTo(_headSort);
@@ -804,7 +798,7 @@ class TableAjax extends ViewAjax {
 				}
 
 				//create the filter checkbox
-				if (_uniqueCol && priv.options.checkboxes) {
+				if (_state.uniqueCol && priv.options.checkboxes) {
 					tooltip = priv.options.types.bool.filterTooltip || 'Toggle between:<br/>indeterminate,<br/>checked,<br/>unchecked';
 					headCell = $('<th></th>').appendTo(_headFilter);
 					elem = $('<input class="filter indeterminate" checked type="checkbox" />').appendTo(headCell);
@@ -833,8 +827,8 @@ class TableAjax extends ViewAjax {
 				}
 
 				//create the column filters
-				for (var i = 0; i < colsSorted.length; i++) {
-					var column = colsSorted[i];
+				for (var i = 0; i < _state.colsSorted.length; i++) {
+					var column = _state.colsSorted[i];
 					var props = _data.cols[column];
 					tooltip = props.filterTooltip === true ? undefined : props.filterTooltip === false ? '' : props.filterTooltip;
 					placeHolder = props.placeHolder === true ? undefined : props.placeHolder === false ? '' : props.placeHolder;
@@ -990,7 +984,7 @@ class TableAjax extends ViewAjax {
 
 					if (priv.options.rowFunctions || priv.options.filtersClean) {
 						var cell = $('<td class="text-right stickyCol1"></td>').appendTo(row);
-						var cellID = _uniqueCol || 'ID';
+						var cellID = _state.uniqueCol || 'ID';
 						cellID = (cellID in props)? props[cellID] : null;
 						$.map(priv.options.rowFunctions, function(funObj, funName){
 							$(funObj.f(cellID)).appendTo(cell);
@@ -998,8 +992,8 @@ class TableAjax extends ViewAjax {
 					}
 
 					//create checkbox
-					if (_uniqueCol && priv.options.checkboxes) {
-						var check = _uniqueCols[props[_uniqueCol]] != undefined ? 'checked' : '';
+					if (_state.uniqueCol && priv.options.checkboxes) {
+						var check = _uniqueCols[props[_state.uniqueCol]] != undefined ? 'checked' : '';
 						var checkable = props['checkable'] === false ? 'disabled' : '';
 						var cell = $('<td></td>').appendTo(row);
 						$('<input class="unique" {0} {1} type="checkbox" />'.f(check, checkable)).appendTo(cell);
@@ -1007,13 +1001,13 @@ class TableAjax extends ViewAjax {
 
 					//create cells
 					var primaryKeyField = '';
-					for (var i = 0; i < colsSorted.length; i++) {
-						var key = colsSorted[i];
+					for (var i = 0; i < _state.colsSorted.length; i++) {
+						var key = _state.colsSorted[i];
 						var val = props[key];
 						if (!_data.cols[key]) return;
 						if (_data.cols[key].unique) row.data('unique', val);
 
-						primaryKeyField = _uniqueCol || 'ID';
+						primaryKeyField = _state.uniqueCol || 'ID';
 						cellID = (primaryKeyField in props)? props[primaryKeyField] : null;
 
 						if (!_data.cols[key].hidden) {
@@ -1189,7 +1183,7 @@ class TableAjax extends ViewAjax {
 					while (rowsAdded < priv.options.pageSize) {
 						var row = $('<tr></tr>').appendTo(_body);
 
-						if (_uniqueCol && priv.options.checkboxes) {
+						if (_state.uniqueCol && priv.options.checkboxes) {
 							var cell = $('<td></td>').appendTo(row);
 							$('<input disabled type="checkbox" />').appendTo(cell);
 						}
@@ -1602,7 +1596,8 @@ class TableAjax extends ViewAjax {
 		 calls the webservice(if defined).
 		 used only inside priv.init
 		 */
-		priv.update = function (callback, skipCols, resetChecked) {
+		priv.update = function (callback) {
+			var skipCols, resetChecked;// undefined
 			if (!priv.options.url) {
 				if (priv.options.debug) console.log('no url found L.<?php echo __LINE__; ?>');
 				return;
@@ -1654,10 +1649,11 @@ class TableAjax extends ViewAjax {
 					if (priv.options.debug) console.log('request finished L.<?php echo __LINE__; ?>');
 
 					// assign the new data
-					if (data.d && data.d.cols)
-						priv.setStateData(data.d, skipCols, resetChecked);
-					else
-						priv.setStateData(data, skipCols, resetChecked);
+					if (data.d && data.d.cols) {
+						priv.setStateCols(data.d.cols);
+					} else if (data && data.cols) {
+						priv.setStateCols(data.cols);
+					}
 
 					// set initial filters (_filterCols)
 					if (filtersInitSet && _data && _data.cols) {
@@ -1713,6 +1709,7 @@ class TableAjax extends ViewAjax {
 					renderParts = [];
 			if (state.data) {
 				if (state.data.cols && state.data.cols.length > 0) {
+					priv.setStateCols(state.data.cols);
 					priv.setStateData(state.data);
 					renderParts.push('head');
 				} else {
@@ -1728,15 +1725,50 @@ class TableAjax extends ViewAjax {
 			}
 		};
 
+		priv.setStateCols = function (cols) {
+			_state.cols = cols;
+			_state.uniqueCol = undefined;
+			// fix col name - props.column
+			$.each(_state.cols, function (col, props) {
+				props.column = col;
+			});
+			// fix col types - default 'string'
+			$.each(_state.cols, function (col, props) {
+				if (!props.type) cols[col].type = "string";
+			});
+			// fix props.unique - set false if not set
+			// set _state.uniqueCol - first column defined as unique
+			$.each(_state.cols, function (col, props) {
+				//if several unique columns is defined, use the first.
+				if (props.unique) {
+					if (!_state.uniqueCol) _state.uniqueCol = col;
+					else props.unique = false;
+				}
+			});
+			// fix props.filter - set true if not set - TODO: allow filter this col?
+			$.each(_state.cols, function (col, props) {
+				if (props.filter == undefined) props.filter = true;
+			});
+			if (_state.uniqueCol) {
+				//create a unique column definition
+				_state.cols["unique"] = {
+					column: "unique",
+					type: "unique",
+					index: -1,
+					hidden: true
+				};
+			}
+			_state.colsSorted = Object.keys(_state.cols).sort(function (a, b) {
+				return _state.cols[a].index - _state.cols[b].index;
+			});
+		};
+
 		priv.setStateData = function (pData, skipCols, resetChecked) {
 			var data = $.extend(true, {}, pData);
 			data.fromRow = _data && _data.fromRow || 0;
 			data.toRow = _data && _data.toRow || 0;
 
-			//use previous column definitions?
-			skipCols = skipCols || false;
-			if (skipCols) data.cols = _data.cols;
-			//else _filterCols = {};
+			data.cols = _state.cols;// always use old cols - change cols mved to priv.setStateCols
 
 			_data = data;
 			_data.rowsOrg = _data.rows;
@@ -1755,31 +1787,6 @@ class TableAjax extends ViewAjax {
 				_data.toRow = Math.max(_data.toRow, _data.total);
 			}
 
-			//wash the new data a bit
-			_uniqueCol = "";
-			$.each(_data.cols, function (col, props) {
-				if (!props.type) _data.cols[col].type = "string";
-
-				//if several unique columns is defined, use the first.
-				if (props.unique) {
-					if (!_uniqueCol) _uniqueCol = col;
-					else props.unique = false;
-				}
-
-				props.column = col;
-
-				//set any initial filter
-				if (!skipCols) {
-					if (props.filter == undefined) props.filter = true;
-					if (props.filter && typeof props.type != "bool" && typeof props.filter != "boolean") {
-						_filterCols[col] = _filterCols[col] || {
-							filter: String(props.filter),
-							col: props
-						};
-					}
-				}
-			});
-
 			//keep any previously checked rows around?
 			if (resetChecked === true || resetChecked === undefined)
 				_uniqueCols = {};
@@ -1788,19 +1795,11 @@ class TableAjax extends ViewAjax {
 					_uniqueCols[key] = priv.getRow(key);
 			}
 
-			if (_uniqueCol) {
-				//create a unique column definition
-				_data.cols["unique"] = {
-					column: "unique",
-					type: "unique",
-					index: -1,
-					hidden: true
-				};
-
+			if (_state.uniqueCol) {
 				//add rows that needs to be pre-checked
 				$.each(_data.rows, function (index, row) {
 					if (row["checked"] === true)
-						_uniqueCols[row[_uniqueCol]] = row;
+						_uniqueCols[row[_state.uniqueCol]] = row;
 				});
 			}
 		};
@@ -1831,7 +1830,7 @@ class TableAjax extends ViewAjax {
 		priv.getRow = function (unique) {
 			var row;
 			$.each(_data.rowsOrg, function (i, r) {
-				if (r[_uniqueCol] == unique) {
+				if (r[_state.uniqueCol] == unique) {
 					row = r;
 					return false;
 				}
@@ -1914,6 +1913,7 @@ class TableAjax extends ViewAjax {
 
 			if (filtersActive) {
 				// TODO: dont remove value from forceFilterInit fields - no field names in search fields?
+				/// TODO: rerender _head (thead.filter)
 				var elems = _head.find('tr.filter').find('input');
 				_head.find('tr.filter').find('input').val('').prop('disabled', false);
 				priv.filter();
@@ -2327,7 +2327,7 @@ class TableAjax extends ViewAjax {
 		 what: row data and other info is returned to caller
 		 */
 		priv.rowClicked = function (e) {
-			if (!_uniqueCol) {
+			if (!_state.uniqueCol) {
 				if (priv.options.debug) console.log('no unique column specified');
 				return;
 			}
@@ -2348,7 +2348,6 @@ class TableAjax extends ViewAjax {
 					checked: isChecked
 				});
 			}
-
 		};
 
 		/**
@@ -2912,7 +2911,7 @@ function tableAjaxBackToTable() {
 
 	// reload first page
 	var con = jQuery('#<?php echo $this->_htmlID; ?>');
-	con.TableAjaxLoadPage(0);// TODO: load current page
+	con.TableAjaxLoadPage();// TODO: load current page
 }
 
 function tableAjaxCopy(args) {