From 34628002fd96639247864afbfc69b4d9b6db844c Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 10 Jul 2014 16:18:04 -0700 Subject: [PATCH] Modernize "repositories" typeahead datasource Summary: Ref T4420. - Allow tokenizers to accept either a `Datasource` object (new style) or a URI (old style). - Read URI and placeholder text from object, if available. - Swap the "repositories" datasource (which seemed like the simplest one) over to the new stuff. - Tweak/update the repo tokens a little bit. Test Plan: - Used tokenizer in Herald, Differential (search), Differential (edit), Push Logs. - Grepped for other callsites. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T4420 Differential Revision: https://secure.phabricator.com/D9874 --- src/__phutil_library_map__.php | 3 +- .../DifferentialRepositoryField.php | 2 +- .../DifferentialRevisionSearchEngine.php | 2 +- .../DiffusionRepositoryDatasource.php | 35 +++++++++++++++++++ .../controller/HeraldRuleController.php | 3 +- ...habricatorRepositoryPHIDTypeRepository.php | 6 ++-- ...abricatorRepositoryPushLogSearchEngine.php | 2 +- ...torTypeaheadCommonDatasourceController.php | 17 --------- .../PhabricatorTypeaheadDatasource.php | 4 +++ .../control/AphrontFormTokenizerControl.php | 20 ++++++++--- 10 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 src/applications/diffusion/typeahead/DiffusionRepositoryDatasource.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index da9fd3434c..0dc58dbcee 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -5,7 +5,6 @@ * @generated * @phutil-library-version 2 */ - phutil_register_library_map(array( '__library_version__' => 2, 'class' => @@ -544,6 +543,7 @@ phutil_register_library_map(array( 'DiffusionRenameHistoryQuery' => 'applications/diffusion/query/DiffusionRenameHistoryQuery.php', 'DiffusionRepositoryController' => 'applications/diffusion/controller/DiffusionRepositoryController.php', 'DiffusionRepositoryCreateController' => 'applications/diffusion/controller/DiffusionRepositoryCreateController.php', + 'DiffusionRepositoryDatasource' => 'applications/diffusion/typeahead/DiffusionRepositoryDatasource.php', 'DiffusionRepositoryDefaultController' => 'applications/diffusion/controller/DiffusionRepositoryDefaultController.php', 'DiffusionRepositoryEditActionsController' => 'applications/diffusion/controller/DiffusionRepositoryEditActionsController.php', 'DiffusionRepositoryEditActivateController' => 'applications/diffusion/controller/DiffusionRepositoryEditActivateController.php', @@ -3245,6 +3245,7 @@ phutil_register_library_map(array( 'DiffusionRefNotFoundException' => 'Exception', 'DiffusionRepositoryController' => 'DiffusionController', 'DiffusionRepositoryCreateController' => 'DiffusionRepositoryEditController', + 'DiffusionRepositoryDatasource' => 'PhabricatorTypeaheadDatasource', 'DiffusionRepositoryDefaultController' => 'DiffusionController', 'DiffusionRepositoryEditActionsController' => 'DiffusionRepositoryEditController', 'DiffusionRepositoryEditActivateController' => 'DiffusionRepositoryEditController', diff --git a/src/applications/differential/customfield/DifferentialRepositoryField.php b/src/applications/differential/customfield/DifferentialRepositoryField.php index 0bd430ec7b..cab25dbbe5 100644 --- a/src/applications/differential/customfield/DifferentialRepositoryField.php +++ b/src/applications/differential/customfield/DifferentialRepositoryField.php @@ -49,7 +49,7 @@ final class DifferentialRepositoryField return id(new AphrontFormTokenizerControl()) ->setName($this->getFieldKey()) - ->setDatasource('/typeahead/common/repositories/') + ->setDatasource(new DiffusionRepositoryDatasource()) ->setValue($control_value) ->setError($this->getFieldError()) ->setLabel($this->getFieldName()) diff --git a/src/applications/differential/query/DifferentialRevisionSearchEngine.php b/src/applications/differential/query/DifferentialRevisionSearchEngine.php index 1b1338aeac..872bab85e0 100644 --- a/src/applications/differential/query/DifferentialRevisionSearchEngine.php +++ b/src/applications/differential/query/DifferentialRevisionSearchEngine.php @@ -167,7 +167,7 @@ final class DifferentialRevisionSearchEngine id(new AphrontFormTokenizerControl()) ->setLabel(pht('Repositories')) ->setName('repositories') - ->setDatasource('/typeahead/common/repositories/') + ->setDatasource(new DiffusionRepositoryDatasource()) ->setValue(array_select_keys($handles, $repository_phids))) ->appendChild( id(new AphrontFormSelectControl()) diff --git a/src/applications/diffusion/typeahead/DiffusionRepositoryDatasource.php b/src/applications/diffusion/typeahead/DiffusionRepositoryDatasource.php new file mode 100644 index 0000000000..cdaa5f7c6c --- /dev/null +++ b/src/applications/diffusion/typeahead/DiffusionRepositoryDatasource.php @@ -0,0 +1,35 @@ +getViewer(); + $raw_query = $this->getRawQuery(); + + $results = array(); + + $repos = id(new PhabricatorRepositoryQuery()) + ->setViewer($viewer) + ->execute(); + foreach ($repos as $repo) { + $results[] = id(new PhabricatorTypeaheadResult()) + ->setName($repo->getMonogram().' '.$repo->getName()) + ->setURI('/diffusion/'.$repo->getCallsign().'/') + ->setPHID($repo->getPHID()) + ->setPriorityString($repo->getMonogram()) + ->setIcon('fa-database bluegrey'); + } + + return $results; + } + +} diff --git a/src/applications/herald/controller/HeraldRuleController.php b/src/applications/herald/controller/HeraldRuleController.php index 7aa00e3f78..e538dd3f2a 100644 --- a/src/applications/herald/controller/HeraldRuleController.php +++ b/src/applications/herald/controller/HeraldRuleController.php @@ -592,7 +592,8 @@ final class HeraldRuleController extends HeraldController { 'source' => array( 'email' => '/typeahead/common/mailable/', 'user' => '/typeahead/common/accounts/', - 'repository' => '/typeahead/common/repositories/', + 'repository' => + id(new DiffusionRepositoryDatasource())->getDatasourceURI(), 'package' => '/typeahead/common/packages/', 'project' => '/typeahead/common/projects/', 'userorproject' => '/typeahead/common/accountsorprojects/', diff --git a/src/applications/repository/phid/PhabricatorRepositoryPHIDTypeRepository.php b/src/applications/repository/phid/PhabricatorRepositoryPHIDTypeRepository.php index 1e1cf8f888..922b4767b0 100644 --- a/src/applications/repository/phid/PhabricatorRepositoryPHIDTypeRepository.php +++ b/src/applications/repository/phid/PhabricatorRepositoryPHIDTypeRepository.php @@ -33,12 +33,14 @@ final class PhabricatorRepositoryPHIDTypeRepository foreach ($handles as $phid => $handle) { $repository = $objects[$phid]; + $monogram = $repository->getMonogram(); $callsign = $repository->getCallsign(); $name = $repository->getName(); - $handle->setName("r{$callsign}"); - $handle->setFullName("r{$callsign} ({$name})"); + $handle->setName($monogram); + $handle->setFullName("{$monogram} {$name}"); $handle->setURI("/diffusion/{$callsign}/"); + $handle->setIcon('fa-database'); } } diff --git a/src/applications/repository/query/PhabricatorRepositoryPushLogSearchEngine.php b/src/applications/repository/query/PhabricatorRepositoryPushLogSearchEngine.php index 6757e9715f..757ef03f27 100644 --- a/src/applications/repository/query/PhabricatorRepositoryPushLogSearchEngine.php +++ b/src/applications/repository/query/PhabricatorRepositoryPushLogSearchEngine.php @@ -74,7 +74,7 @@ final class PhabricatorRepositoryPushLogSearchEngine $form ->appendChild( id(new AphrontFormTokenizerControl()) - ->setDatasource('/typeahead/common/repositories/') + ->setDatasource(new DiffusionRepositoryDatasource()) ->setName('repositories') ->setLabel(pht('Repositories')) ->setValue($repository_handles)) diff --git a/src/applications/typeahead/controller/PhabricatorTypeaheadCommonDatasourceController.php b/src/applications/typeahead/controller/PhabricatorTypeaheadCommonDatasourceController.php index adcd510cd7..c9c080913c 100644 --- a/src/applications/typeahead/controller/PhabricatorTypeaheadCommonDatasourceController.php +++ b/src/applications/typeahead/controller/PhabricatorTypeaheadCommonDatasourceController.php @@ -26,7 +26,6 @@ final class PhabricatorTypeaheadCommonDatasourceController $need_applications = false; $need_lists = false; $need_projs = false; - $need_repos = false; $need_packages = false; $need_upforgrabs = false; $need_arcanist_projects = false; @@ -78,9 +77,6 @@ final class PhabricatorTypeaheadCommonDatasourceController $need_projs = true; $need_packages = true; break; - case 'repositories': - $need_repos = true; - break; case 'packages': $need_packages = true; break; @@ -298,19 +294,6 @@ final class PhabricatorTypeaheadCommonDatasourceController } } - if ($need_repos) { - $repos = id(new PhabricatorRepositoryQuery()) - ->setViewer($viewer) - ->execute(); - foreach ($repos as $repo) { - $results[] = id(new PhabricatorTypeaheadResult()) - ->setName('r'.$repo->getCallsign().' ('.$repo->getName().')') - ->setURI('/diffusion/'.$repo->getCallsign().'/') - ->setPHID($repo->getPHID()) - ->setPriorityString('r'.$repo->getCallsign()); - } - } - if ($need_packages) { $packages = id(new PhabricatorOwnersPackage())->loadAll(); foreach ($packages as $package) { diff --git a/src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php b/src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php index e017cd1166..7ec636dda0 100644 --- a/src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php +++ b/src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php @@ -43,6 +43,10 @@ abstract class PhabricatorTypeaheadDatasource extends Phobject { return $this->query; } + public function getDatasourceURI() { + return '/typeahead/class/'.get_class($this).'/'; + } + abstract public function getPlaceholderText(); abstract public function getDatasourceApplicationClass(); abstract public function loadResults(); diff --git a/src/view/form/control/AphrontFormTokenizerControl.php b/src/view/form/control/AphrontFormTokenizerControl.php index b01e199366..ff82986a99 100644 --- a/src/view/form/control/AphrontFormTokenizerControl.php +++ b/src/view/form/control/AphrontFormTokenizerControl.php @@ -43,8 +43,10 @@ final class AphrontFormTokenizerControl extends AphrontFormControl { $id = celerity_generate_unique_node_id(); } - if (!$this->placeholder) { - $this->placeholder = $this->getDefaultPlaceholder(); + if (!strlen($this->placeholder)) { + $placeholder = $this->getDefaultPlaceholder(); + } else { + $placeholder = $this->placeholder; } $template = new AphrontTokenizerTemplateView(); @@ -57,15 +59,21 @@ final class AphrontFormTokenizerControl extends AphrontFormControl { $username = $this->user->getUsername(); } + if ($this->datasource instanceof PhabricatorTypeaheadDatasource) { + $datasource_uri = $this->datasource->getDatasourceURI(); + } else { + $datasource_uri = $this->datasource; + } + if (!$this->disableBehavior) { Javelin::initBehavior('aphront-basic-tokenizer', array( 'id' => $id, - 'src' => $this->datasource, + 'src' => $datasource_uri, 'value' => mpull($values, 'getFullName', 'getPHID'), 'icons' => mpull($values, 'getIcon', 'getPHID'), 'limit' => $this->limit, 'username' => $username, - 'placeholder' => $this->placeholder, + 'placeholder' => $placeholder, )); } @@ -75,6 +83,10 @@ final class AphrontFormTokenizerControl extends AphrontFormControl { private function getDefaultPlaceholder() { $datasource = $this->datasource; + if ($datasource instanceof PhabricatorTypeaheadDatasource) { + return $datasource->getPlaceholderText(); + } + $matches = null; if (!preg_match('@^/typeahead/common/(.*)/$@', $datasource, $matches)) { return null;