Skip to content

Commit 117eca0

Browse files
authored
Enable API search with checkbox on the UI. (#1444)
1 parent fce5b2c commit 117eca0

11 files changed

+96
-19
lines changed

app/lib/frontend/handlers.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,15 @@ SearchQuery _parseSearchQuery(shelf.Request request, String platform) {
372372
final SearchOrder sortOrder = (sortParam == null || sortParam.isEmpty)
373373
? null
374374
: parseSearchOrder(sortParam);
375-
375+
final isApiEnabled = request.requestedUri.queryParameters['api'] != '0';
376376
return new SearchQuery.parse(
377-
query: queryText,
378-
platform: platform,
379-
order: sortOrder,
380-
offset: offset,
381-
limit: PackageLinks.resultsPerPage);
377+
query: queryText,
378+
platform: platform,
379+
order: sortOrder,
380+
offset: offset,
381+
limit: PackageLinks.resultsPerPage,
382+
apiEnabled: isApiEnabled,
383+
);
382384
}
383385

384386
/// Handles requests for /packages/... - multiplexes to HTML/JSON handlers

app/lib/frontend/templates.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ class TemplateService {
723723
'search_query_placeholder': 'Search ${platformDict.name} packages',
724724
'search_sort_param': searchSort,
725725
'platform_tabs_html': platformTabs,
726+
'api_search_enabled': searchQuery?.isApiEnabled ?? true,
726727
'landing_blurb_html': platformDict.landingBlurb,
727728
// This is not escaped as it is already escaped by the caller.
728729
'content_html': contentHtml,

app/lib/search/index_simple.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@ class SimplePackageIndex implements PackageIndex {
184184
}
185185

186186
// do text matching
187-
final textResults = _searchText(packages, query.parsedQuery.text,
188-
_apiSearchEnabled || query.parsedQuery.isApiEnabled);
187+
final isApiEnabled = query.isApiEnabled ||
188+
_apiSearchEnabled ||
189+
query.parsedQuery.isApiEnabled;
190+
final textResults =
191+
_searchText(packages, query.parsedQuery.text, isApiEnabled);
189192

190193
// filter packages that doesn't match text query
191194
if (textResults != null) {

app/lib/shared/search_service.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ class SearchQuery {
207207
final int offset;
208208
final int limit;
209209
final bool isAd;
210+
final bool isApiEnabled;
210211

211212
SearchQuery._({
212213
this.query,
@@ -215,6 +216,7 @@ class SearchQuery {
215216
this.offset,
216217
this.limit,
217218
this.isAd,
219+
this.isApiEnabled,
218220
}) : parsedQuery = new ParsedQuery._parse(query),
219221
platform = (platform == null || platform.isEmpty) ? null : platform;
220222

@@ -225,6 +227,7 @@ class SearchQuery {
225227
int offset: 0,
226228
int limit: 10,
227229
bool isAd,
230+
bool apiEnabled,
228231
}) {
229232
final String q =
230233
query != null && query.trim().isNotEmpty ? query.trim() : null;
@@ -235,6 +238,7 @@ class SearchQuery {
235238
offset: offset,
236239
limit: limit,
237240
isAd: isAd ?? false,
241+
isApiEnabled: apiEnabled ?? true,
238242
);
239243
}
240244

@@ -252,6 +256,7 @@ class SearchQuery {
252256
limit = max(_minSearchLimit, limit);
253257

254258
final isAd = (uri.queryParameters['ad'] ?? '0') == '1';
259+
final apiEnabled = uri.queryParameters['api'] != '0';
255260

256261
return new SearchQuery.parse(
257262
query: q,
@@ -260,6 +265,7 @@ class SearchQuery {
260265
offset: offset,
261266
limit: limit,
262267
isAd: isAd,
268+
apiEnabled: apiEnabled,
263269
);
264270
}
265271

@@ -270,6 +276,7 @@ class SearchQuery {
270276
int offset,
271277
int limit,
272278
bool isAd,
279+
bool apiEnabled,
273280
}) {
274281
return new SearchQuery._(
275282
query: query ?? this.query,
@@ -278,6 +285,7 @@ class SearchQuery {
278285
offset: offset ?? this.offset,
279286
limit: limit ?? this.limit,
280287
isAd: isAd ?? this.isAd,
288+
isApiEnabled: apiEnabled ?? this.isApiEnabled,
281289
);
282290
}
283291

@@ -289,13 +297,9 @@ class SearchQuery {
289297
'limit': limit?.toString(),
290298
'order': serializeSearchOrder(order),
291299
'ad': isAd ? '1' : null,
300+
'api': isApiEnabled ? null : '0',
292301
};
293-
294-
for (var key in map.keys.toList()) {
295-
if (map[key] == null) {
296-
map.remove(key);
297-
}
298-
}
302+
map.removeWhere((k, v) => v == null);
299303
return map;
300304
}
301305

@@ -329,6 +333,9 @@ class SearchQuery {
329333
final String paramName = 'sort';
330334
params[paramName] = serializeSearchOrder(order);
331335
}
336+
if (!isApiEnabled) {
337+
params['api'] = '0';
338+
}
332339
if (page != null) {
333340
params['page'] = page.toString();
334341
}

app/test/frontend/golden/pkg_index_page.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,18 @@ <h1 class="_visuallyhidden">Dart pub</h1>
6767
<input class="input" name="q" placeholder="Search Dart packages" autocomplete="on" autofocus>
6868
<button class="icon"></button>
6969

70+
<input id="search-api-field" type="hidden" name="api" value="0" disabled="disabled" />
7071
</form>
71-
<div class="list-filters">
72+
<div class="search-bar-details">
73+
<div class="list-filters">
7274
<a class="filter " href="/flutter/packages">Flutter</a>
7375
<a class="filter " href="/web/packages">Web</a>
7476
<a class="filter -active" href="/packages">All</a>
7577
</div>
78+
<div class="search-bar-options">
79+
<input id="search-api-checkbox" type="checkbox" name="api" checked="checked" /> Include API results
80+
</div>
81+
</div>
7682
</main>
7783
</div>
7884

app/test/frontend/golden/search_page.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,18 @@ <h1 class="_visuallyhidden">Dart pub</h1>
6767
<input class="input" name="q" placeholder="Search Dart packages" autocomplete="on" autofocus value="foobar">
6868
<button class="icon"></button>
6969
<input type="hidden" name="sort" value="top"/>
70+
<input id="search-api-field" type="hidden" name="api" value="0" disabled="disabled" />
7071
</form>
71-
<div class="list-filters">
72+
<div class="search-bar-details">
73+
<div class="list-filters">
7274
<a class="filter " href="/flutter/packages?q=foobar&amp;sort=top">Flutter</a>
7375
<a class="filter " href="/web/packages?q=foobar&amp;sort=top">Web</a>
7476
<a class="filter -active" href="/packages?q=foobar&amp;sort=top">All</a>
7577
</div>
78+
<div class="search-bar-options">
79+
<input id="search-api-checkbox" type="checkbox" name="api" checked="checked" /> Include API results
80+
</div>
81+
</div>
7682
</main>
7783
</div>
7884

app/test/frontend/golden/search_supported_qualifier.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,18 @@ <h1 class="_visuallyhidden">Dart pub</h1>
6767
<input class="input" name="q" placeholder="Search Dart packages" autocomplete="on" autofocus value="email:[email protected]">
6868
<button class="icon"></button>
6969

70+
<input id="search-api-field" type="hidden" name="api" value="0" disabled="disabled" />
7071
</form>
71-
<div class="list-filters">
72+
<div class="search-bar-details">
73+
<div class="list-filters">
7274
<a class="filter " href="/flutter/packages?q=email%3Auser%40domain.com">Flutter</a>
7375
<a class="filter " href="/web/packages?q=email%3Auser%40domain.com">Web</a>
7476
<a class="filter -active" href="/packages?q=email%3Auser%40domain.com">All</a>
7577
</div>
78+
<div class="search-bar-options">
79+
<input id="search-api-checkbox" type="checkbox" name="api" checked="checked" /> Include API results
80+
</div>
81+
</div>
7682
</main>
7783
</div>
7884

app/test/frontend/golden/search_unsupported_qualifier.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,18 @@ <h1 class="_visuallyhidden">Dart pub</h1>
6767
<input class="input" name="q" placeholder="Search Dart packages" autocomplete="on" autofocus value="foo:bar">
6868
<button class="icon"></button>
6969

70+
<input id="search-api-field" type="hidden" name="api" value="0" disabled="disabled" />
7071
</form>
71-
<div class="list-filters">
72+
<div class="search-bar-details">
73+
<div class="list-filters">
7274
<a class="filter " href="/flutter/packages?q=foo%3Abar">Flutter</a>
7375
<a class="filter " href="/web/packages?q=foo%3Abar">Web</a>
7476
<a class="filter -active" href="/packages?q=foo%3Abar">All</a>
7577
</div>
78+
<div class="search-bar-options">
79+
<input id="search-api-checkbox" type="checkbox" name="api" checked="checked" /> Include API results
80+
</div>
81+
</div>
7682
</main>
7783
</div>
7884

app/views/layout.mustache

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,14 @@
9191
<input class="input" name="q" placeholder="{{search_query_placeholder}}" autocomplete="on" autofocus{{#search_query}} value="{{search_query}}"{{/search_query}}>
9292
<button class="icon"></button>
9393
{{#search_sort_param}}<input type="hidden" name="sort" value="{{search_sort_param}}"/>{{/search_sort_param}}
94+
<input id="search-api-field" type="hidden" name="api" value="0"{{#api_search_enabled}} disabled="disabled"{{/api_search_enabled}} />
9495
</form>
95-
{{& platform_tabs_html }}
96+
<div class="search-bar-details">
97+
{{& platform_tabs_html }}
98+
<div class="search-bar-options">
99+
<input id="search-api-checkbox" type="checkbox" name="api"{{#api_search_enabled}} checked="checked"{{/api_search_enabled}} /> Include API results
100+
</div>
101+
</div>
96102
</main>
97103
</div>
98104
{{/listing_banner}}

static/css/style.css

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/js/script.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void main() {
2020
_setEventForSearchInput();
2121
_fixIssueLinks();
2222
_setEventForSortControl();
23+
_setEventForCheckboxChanges();
2324
_updateDartdocStatus();
2425
}
2526

@@ -255,6 +256,22 @@ void _setEventForSortControl() {
255256
sortControl.append(select);
256257
}
257258

259+
void _setEventForCheckboxChanges() {
260+
final hiddenApiField =
261+
document.getElementById('search-api-field') as InputElement;
262+
final visibleApiCheckbox =
263+
document.getElementById('search-api-checkbox') as CheckboxInputElement;
264+
if (hiddenApiField == null || visibleApiCheckbox == null) {
265+
return;
266+
}
267+
final formElement = hiddenApiField.form;
268+
visibleApiCheckbox.onChange.listen((_) {
269+
hiddenApiField.disabled = visibleApiCheckbox.checked;
270+
// TODO: instead of submitting, compose the URL here (also removing the single `?`)
271+
formElement.submit();
272+
});
273+
}
274+
258275
void _fixIssueLinks() {
259276
for (AnchorElement bugLink in document.querySelectorAll('a.github_issue')) {
260277
var url = Uri.parse(bugLink.href);

0 commit comments

Comments
 (0)