[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Provides a navigation sidebar. For example: 5 * 6 * $nav = new AphrontSideNavFilterView(); 7 * $nav 8 * ->setBaseURI($some_uri) 9 * ->addLabel('Cats') 10 * ->addFilter('meow', 'Meow') 11 * ->addFilter('purr', 'Purr') 12 * ->addLabel('Dogs') 13 * ->addFilter('woof', 'Woof') 14 * ->addFilter('bark', 'Bark'); 15 * $valid_filter = $nav->selectFilter($user_selection, $default = 'meow'); 16 * 17 */ 18 final class AphrontSideNavFilterView extends AphrontView { 19 20 private $items = array(); 21 private $baseURI; 22 private $selectedFilter = false; 23 private $flexible; 24 private $collapsed = false; 25 private $active; 26 private $menu; 27 private $crumbs; 28 private $classes = array(); 29 private $menuID; 30 31 public function setMenuID($menu_id) { 32 $this->menuID = $menu_id; 33 return $this; 34 } 35 public function getMenuID() { 36 return $this->menuID; 37 } 38 39 public function __construct() { 40 $this->menu = new PHUIListView(); 41 } 42 43 public function addClass($class) { 44 $this->classes[] = $class; 45 return $this; 46 } 47 48 public static function newFromMenu(PHUIListView $menu) { 49 $object = new AphrontSideNavFilterView(); 50 $object->setBaseURI(new PhutilURI('/')); 51 $object->menu = $menu; 52 return $object; 53 } 54 55 public function setCrumbs(PhabricatorCrumbsView $crumbs) { 56 $this->crumbs = $crumbs; 57 return $this; 58 } 59 60 public function getCrumbs() { 61 return $this->crumbs; 62 } 63 64 public function setActive($active) { 65 $this->active = $active; 66 return $this; 67 } 68 69 public function setFlexible($flexible) { 70 $this->flexible = $flexible; 71 return $this; 72 } 73 74 public function setCollapsed($collapsed) { 75 $this->collapsed = $collapsed; 76 return $this; 77 } 78 79 public function getMenuView() { 80 return $this->menu; 81 } 82 83 public function addMenuItem(PHUIListItemView $item) { 84 $this->menu->addMenuItem($item); 85 return $this; 86 } 87 88 public function getMenu() { 89 return $this->menu; 90 } 91 92 public function addFilter($key, $name, $uri = null) { 93 return $this->addThing( 94 $key, $name, $uri, PHUIListItemView::TYPE_LINK); 95 } 96 97 public function addButton($key, $name, $uri = null) { 98 return $this->addThing( 99 $key, $name, $uri, PHUIListItemView::TYPE_BUTTON); 100 } 101 102 private function addThing( 103 $key, 104 $name, 105 $uri = null, 106 $type) { 107 108 $item = id(new PHUIListItemView()) 109 ->setName($name) 110 ->setType($type); 111 112 if (strlen($key)) { 113 $item->setKey($key); 114 } 115 116 if ($uri) { 117 $item->setHref($uri); 118 } else { 119 $href = clone $this->baseURI; 120 $href->setPath(rtrim($href->getPath().$key, '/').'/'); 121 $href = (string)$href; 122 123 $item->setHref($href); 124 } 125 126 return $this->addMenuItem($item); 127 } 128 129 public function addCustomBlock($block) { 130 $this->menu->addMenuItem( 131 id(new PHUIListItemView()) 132 ->setType(PHUIListItemView::TYPE_CUSTOM) 133 ->appendChild($block)); 134 return $this; 135 } 136 137 public function addLabel($name) { 138 return $this->addMenuItem( 139 id(new PHUIListItemView()) 140 ->setType(PHUIListItemView::TYPE_LABEL) 141 ->setName($name)); 142 } 143 144 public function setBaseURI(PhutilURI $uri) { 145 $this->baseURI = $uri; 146 return $this; 147 } 148 149 public function getBaseURI() { 150 return $this->baseURI; 151 } 152 153 public function selectFilter($key, $default = null) { 154 $this->selectedFilter = $default; 155 if ($this->menu->getItem($key) && strlen($key)) { 156 $this->selectedFilter = $key; 157 } 158 return $this->selectedFilter; 159 } 160 161 public function getSelectedFilter() { 162 return $this->selectedFilter; 163 } 164 165 public function render() { 166 if ($this->menu->getItems()) { 167 if (!$this->baseURI) { 168 throw new Exception(pht('Call setBaseURI() before render()!')); 169 } 170 if ($this->selectedFilter === false) { 171 throw new Exception(pht('Call selectFilter() before render()!')); 172 } 173 } 174 175 if ($this->selectedFilter !== null) { 176 $selected_item = $this->menu->getItem($this->selectedFilter); 177 if ($selected_item) { 178 $selected_item->addClass('phui-list-item-selected'); 179 } 180 } 181 182 require_celerity_resource('phabricator-side-menu-view-css'); 183 184 return $this->renderFlexNav(); 185 } 186 187 private function renderFlexNav() { 188 189 $user = $this->user; 190 191 require_celerity_resource('phabricator-nav-view-css'); 192 193 $nav_classes = array(); 194 $nav_classes[] = 'phabricator-nav'; 195 196 $nav_id = null; 197 $drag_id = null; 198 $content_id = celerity_generate_unique_node_id(); 199 $local_id = null; 200 $background_id = null; 201 $local_menu = null; 202 $main_id = celerity_generate_unique_node_id(); 203 204 if ($this->flexible) { 205 $drag_id = celerity_generate_unique_node_id(); 206 $flex_bar = phutil_tag( 207 'div', 208 array( 209 'class' => 'phabricator-nav-drag', 210 'id' => $drag_id, 211 ), 212 ''); 213 } else { 214 $flex_bar = null; 215 } 216 217 $nav_menu = null; 218 if ($this->menu->getItems()) { 219 $local_id = celerity_generate_unique_node_id(); 220 $background_id = celerity_generate_unique_node_id(); 221 222 if (!$this->collapsed) { 223 $nav_classes[] = 'has-local-nav'; 224 } 225 226 $menu_background = phutil_tag( 227 'div', 228 array( 229 'class' => 'phabricator-nav-column-background', 230 'id' => $background_id, 231 ), 232 ''); 233 234 $local_menu = array( 235 $menu_background, 236 phutil_tag( 237 'div', 238 array( 239 'class' => 'phabricator-nav-local phabricator-side-menu', 240 'id' => $local_id, 241 ), 242 $this->menu->setID($this->getMenuID())), 243 ); 244 } 245 246 $crumbs = null; 247 if ($this->crumbs) { 248 $crumbs = $this->crumbs->render(); 249 $nav_classes[] = 'has-crumbs'; 250 } 251 252 if ($this->flexible) { 253 if (!$this->collapsed) { 254 $nav_classes[] = 'has-drag-nav'; 255 } 256 257 Javelin::initBehavior( 258 'phabricator-nav', 259 array( 260 'mainID' => $main_id, 261 'localID' => $local_id, 262 'dragID' => $drag_id, 263 'contentID' => $content_id, 264 'backgroundID' => $background_id, 265 'collapsed' => $this->collapsed, 266 )); 267 268 if ($this->active) { 269 Javelin::initBehavior( 270 'phabricator-active-nav', 271 array( 272 'localID' => $local_id, 273 )); 274 } 275 } 276 277 $nav_classes = array_merge($nav_classes, $this->classes); 278 279 return phutil_tag( 280 'div', 281 array( 282 'class' => implode(' ', $nav_classes), 283 'id' => $main_id, 284 ), 285 array( 286 $local_menu, 287 $flex_bar, 288 phutil_tag( 289 'div', 290 array( 291 'class' => 'phabricator-nav-content mlb', 292 'id' => $content_id, 293 ), 294 array( 295 $crumbs, 296 $this->renderChildren(), 297 )), 298 )); 299 } 300 301 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |