YUI – Panel close

Click outside of a panel to close it

var treePanel = new YAHOO.widget.Panel(...);

function isInsideTreePanel(clicked_element) {
 var current_element = clicked_element;
 while(current_element && current_element != document.body) {
 if (current_element == treePanel.element) {
 return true;
 }
 current_element = current_element.parentNode;
 }
 return false;
}

function onDocumentMouseDown(e) {
 if (treePanel.cfg.getProperty('visible')) {
 if (!isInsideTreePanel(e.target)) {
 treePanel.hide();
 YAHOO.util.Event.removeListener(document, onDocumentMouseDown);
 }
 }
}

treePanel.show();
YAHOO.util.Event.addListener(document, 'mousedown', onDocumentMouseDown, null);

Leave a Reply

Your email address will not be published. Required fields are marked *

*