lib/QuickMenu.class.php

Go to the documentation of this file.
00001 <?php
00021 class QuickMenu {
00022   private $imgExpanded_;
00023   private $imgCollapsed_;
00024   private $imgNoChildern_;
00025   private $imgClearSearch_;
00026   private $imgExpandAll_;
00027   public function __construct() {
00028     $config = Config::instance();
00029     $imgRoot = $config->webRoot().$config->imgDir();
00030     $this->imgExpanded_ = $imgRoot.'/tree_minus.png';
00031     $this->imgCollapsed_ = $imgRoot.'/tree_plus.png';
00032     $this->imgNoChildern_ = $imgRoot.'/tree_item.png';
00033     $this->imgClearSearch_ = $imgRoot.'/icon_drop.png';
00034     $this->imgExpandAll_ = $imgRoot.'/fast_forward.png';
00035   }
00036   
00037   private $visibleDepth_ = 1;
00042   public function visibleDepth() {
00043     return $this->visibleDepth_;
00044   }
00049   public function setVisibleDepth($visibleDepth) {
00050     $this->visibleDepth_ = $visibleDepth;
00051   }
00052   
00053   private $minimalInputLength_ = 1;
00058   public function minimalInputLength() {
00059     return $this->minimalInputLength_;
00060   }
00065   public function setMinimalInputLength($minimalInputLength) {
00066     $this->minimalInputLength_ = $minimalInputLength;
00067   }
00068   
00069   private $activeItemHREF_;
00074   public function activeItemHREF() {
00075     return $this->activeItemHREF_;
00076   }
00081   public function setActiveItemHREF($activeItemHREF) {
00082     $this->activeItemHREF_ = $activeItemHREF;
00083   }
00084   
00092   public function genHead() {
00093     $config = Config::instance();
00094     $qmScript = $config->webRoot().$config->scriptDir().'/QuickMenu.class.js';
00095     echo '  <script type="text/javascript" src="'.$qmScript.'"></script>'."\n";
00096     ?>  <script type="text/javascript">
00097 qmFacade = null;
00098 qmInput = null;
00099 qmClear = null;
00100 qmExpandAll = null;
00101 function qmOnload() {
00102   (new Image).src='<?php echo $this->imgExpanded_; ?>';
00103   (new Image).src='<?php echo $this->imgCollapsed_; ?>';
00104   (new Image).src='<?php echo $this->imgNoChildern_; ?>';
00105   (new Image).src='<?php echo $this->imgClearSearch_; ?>';
00106   qm = new QuickMenu('QuickMenu', 'qmInput');
00107   qm.visibleDepth = <?php echo $this->visibleDepth(); ?>;
00108   qm.minimalInputLength = <?php echo $this->minimalInputLength(); ?>;
00109   qm.imgExpanded = '<?php echo $this->imgExpanded_; ?>';
00110   qm.imgCollapsed = '<?php echo $this->imgCollapsed_; ?>';
00111   qm.imgNoChildern = '<?php echo $this->imgNoChildern_; ?>';
00112   qm.init();
00113   qmFacade = document.getElementById('qmFacade');
00114   qmInput = document.getElementById('qmInput');
00115   qmClear = document.getElementById('qmClearInput');
00116   qmExpandAll = document.getElementById('qmExpandAll');
00117   qmFacade.onfocus = function() {
00118     qmInput.style.display = '';
00119     qmClear.style.display = '';
00120     this.style.display = 'none';
00121     qmExpandAll.style.display = 'none';
00122     if (-1==navigator.appName.search(/konqueror/i))
00123       qmInput.focus();
00124     else
00125       // Special version for Konqueror
00126       setTimeout("qmInput.focus()", 100);
00127   }
00128   qmInput.onblur = function() {
00129     if (this.value.length >= qm.minimalInputLength)
00130       return;
00131     this.style.display = 'none';
00132     qmClear.style.display = 'none';
00133     this.value = '';
00134     qmFacade.style.display = '';
00135     qmExpandAll.style.display = '';
00136   }
00137   qmClear.onclick = function() {
00138     qmInput.value = '';
00139     qmInput.blur();
00140     qmInput.onblur();
00141   }
00142   qmExpandAll.onclick = function() {
00143     qmInput.value = '*';
00144     qmFacade.onfocus();
00145     if (-1==navigator.appName.search(/konqueror/i))
00146       qmInput.select();
00147     else
00148       // Special version for Konqueror
00149       setTimeout("qmInput.select()", 200);
00150   }
00151   qmInput.onblur.call(qmInput);
00152 }
00153   </script>
00154 <?php
00155     JSOnloadList::instance()->addFunction('qmOnload');
00156   }
00157   
00158   private $currentId = 1;                       
00159   private $mapIdToText = Array();               
00160   private $mapIdToHREF = Array();               
00161   private $mapIdToMenuList = Array(0=>Array()); 
00162   
00170   public function addItem($szText, $szHREF, $idParent=0) {
00171     $id = $this->currentId++;
00172     $this->mapIdToText[$id] = $szText;
00173     $this->mapIdToHREF[$id] = $szHREF;
00174     $this->mapIdToMenuList[$id] = Array();
00175     $this->mapIdToMenuList[$idParent] []= $id;
00176     return $id;
00177   }
00178   
00184   private function genSubMenu($id, $xmlId=null) {
00185     $menuList = $this->mapIdToMenuList[$id];
00186     if (0==count($menuList))
00187       return;
00188     
00189     // Initiate list
00190     $indentor = Indentor::singleton();
00191     if (isset($xmlId))
00192       $indentor->puts("<ul id=\"$xmlId\">");
00193     else
00194       $indentor->puts('<ul>');
00195     $indentor->enter();
00196     
00197     // Generate items
00198     foreach ($menuList as $current)
00199       $this->genMenuItem($current);
00200     
00201     // Terminate list
00202     $indentor->leave();
00203     $indentor->puts('</ul>');
00204   }
00205   
00210   private function genMenuItem($id) {
00211     $indentor = Indentor::singleton();
00212     $indentor->indentDirect();
00213     $szText = $this->mapIdToText[$id];
00214     $szHREF = $this->mapIdToHREF[$id];
00215     $menuList = $this->mapIdToMenuList[$id];
00216     
00217     // Is current item active menu item?
00218     $isActive = $szHREF == $this->activeItemHREF_;
00219     $szItemId = ($isActive)?' id="qmActive"':'';
00220     
00221     // Initiate list item
00222     echo "<li$szItemId class=\"qmDefault\">";
00223     
00224     // Has current item childern
00225     $hasChilds = 0!= count($menuList);
00226     $szImgSrc = ($hasChilds)?
00227       $this->imgExpanded_:
00228       $this->imgNoChildern_;
00229     echo "<img src=\"$szImgSrc\" title=\"$szText\" alt=\"-\" />";
00230     
00231     // Generate item link
00232     $szFullHREF = Config::instance()->webRoot().'/'.$szHREF;
00233     $szLinkId = ($isActive)?' id="qmActiveLink"':'';
00234     echo "<a$szLinkId class=\"qmDefault\" href=\"$szFullHREF\">$szText</a>";
00235     
00236     if ($hasChilds) {
00237       // Generate subitems recursively
00238       echo "\n";
00239       $indentor->enter();
00240       $this->genSubMenu($id);
00241       $indentor->leave();
00242       $indentor->indentDirect();
00243     }
00244     
00245     // Terminate list item
00246     echo "</li>\n";
00247   }
00248   
00266   public function genMenu() {
00267     $indentor = Indentor::singleton();
00268     $indentor->puts('<div id="qmWrapper">');
00269     $indentor->enter();
00270       $indentor->puts('<div id="qmInputBox">');
00271       $indentor->enter();
00272         $indentor->puts('<input type="text" id="qmInput" style="display:none;"/>');
00273         $indentor->puts('<input type="text" id="qmFacade" value="QuickFind" style="display:none;"/>');
00274         $indentor->puts('<img src="'.$this->imgClearSearch_.'" alt="Clear search" id="qmClearInput" style="display:none;"/>');
00275         $indentor->puts('<img src="'.$this->imgExpandAll_.'" alt="Expand all" id="qmExpandAll" style="display:none;"/>');
00276       $indentor->leave();
00277       $indentor->puts('</div>');
00278       $this->genSubMenu(0, 'QuickMenu');
00279     $indentor->leave();
00280     $indentor->puts('</div>');
00281   }
00282   
00288   private function addXmlItem($item, $idParent=0) {
00289     // Localized item text
00290     $lang = LangSelect::singleton()->getLang();
00291     $textArray = $item->xpath('child::qmText[@lang=\''.$lang.'\']');
00292     if (0==count($textArray))
00293       // Implicit item text (not localized)
00294       $textArray = $item->xpath('child::qmText[not(@lang)]');
00295     if (0==count($textArray))
00296       // Any item text (emergency case)
00297       $textArray = $item->xpath('child::qmText');
00298     
00299     // Add current item
00300     $hrefArray = $item->xpath('@href');
00301     $id = $this->addItem($textArray[0], $hrefArray[0], $idParent);
00302     
00303     // Scan for childern
00304     $childArray = $item->xpath('child::qmItem');
00305     foreach ($childArray as $subItem)
00306       // Direct recursion
00307       $this->addXmlItem($subItem, $id);
00308   }
00309   
00316   public function loadXmlTemplate($qmTemplate, $idParent=0) {
00317     $xml = simplexml_load_file($qmTemplate);
00318     $catArray = $xml->xpath('/QuickMenu/child::qmItem');
00319     foreach ($catArray as $current)
00320       $this->addXmlItem($current, $idParent);
00321   }
00322 }
00323 ?>

Generated on Sat Mar 8 10:26:43 2008 for Dudka.cz by  doxygen 1.5.4