From d7e25579862a79d8c27cec0b9809723109557e0f Mon Sep 17 00:00:00 2001 From: Robyn Thiessen-Bock Date: Thu, 30 Mar 2023 12:35:20 -0400 Subject: [PATCH] Add init options to CatalogSearchView Relates to #2069 --- .../models/connectors/Map-Search-Filters.js | 7 ++- src/js/views/search/CatalogSearchView.js | 47 ++++++++++++------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/js/models/connectors/Map-Search-Filters.js b/src/js/models/connectors/Map-Search-Filters.js index 9b6757ea7..fe0f3d42e 100644 --- a/src/js/models/connectors/Map-Search-Filters.js +++ b/src/js/models/connectors/Map-Search-Filters.js @@ -50,6 +50,7 @@ define([ /** * Initialize the model. + * @param {Object} attrs - The attributes for this model. * @param {Object} options - The options for this model. * @param {Map | Object} [options.map] - The Map model to use for this * connector or a JSON object with options to create a new Map model. If @@ -64,11 +65,11 @@ define([ * FilterGroup models. If a single FilterGroup is passed, it will be * wrapped in an array. If not provided, the default from the appModel * will be used. See {@link AppModel#defaultFilterGroups}. - * @param {boolean} [addGeohashLayer=true] - If set to true, a Geohash + * @param {boolean} [options.addGeohashLayer=true] - If set to true, a Geohash * layer will be added to the Map model if one is not already present. If * set to false, no Geohash layer will be added. A geohash layer is * required for the Search-Map connector to work. - * @param {boolean} [addSpatialFilter=true] - If set to true, a spatial + * @param {boolean} [options.addSpatialFilter=true] - If set to true, a spatial * filter will be added to the Filters model if one is not already * present. If set to false, no spatial filter will be added. A spatial * filter is required for the Filters-Map connector to work. @@ -80,7 +81,6 @@ define([ * results to un-obsoleted metadata. */ initialize: function (attrs, options = {}) { - // TODO: allow setting these with args here if (!options) options = {}; const app = MetacatUI.appModel; const map = options.map || app.get("catalogSearchMapOptions"); @@ -133,7 +133,6 @@ define([ setFilters: function (filtersArray, catalogSearch = true) { const filterGroups = []; const filters = new Filters(null, { catalogSearch: catalogSearch }); - // TODO: catalogSearch should be an option set in initialize filtersArray = Array.isArray(filtersArray) ? filtersArray diff --git a/src/js/views/search/CatalogSearchView.js b/src/js/views/search/CatalogSearchView.js index e3c32d4c1..57f88edd9 100644 --- a/src/js/views/search/CatalogSearchView.js +++ b/src/js/views/search/CatalogSearchView.js @@ -182,26 +182,41 @@ define([ }, /** - * Initializes the view - * @param {Object} options - * @param {string} options.initialQuery - The initial text query to run + * Initialize the view. In addition to the options described below, any + * option that is available in the + * {@link MapSearchFiltersConnector#initialize} method can be passed to + * this view, such as Map, SolrResult, and FilterGroup models, and whether + * to create a geohash layer or spatial filter if they are not present. + * @param {Object} options - The options for this view. + * @param {string} [options.initialQuery] - The initial text query to run * when the view is rendered. + * @param {MapSearchFiltersConnector} [options.model] - A + * MapSearchFiltersConnector model to use for this view. If not provided, + * a new one will be created. If one is provided, then other options that + * would be passed to the MapSearchFiltersConnector model will be ignored + * (such as map, searchResults, filterGroups, catalogSearch, etc.) * @since x.x.x */ initialize: function (options) { - this.initialQuery = options?.initialQuery; - // TODO: allow for initial models/filters to be passed in, as well as - // options like, whether or not to create a SpatialFilter or Geohash - // layer if not present, etc. - - // const mapOptions = Object.assign( - // {}, - // MetacatUI.appModel.get("catalogSearchMapOptions") || {} - // ); - // Create the Map model and view - - this.model = new MapSearchFiltersConnector(); - this.model.connect(); + if (!options) options = {}; + + this.initialQuery = options.initialQuery || null; + + let model = options.model; + if (!model) { + const app = MetacatUI.appModel; + model = new MapSearchFiltersConnector({ + map: options.map || app.get("catalogSearchMapOptions"), + searchResults: options.searchResults || null, + filterGroups: + options.filterGroups || app.get("defaultFilterGroups"), + catalogSearch: options.catalogSearch !== false, + addGeohashLayer: options.addGeohashLayer !== false, + addSpatialFilter: options.addSpatialFilter !== false, + }); + } + model.connect(); + this.model = model; }, /**