[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PHUIListView extends AphrontTagView { 4 5 const NAVBAR_LIST = 'phui-list-navbar'; 6 const SIDENAV_LIST = 'phui-list-sidenav'; 7 const TABBAR_LIST = 'phui-list-tabbar'; 8 9 private $items = array(); 10 private $type; 11 12 protected function canAppendChild() { 13 return false; 14 } 15 16 public function newLabel($name, $key = null) { 17 $item = id(new PHUIListItemView()) 18 ->setType(PHUIListItemView::TYPE_LABEL) 19 ->setName($name); 20 21 if ($key !== null) { 22 $item->setKey($key); 23 } 24 25 $this->addMenuItem($item); 26 27 return $item; 28 } 29 30 public function newLink($name, $href, $key = null) { 31 $item = id(new PHUIListItemView()) 32 ->setType(PHUIListItemView::TYPE_LINK) 33 ->setName($name) 34 ->setHref($href); 35 36 if ($key !== null) { 37 $item->setKey($key); 38 } 39 40 $this->addMenuItem($item); 41 42 return $item; 43 } 44 45 public function newButton($name, $href) { 46 $item = id(new PHUIListItemView()) 47 ->setType(PHUIListItemView::TYPE_BUTTON) 48 ->setName($name) 49 ->setHref($href); 50 51 $this->addMenuItem($item); 52 53 return $item; 54 } 55 56 public function addMenuItem(PHUIListItemView $item) { 57 return $this->addMenuItemAfter(null, $item); 58 } 59 60 public function addMenuItemAfter($key, PHUIListItemView $item) { 61 if ($key === null) { 62 $this->items[] = $item; 63 return $this; 64 } 65 66 if (!$this->getItem($key)) { 67 throw new Exception(pht("No such key '%s' to add menu item after!", 68 $key)); 69 } 70 71 $result = array(); 72 foreach ($this->items as $other) { 73 $result[] = $other; 74 if ($other->getKey() == $key) { 75 $result[] = $item; 76 } 77 } 78 79 $this->items = $result; 80 return $this; 81 } 82 83 public function addMenuItemBefore($key, PHUIListItemView $item) { 84 if ($key === null) { 85 array_unshift($this->items, $item); 86 return $this; 87 } 88 89 $this->requireKey($key); 90 91 $result = array(); 92 foreach ($this->items as $other) { 93 if ($other->getKey() == $key) { 94 $result[] = $item; 95 } 96 $result[] = $other; 97 } 98 99 $this->items = $result; 100 return $this; 101 } 102 103 public function addMenuItemToLabel($key, PHUIListItemView $item) { 104 $this->requireKey($key); 105 106 $other = $this->getItem($key); 107 if ($other->getType() != PHUIListItemView::TYPE_LABEL) { 108 throw new Exception(pht("Menu item '%s' is not a label!", $key)); 109 } 110 111 $seen = false; 112 $after = null; 113 foreach ($this->items as $other) { 114 if (!$seen) { 115 if ($other->getKey() == $key) { 116 $seen = true; 117 } 118 } else { 119 if ($other->getType() == PHUIListItemView::TYPE_LABEL) { 120 break; 121 } 122 } 123 $after = $other->getKey(); 124 } 125 126 return $this->addMenuItemAfter($after, $item); 127 } 128 129 private function requireKey($key) { 130 if (!$this->getItem($key)) { 131 throw new Exception(pht("No menu item with key '%s' exists!", $key)); 132 } 133 } 134 135 public function getItem($key) { 136 $key = (string)$key; 137 138 // NOTE: We could optimize this, but need to update any map when items have 139 // their keys change. Since that's moderately complex, wait for a profile 140 // or use case. 141 142 foreach ($this->items as $item) { 143 if ($item->getKey() == $key) { 144 return $item; 145 } 146 } 147 148 return null; 149 } 150 151 public function getItems() { 152 return $this->items; 153 } 154 155 protected function willRender() { 156 $key_map = array(); 157 foreach ($this->items as $item) { 158 $key = $item->getKey(); 159 if ($key !== null) { 160 if (isset($key_map[$key])) { 161 throw new Exception( 162 pht("Menu contains duplicate items with key '%s'!", $key)); 163 } 164 $key_map[$key] = $item; 165 } 166 } 167 } 168 169 public function getTagName() { 170 return 'ul'; 171 } 172 173 public function setType($type) { 174 $this->type = $type; 175 return $this; 176 } 177 178 protected function getTagAttributes() { 179 require_celerity_resource('phui-list-view-css'); 180 $classes = array(); 181 $classes[] = 'phui-list-view'; 182 if ($this->type) { 183 $classes[] = $this->type; 184 } 185 return array( 186 'class' => implode(' ', $classes), 187 ); 188 } 189 190 protected function getTagContent() { 191 return $this->items; 192 } 193 }
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 |