PHPverse 2025

Voting

The Note You're Voting On

danf dot 1979 at []gmail[] dot com
17 years ago
This is a couple of classes to deal with yahoo yui menu.

/*
$menubar = new MenuBar();

$file = new Menu("File");
$file->setAttribute("href", "http://file.com");

$quit = new Menu("Quit");
$quit->setAttribute("href", "http://quit.com");

$file->appendChild($quit);
$menubar->appendChild($file);

echo $menubar->grab();
*/

//
// Author: Daniel Queirolo.
// LGPL
//

/** ---------------------------------
/** Class MenuBar()
/** Creates a the menubar and appends
/** yuimenubaritems to it.
/** ---------------------------------*/

class MenuBar extends DOMDocument
{

public $menuID = "nav_menu"; // holds the css id that javascript yui menu code should have to recognize
private $UL; // This node holds every menu, This is THE node.

/** ---------------------------------
/** Constructor
/** Generates a menubar skeleton and the UL node
/** ---------------------------------*/

public function __construct() {

parent::__construct();

$rootdiv = parent::createElement("div");
$rootdiv->setAttribute("class", "yui-skin-sam");

parent::appendChild($rootdiv);

$yui_menubar = parent::createElement("div");
$yui_menubar->setAttribute("id", $this->menuID);
$yui_menubar->setAttribute("class", "yuimenubar");

$rootdiv->appendChild($yui_menubar);

$bd = parent::createElement("div");
$bd->setAttribute("class", "bd");

$yui_menubar->appendChild($bd);

$ul = parent::createElement("ul");
$ul->setAttribute("class", "first-of-type");

// ALL Menu() instances ocurr inside an <ul> tag.

$this->UL = $bd->appendChild($ul);

}

/** ---------------------------------
/** appendChild()
/** Appends a new yuimenubaritem to the menubar UL node.
/** This function changes <li> and <a> classes to yuiMENUBARsomething
/** ---------------------------------*/

public function appendChild($child) {

$li = parent::importNode($child->LI, true);

$li->setAttribute("class", "yuimenubaritem");

$li->getElementsByTagName("a")->item(0)->setAttribute("class", "yuimenubaritemlabel");

$this->UL->appendChild($li);

}

public function grab() {

return parent::saveHTML();

}

}

/** ---------------------------------
/** Class Menu()
/** Creates a yuimenuitem li node
/** ---------------------------------*/

class Menu extends DOMDocument {

public $LI; // stores the <li> node (THE link) that will be exported to MenuBar() or used on appendChild()

/** ---------------------------------
/** Constructor
/** Generates a yuimenuitem li node
/** No yuimenubar items are created here. MenuBar handles that.
/** ---------------------------------*/

public function __construct($link_name) {

parent::__construct();

$li = parent::createElement("li");
$li->setAttribute("class", "yuimenuitem");

// LI node stores THE link.
// if appendChild is used, the new (sub) Menu() would be LI node child.

$this->LI = parent::appendChild($li);

$a = parent::createElement("a", $link_name);
$a->setAttribute("class", "yuimenuitemlabel");

$li->appendChild($a);

$this->li = $li;
$this->a = $a;

}

/** ---------------------------------
/** appendChild
/** Appends a (sub) Menu() to current Menu() in LI
/** ---------------------------------*/

public function appendChild($child) {

$yuimenu = parent::createElement("div");
$yuimenu->setAttribute("class", "yuimenu");

$this->LI->appendChild($yuimenu);

$bd = parent::createElement("div");
$bd->setAttribute("class", "bd");

$yuimenu->appendChild($bd);

$ul = parent::createElement("ul");

$bd->appendChild($ul);

// child->NODE holds THE link from the new child (from child's __construct())

$ul->appendChild(parent::importNode($child->LI, true));

}

public function setAttribute($name, $value, $node="a") {

if ($node == "a") {
$this->a->setAttribute($name, $value);
}

else {
$this->li->setAttribute($name, $value);
}
}

}

<< Back to user notes page

To Top