MozillaZine


Display XUL elements depending on preference settings

Talk about add-ons and extension development.
jmozmoz
 
Posts: 365
Joined: August 3rd, 2009, 12:28 pm

Post Posted August 11th, 2009, 3:43 pm

Hello,

Google didn't help, so I ask here:

Can somebody give me a (simple) example of how to switch the display of some xul elements, e.g. buttons, depending of preference settings. The preference settings can be changed via the option dialog of the addon.

I could add the following code to my .css file to remove the button altogether
Code: Select all
.XXXButton { display: none !important; }

How do I set this dynamicly?

Thanks for your help.

rsx11m
Moderator
 
Posts: 14430
Joined: May 3rd, 2007, 7:40 am
Location: US

Post Posted August 11th, 2009, 4:31 pm

I'm not aware of any method to do this in CSS itself, but each XUL element should come with a "hidden" attribute that you can set, e.g., in a JavaScript construct. You will need to know the ID of that element to switch it on and off.

Hide:
Code: Select all
  var myElement = document.getElementById("myXULelement");
  myElement.setAttribute("hidden", "true");

Show:
Code: Select all
  var myElement = document.getElementById("myXULelement");
  myElement.removeAttribute("hidden");


Those toggle the following rule in xul.css:
Code: Select all
[hidden="true"] {
  display: none;
}

Hope that's what you were looking for.

jmozmoz
 
Posts: 365
Joined: August 3rd, 2009, 12:28 pm

Post Posted August 12th, 2009, 2:29 pm

Thank you very much for your help. Your answer got me half way to what I would like to do:

If I set the preferences in an option dialog and after that manually update the panel containing the buttons (e.g. by closing and opening it again) the buttons are shown according to the preferences settings.

Now how can I manually issue an update of the panel? By some EventListener magic? Where should I start?

The problem seems to be that within the option dialog I cannot access the elements in the other panel.
Code: Select all
document.getElementById("myXULelement")

returns null.

rsx11m
Moderator
 
Posts: 14430
Joined: May 3rd, 2007, 7:40 am
Location: US

Post Posted August 12th, 2009, 3:51 pm

You are welcome. Unfortunately, I don't know how to update the panel once you changed it while visible.

Rod Whiteley

User avatar
 
Posts: 11480
Joined: December 6th, 2004, 3:41 am
Location: UK

Post Posted August 13th, 2009, 1:59 am

The panel can observe the preference. See: https://developer.mozilla.org/en/Code_s ... _observers

By the way, a XUL element generally has a 'hidden' property that is easier to use than the attribute, so you can write something like: myElement.hidden = myPrefBranch.getBoolPref(...
Rod

jmozmoz
 
Posts: 365
Joined: August 3rd, 2009, 12:28 pm

Post Posted August 13th, 2009, 3:49 pm

Rod Whiteley wrote:The panel can observe the preference. See: https://developer.mozilla.org/en/Code_s ... _observers


Thank you very much. This is exactly what I was looking for.

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 2 guests