Skip to content
This repository
Browse code

BUGFIX Updating tree state (after adding/renaming nodes, changing par…

…ent). Deselecting nodes when switching to edit view (fixes #7389, #7336, #7309)
  • Loading branch information...
commit c18c29f464f1b624bb665e67f4ce2183af839250 1 parent 9898cd9
Ingo Schommer authored June 03, 2012
3  admin/javascript/LeftAndMain.Content.js
@@ -226,6 +226,9 @@
226 226
 
227 227
 					var url = $(node).find('a:first').attr('href');
228 228
 					if(url && url != '#') {
  229
+						// Deselect all nodes (will be reselected after load according to form state)
  230
+						self.jstree('deselect_all');
  231
+						self.jstree('uncheck_all');
229 232
 
230 233
 						// Ensure URL is absolute (important for IE)
231 234
 						if($.path.isExternal($(node).find('a:first'))) url = url = $.path.makeUrlAbsolute(url, $('base').attr('href'));
72  admin/javascript/LeftAndMain.Tree.js
@@ -44,6 +44,7 @@
44 44
 					this
45 45
 						.jstree(this.getTreeConfig())
46 46
 						.bind('loaded.jstree', function(e, data) {
  47
+							self.updateFromEditForm();
47 48
 							self.css('visibility', 'visible');
48 49
 							// Add ajax settings after init period to avoid unnecessary initial ajax load
49 50
 							// of existing tree in DOM - see load_node_html()
@@ -96,11 +97,16 @@
96 97
 							});
97 98
 						});
98 99
 					
99  
-					this.parents('.cms-container').bind('afterstatechange', function(e, data) {
100  
-						self._onLoadNewPage(e, data);
  100
+					$('.cms-container').bind('afterstatechange.tree', function(e, data) {
  101
+						self.updateFromEditForm(e.origData);
  102
+					});
  103
+					$('.cms-content').bind('reloadeditform.tree', function(e, data) {
  104
+						self.updateFromEditForm(e.origData);
101 105
 					});
102 106
 			},
103 107
 			onunmatch: function() {
  108
+				$('.cms-container').unbind('afterstatechange.tree');
  109
+				$('.cms-content').unbind('reloadeditform.tree');
104 110
 				this._super();
105 111
 			},
106 112
 
@@ -204,48 +210,43 @@
204 210
 			 * Assumes to be triggered by a form element with the following input fields:
205 211
 			 * ID, ParentID, TreeTitle (or Title), ClassName
206 212
 			 */
207  
-			_onLoadNewPage: function(e, eventData) {
208  
-				var self = this;
209  
-				
210  
-				// finds a certain value in an array generated by jQuery.serializeArray()
211  
-				var findInSerializedArray = function(arr, name) {
212  
-					for(var i=0; i<arr.length; i++) {
213  
-						if(arr[i].name == name) return arr[i].value;
214  
-					};
215  
-					return false;
216  
-				};
217  
-
218  
-				var handledform = $(e.target).is('.cms-edit-form') ? $(e.target)[0] : $(e.target).find('.cms-edit-form')[0];
219  
-				var id = handledform ? $(handledform.ID).val() : null;
  213
+			updateFromEditForm: function(origData) {
  214
+				var self = this, 
  215
+					form = $('.cms-edit-form').get(0),
  216
+					id = form ? $(form.ID).val() : null;
220 217
 
221 218
 				// check if a form with a valid ID exists
222 219
 				if(id) {
223  
-					var parentID = $(handledform.ParentID).val(), 
  220
+					var parentID = $(form.ParentID).val(), 
224 221
 						parentNode = this.find('li[data-id='+parentID+']');
225 222
 						node = this.find('li[data-id='+id+']'),
226  
-						title = $((handledform.TreeTitle) ? handledform.TreeTitle : handledform.Title).val(),
227  
-						className = $(handledform.ClassName).val();
  223
+						title = $((form.TreeTitle) ? form.TreeTitle : form.Title).val(),
  224
+						className = $(form.ClassName).val();
228 225
 
229 226
 					// set title (either from TreeTitle or from Title fields)
230 227
 					// Treetitle has special HTML formatting to denote the status changes.
231 228
 					if(title) this.jstree('rename_node', node, title);
232 229
 
233  
-					// TODO Fix node icon setting
234  
-					// // update icon (only if it has changed)
235  
-					// if(className) this.setNodeIcon(id, className);
236  
-
237 230
 					// check if node exists, might have been created instead
238 231
 					if(!node.length) {
239 232
 						this.jstree(
240 233
 							'create_node', 
241 234
 							parentNode, 
242 235
 							'inside', 
243  
-							{data: '', attr: {'class': className, 'data-id': id}},
  236
+							{
  237
+								data: '', 
  238
+								attr: {
  239
+									'data-class': className, 
  240
+									'class': 'class-' + className, 
  241
+									'data-id': id
  242
+								}
  243
+							},
244 244
 							function() {
245 245
 								var newNode = self.find('li[data-id='+id+']');
246  
-								// TODO Fix hardcoded link
247 246
 								// TODO Fix replacement of jstree-icon inside <a> tag
248  
-								newNode.find('a:first').html(title).attr('href', 'admin/show/'+id);
  247
+								newNode.find('a:first').html(title).attr('href', ss.i18n.sprintf(
  248
+									self.data('urlEditpage'), id
  249
+								));
249 250
 								self.jstree('deselect_node', parentNode);
250 251
 								self.jstree('select_node', newNode);
251 252
 							}
@@ -254,17 +255,20 @@
254 255
 						this.jstree('select_node', node);
255 256
 					}
256 257
 
257  
-					// TODO Fix node parent setting
258  
-					// // set correct parent (only if it has changed)
259  
-					// if(parentID) this.setNodeParentID(id, jQuery(e.target.ParentID).val());
  258
+					// set correct parent (only if it has changed)
  259
+					if(parentID && parentID != node.parents('li:first').data('id')) {
  260
+						this.jstree('move_node', node, parentNode.length ? parentNode : -1, 'last');
  261
+					}
260 262
 
261  
-					// TODO Fix doubleup when replacing page form with root form, reloads the old form over the root
262  
-					// set current tree element regardless of wether the item was new
263  
-					// this.jstree('select_node', node);
  263
+					this.jstree('select_node', node);
264 264
 				} else {
265  
-					if(typeof eventData.origData != 'undefined') {
266  
-						var node = this.find('li[data-id='+eventData.origData.ID+']');
267  
-						if(node && node.data('id') != 0) this.jstree('delete_node', node);
  265
+					// If no ID exists in a form view, we're displaying the tree on its own,
  266
+					// hence to page should show as active
  267
+					this.jstree('deselect_all');
  268
+
  269
+					if(typeof origData != 'undefined') {
  270
+						var node = this.find('li[data-id='+origData.ID+']');
  271
+						if(node && node.data('id') !== 0) this.jstree('delete_node', node);
268 272
 					}
269 273
 				}
270 274
 

0 notes on commit c18c29f

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