1 | /* |
---|
2 | * Copyright (C) 2014 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.vlo.wicket.panels.search; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.JavaScriptResources; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
21 | import eu.clarin.cmdi.vlo.service.solr.AutoCompleteService; |
---|
22 | import java.util.Iterator; |
---|
23 | import org.apache.wicket.Component; |
---|
24 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
25 | import org.apache.wicket.ajax.attributes.AjaxCallListener; |
---|
26 | import org.apache.wicket.ajax.attributes.AjaxRequestAttributes; |
---|
27 | import org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton; |
---|
28 | import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField; |
---|
29 | import org.apache.wicket.markup.head.CssHeaderItem; |
---|
30 | import org.apache.wicket.markup.head.IHeaderResponse; |
---|
31 | import org.apache.wicket.markup.head.JavaScriptHeaderItem; |
---|
32 | import org.apache.wicket.markup.html.form.Form; |
---|
33 | import org.apache.wicket.markup.html.panel.GenericPanel; |
---|
34 | import org.apache.wicket.model.IModel; |
---|
35 | import org.apache.wicket.model.PropertyModel; |
---|
36 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
37 | |
---|
38 | /** |
---|
39 | * Form for textual query on the faceted search page |
---|
40 | * |
---|
41 | * @author twagoo |
---|
42 | */ |
---|
43 | public abstract class SearchFormPanel extends GenericPanel<QueryFacetsSelection> { |
---|
44 | |
---|
45 | @SpringBean |
---|
46 | private AutoCompleteService autoCompleteDao; |
---|
47 | |
---|
48 | //private final AjaxIndicatorAppender indicatorAppender = new AjaxIndicatorAppender(); |
---|
49 | public SearchFormPanel(String id, IModel<QueryFacetsSelection> model) { |
---|
50 | super(id, model); |
---|
51 | |
---|
52 | final Form<QueryFacetsSelection> form = new Form<>("search", model); |
---|
53 | |
---|
54 | // Bind search field to 'query' property of model |
---|
55 | form.add(new AutoCompleteTextField("query", new PropertyModel<String>(model, "query")) { |
---|
56 | |
---|
57 | @Override |
---|
58 | protected Iterator<String> getChoices(String input) { |
---|
59 | return autoCompleteDao.getChoices(input); |
---|
60 | } |
---|
61 | |
---|
62 | }); |
---|
63 | |
---|
64 | // Button allows partial updates but can fall back to a full (non-JS) refresh |
---|
65 | form.add(new AjaxFallbackButton("searchSubmit", form) { |
---|
66 | |
---|
67 | @Override |
---|
68 | protected void onSubmit(AjaxRequestTarget target, Form<?> form) { |
---|
69 | SearchFormPanel.this.onSubmit(target); |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { |
---|
74 | super.updateAjaxAttributes(attributes); |
---|
75 | |
---|
76 | // listener to start/stop indicating progress |
---|
77 | AjaxCallListener listener = new AjaxCallListener() { |
---|
78 | |
---|
79 | @Override |
---|
80 | public CharSequence getBeforeHandler(Component component) { |
---|
81 | return ("startSearch();"); |
---|
82 | } |
---|
83 | |
---|
84 | @Override |
---|
85 | public CharSequence getCompleteHandler(Component component) { |
---|
86 | return ("endSearch();"); |
---|
87 | } |
---|
88 | |
---|
89 | }; |
---|
90 | attributes.getAjaxCallListeners().add(listener); |
---|
91 | } |
---|
92 | |
---|
93 | } |
---|
94 | ); |
---|
95 | |
---|
96 | add(form); |
---|
97 | } |
---|
98 | |
---|
99 | protected abstract void onSubmit(AjaxRequestTarget target); |
---|
100 | |
---|
101 | @Override |
---|
102 | public void renderHead(IHeaderResponse response) { |
---|
103 | // include script that activates the help button tooltip and JQuery UI on which it depends |
---|
104 | response.render(CssHeaderItem.forReference(JavaScriptResources.getJQueryUICSS())); |
---|
105 | response.render(JavaScriptHeaderItem.forReference(JavaScriptResources.getJQueryUIJS())); |
---|
106 | response.render(JavaScriptHeaderItem.forReference(JavaScriptResources.getSyntaxHelpJS())); |
---|
107 | response.render(JavaScriptHeaderItem.forReference(JavaScriptResources.getSearchFormJS())); |
---|
108 | } |
---|
109 | } |
---|