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.FacetConstants; |
---|
20 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
21 | import eu.clarin.cmdi.vlo.pojo.ExpansionState; |
---|
22 | import eu.clarin.cmdi.vlo.pojo.SearchContext; |
---|
23 | import eu.clarin.cmdi.vlo.wicket.HighlightSearchTermScriptFactory; |
---|
24 | import eu.clarin.cmdi.vlo.wicket.components.RecordPageLink; |
---|
25 | import eu.clarin.cmdi.vlo.wicket.components.SolrFieldLabel; |
---|
26 | import eu.clarin.cmdi.vlo.wicket.model.SolrFieldStringModel; |
---|
27 | import org.apache.solr.common.SolrDocument; |
---|
28 | import org.apache.wicket.MarkupContainer; |
---|
29 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
30 | import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxFallbackLink; |
---|
31 | import org.apache.wicket.markup.html.WebMarkupContainer; |
---|
32 | import org.apache.wicket.markup.html.basic.Label; |
---|
33 | import org.apache.wicket.markup.html.link.Link; |
---|
34 | import org.apache.wicket.markup.html.panel.Panel; |
---|
35 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
36 | import org.apache.wicket.model.IModel; |
---|
37 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
38 | import org.apache.wicket.util.string.Strings; |
---|
39 | |
---|
40 | /** |
---|
41 | * |
---|
42 | * @author twagoo |
---|
43 | */ |
---|
44 | public class SearchResultItemPanel extends Panel { |
---|
45 | |
---|
46 | @SpringBean |
---|
47 | private VloConfig config; |
---|
48 | |
---|
49 | private final Panel collapsedDetails; |
---|
50 | private final Panel expandedDetails; |
---|
51 | private final IModel<ExpansionState> expansionStateModel; |
---|
52 | private final IModel<SearchContext> selectionModel; |
---|
53 | |
---|
54 | /** |
---|
55 | * |
---|
56 | * @param id markup id of the panel |
---|
57 | * @param documentModel model of document that this search item represents |
---|
58 | * @param selectionModel model of current selection (will be passed on to |
---|
59 | * record page when link is clicked) |
---|
60 | * @param expansionStateModel model for the expansion state of this search |
---|
61 | * item |
---|
62 | */ |
---|
63 | public SearchResultItemPanel(String id, IModel<SolrDocument> documentModel, IModel<SearchContext> selectionModel, IModel<ExpansionState> expansionStateModel) { |
---|
64 | super(id, documentModel); |
---|
65 | this.expansionStateModel = expansionStateModel; |
---|
66 | this.selectionModel = selectionModel; |
---|
67 | |
---|
68 | final Link recordLink = new RecordPageLink("recordLink", documentModel, selectionModel); |
---|
69 | recordLink.add(new SolrFieldLabel("title", documentModel, FacetConstants.FIELD_NAME, "Unnamed record")); |
---|
70 | add(recordLink); |
---|
71 | |
---|
72 | // add a link to toggle the expansion state |
---|
73 | add(createExpansionStateToggle("expansionStateToggle")); |
---|
74 | |
---|
75 | // add a collapsed details panel; only shown when expansion state is collapsed (through onConfigure) |
---|
76 | collapsedDetails = new SearchResultItemCollapsedPanel("collapsedDetials", documentModel, selectionModel); |
---|
77 | add(collapsedDetails); |
---|
78 | |
---|
79 | // add a collapsed details panel; only shown when expansion state is expanded (through onConfigure) |
---|
80 | expandedDetails = new SearchResultItemExpandedPanel("expandedDetails", documentModel, selectionModel); |
---|
81 | add(expandedDetails); |
---|
82 | |
---|
83 | final MarkupContainer scoreContainer = new WebMarkupContainer("scoreContainer"); |
---|
84 | scoreContainer.add(new Label("score", new SolrFieldStringModel(documentModel, FacetConstants.FIELD_SOLR_SCORE))); |
---|
85 | scoreContainer.setVisible(config.isShowResultScores()); |
---|
86 | add(scoreContainer); |
---|
87 | |
---|
88 | setOutputMarkupId(true); |
---|
89 | } |
---|
90 | |
---|
91 | private Link createExpansionStateToggle(String id) { |
---|
92 | final Link expansionStateToggle = new IndicatingAjaxFallbackLink(id) { |
---|
93 | |
---|
94 | @Override |
---|
95 | public void onClick(AjaxRequestTarget target) { |
---|
96 | // toggle the expansion state |
---|
97 | if (expansionStateModel.getObject() == ExpansionState.COLLAPSED) { |
---|
98 | expansionStateModel.setObject(ExpansionState.EXPANDED); |
---|
99 | } else { |
---|
100 | expansionStateModel.setObject(ExpansionState.COLLAPSED); |
---|
101 | } |
---|
102 | |
---|
103 | if (target != null) { |
---|
104 | // parial update (just this search result item) |
---|
105 | target.add(SearchResultItemPanel.this); |
---|
106 | |
---|
107 | // in case of a query, update highlight matching search terms after collapse/expand |
---|
108 | final String query = selectionModel.getObject().getSelection().getQuery(); |
---|
109 | if (!Strings.isEmpty(query)) { |
---|
110 | final HighlightSearchTermScriptFactory scriptFactory = new HighlightSearchTermScriptFactory(); |
---|
111 | final String selector = "#" + SearchResultItemPanel.this.getMarkupId(); |
---|
112 | target.appendJavaScript(scriptFactory.createScript(selector, query)); |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | }; |
---|
117 | expansionStateToggle.add(new Label("state", new AbstractReadOnlyModel<String>() { |
---|
118 | |
---|
119 | @Override |
---|
120 | public String getObject() { |
---|
121 | if (expansionStateModel.getObject() == ExpansionState.COLLAPSED) { |
---|
122 | return "Expand"; |
---|
123 | } else { |
---|
124 | return "Collapse"; |
---|
125 | } |
---|
126 | } |
---|
127 | })); |
---|
128 | return expansionStateToggle; |
---|
129 | } |
---|
130 | |
---|
131 | @Override |
---|
132 | protected void onConfigure() { |
---|
133 | super.onConfigure(); |
---|
134 | // this is called once per request; set visibility state for detail panels |
---|
135 | // according to expansion state |
---|
136 | collapsedDetails.setVisible(expansionStateModel.getObject() == ExpansionState.COLLAPSED); |
---|
137 | expandedDetails.setVisible(expansionStateModel.getObject() == ExpansionState.EXPANDED); |
---|
138 | } |
---|
139 | |
---|
140 | @Override |
---|
141 | public void detachModels() { |
---|
142 | super.detachModels(); |
---|
143 | expansionStateModel.detach(); |
---|
144 | } |
---|
145 | } |
---|