Skip to content

Commit 1161ac1

Browse files
tulinkryVladimir Kotal
authored andcommitted
Short URL for all projects search implemented (#2717)
fixes #1357
1 parent 1961888 commit 1161ac1

File tree

6 files changed

+107
-17
lines changed

6 files changed

+107
-17
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/web/PageConfig.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public final class PageConfig {
106106
public static final String OPEN_GROK_PROJECT = "OpenGrokProject";
107107

108108
// query parameters
109+
protected static final String ALL_PROJECT_SEARCH = "searchall";
109110
protected static final String PROJECT_PARAM_NAME = "project";
110111
protected static final String GROUP_PARAM_NAME = "group";
111112

@@ -806,6 +807,8 @@ public String getRequestedProjectsAsString() {
806807
* <li>If there is no project in the configuration an empty set is returned. Otherwise:</li>
807808
* <li>If there is only one project in the configuration,
808809
* this one gets returned (no matter, what the request actually says). Otherwise</li>
810+
* <li>If the request parameter {@code ALL_PROJECT_SEARCH} contains a true value,
811+
* all projects are added to searching. Otherwise:</li>
809812
* <li>If the request parameter {@code PROJECT_PARAM_NAME} contains any available project,
810813
* the set with invalid projects removed gets returned. Otherwise:</li>
811814
* <li>If the request parameter {@code GROUP_PARAM_NAME} contains any available group,
@@ -819,14 +822,15 @@ public String getRequestedProjectsAsString() {
819822
* </ol>
820823
*
821824
* @return a possible empty set of project names but never {@code null}.
825+
* @see #ALL_PROJECT_SEARCH
822826
* @see #PROJECT_PARAM_NAME
823827
* @see #GROUP_PARAM_NAME
824828
* @see #OPEN_GROK_PROJECT
825829
*/
826830
public SortedSet<String> getRequestedProjects() {
827831
if (requestedProjects == null) {
828832
requestedProjects
829-
= getRequestedProjects(PROJECT_PARAM_NAME, GROUP_PARAM_NAME, OPEN_GROK_PROJECT);
833+
= getRequestedProjects(ALL_PROJECT_SEARCH, PROJECT_PARAM_NAME, GROUP_PARAM_NAME, OPEN_GROK_PROJECT);
830834
}
831835
return requestedProjects;
832836
}
@@ -892,13 +896,15 @@ private List<String> getParamVals(String paramName) {
892896
* Same as {@link #getRequestedProjects()}, but with a variable cookieName
893897
* and parameter name.
894898
*
895-
* @param projectParamName the name of the request parameter corresponding to a project name.
896-
* @param groupParamName the name of the request parameter corresponding to a group name
897-
* @param cookieName name of the cookie which possible contains project
898-
* names used as fallback
899+
* @param searchAllParamName the name of the request parameter corresponding to search all projects.
900+
* @param projectParamName the name of the request parameter corresponding to a project name.
901+
* @param groupParamName the name of the request parameter corresponding to a group name
902+
* @param cookieName name of the cookie which possible contains project
903+
* names used as fallback
899904
* @return set of project names. Possibly empty set but never {@code null}.
900905
*/
901906
protected SortedSet<String> getRequestedProjects(
907+
String searchAllParamName,
902908
String projectParamName,
903909
String groupParamName,
904910
String cookieName
@@ -911,6 +917,14 @@ protected SortedSet<String> getRequestedProjects(
911917
return projectNames;
912918
}
913919

920+
if (Boolean.parseBoolean(req.getParameter(searchAllParamName))) {
921+
return getProjectHelper()
922+
.getAllProjects()
923+
.stream()
924+
.map(Project::getName)
925+
.collect(Collectors.toCollection(TreeSet::new));
926+
}
927+
914928
// Use a project determined directly from the URL
915929
if (getProject() != null && getProject().isIndexed()) {
916930
projectNames.add(getProject().getName());

opengrok-indexer/src/main/java/org/opengrok/indexer/web/Scripts.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ public String toHtml() {
104104
SCRIPTS.put("jquery-ui", new FileScript("js/jquery-ui-1.12.1-custom.min.js", 11));
105105
SCRIPTS.put("jquery-tablesorter", new FileScript("js/jquery-tablesorter-2.26.6.min.js", 12));
106106
SCRIPTS.put("tablesorter-parsers", new FileScript("js/tablesorter-parsers-0.0.1.js", 13));
107-
SCRIPTS.put("searchable-option-list", new FileScript("js/searchable-option-list-2.0.6.js", 14));
108-
SCRIPTS.put("utils", new FileScript("js/utils-0.0.30.js", 15));
107+
SCRIPTS.put("searchable-option-list", new FileScript("js/searchable-option-list-2.0.7.min.js", 14));
108+
SCRIPTS.put("utils", new FileScript("js/utils-0.0.31.js", 15));
109109
SCRIPTS.put("repos", new FileScript("js/repos-0.0.1.js", 20));
110110
SCRIPTS.put("diff", new FileScript("js/diff-0.0.3.js", 20));
111111
SCRIPTS.put("jquery-caret", new FileScript("js/jquery.caret-1.5.2.min.js", 25));

opengrok-indexer/src/test/java/org/opengrok/indexer/web/PageConfigRequestedProjectsTest.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public void testNonIndexedInGroup() {
180180
*/
181181
@Test
182182
public void testNonExistentProject() {
183-
final HttpServletRequest request = createRequest(null, new String[]{"no-project"});
183+
final HttpServletRequest request = createRequest(new String[]{"no-project"}, null);
184184

185185
final PageConfig cfg = PageConfig.get(request);
186186
Assert.assertEquals(new HashSet<>(), cfg.getRequestedProjects());
@@ -197,6 +197,42 @@ public void testNonExistentGroup() {
197197
Assert.assertEquals(new HashSet<>(), cfg.getRequestedProjects());
198198
}
199199

200+
@Test
201+
public void testSelectAllProjects() {
202+
final HttpServletRequest request = createRequest(null, null);
203+
Mockito.when(request.getParameter(PageConfig.ALL_PROJECT_SEARCH)).thenReturn("true");
204+
205+
final PageConfig cfg = PageConfig.get(request);
206+
Assert.assertEquals(new TreeSet<>(env.getProjectNames()), cfg.getRequestedProjects());
207+
}
208+
209+
@Test
210+
public void testSelectAllProjectsOverrideProjectParam() {
211+
final HttpServletRequest request = createRequest(new String[]{"project-1", "project-2"}, null);
212+
Mockito.when(request.getParameter(PageConfig.ALL_PROJECT_SEARCH)).thenReturn("true");
213+
214+
final PageConfig cfg = PageConfig.get(request);
215+
Assert.assertEquals(new TreeSet<>(env.getProjectNames()), cfg.getRequestedProjects());
216+
}
217+
218+
@Test
219+
public void testSelectAllProjectsOverrideGroupParam() {
220+
final HttpServletRequest request = createRequest(null, new String[]{"group-1-2-3"});
221+
Mockito.when(request.getParameter(PageConfig.ALL_PROJECT_SEARCH)).thenReturn("true");
222+
223+
final PageConfig cfg = PageConfig.get(request);
224+
Assert.assertEquals(new TreeSet<>(env.getProjectNames()), cfg.getRequestedProjects());
225+
}
226+
227+
@Test
228+
public void testSelectAllOverrideNonExistentProject() {
229+
final HttpServletRequest request = createRequest(new String[]{"no-project"}, null);
230+
Mockito.when(request.getParameter(PageConfig.ALL_PROJECT_SEARCH)).thenReturn("true");
231+
232+
final PageConfig cfg = PageConfig.get(request);
233+
Assert.assertEquals(new TreeSet<>(env.getProjectNames()), cfg.getRequestedProjects());
234+
}
235+
200236
/**
201237
* Create a request with the specified path elements.
202238
*

opengrok-web/src/main/webapp/js/searchable-option-list-2.0.6.js renamed to opengrok-web/src/main/webapp/js/searchable-option-list-2.0.7.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,15 +1192,15 @@
11921192
}
11931193
},
11941194
/*
1195-
* Modified 2016
1195+
* Modified 2016, 2019
11961196
*/
1197-
invert: function() {
1197+
invert: function () {
11981198
if (this.config.multiple) {
11991199
var $closedInputs = this.$selectionContainer
1200-
.find('input[type="checkbox"]:not([disabled], :checked)')
1200+
.find('input[type="checkbox"][name=project]:not([disabled], :checked)')
12011201
var $openedInputs = this.$selectionContainer
1202-
.find('input[type="checkbox"]').filter('[disabled], :checked')
1203-
1202+
.find('input[type="checkbox"][name=project]').filter('[disabled], :checked')
1203+
12041204
$openedInputs.prop('checked', false)
12051205
.trigger('change', true);
12061206
$closedInputs.prop('checked', true)

opengrok-web/src/main/webapp/js/searchable-option-list-2.0.6.min.js renamed to opengrok-web/src/main/webapp/js/searchable-option-list-2.0.7.min.js

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

opengrok-web/src/main/webapp/js/utils-0.0.30.js renamed to opengrok-web/src/main/webapp/js/utils-0.0.31.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,11 +2273,49 @@ function isOnSearchPage() {
22732273
return $(document.documentElement).hasClass('search');
22742274
}
22752275

2276-
function transform_projects_to_groups() {
2276+
/**
2277+
* Preprocess the searched projects in the form with:
2278+
*
2279+
* <ol>
2280+
* <li>For all project search -> replace the projects with simple searchall parameter</li>
2281+
* <li>For group search -> replace all projects in a group by the group parameter</li>
2282+
* </ol>
2283+
* @param form the form containing the checkboxes
2284+
*/
2285+
function preprocess_searched_projects(form) {
22772286
var sol = $('#project').searchableOptionList();
22782287

22792288
var $sel = sol.$selectionContainer;
22802289

2290+
/*
2291+
* For all project search check if all project checkbox are checked and then uncheck them (they
2292+
* would appear in the url) and add a hidden checkbox with searchall name.
2293+
*/
2294+
var allProjectsSearch = $.makeArray($sel.find('.sol-checkbox[name=project]')).every(function (checkbox) {
2295+
return $(checkbox).is(':checked')
2296+
});
2297+
2298+
if (allProjectsSearch && $('#search_all_projects').length === 0) {
2299+
$sel.find('.sol-checkbox').prop('checked', false);
2300+
var $input = $('<input>')
2301+
var $all = $input
2302+
.attr({
2303+
id: 'search_all_projects',
2304+
type: 'checkbox',
2305+
value: true,
2306+
name: 'searchall',
2307+
})
2308+
.prop('checked', true)
2309+
.css('display', 'none')
2310+
$all.appendTo($(form));
2311+
return;
2312+
}
2313+
2314+
/*
2315+
* For selecting groups instead of projects, ommit the "Other" group. Loop over
2316+
* all project checkbox in a group and when all of them are checked, uncheck them (they
2317+
* would appear in the URL) and then check the group checkbox.
2318+
*/
22812319
$sel.find('.sol-optiongroup').each(function () {
22822320
var $el = $(this);
22832321

@@ -2324,8 +2362,9 @@ function searchSubmit(form) {
23242362
form.appendChild(input);
23252363
}
23262364

2365+
// replace all projects search with searchall parameter
23272366
// select groups instead of projects if all projects in one group are selected
2328-
transform_projects_to_groups();
2367+
preprocess_searched_projects(form);
23292368
}
23302369

23312370
/**

0 commit comments

Comments
 (0)