1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-18 18:51:12 +01:00

Add basic NUX support to SearchEngines

Summary:
This is just putting a hook in that pretty much works. Behavior:

  - If you visit `/maniphest/?nux=true`, it always shows NUX for testing.
  - Otherwise, it shows NUX if there are no objects in the application yet.

Test Plan: {F1031846}

Reviewers: chad

Reviewed By: chad

Differential Revision: https://secure.phabricator.com/D14828
This commit is contained in:
epriestley 2015-12-19 12:43:05 -08:00
parent 9d1ba9c038
commit dbb84f1ddc
2 changed files with 129 additions and 42 deletions

View file

@ -107,11 +107,19 @@ final class PhabricatorApplicationSearchController
// URIs like "/query/?users=a,b".
$pt_data = $request->getPassthroughRequestData();
$exempt = array(
'before' => true,
'after' => true,
'nux' => true,
);
foreach ($pt_data as $pt_key => $pt_value) {
if ($pt_key != 'before' && $pt_key != 'after') {
$found_query_data = true;
break;
if (isset($exempt[$pt_key])) {
continue;
}
$found_query_data = true;
break;
}
}
@ -203,12 +211,15 @@ final class PhabricatorApplicationSearchController
$body[] = $box;
if ($run_query) {
$box->setAnchor(
id(new PhabricatorAnchorView())
->setAnchorName('R'));
try {
$engine->setRequest($request);
$query = $engine->buildQueryFromSavedQuery($saved_query);
$pager = $engine->newPagerForSavedQuery($saved_query);
@ -216,49 +227,58 @@ final class PhabricatorApplicationSearchController
$objects = $engine->executeQuery($query, $pager);
$engine->setRequest($request);
$list = $engine->renderResults($objects, $saved_query);
if (!($list instanceof PhabricatorApplicationSearchResultView)) {
throw new Exception(
pht(
'SearchEngines must render a "%s" object, but this engine '.
'(of class "%s") rendered something else.',
'PhabricatorApplicationSearchResultView',
get_class($engine)));
$force_nux = $request->getBool('nux');
if (!$objects || $force_nux) {
$nux_view = $this->renderNewUserView($engine, $force_nux);
} else {
$nux_view = null;
}
if ($list->getActions()) {
foreach ($list->getActions() as $action) {
$header->addActionLink($action);
if ($nux_view) {
$box->appendChild($nux_view);
} else {
$list = $engine->renderResults($objects, $saved_query);
if (!($list instanceof PhabricatorApplicationSearchResultView)) {
throw new Exception(
pht(
'SearchEngines must render a "%s" object, but this engine '.
'(of class "%s") rendered something else.',
'PhabricatorApplicationSearchResultView',
get_class($engine)));
}
if ($list->getActions()) {
foreach ($list->getActions() as $action) {
$header->addActionLink($action);
}
}
if ($list->getObjectList()) {
$box->setObjectList($list->getObjectList());
}
if ($list->getTable()) {
$box->setTable($list->getTable());
}
if ($list->getInfoView()) {
$box->setInfoView($list->getInfoView());
}
if ($list->getContent()) {
$box->appendChild($list->getContent());
}
if ($list->getCollapsed()) {
$box->setCollapsed(true);
}
if ($pager->willShowPagingControls()) {
$pager_box = id(new PHUIBoxView())
->addPadding(PHUI::PADDING_MEDIUM)
->addMargin(PHUI::MARGIN_LARGE)
->setBorder(true)
->appendChild($pager);
$body[] = $pager_box;
}
}
if ($list->getObjectList()) {
$box->setObjectList($list->getObjectList());
}
if ($list->getTable()) {
$box->setTable($list->getTable());
}
if ($list->getInfoView()) {
$box->setInfoView($list->getInfoView());
}
if ($list->getContent()) {
$box->appendChild($list->getContent());
}
if ($list->getCollapsed()) {
$box->setCollapsed(true);
}
if ($pager->willShowPagingControls()) {
$pager_box = id(new PHUIBoxView())
->addPadding(PHUI::PADDING_MEDIUM)
->addMargin(PHUI::MARGIN_LARGE)
->setBorder(true)
->appendChild($pager);
$body[] = $pager_box;
}
} catch (PhabricatorTypeaheadInvalidTokenException $ex) {
$errors[] = pht(
'This query specifies an invalid parameter. Review the '.
@ -396,4 +416,47 @@ final class PhabricatorApplicationSearchController
return $nav;
}
private function renderNewUserView(
PhabricatorApplicationSearchEngine $engine,
$force_nux) {
// Don't render NUX if the user has clicked away from the default page.
if (strlen($this->getQueryKey())) {
return null;
}
// Don't put NUX in panels because it would be weird.
if ($engine->isPanelContext()) {
return null;
}
// Try to render the view itself first, since this should be very cheap
// (just returning some text).
$nux_view = $engine->renderNewUserView();
if (!$nux_view) {
return null;
}
$query = $engine->newQuery();
if (!$query) {
return null;
}
// Try to load any object at all. If we can, the application has seen some
// use so we just render the normal view.
if (!$force_nux) {
$object = $query
->setViewer(PhabricatorUser::getOmnipotentUser())
->setLimit(1)
->execute();
if ($object) {
return null;
}
}
return $nux_view;
}
}

View file

@ -1354,4 +1354,28 @@ abstract class PhabricatorApplicationSearchEngine extends Phobject {
return $attachments;
}
final public function renderNewUserView() {
$head = $this->getNewUserHeader();
$body = $this->getNewUserBody();
if (!strlen($head) && !strlen($body)) {
return null;
}
$viewer = $this->requireViewer();
return id(new PHUIBoxView())
->addMargin(PHUI::MARGIN_LARGE)
->appendChild($head)
->appendChild(new PHUIRemarkupView($viewer, $body));
}
protected function getNewUserHeader() {
return null;
}
protected function getNewUserBody() {
return null;
}
}