diff --git a/src/applications/differential/controller/revisionlist/DifferentialRevisionListController.php b/src/applications/differential/controller/revisionlist/DifferentialRevisionListController.php index 57dbfba859..6356b6a744 100644 --- a/src/applications/differential/controller/revisionlist/DifferentialRevisionListController.php +++ b/src/applications/differential/controller/revisionlist/DifferentialRevisionListController.php @@ -312,30 +312,26 @@ class DifferentialRevisionListController extends DifferentialController { ->setValue($value) ->setLimit(1); case 'status': - $links = $this->renderToggleButtons( - array( - 'all' => 'All', - 'open' => 'Open', - 'committed' => 'Committed', - ), - $params['status'], - $uri, - 'status'); return id(new AphrontFormToggleButtonsControl()) ->setLabel('Status') - ->setValue($links); + ->setValue($params['status']) + ->setBaseURI($uri, 'status') + ->setButtons( + array( + 'all' => 'All', + 'open' => 'Open', + 'committed' => 'Committed', + )); case 'order': - $links = $this->renderToggleButtons( - array( - 'modified' => 'Modified', - 'created' => 'Created', - ), - $params['order'], - $uri, - 'order'); return id(new AphrontFormToggleButtonsControl()) ->setLabel('Order') - ->setValue($links); + ->setValue($params['order']) + ->setBaseURI($uri, 'order') + ->setButtons( + array( + 'modified' => 'Modified', + 'created' => 'Created', + )); default: throw new Exception("Unknown control '{$control}'!"); } @@ -417,23 +413,5 @@ class DifferentialRevisionListController extends DifferentialController { return $views; } - private function renderToggleButtons($buttons, $selected, $uri, $param) { - $links = array(); - foreach ($buttons as $value => $name) { - if ($value == $selected) { - $more = ' toggle-selected toggle-fixed'; - } else { - $more = null; - } - $links[] = phutil_render_tag( - 'a', - array( - 'class' => 'toggle'.$more, - 'href' => $uri->alter($param, $value), - ), - phutil_escape_html($name)); - } - return implode('', $links); - } } diff --git a/src/applications/maniphest/controller/tasklist/ManiphestTaskListController.php b/src/applications/maniphest/controller/tasklist/ManiphestTaskListController.php index 7f216f1cfb..26b780b2f6 100644 --- a/src/applications/maniphest/controller/tasklist/ManiphestTaskListController.php +++ b/src/applications/maniphest/controller/tasklist/ManiphestTaskListController.php @@ -77,9 +77,9 @@ class ManiphestTaskListController extends ManiphestController { 'triage' => true, ); - list($status_map, $status_links) = $this->renderStatusLinks(); - list($grouping, $group_links) = $this->renderGroupLinks(); - list($order, $order_links) = $this->renderOrderLinks(); + list($status_map, $status_control) = $this->renderStatusLinks(); + list($grouping, $group_control) = $this->renderGroupLinks(); + list($order, $order_control) = $this->renderOrderLinks(); $user_phids = $request->getStr('users'); if (strlen($user_phids)) { @@ -150,18 +150,9 @@ class ManiphestTaskListController extends ManiphestController { ->setValue($tokens)); $form - ->appendChild( - id(new AphrontFormToggleButtonsControl()) - ->setLabel('Status') - ->setValue($status_links)) - ->appendChild( - id(new AphrontFormToggleButtonsControl()) - ->setLabel('Group') - ->setValue($group_links)) - ->appendChild( - id(new AphrontFormToggleButtonsControl()) - ->setLabel('Order') - ->setValue($order_links)); + ->appendChild($status_control) + ->appendChild($group_control) + ->appendChild($order_control); $form->appendChild( id(new AphrontFormSubmitControl()) @@ -407,15 +398,18 @@ class ManiphestTaskListController extends ManiphestController { $status = 'o'; } - $button_names = array( - 'Open' => 'o', - 'Closed' => 'c', - 'All' => 'oc', - ); + $status_control = id(new AphrontFormToggleButtonsControl()) + ->setLabel('Status') + ->setValue($status) + ->setBaseURI($request->getRequestURI(), 's') + ->setButtons( + array( + 'o' => 'Open', + 'c' => 'Closed', + 'oc' => 'All', + )); - $status_links = $this->renderFilterLinks($button_names, $status, 's'); - - return array($statuses[$status], $status_links); + return array($statuses[$status], $status_control); } public function renderOrderLinks() { @@ -432,15 +426,18 @@ class ManiphestTaskListController extends ManiphestController { } $order_by = $orders[$order]; - $order_names = array( - 'Priority' => 'p', - 'Updated' => 'u', - 'Created' => 'c', - ); + $order_control = id(new AphrontFormToggleButtonsControl()) + ->setLabel('Order') + ->setValue($order) + ->setBaseURI($request->getRequestURI(), 'o') + ->setButtons( + array( + 'p' => 'Priority', + 'u' => 'Updated', + 'c' => 'Created', + )); - $order_links = $this->renderFilterLinks($order_names, $order, 'o'); - - return array($order_by, $order_links); + return array($order_by, $order_control); } public function renderGroupLinks() { @@ -458,40 +455,20 @@ class ManiphestTaskListController extends ManiphestController { } $group_by = $groups[$group]; - $group_names = array( - 'Priority' => 'p', - 'Owner' => 'o', - 'Status' => 's', - 'None' => 'n', - ); - $group_links = $this->renderFilterLinks($group_names, $group, 'g'); - - return array($group_by, $group_links); - } - - private function renderFilterLinks($filter_map, $selected, $uri_param) { - $request = $this->getRequest(); - $uri = $request->getRequestURI(); - - $links = array(); - foreach ($filter_map as $name => $value) { - if ($value == $selected) { - $more = ' toggle-selected toggle-fixed'; - $href = null; - } else { - $more = null; - $href = $uri->alter($uri_param, $value); - } - $links[] = phutil_render_tag( - 'a', + $group_control = id(new AphrontFormToggleButtonsControl()) + ->setLabel('Group') + ->setValue($group) + ->setBaseURI($request->getRequestURI(), 'g') + ->setButtons( array( - 'class' => 'toggle'.$more, - 'href' => $href, - ), - $name); - } - return implode("\n", $links); + 'p' => 'Priority', + 'o' => 'Owner', + 's' => 'Status', + 'n' => 'None', + )); + + return array($group_by, $group_control); } private function renderBatchEditor() { diff --git a/src/view/form/control/togglebuttons/AphrontFormToggleButtonsControl.php b/src/view/form/control/togglebuttons/AphrontFormToggleButtonsControl.php index 4b7020d8f9..48fc3c2c61 100644 --- a/src/view/form/control/togglebuttons/AphrontFormToggleButtonsControl.php +++ b/src/view/form/control/togglebuttons/AphrontFormToggleButtonsControl.php @@ -1,7 +1,7 @@ baseURI = $uri; + $this->param = $param; + return $this; + } + + public function setButtons(array $buttons) { + $this->buttons = $buttons; + return $this; + } + protected function getCustomControlClass() { return 'aphront-form-control-togglebuttons'; } protected function renderInput() { - return $this->getValue(); + if (!$this->baseURI) { + throw new Exception('Call setBaseURI() before render()!'); + } + + $selected = $this->getValue(); + + $out = array(); + foreach ($this->buttons as $value => $label) { + if ($value == $selected) { + $more = ' toggle-selected toggle-fixed'; + } else { + $more = null; + } + + $out[] = phutil_render_tag( + 'a', + array( + 'class' => 'toggle'.$more, + 'href' => $this->baseURI->alter($this->param, $value), + ), + phutil_escape_html($label)); + } + + return implode('', $out); } } diff --git a/src/view/form/control/togglebuttons/__init__.php b/src/view/form/control/togglebuttons/__init__.php index 83ddf6a6bb..2f977c9f0d 100644 --- a/src/view/form/control/togglebuttons/__init__.php +++ b/src/view/form/control/togglebuttons/__init__.php @@ -8,5 +8,7 @@ phutil_require_module('phabricator', 'view/form/control/base'); +phutil_require_module('phutil', 'markup'); + phutil_require_source('AphrontFormToggleButtonsControl.php');