/* This file is part of ArciveList. # SPDX-FileCopyrightText: 2021 Hangar.org # SPDX-License-Identifier: AGPL-3.0-or-later */ import Vue from 'vue' import Vuex from 'vuex' import _ from 'underscore'; import axios from 'axios'; Vue.use(Vuex) const store = new Vuex.Store({ state: { search_text: "", sites: [], filtered_sites: [], page_count: 1, active_column: null, column_order: null }, mutations: { setSearchText(state, text) { state.search_text = text }, incrementPageCount(state) { state.page_count = state.page_count +1 }, setFilteredSites(state, filtered_site_ids) { //console.log(filtered_site_ids) var sites = [] for (const [site_id, weight] of Object.entries(filtered_site_ids)) { var site = state.sites.find(x => x.id == site_id) if (site) { sites.push(site) } } state.filtered_sites = sites state.page_count = 1 }, setColumnOrder(state, payload) { state.active_column = payload.active_column state.column_order = payload.column_order state.sites = order_items(state.sites, payload.active_column, payload.column_order) state.filtered_sites = order_items(state.filtered_sites, payload.active_column, payload.column_order) }, }, actions: { setSites(context, payload) { context.state.sites = payload.sites context.commit("setColumnOrder", {'active_column': payload.active_column, 'column_order': payload.column_order}) }, siteSearch (context) { console.log('new site search') var api_endpoint = '/api/collections/search?fulltext_search=' axios.post(api_endpoint + context.getters.getSearchText, {}) .then(function (response) { var local_search_result = context.getters.searchLocalSiteData var merged_results = Object.assign({}, local_search_result, response.data) context.commit("setFilteredSites", merged_results) context.commit("setColumnOrder", {'active_column': context.state.active_column, 'column_order': context.state.column_order}) }) .catch(function (error) { console.log(error); }); }, }, getters: { getSearchText: state => { return state.search_text }, getSitesForDisplay: state => { if (state.search_text === "") { return state.sites } return state.filtered_sites }, getPageCount: state => { return state.page_count }, searchLocalSiteData: state => { var result = {} if (state.search_text==="") { return result } for (let site in state.sites) { var weight = 0 var found = state.sites[site].summary.toLowerCase().search(state.search_text.toLowerCase()) if (found != -1) { weight = weight +1 } var found = state.sites[site].collection.toLowerCase().search(state.search_text.toLowerCase()) if (found != -1) { weight = weight +1 } var found = state.sites[site].currator.toLowerCase().search(state.search_text.toLowerCase()) if (found != -1) { weight = weight +1 } var found = state.sites[site].title.toLowerCase().search(state.search_text.toLowerCase()) if (found != -1) { weight = weight +1 } if (weight > 0) { var id = state.sites[site].id result[id] = weight } } return result }, } }) function order_items(items, field_name, ascending) { items.sort(function compareFn(first_el, second_el) { if (field_name == 'artist') { field_name = 'currator' } if (field_name == 'date') { field_name = 'project_date' } if (first_el[field_name] === undefined || first_el[field_name] === ""){ return -1 } if (second_el[field_name] === undefined || second_el[field_name] === ""){ return 1 } first_el = first_el[field_name].toLowerCase() second_el = second_el[field_name].toLowerCase() if (first_el < second_el) { return -1; } return 1; }) return ascending == 'asc' ? items : items.reverse() } export default store