|
66 | 66 | </div> |
67 | 67 | </div> |
68 | 68 | </template> |
69 | | -<script> |
70 | | -import { mapStores, mapWritableState } from "pinia"; |
| 69 | +<script setup> |
| 70 | +import { computed, inject, provide, ref, watch } from "vue"; |
| 71 | +import { useRoute } from "vue-router"; |
| 72 | +import { storeToRefs } from "pinia"; |
| 73 | +import { useI18n } from "vue-i18n"; |
71 | 74 | import { useMainStore } from "../stores/main"; |
72 | 75 | import { debug, deepEqual, paramsFilter, paramsToRoute } from "../utils.js"; |
73 | 76 | import citations from "./Citations"; |
74 | 77 | import ResultsSummary from "./ResultsSummary"; |
75 | 78 |
|
76 | | -export default { |
77 | | - name: "aggregation-report", |
78 | | - components: { citations, ResultsSummary }, |
79 | | - inject: ["$http"], |
80 | | - provide() { |
81 | | - return { |
82 | | - results: this.aggregationResults, |
83 | | - }; |
84 | | - }, |
85 | | - computed: { |
86 | | - ...mapWritableState(useMainStore, [ |
87 | | - "formData", |
88 | | - "resultsLength", |
89 | | - "aggregationCache", |
90 | | - "searching", |
91 | | - "currentReport", |
92 | | - "urlUpdate" |
93 | | - ]), |
94 | | - ...mapStores(useMainStore), |
95 | | - statsConfig() { |
96 | | - for (let fieldObject of this.$philoConfig.aggregation_config) { |
97 | | - if (fieldObject.field == this.$route.query.group_by) { |
98 | | - return fieldObject; |
99 | | - } |
100 | | - } |
101 | | - return null; |
102 | | - }, |
103 | | - }, |
104 | | - data() { |
105 | | - return { |
106 | | - loading: false, |
107 | | - aggregationResults: [], |
108 | | - lastResult: 50, |
109 | | - infiniteId: 0, |
110 | | - groupedByField: this.$route.query.group_by, |
111 | | - breakUpFields: [], |
112 | | - breakUpFieldName: "", |
113 | | - }; |
114 | | - }, |
115 | | - created() { |
116 | | - this.formData.report = "aggregation"; |
117 | | - this.currentReport = "aggregation"; |
118 | | - this.fetchResults(); |
119 | | - }, |
120 | | - watch: { |
121 | | - urlUpdate() { |
122 | | - if (this.formData.report == "aggregation") { |
123 | | - this.groupedByField = this.$route.query.group_by; |
124 | | - this.fetchResults(); |
125 | | - } |
126 | | - }, |
127 | | - }, |
128 | | - methods: { |
129 | | - fetchResults() { |
130 | | - if ( |
131 | | - deepEqual( |
132 | | - { ...this.aggregationCache.query, start: "", end: "" }, |
133 | | - { ...this.$route.query, start: "", end: "" } |
134 | | - ) |
135 | | - ) { |
136 | | - this.aggregationResults = this.aggregationCache.results; |
137 | | - this.breakUpFields = this.aggregationResults.map((results) => ({ |
138 | | - show: false, |
139 | | - results: results.break_up_field, |
140 | | - })); |
141 | | - this.resultsLength = this.aggregationCache.totalResults; |
| 79 | +const $http = inject("$http"); |
| 80 | +const $dbUrl = inject("$dbUrl"); |
| 81 | +const philoConfig = inject("$philoConfig"); |
| 82 | +const route = useRoute(); |
| 83 | +const { t } = useI18n(); |
| 84 | +const store = useMainStore(); |
| 85 | +const { |
| 86 | + formData, |
| 87 | + resultsLength, |
| 88 | + aggregationCache, |
| 89 | + searching, |
| 90 | + currentReport, |
| 91 | + urlUpdate, |
| 92 | +} = storeToRefs(store); |
| 93 | +
|
| 94 | +const aggregationResults = ref([]); |
| 95 | +const lastResult = ref(50); |
| 96 | +const infiniteId = ref(0); |
| 97 | +const groupedByField = ref(route.query.group_by); |
| 98 | +const breakUpFields = ref([]); |
| 99 | +const breakUpFieldName = ref(""); |
| 100 | +
|
| 101 | +const statsConfig = computed(() => { |
| 102 | + for (const fieldObject of philoConfig.aggregation_config) { |
| 103 | + if (fieldObject.field === route.query.group_by) return fieldObject; |
| 104 | + } |
| 105 | + return null; |
| 106 | +}); |
| 107 | +
|
| 108 | +provide("results", aggregationResults); |
| 109 | +
|
| 110 | +function fetchResults() { |
| 111 | + if ( |
| 112 | + deepEqual( |
| 113 | + { ...aggregationCache.value.query, start: "", end: "" }, |
| 114 | + { ...route.query, start: "", end: "" } |
| 115 | + ) |
| 116 | + ) { |
| 117 | + aggregationResults.value = aggregationCache.value.results; |
| 118 | + breakUpFields.value = aggregationResults.value.map((r) => ({ |
| 119 | + show: false, |
| 120 | + results: r.break_up_field, |
| 121 | + })); |
| 122 | + resultsLength.value = aggregationCache.value.totalResults; |
| 123 | + return; |
| 124 | + } |
| 125 | +
|
| 126 | + searching.value = true; |
| 127 | + $http |
| 128 | + .get(`${$dbUrl}/reports/aggregation.py`, { |
| 129 | + params: paramsFilter({ ...formData.value }), |
| 130 | + }) |
| 131 | + .then((response) => { |
| 132 | + infiniteId.value += 1; |
| 133 | + aggregationResults.value = Object.freeze(buildStatResults(response.data.results)); |
| 134 | + lastResult.value = 50; |
| 135 | + breakUpFields.value = aggregationResults.value.map((r) => ({ |
| 136 | + show: false, |
| 137 | + results: r.break_up_field, |
| 138 | + limit: 1000, |
| 139 | + })); |
| 140 | + const aliasName = |
| 141 | + philoConfig.metadata_aliases[response.data.break_up_field] || |
| 142 | + response.data.break_up_field; |
| 143 | + breakUpFieldName.value = aliasName ? aliasName.toLowerCase() : ""; |
| 144 | + resultsLength.value = response.data.total_results; |
| 145 | + searching.value = false; |
| 146 | + }) |
| 147 | + .catch((error) => { |
| 148 | + searching.value = false; |
| 149 | + debug({ $options: { name: "aggregation-report" } }, error); |
| 150 | + }); |
| 151 | +} |
| 152 | +
|
| 153 | +function handleFullResultsScroll() { |
| 154 | + const scrollPosition = |
| 155 | + document.getElementById("aggregation-results").getBoundingClientRect().bottom - 200; |
| 156 | + if (scrollPosition < window.innerHeight) { |
| 157 | + lastResult.value += 50; |
| 158 | + } |
| 159 | +} |
| 160 | +
|
| 161 | +function buildStatResults(results) { |
| 162 | + return results.map((result) => { |
| 163 | + result.citation = buildCitationObject( |
| 164 | + groupedByField.value, |
| 165 | + statsConfig.value.field_citation, |
| 166 | + result.metadata_fields |
| 167 | + ); |
| 168 | + return result; |
| 169 | + }); |
| 170 | +} |
| 171 | +
|
| 172 | +function buildCitationObject(fieldToLink, citationObject, metadataFields) { |
| 173 | + const out = []; |
| 174 | + for (const citation of citationObject) { |
| 175 | + let label = metadataFields[citation.field]; |
| 176 | + if ((label == null || label.length === 0) && citation.field !== fieldToLink) { |
| 177 | + continue; |
| 178 | + } |
| 179 | + if (citation.field === fieldToLink) { |
| 180 | + const queryParams = { ...formData.value, start: "0", end: "25" }; |
| 181 | + if (label == null || label.length === 0) { |
| 182 | + // Should be NULL, but that's broken in the philo lib |
| 183 | + queryParams[fieldToLink] = ""; |
| 184 | + label = t("common.na"); |
142 | 185 | } else { |
143 | | - this.searching = true; |
144 | | - this.$http |
145 | | - .get(`${this.$dbUrl}/reports/aggregation.py`, { |
146 | | - params: paramsFilter({ |
147 | | - ...this.formData, |
148 | | - }), |
149 | | - }) |
150 | | - .then((response) => { |
151 | | - this.infiniteId += 1; |
152 | | - this.aggregationResults = Object.freeze(this.buildStatResults(response.data.results)); |
153 | | - this.lastResult = 50; |
154 | | - this.breakUpFields = this.aggregationResults.map((results) => ({ |
155 | | - show: false, |
156 | | - results: results.break_up_field, |
157 | | - limit: 1000, |
158 | | - })); |
159 | | - this.breakUpFieldName = |
160 | | - this.$philoConfig.metadata_aliases[response.data.break_up_field] || |
161 | | - response.data.break_up_field; |
162 | | - if (typeof this.breakUpFieldName != "undefined" || this.breakUpFieldName != null) { |
163 | | - this.breakUpFieldName = this.breakUpFieldName.toLowerCase(); |
164 | | - } |
165 | | - this.resultsLength = response.data.total_results; |
166 | | - this.searching = false; |
167 | | - }) |
168 | | - .catch((error) => { |
169 | | - this.searching = false; |
170 | | - debug(this, error); |
171 | | - }); |
172 | | - } |
173 | | - }, |
174 | | - handleFullResultsScroll() { |
175 | | - let scrollPosition = document.getElementById("aggregation-results").getBoundingClientRect().bottom - 200; |
176 | | - if (scrollPosition < window.innerHeight) { |
177 | | - this.lastResult += 50; |
178 | | - } |
179 | | - }, |
180 | | - buildStatResults(results) { |
181 | | - let resultsWithCiteObject = []; |
182 | | - for (let result of results) { |
183 | | - result.citation = this.buildCitationObject( |
184 | | - this.groupedByField, |
185 | | - this.statsConfig.field_citation, |
186 | | - result.metadata_fields |
187 | | - ); |
188 | | - resultsWithCiteObject.push(result); |
| 186 | + queryParams[fieldToLink] = `"${label}"`; |
189 | 187 | } |
190 | | - return resultsWithCiteObject; |
191 | | - }, |
192 | | - buildCitationObject(fieldToLink, citationObject, metadataFields) { |
193 | | - let citations = []; |
194 | | - for (let citation of citationObject) { |
195 | | - let label = metadataFields[citation.field]; |
196 | | - if (label == null || label.length == 0) { |
197 | | - if (citation["field"] != fieldToLink) { |
198 | | - continue; |
199 | | - } |
200 | | - } |
201 | | - if (citation["field"] == fieldToLink) { |
202 | | - let queryParams = { |
203 | | - ...this.formData, |
204 | | - start: "0", |
205 | | - end: "25", |
206 | | - }; |
207 | | - if (label == null || label.length == 0) { |
208 | | - queryParams[fieldToLink] = ""; // Should be NULL, but that's broken in the philo lib |
209 | | - label = this.$t("common.na"); |
210 | | - } else { |
211 | | - queryParams[fieldToLink] = `"${label}"`; |
212 | | - } |
213 | | - if (fieldToLink != this.groupedByField) { |
214 | | - queryParams[this.groupedByField] = `"${metadataFields[this.groupedByField]}"`; |
215 | | - } |
216 | | - let link = ""; |
217 | | - // workaround for broken NULL searches |
218 | | - if (queryParams[fieldToLink].length) { |
219 | | - link = paramsToRoute({ |
220 | | - ...queryParams, |
221 | | - report: "concordance", |
222 | | - }); |
223 | | - citations.push({ ...citation, href: link, label: label }); |
224 | | - } else { |
225 | | - citations.push({ ...citation, href: "", label: label }); |
226 | | - } |
227 | | - } else { |
228 | | - citations.push({ ...citation, href: "", label: label }); |
229 | | - } |
| 188 | + if (fieldToLink !== groupedByField.value) { |
| 189 | + queryParams[groupedByField.value] = `"${metadataFields[groupedByField.value]}"`; |
230 | 190 | } |
231 | | - return citations; |
232 | | - }, |
233 | | - toggleBreakUp(resultIndex) { |
234 | | - this.breakUpFields[resultIndex].show = !this.breakUpFields[resultIndex].show; |
235 | | - }, |
236 | | - }, |
237 | | -}; |
| 191 | + // workaround for broken NULL searches |
| 192 | + const href = queryParams[fieldToLink].length |
| 193 | + ? paramsToRoute({ ...queryParams, report: "concordance" }) |
| 194 | + : ""; |
| 195 | + out.push({ ...citation, href, label }); |
| 196 | + } else { |
| 197 | + out.push({ ...citation, href: "", label }); |
| 198 | + } |
| 199 | + } |
| 200 | + return out; |
| 201 | +} |
| 202 | +
|
| 203 | +function toggleBreakUp(resultIndex) { |
| 204 | + breakUpFields.value[resultIndex].show = !breakUpFields.value[resultIndex].show; |
| 205 | +} |
| 206 | +
|
| 207 | +watch(urlUpdate, () => { |
| 208 | + if (formData.value.report === "aggregation") { |
| 209 | + groupedByField.value = route.query.group_by; |
| 210 | + fetchResults(); |
| 211 | + } |
| 212 | +}); |
| 213 | +
|
| 214 | +formData.value.report = "aggregation"; |
| 215 | +currentReport.value = "aggregation"; |
| 216 | +fetchResults(); |
238 | 217 | </script> |
239 | 218 | <style scoped lang="scss"> |
240 | 219 | @use "../assets/styles/theme.module.scss" as theme; |
|
0 commit comments