فهرست منبع

updated TableAjax, avoid fetch data when has function

Piotr Labudda 8 سال پیش
والد
کامیت
8ed5784241
1فایلهای تغییر یافته به همراه146 افزوده شده و 166 حذف شده
  1. 146 166
      SE/se-lib/TableAjax.php

+ 146 - 166
SE/se-lib/TableAjax.php

@@ -406,23 +406,23 @@ class TableAjax extends ViewAjax {
 		UI::showMessagesForTable($this->_tbl);
 		UI::endContainer();
 		$namespace = $acl->getNamespace();
-		?>
-		<div class="AjaxTableCont">
-			<ul class="breadcrumb">
-				<?php
-				echo UI::h('li', [], [
+		echo UI::h('div', [ 'class' => "AjaxTableCont" ], [
+			UI::h('ul', [ 'class' => "breadcrumb" ], [
+				UI::h('li', [], [
 					UI::h('a', [ 'href' => "#", 'onclick' => "return tableAjaxBackToTable();" ], $this->getLabelHtml() ),
-				]);
-				if ($hasPermCreate) {
-					echo UI::h('li', [], [
+				]),
+
+				($hasPermCreate)
+				?	UI::h('li', [], [
 						UI::h('a', [ 'title' => "Dodaj nowy rekord", 'class' => "", 'href' => "#CREATE" ], [
 							UI::h('span', [ 'class' => "glyphicon glyphicon-plus" ]),
 							" Dodaj nowy rekord",
 						]),
-					]);
-				}
-				if ($this->showProcesInitFiltr) {
-					echo UI::h('div', [ 'class' => "btn-group pull-right" ], [
+					])
+				:	'',
+
+				($this->showProcesInitFiltr)
+				?	UI::h('div', [ 'class' => "btn-group pull-right" ], [
 						UI::h('a', [ 'class' => "btn btn-xs btn-info dropdown-toggle",
 							'data-toggle' => "dropdown",
 							'href' => "#",
@@ -434,10 +434,11 @@ class TableAjax extends ViewAjax {
 								UI::h('a', [ 'href' => "#", 'onclick' => "return false;" ], " loading ... "),
 							]),
 						]),
-					]);
-				}
-				if ($this->showTableTools) {
-					echo UI::h('div', [ 'class' => "btn-group pull-right" ], [
+					])
+				:	'',
+
+				($this->showTableTools)
+				?	UI::h('div', [ 'class' => "btn-group pull-right" ], [
 						UI::h('a', [ 'class' => "btn btn-xs btn-default dropdown-toggle",
 							'style' => "margin-right:16px",
 							'data-toggle' => "dropdown",
@@ -450,57 +451,56 @@ class TableAjax extends ViewAjax {
 								UI::h('a', [ 'href' => "#", 'onclick' => "return false;" ], " loading ... "),
 							]),
 						]),
-					]);
-				}
-				?>
-				<?php
-				if ($this->_tbl == 'IN7_MK_BAZA_DYSTRYBUCJI') {
-					echo UI::h('a', [ 'class' => "pull-right", 'style' => "padding:0 20px 0 0", 'href' => "index.php?MENU_INIT=TREEJS&ZASOB_ID={$this->_zasobID}" ], "<i class=\"glyphicon glyphicon-eye-open\"></i> Drzewo");
-				} else if ($this->allowTreeView()) {
-					echo UI::h('a', [ 'class' => "pull-right", 'style' => "padding:0 20px 0 0", 'href' => "index.php?MENU_INIT=VIEWTREE_AJAX&ZASOB_ID={$this->_zasobID}" ], "<i class=\"glyphicon glyphicon-eye-open\"></i> Drzewo");
-				}
-				if ($this->_tbl == 'WMS_MAP_GEOREFERENCES') {
-					echo UI::h('a', [ 'class' => "pull-right", 'style' => "padding:0 20px 0 0", 'href' => "index.php?_route=GeoreferencesManager" ], "<i class=\"glyphicon glyphicon glyphicon-wrench\"></i> Zarządzaj punktami");
-				}
-				?>
-			</ul>
-			<?php
-				if ($this->_backRefFilter) {
-					echo UI::h('div', [ 'class' => "container" ], [
-						UI::h('div', [ 'class' => "alert alert-info" ], [
-							"Rekordy powiązane z rekordem {$this->_backRefFilter['primaryKey']} z ",
-							UI::h('a', [
-								'href' => "index.php?_route=ViewTableAjax&namespace={$this->_backRefFilter['namespace']}",
-							], $this->_backRefFilter['namespace']),
-							" - ",
-							UI::h('a', [
-								'href' => $this->rootUrl,
-								'style' => "color:red",
-							], "usuń filtr"),
-						]),
-					]);
-				}
+					])
+				:	'',
 
-				if ($this->_childRefFilter) {
-					echo UI::h('div', [ 'class' => "container" ], [
-						UI::h('div', [ 'class' => "alert alert-info" ], [
-							"Rekordy powiązane z rekordem {$this->_childRefFilter['primaryKey']} z ",
-							UI::h('a', [
-								'href' => "index.php?_route=ViewTableAjax&namespace={$this->_childRefFilter['namespace']}",
-							], $this->_childRefFilter['namespace']),
-							" - ",
-							UI::h('a', [
-								'href' => $this->rootUrl,
-								'style' => "color:red",
-							], "usuń filtr"),
-						]),
-					]);
-				}
+				($this->_tbl == 'IN7_MK_BAZA_DYSTRYBUCJI')
+				?	UI::h('a', [ 'class' => "pull-right", 'style' => "padding:0 20px 0 0", 'href' => "index.php?MENU_INIT=TREEJS&ZASOB_ID={$this->_zasobID}" ], "<i class=\"glyphicon glyphicon-eye-open\"></i> Drzewo")
+				:	'',
 
-				echo UI::h('div', [ 'id' => $this->_htmlID ]);
-			?>
-		</div>
-		<?php
+				($this->_tbl !== 'IN7_MK_BAZA_DYSTRYBUCJI' && $this->allowTreeView())
+				?	UI::h('a', [ 'class' => "pull-right", 'style' => "padding:0 20px 0 0", 'href' => "index.php?MENU_INIT=VIEWTREE_AJAX&ZASOB_ID={$this->_zasobID}" ], "<i class=\"glyphicon glyphicon-eye-open\"></i> Drzewo")
+				:	'',
+
+				($this->_tbl == 'WMS_MAP_GEOREFERENCES')
+				?	UI::h('a', [ 'class' => "pull-right", 'style' => "padding:0 20px 0 0", 'href' => "index.php?_route=GeoreferencesManager" ], "<i class=\"glyphicon glyphicon glyphicon-wrench\"></i> Zarządzaj punktami")
+				:	'',
+			]),
+
+			($this->_backRefFilter)
+			?	UI::h('div', [ 'class' => "container" ], [
+					UI::h('div', [ 'class' => "alert alert-info" ], [
+						"Rekordy powiązane z rekordem {$this->_backRefFilter['primaryKey']} z ",
+						UI::h('a', [
+							'href' => "index.php?_route=ViewTableAjax&namespace={$this->_backRefFilter['namespace']}",
+						], $this->_backRefFilter['namespace']),
+						" - ",
+						UI::h('a', [
+							'href' => $this->rootUrl,
+							'style' => "color:red",
+						], "usuń filtr"),
+					]),
+				])
+			:	'',
+
+			($this->_childRefFilter)
+			?	UI::h('div', [ 'class' => "container" ], [
+					UI::h('div', [ 'class' => "alert alert-info" ], [
+						"Rekordy powiązane z rekordem {$this->_childRefFilter['primaryKey']} z ",
+						UI::h('a', [
+							'href' => "index.php?_route=ViewTableAjax&namespace={$this->_childRefFilter['namespace']}",
+						], $this->_childRefFilter['namespace']),
+						" - ",
+						UI::h('a', [
+							'href' => $this->rootUrl,
+							'style' => "color:red",
+						], "usuń filtr"),
+					]),
+				])
+			:	'',
+
+			UI::h('div', [ 'id' => $this->_htmlID ]),
+		]);
 		UI::inlineJS(__FILE__ . '.generateFunctionNode.js');
 		UI::inlineJS(__FILE__ . '.GeomField.js');
 		if ($this->showProcesInitFiltr) {
@@ -620,8 +620,11 @@ class TableAjax extends ViewAjax {
 				});
 			}
 
