Updating buttons on a panel upon closing an floating panel??

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
mgravish
Posts: 4
Joined: September 28th, 2011, 10:34 am

So in my script I've got a dockable panel with a bunch of buttons created with this code:

Code: Select all

	function createUI(thisObj) 
	{
		 myPanel = ( thisObj instanceof Panel) ? thisObj : new Window("palette", "Your Panel Name", undefined, {resizeable:true});
		 
		 var editBtn = myPanel.add("button",{x:100,y:0,width:75,height:20}, "Edit");
		 editBtn.onClick = function () {onEditButtonClick(myPanel);};
		 addButtons(myPanel);
}
The addButtons(myPanel) is an external function that adds more buttons to the panel. The onEditButtonClick(myPanel) function opens up a floating window with more options so you can customize the contents of the docked panel (I'm trying to implement functionality similar to ft_ToolBar). The problem I'm running into is trying to update the contents of the docked panel after closing the floating window. Right now, as a first step to understanding the issue I'm just trying to clear all the buttons from the docked panel upon closing the floating window with this code that is executed when you close the window:

Code: Select all

	function onClose(edit_palette,main_palette) 
	{
		edit_palette.close();
		var count = myPanel.children.length;
		alert(count);
		for(var i = 0;i<count;i++)
		{
			myPanel.remove(myPanel.children[i]);
		}
	}
For whatever reason, this code only removes every other button from the docked panel and it's driving me mad.
Especially so because if I replace the line

Code: Select all

myPanel.remove(myPanel.children[i]);
with

Code: Select all

alert(myPanel.children[i].text);
it alerts all the button names, so it's obviously iterating through them all. I just can't seem to get it to remove them all. Wtf? Anyone have any guidance?

Please help!

Thanks!!!
mgravish
Posts: 4
Joined: September 28th, 2011, 10:34 am

Ok. Finally I figured it out. I don't know why this didn't occur to me before...

I wasn't taking into account the fact that when you remove something from the front of an array, all the objects behind it in the list get moved up in line, so to speak. So if I have an array of values ["a","b","c","d"] and remove index 0 ("a"), then "b" will be bumped up and become index 0, "c" will become index 1, "d" will become index 2... So in the next iteration of the loop, when I remove index 1, the "c" will be removed.

I solved it by changing the code to:

Code: Select all

function onClose(edit_palette,mainPalette) 
   {
      edit_palette.close();
      var count = mainPalette.children.length;
      alert(count);
      for(var i = 0;i<count;i++)
      {
         mainPalette.remove(0);
      }
   }
so that the front of the array is removed until all the objects are cleared out.
Post Reply