Documentation search result UI.

Please note that the entry point of search is not in the UI yet. To see the
search result UI, you'll have to build with docs, then point your browser to the
URL directly now:

http[s]://[your gerrit instance]/#/Documentation/[keyword]

TODO: Currently we skipped paging. Maybe add it back later.

Change-Id: I7d920d2f4b9044bdd0de9b30f005405b140c0458
This commit is contained in:
Yuxuan 'fishy' Wang
2013-10-28 17:48:09 -07:00
parent 93d6838485
commit ff13be1fd0
10 changed files with 324 additions and 0 deletions

View File

@@ -71,6 +71,7 @@ import com.google.gerrit.client.changes.QueryScreen;
import com.google.gerrit.client.dashboards.DashboardInfo;
import com.google.gerrit.client.dashboards.DashboardList;
import com.google.gerrit.client.diff.SideBySide2;
import com.google.gerrit.client.documentation.DocScreen;
import com.google.gerrit.client.groups.GroupApi;
import com.google.gerrit.client.groups.GroupInfo;
import com.google.gerrit.client.patches.PatchScreen;
@@ -201,6 +202,9 @@ public class Dispatcher {
if (matchPrefix("/q/", token)) {
query(token);
} else if (matchPrefix("/Documentation/", token)) {
docSearch(token);
} else if (matchPrefix("/c/", token)) {
change(token);
@@ -884,4 +888,12 @@ public class Dispatcher {
}
}
}
private static void docSearch(final String token) {
GWT.runAsync(new AsyncSplit(token) {
public void onSuccess() {
Gerrit.display(token, new DocScreen(skip(token)));
}
});
}
}

View File

@@ -0,0 +1,25 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.client.documentation;
import com.google.gwt.i18n.client.Constants;
public interface DocConstants extends Constants {
String keyReloadSearch();
String docItemHelp();
String docTableColumnTitle();
String docTableNone();
}

View File

@@ -0,0 +1,5 @@
keyReloadSearch = Reload documentation list
docItemHelp = documentation
docTableColumnTitle = Title
docTableNone = (None)

View File

@@ -0,0 +1,31 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.client.documentation;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
public class DocInfo extends JavaScriptObject {
public final native String title() /*-{ return this.title; }-*/;
public final native String url() /*-{ return this.url; }-*/;
protected DocInfo() {
}
public final String getFullUrl() {
return GWT.getHostPageBaseURL() + url();
}
}

View File

@@ -0,0 +1,22 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.client.documentation;
import com.google.gwt.i18n.client.Messages;
public interface DocMessages extends Messages {
String docQueryWindowTitle(String query);
String docQueryPageTitle(String query);
}

View File

@@ -0,0 +1,2 @@
docQueryWindowTitle = {0}
docQueryPageTitle = Search for {0} in documentation

View File

@@ -0,0 +1,78 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.client.documentation;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.rpc.RestApi;
import com.google.gerrit.client.ui.Screen;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtorm.client.KeyUtil;
public class DocScreen extends Screen {
private static final String URI = "/Documentation/";
private DocTable table;
private final String query;
public DocScreen(String query) {
this.query = KeyUtil.decode(query);
}
@Override
protected void onInitUI() {
super.onInitUI();
table = new DocTable();
table.setSavePointerId(query);
add(table);
setWindowTitle(Util.M.docQueryWindowTitle(query));
setPageTitle(Util.M.docQueryPageTitle(query));
}
@Override
protected void onLoad() {
super.onLoad();
doQuery();
}
@Override
public void registerKeys() {
super.registerKeys();
table.setRegisterKeys(true);
}
private AsyncCallback<JsArray<DocInfo>> loadCallback() {
return new GerritCallback<JsArray<DocInfo>>() {
@Override
public void onSuccess(JsArray<DocInfo> result) {
displayResults(result);
display();
}
};
}
private void displayResults(JsArray<DocInfo> result) {
table.display(result);
table.finishDisplay();
}
private void doQuery() {
RestApi call = new RestApi(URI);
call.addParameterRaw("q", KeyUtil.encode(query));
call.get(loadCallback());
}
}

View File

@@ -0,0 +1,125 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.client.documentation;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.ui.NavigationTable;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
class DocTable extends NavigationTable<DocInfo> {
private static final int C_TITLE = 1;
private int rows = 0;
private int dataBeginRow = 0;
public DocTable() {
super(Util.C.docItemHelp());
table.setText(0, C_TITLE, Util.C.docTableColumnTitle());
FlexCellFormatter fmt = table.getFlexCellFormatter();
fmt.addStyleName(0, C_TITLE, Gerrit.RESOURCES.css().dataHeader());
table.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Cell cell = table.getCellForEvent(event);
if (cell == null) {
return;
}
if (getRowItem(cell.getRowIndex()) != null) {
movePointerTo(cell.getRowIndex());
}
}
});
}
@Override
protected Object getRowItemKey(DocInfo item) {
return item.url();
}
@Override
protected void onOpenRow(int row) {
DocInfo d = getRowItem(row);
Window.Location.assign(d.getFullUrl());
}
private void insertNoneRow(int row) {
table.insertRow(row);
table.setText(row, 0, Util.C.docTableNone());
FlexCellFormatter fmt = table.getFlexCellFormatter();
fmt.setStyleName(row, 0, Gerrit.RESOURCES.css().emptySection());
}
private void insertDocRow(int row) {
table.insertRow(row);
applyDataRowStyle(row);
}
@Override
protected void applyDataRowStyle(int row) {
super.applyDataRowStyle(row);
CellFormatter fmt = table.getCellFormatter();
fmt.addStyleName(row, C_TITLE, Gerrit.RESOURCES.css().dataCell());
fmt.addStyleName(row, C_TITLE, Gerrit.RESOURCES.css().cSUBJECT());
}
private void populateDocRow(int row, DocInfo d) {
table.setWidget(row, C_TITLE, new DocLink(d));
setRowItem(row, d);
}
public void display(JsArray<DocInfo> docList) {
int sz = docList != null ? docList.length() : 0;
boolean hadData = rows > 0;
if (hadData) {
while (sz < rows) {
table.removeRow(dataBeginRow);
rows--;
}
} else {
table.removeRow(dataBeginRow);
}
if (sz == 0) {
insertNoneRow(dataBeginRow);
return;
}
while (rows < sz) {
insertDocRow(dataBeginRow + rows);
rows++;
}
for (int i = 0; i < sz; i++) {
populateDocRow(dataBeginRow + i, docList.get(i));
}
}
public static class DocLink extends Anchor {
public DocLink(DocInfo d) {
super(com.google.gerrit.client.changes.Util.cropSubject(d.title()));
setHref(d.getFullUrl());
}
}
}

View File

@@ -0,0 +1,22 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.client.documentation;
import com.google.gwt.core.client.GWT;
public class Util {
public static final DocConstants C = GWT.create(DocConstants.class);
public static final DocMessages M = GWT.create(DocMessages.class);
}

View File

@@ -82,6 +82,8 @@ public class QueryDocumentationExecutor {
}
try {
Query query = parser.parse(q);
// TODO(fishywang): Currently as we don't have much documentation, we just use MAX_VALUE here
// and skipped paging. Maybe add paging later.
TopDocs results = searcher.search(query, Integer.MAX_VALUE);
ScoreDoc[] hits = results.scoreDocs;
int totalHits = results.totalHits;