Skip to content
This repository
Browse code

BUG Updating node would position wrong, Sort isnt === offset

  • Loading branch information...
commit 22d6c7afa4f4c0f86df60ad371206858bde8e671 1 parent 120de7c
Hamish Friedlander authored July 23, 2012
15  admin/code/LeftAndMain.php
@@ -748,12 +748,25 @@ public function updatetreenodes($request) {
748 748
 		foreach($ids as $id) {
749 749
 			$record = $this->getRecord($id);
750 750
 			$recordController = ($this->stat('tree_class') == 'SiteTree') ?  singleton('CMSPageEditController') : $this;
  751
+
  752
+			// Find the next & previous nodes, for proper positioning (Sort isn't good enough - it's not a raw offset)
  753
+			// TODO: These methods should really be in hierarchy - for a start it assumes Sort exists
  754
+			$next = $prev = null;
  755
+
  756
+			$className = $this->stat('tree_class');
  757
+			$next = DataObject::get($className, 'ParentID = '.$record->ParentID.' AND Sort > '.$record->Sort)->first();
  758
+			if (!$next) {
  759
+				$prev = DataObject::get($className, 'ParentID = '.$record->ParentID.' AND Sort < '.$record->Sort)->reverse()->first();
  760
+			}
  761
+
751 762
 			$link = Controller::join_links($recordController->Link("show"), $record->ID);
752 763
 			$html = LeftAndMain_TreeNode::create($record, $link, $this->isCurrentPage($record))->forTemplate() . '</li>';
  764
+
753 765
 			$data[$id] = array(
754 766
 				'html' => $html, 
755 767
 				'ParentID' => $record->ParentID,
756  
-				'Sort' => $record->Sort
  768
+				'NextID' => $next ? $next->ID : null,
  769
+				'PrevID' => $prev ? $prev->ID : null
757 770
 			);
758 771
 		}
759 772
 		$this->response->addHeader('Content-Type', 'text/json');
18  admin/javascript/LeftAndMain.Tree.js
@@ -271,8 +271,11 @@
271 271
 			 *  (Object) Map of additional data, e.g. ParentID
272 272
 			 */
273 273
 			updateNode: function(node, html, data) {
274  
-				var self = this, newNode = $(html), origClasses = node.attr('class'),
275  
-					parentNode = data.ParentID ? this.find('li[data-id='+data.ParentID+']') : false;
  274
+				var self = this, newNode = $(html), origClasses = node.attr('class');
  275
+
  276
+				var nextNode = data.NextID ? this.find('li[data-id='+data.NextID+']') : false;
  277
+				var prevNode = data.PrevID ? this.find('li[data-id='+data.PrevID+']') : false;
  278
+				var parentNode = data.ParentID ? this.find('li[data-id='+data.ParentID+']') : false;
276 279
 
277 280
 				// Copy attributes. We can't replace the node completely
278 281
 				// without removing or detaching its children nodes.
@@ -284,8 +287,15 @@
284 287
 				// Replace inner content
285 288
 				node.addClass(origClasses).html(newNode.html());
286 289
 
287  
-				// Set correct parent
288  
-				this.jstree('move_node', node, parentNode.length ? parentNode : -1, data.Sort);
  290
+				if (nextNode && nextNode.length) {
  291
+					this.jstree('move_node', node, nextNode, 'before');
  292
+				}
  293
+				else if (prevNode && prevNode.length) {
  294
+					this.jstree('move_node', node, prevNode, 'after');
  295
+				}
  296
+				else {
  297
+					this.jstree('move_node', node, parentNode.length ? parentNode : -1);
  298
+				}
289 299
 			},
290 300
 			
291 301
 			/**

0 notes on commit 22d6c7a

Please sign in to comment.
Something went wrong with that request. Please try again.