1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-11 15:21:03 +01:00

Unify "toggle buttons" controls

Summary: This control is a very thin shell right now with Maniphest/Differential
code duplication; unify the implemenations better for use in Audit.

Test Plan: Clicked toggle buttons in Differential and Maniphest.

Reviewers: btrahan, jungejason

Reviewed By: btrahan

CC: aran, epriestley

Differential Revision: https://secure.phabricator.com/D1700
This commit is contained in:
epriestley 2012-02-27 12:59:05 -08:00
parent d7a7bca85c
commit 3289059452
4 changed files with 98 additions and 102 deletions

View file

@ -312,30 +312,26 @@ class DifferentialRevisionListController extends DifferentialController {
->setValue($value) ->setValue($value)
->setLimit(1); ->setLimit(1);
case 'status': case 'status':
$links = $this->renderToggleButtons( return id(new AphrontFormToggleButtonsControl())
->setLabel('Status')
->setValue($params['status'])
->setBaseURI($uri, 'status')
->setButtons(
array( array(
'all' => 'All', 'all' => 'All',
'open' => 'Open', 'open' => 'Open',
'committed' => 'Committed', 'committed' => 'Committed',
), ));
$params['status'],
$uri,
'status');
return id(new AphrontFormToggleButtonsControl())
->setLabel('Status')
->setValue($links);
case 'order': case 'order':
$links = $this->renderToggleButtons( return id(new AphrontFormToggleButtonsControl())
->setLabel('Order')
->setValue($params['order'])
->setBaseURI($uri, 'order')
->setButtons(
array( array(
'modified' => 'Modified', 'modified' => 'Modified',
'created' => 'Created', 'created' => 'Created',
), ));
$params['order'],
$uri,
'order');
return id(new AphrontFormToggleButtonsControl())
->setLabel('Order')
->setValue($links);
default: default:
throw new Exception("Unknown control '{$control}'!"); throw new Exception("Unknown control '{$control}'!");
} }
@ -417,23 +413,5 @@ class DifferentialRevisionListController extends DifferentialController {
return $views; 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);
}
} }

View file

@ -77,9 +77,9 @@ class ManiphestTaskListController extends ManiphestController {
'triage' => true, 'triage' => true,
); );
list($status_map, $status_links) = $this->renderStatusLinks(); list($status_map, $status_control) = $this->renderStatusLinks();
list($grouping, $group_links) = $this->renderGroupLinks(); list($grouping, $group_control) = $this->renderGroupLinks();
list($order, $order_links) = $this->renderOrderLinks(); list($order, $order_control) = $this->renderOrderLinks();
$user_phids = $request->getStr('users'); $user_phids = $request->getStr('users');
if (strlen($user_phids)) { if (strlen($user_phids)) {
@ -150,18 +150,9 @@ class ManiphestTaskListController extends ManiphestController {
->setValue($tokens)); ->setValue($tokens));
$form $form
->appendChild( ->appendChild($status_control)
id(new AphrontFormToggleButtonsControl()) ->appendChild($group_control)
->setLabel('Status') ->appendChild($order_control);
->setValue($status_links))
->appendChild(
id(new AphrontFormToggleButtonsControl())
->setLabel('Group')
->setValue($group_links))
->appendChild(
id(new AphrontFormToggleButtonsControl())
->setLabel('Order')
->setValue($order_links));
$form->appendChild( $form->appendChild(
id(new AphrontFormSubmitControl()) id(new AphrontFormSubmitControl())
@ -407,15 +398,18 @@ class ManiphestTaskListController extends ManiphestController {
$status = 'o'; $status = 'o';
} }
$button_names = array( $status_control = id(new AphrontFormToggleButtonsControl())
'Open' => 'o', ->setLabel('Status')
'Closed' => 'c', ->setValue($status)
'All' => 'oc', ->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_control);
return array($statuses[$status], $status_links);
} }
public function renderOrderLinks() { public function renderOrderLinks() {
@ -432,15 +426,18 @@ class ManiphestTaskListController extends ManiphestController {
} }
$order_by = $orders[$order]; $order_by = $orders[$order];
$order_names = array( $order_control = id(new AphrontFormToggleButtonsControl())
'Priority' => 'p', ->setLabel('Order')
'Updated' => 'u', ->setValue($order)
'Created' => 'c', ->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_control);
return array($order_by, $order_links);
} }
public function renderGroupLinks() { public function renderGroupLinks() {
@ -458,40 +455,20 @@ class ManiphestTaskListController extends ManiphestController {
} }
$group_by = $groups[$group]; $group_by = $groups[$group];
$group_names = array(
'Priority' => 'p',
'Owner' => 'o',
'Status' => 's',
'None' => 'n',
);
$group_links = $this->renderFilterLinks($group_names, $group, 'g'); $group_control = id(new AphrontFormToggleButtonsControl())
->setLabel('Group')
return array($group_by, $group_links); ->setValue($group)
} ->setBaseURI($request->getRequestURI(), 'g')
->setButtons(
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',
array( array(
'class' => 'toggle'.$more, 'p' => 'Priority',
'href' => $href, 'o' => 'Owner',
), 's' => 'Status',
$name); 'n' => 'None',
} ));
return implode("\n", $links);
return array($group_by, $group_control);
} }
private function renderBatchEditor() { private function renderBatchEditor() {

View file

@ -1,7 +1,7 @@
<?php <?php
/* /*
* Copyright 2011 Facebook, Inc. * Copyright 2012 Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,12 +18,51 @@
class AphrontFormToggleButtonsControl extends AphrontFormControl { class AphrontFormToggleButtonsControl extends AphrontFormControl {
private $baseURI;
private $param;
private $buttons;
public function setBaseURI(PhutilURI $uri, $param) {
$this->baseURI = $uri;
$this->param = $param;
return $this;
}
public function setButtons(array $buttons) {
$this->buttons = $buttons;
return $this;
}
protected function getCustomControlClass() { protected function getCustomControlClass() {
return 'aphront-form-control-togglebuttons'; return 'aphront-form-control-togglebuttons';
} }
protected function renderInput() { 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);
} }
} }

View file

@ -8,5 +8,7 @@
phutil_require_module('phabricator', 'view/form/control/base'); phutil_require_module('phabricator', 'view/form/control/base');
phutil_require_module('phutil', 'markup');
phutil_require_source('AphrontFormToggleButtonsControl.php'); phutil_require_source('AphrontFormToggleButtonsControl.php');