note 81360 added to ref.dom

From: Date: Sun, 24 Feb 2008 15:47:12 +0000
Subject: note 81360 added to ref.dom
Groups: php.notes 
Request: Send a blank email to php-notes+get-136458@lists.php.net to get a copy of this message
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); } } } ---- Server IP: 200.24.234.70 Probable Submitter: 200.104.92.138 ---- Manual Page -- http://www.php.net/manual/en/ref.dom.php Edit -- https://master.php.net/note/edit/81360 Del: integrated -- https://master.php.net/note/delete/81360/integrated Del: useless -- https://master.php.net/note/delete/81360/useless Del: bad code -- https://master.php.net/note/delete/81360/bad+code Del: spam -- https://master.php.net/note/delete/81360/spam Del: non-english -- https://master.php.net/note/delete/81360/non-english Del: in docs -- https://master.php.net/note/delete/81360/in+docs Del: other reasons-- https://master.php.net/note/delete/81360 Reject -- https://master.php.net/note/reject/81360 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136458) next »