-			//try call webservice for data
-			priv.update(priv.options.router);
+			if (location.hash) {
+				priv.options.router()
+			} else {
+				priv.update(priv.options.router)
+			}
 
 			if (typeof priv.options.router == "function") {
 				$(window).bind('hashchange', function() {
@@ -711,65 +714,37 @@ class TableAjax extends ViewAjax {
 			priv.renderInlineEditBox();// .tblAjax__inlineEditBox
 
 			_foot = _head = _body = _headSort = _headFilter = undefined;// TODO: refactor
-			/// console.log('L.<?php echo __LINE__; ?> priv.initialRender, _uiNode$Table: ', _uiNode$Table.html())
 		};
 
-		/*
-		 creates the table with all its parts and handlers.
-		 note that only the parts we need is created.
-		 (yeah, the function is huge)
-		 */
 		priv.renderTable = function() {
-			//create table itself
-			// console.log('renderTable:: _data', _data);//TODO:DBG:RMME
-			/// console.log('renderTable:: _uiNode$Table', _uiNode$Table);//TODO:DBG:RMME
-
-			//create the header which will later hold both sorting and filtering
-			/// console.log('renderTable:: _head', _head);//TODO:DBG:RMME
+			if (!_state.colsSorted) return;
 			if (!_head) {
 				_headSort = _headFilter = _headSpecialFilter = undefined;
-				//_head = $('<thead></thead>');
-				//	$('<tr class="sort"></tr>').prependTo(_head);
-				//_uiNode$Table.find('thead').replaceWith(_head);
 				_head = _uiNode$Table.find('thead');
 			}
-
-			//create the header sorting row
-			if (!_headSort) {
-				/// console.log('renderTable::renderTableTheadSort...');//TODO:DBG:RMME
-				priv.renderTableTheadSort();
-			}
-
-			//create the header filtering row
+			if (!_headSort) priv.renderTableTheadSort();
 			if (!_headFilter && priv.options.filter) {
-				/// console.log('renderTable::renderTableTheadFilter...');//TODO:DBG:RMME
 				priv.renderTableTheadFilter();
 				_headFilter = true;
 			}
-
-			//create the header special filters row
 			if (!_headSpecialFilter && priv.options.specialFilterFunctions) {
 				priv.renderHeadSpecialFilters();
 				_headSpecialFilter = true;
 			}
-
-			//create the body
 			if (!_body) {
 				_uiNode$Table.find('tbody').remove();
 				_bodyNode = $('<tbody></tbody>').insertAfter(_head);
 				//_bodyNode.on('change', '.unique', priv.rowChecked);
 
-				//find out what rows to show next...
+				// find out what rows to show next...
 				var rowsAdded = 0;
 
-				//slice out the chunk of data we need and create rows
+				// slice out the chunk of data we need and create rows
 				$.each(_data.rows, function(index, props) {
 					var rowNode = priv.renderRow(props);
 					if (rowNode) rowNode.appendTo(_bodyNode);
 					rowsAdded++;
-
-					//enough rows created?
-					if (rowsAdded >= priv.options.pageSize) {
+					if (rowsAdded >= priv.options.pageSize) { // enough rows created
 						return false;
 					}
 				});
@@ -778,8 +753,7 @@ class TableAjax extends ViewAjax {
 					_totalPages = Math.ceil(_data.total / priv.options.pageSize);
 				}
 
-				//pad with empty rows if we're at last page.
-				if (_state.page == _totalPages) {
+				if (_state.page == _totalPages) { // pad with empty rows if we're at last page.
 					while (rowsAdded < priv.options.pageSize) {
 						var rowNode = priv.renderRowEmptyNode();
 						if (rowNode) rowNode.appendTo(_bodyNode);
@@ -788,7 +762,6 @@ class TableAjax extends ViewAjax {
 				}
 			}
 
-			//create the footer
 			if (!_foot) {
 				priv.renderTableTfoot();
 				priv.renderFooter();
@@ -807,19 +780,21 @@ class TableAjax extends ViewAjax {
 				var colsW = stickyCol1Width + stickyCol2Width;
 				jQuery(_uiNodeCont).css({width:'' + (contW - colsW) + 'px', marginLeft:'' + colsW + 'px', overflowX:'scroll', overflowY:'visible', paddingBottom:'1px'});
 			}
-			_uiNode$Table.find('.stickyCol1').css({position:'absolute',
-				left:'0',
-				top:'auto',
-				width:'' + stickyCol1Width + 'px',
+			_uiNode$Table.find('.stickyCol1').css({
+				position: 'absolute',
+				left: '0',
+				top: 'auto',
+				width: '' + stickyCol1Width + 'px',
 			});
 			//_uiNode$Table.find('.filter').find('.stickyCol1').css({height:'34px'});
 			var sortStickyColHeight = _uiNode$Table.find('.sort').find('th:last').outerHeight();
 			_uiNode$Table.find('.sort').find('.stickyCol1').css({height: sortStickyColHeight + 'px'});
 			_uiNode$Table.find('.sort').find('.stickyCol2').css({height: sortStickyColHeight + 'px'});
-			_uiNode$Table.find('.stickyCol2').css({position:'absolute',
-				left:'' + (stickyCol1Width) + 'px',
-				top:'auto',
-				width:'' + stickyCol2Width + 'px',
+			_uiNode$Table.find('.stickyCol2').css({
+				position: 'absolute',
+				left: '' + (stickyCol1Width) + 'px',
+				top: 'auto',
+				width: '' + stickyCol2Width + 'px',
 				borderRight: '1px solid silver',
 				overflow: 'hidden'
 			});
@@ -2104,14 +2079,14 @@ class TableAjax extends ViewAjax {
 		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__; ?>');
+				if (priv.options.debug) console.log('no url found');
 				return;
 			}
 
-			if (priv.options.debug) console.log(p5Utils__format('requesting data from url:{0} data:{1} L.<?php echo __LINE__; ?>', [priv.options.url, JSON.stringify(priv.options.urlData) || '']));
+			if (priv.options.debug) console.log(p5Utils__format('requesting data from url:{0} data:{1}', [priv.options.url, JSON.stringify(priv.options.urlData) || '']));
 
-			var initUrlAdd = '',
-				filtersInitSet = false;
+			var initUrlAdd = '';
+			var filtersInitSet = false;
 			initUrlAdd += '&currSortCol=' + _state.filters.currSortCol;
 			initUrlAdd += '&currSortFlip=' + ((_state.filters.currSortCol)? "desc" : "asc");
 			if (Object.keys(_state.filters.filterCols).length > 0) {
@@ -2143,52 +2118,56 @@ class TableAjax extends ViewAjax {
 			}).then(function (response) {
 				return response.json()
 			}).then(function (data) {
-				if (priv.options.debug) console.log('loadDataAjax:fetch:update: request finished L.<?php echo __LINE__; ?> data:', data);
+				if (priv.options.debug) console.log('loadDataAjax:fetch:update: request finished data:', data);
 				if ('success' == data.type) {
 					// p5UI__notifyAjaxCallback(data);
-					if (data && data.cols) {
-						priv.setStateCols(data.cols, data.primaryKey);
-					}
-
-					// set initial filters (_state.filters.filterCols)
-					if (filtersInitSet && _data && _data.cols) {
-						$.map(reqData, function(fltrValue, fltr){
-							var colName = null;
-							if (fltr.substr(0, 3) == 'sf_') {
-								colName = fltr.substr(3);
-							}
-							else if (fltr.substr(0, 2) == 'f_') {
-								colName = fltr.substr(2);
-							}
-
-							if (colName && _data.cols[colName]) {
-								//add the filter to the filter array
-								_state.filters.filterCols[colName] = {
-									filter: fltrValue,
-									colName: colName
-								};
-							}
-						});
-					}
-
-					// assign the new state (data)
-					state = {data: {}};
-					if (!skipCols) state.data.cols = data.cols || {};
-					state.data.rows = data.rows || [];
-					state.data.total = data.total || 0;
-					state.page = data.page || 0;
-					state.pageSize = data.pageSize || priv.options.pageSize;
-					state.filters = data.filters || {};
-					priv.setState(state);
-
-					if (typeof callback == "function") {
-						callback.call(this);
-					}
+					return data;
 				} else if ('error' == data.type) {
 					p5UI__notifyAjaxCallback(data);
 				} else {
 					p5UI__notifyAjaxCallback(data);
 				}
+				return null;
+			}).then(function (data) {
+				if (!data) return;
+				if (data && data.cols) {
+					priv.setStateCols(data.cols, data.primaryKey);
+				}
+
+				// set initial filters (_state.filters.filterCols)
+				if (filtersInitSet && _data && _data.cols) {
+					$.map(reqData, function(fltrValue, fltr){
+						var colName = null;
+						if (fltr.substr(0, 3) == 'sf_') {
+							colName = fltr.substr(3);
+						}
+						else if (fltr.substr(0, 2) == 'f_') {
+							colName = fltr.substr(2);
+						}
+
+						if (colName && _data.cols[colName]) {
+							//add the filter to the filter array
+							_state.filters.filterCols[colName] = {
+								filter: fltrValue,
+								colName: colName
+							};
+						}
+					});
+				}
+
+				// assign the new state (data)
+				state = {data: {}};
+				if (!skipCols) state.data.cols = data.cols || {};
+				state.data.rows = data.rows || [];
+				state.data.total = data.total || 0;
+				state.page = data.page || 0;
+				state.pageSize = data.pageSize || priv.options.pageSize;
+				state.filters = data.filters || {};
+				priv.setState(state);
+
+				if (typeof callback == "function") {
+					callback.call(this);
+				}
 			}).catch(function (e) {
 				console.log('loadDataAjax:fetch: ERR:', e);
 			});
@@ -2198,15 +2177,15 @@ class TableAjax extends ViewAjax {
 		 assigns the new data.
 		 */
 		priv.setState = function(state) {
-			var oldState = _state,// TODO: use to check what really changed (use extend!)
-					renderParts = {};
+			var oldState = _state; // TODO: use to check what really changed (use extend!)
+			var renderParts = {};
 			if (state.data) {
-				if (state.data.cols && state.data.cols.length > 0) {// TODO: never happen, but if happend then rerender all
+				if (state.data.cols && Object.keys(state.data.cols).length > 0) {// TODO: never happen, but if happend then rerender all
 					priv.setStateCols(state.data.cols, state.data.primaryKey);
 					priv.setStateData(state.data);
 					renderParts['head'] = true;
 				} else {
-					priv.setStateData(state.data, true);
+					priv.setStateData(state.data);
 				}
 				renderParts['body'] = true;
 				renderParts['foot_pagination'] = true;
@@ -2228,9 +2207,7 @@ class TableAjax extends ViewAjax {
 				}
 			}
 			if (state.hasOwnProperty('filters')) {
-/// console.log('L.<?php echo __LINE__; ?> setState::state.filters: ', state.filters);//TODO:DBG:RMME
 				priv.setStateFilters(state.filters);
-				// TODO: if state diff's
 				renderParts['head__specialFilters'] = true;
 				renderParts['foot_pagination'] = true;
 			}
@@ -2238,7 +2215,6 @@ class TableAjax extends ViewAjax {
 			renderParts = Object.keys(renderParts);
 
 			if (priv.options.debug) console.log('setState::renderParts: ', renderParts);//TODO:DBG:RMME
-///console.log('L.<?php echo __LINE__; ?> setState::renderParts: ', renderParts);//TODO:DBG:RMME
 			if (renderParts.length > 0) {
 				jQuery(_uiNodeCont).trigger('TableAjax:render', renderParts);
 			}
@@ -3380,8 +3356,7 @@ class TableAjax extends ViewAjax {
 			priv.loadPage(page, pageSize);
 		}
 		priv.loadPage = function(page, pageSize) {
-			var skipCols = true, // skipCols = true - prevent columns delete
-				resetChecked = false;
+			var resetChecked = false;
 			var reqData = {
 				page: page,
 				pageSize: (pageSize || priv.options.pageSize),
@@ -3400,7 +3375,6 @@ class TableAjax extends ViewAjax {
 						urlAdd += '&f_' + col + '=' + encodeURIComponent(colProps.filter);
 					}
 				});
-				skipCols = true;
 			}
 
 			// specialFilters
@@ -3430,9 +3404,10 @@ class TableAjax extends ViewAjax {
 				// p5UI__notifyAjaxCallback(data);
 				if ('success' == data.type) {
 					state = {data: {}};
-					if (!skipCols) state.data.cols = data.cols || {};
+					state.data.cols = data.cols || {};
 					state.data.rows = data.rows || [];
 					state.data.total = data.total || 0;
+					state.data.primaryKey = data.primaryKey || 'ID';
 					state.page = data.page || 0;
 					state.pageSize = data.pageSize || priv.options.pageSize;
 					state.filters = data.filters || {};
@@ -4839,7 +4814,12 @@ jQuery(document).ready(function(){
 		if (!$record) throw new HttpException("404: No item ID({$id})", 404);
 
 		$visibleCols = $acl->getRealFieldList();
-		$rowsHist = $acl->getHistItems($id);
+		try {
+			$rowsHist = $acl->getHistItems($id);
+		} catch (Exception $e) {
+			DBG::log($e);
+			$rowsHist = [];
+		}
 
 		$jsonResponse['row_functions'] = $this->_parseRowFunctions($record['ID'], array('hist', 'cp'), $showLabel = true);