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