Commit 33b82402 authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Mejora parametrización de búsqueda y sugerencias

Revisa atributos disponibles para permitir una mayor personalización de
TextSearch a la hora de pedir datos o sugerencias, simplificando el paso
de valores y callbacks personalizados.
parent e1df9e4e
Loading
Loading
Loading
Loading
+8 −13
Original line number Diff line number Diff line
@@ -17,7 +17,8 @@ define([
		postMixInProperties: function() {

			const defaultConfig = {
				searchFields: null
				getRequestPathParams: null,
				getRequestQueryParams: null
			};

			this._mergeOwnAttributes(defaultConfig);
@@ -29,31 +30,25 @@ define([

			this.inherited(arguments);

			this._onEvt('SEARCH', obj => this._onSearchEvent(obj));
			this._onEvt('SEARCH', obj => this._onSearchEvent(obj.value));
		},

		_onSearchEvent: function(/*Object*/ searchObj) {
		_onSearchEvent: function(/*String*/ value) {

			this.inherited(arguments);

			this._emitEvt('REQUEST', {
				target: this._getTarget(),
				params: this._createSearchRequestParams(searchObj)
				params: this._createSearchRequestParams(value)
			});
		},

		_createSearchRequestParams: function(searchObj) {
		_createSearchRequestParams: function(value) {

			this.inherited(arguments);

			const path = {
				id: this.id
			};

			const query = {
				text: searchObj.value,
				searchFields: this.searchFields
			};
			const path = this.getRequestPathParams?.(value) ?? {};
			const query = this.getRequestQueryParams?.(value) ?? {text: value};

			return {path, query};
		},
+20 −21
Original line number Diff line number Diff line
@@ -25,11 +25,12 @@ define([
				suggestionItemClass: 'suggestion',
				suggestionHighlightedClass: 'highlighted',

				suggestionsTarget: null,
				suggestionsRequestMethod: 'GET',
				suggestionsTargetSuffix: 'suggestions',

				itemLabel: null,
				requestSuggestionsTimeout: 300
				requestSuggestionsTimeout: 300,
				getSuggestionItemLabel: null,
				getSuggestionsPathParams: null,
				getSuggestionsQueryParams: null
			};

			this._mergeOwnAttributes(defaultConfig);
@@ -130,23 +131,21 @@ define([

			this._emitEvt('REQUEST', {
				method: this.suggestionsRequestMethod,
				target: `${this._getTarget()}/${this.suggestionsTargetSuffix}`,
				target: this._getSuggestionsTarget(),
				requesterId: this.getOwnChannel(),
				params: this._createSuggestionsRequestParams(value)
			});
		},

		_createSuggestionsRequestParams: function(value) {
		_getSuggestionsTarget: function() {

			const path = {
				id: this.id
			};
			return this.suggestionsTarget ?? `${this._getTarget()}/suggestions`;
		},

			const query = {
				text: value,
				fields: this.suggestFields,
				highlightFields: this.highlightField
			};
		_createSuggestionsRequestParams: function(value) {

			const path = this.getSuggestionsPathParams?.(value) ?? {};
			const query = this.getSuggestionsQueryParams?.(value) ?? {text: value};

			return {path, query};
		},
@@ -158,7 +157,7 @@ define([

		_suggestionsTargetIsMine: function(target) {

			return `${this._getTarget()}/${this.suggestionsTargetSuffix}` === target;
			return this._getSuggestionsTarget() === target;
		},

		_dataAvailable: function(res, resWrapper) {
@@ -257,19 +256,19 @@ define([

		_getSuggestionLabel: function(item) {

			if (typeof this.itemLabel === 'function') {
				return this.itemLabel(item);
			if (typeof this.getSuggestionItemLabel === 'function') {
				return this.getSuggestionItemLabel(item);
			}

			if (typeof this.itemLabel !== 'string') {
			if (typeof this.getSuggestionItemLabel !== 'string') {
				return item;
			}

			if (this.itemLabel.includes('{')) {
				return lang.replace(this.itemLabel, item);
			if (this.getSuggestionItemLabel.includes('{')) {
				return lang.replace(this.getSuggestionItemLabel, item);
			}

			return item[this.itemLabel];
			return item[this.getSuggestionItemLabel];
		},

		_highlightSuggestion: function(suggestionNode) {
+1 −2
Original line number Diff line number Diff line
@@ -70,8 +70,7 @@ define([

			this.mergeComponentAttribute('searchConfig', {
				parentChannel,
				target,
				id: this.pathVariableId
				target
			});

			this.mergeComponentAttribute('browserConfig', {
+10 −4
Original line number Diff line number Diff line
@@ -75,6 +75,12 @@ define([
			}, {
				arrayMergingStrategy: 'overwrite'
			});

			const getPathParams = () => ({id: this.pathVariableId});
			this.mergeComponentAttribute('searchConfig', {
				getRequestPathParams: getPathParams,
				getSuggestionsPathParams: getPathParams
			});
		},

		_defineSubscriptions: function() {
@@ -91,7 +97,7 @@ define([

		_subMapLayerStationPopupLoaded: function(res) {

			var popupNode = res?._contentNode,
			const popupNode = res?._contentNode,
				popupData = res?._source?.feature?.properties;

			if (!popupNode || !popupData) {
@@ -99,7 +105,7 @@ define([
			}
			popupData.id = popupData.id ?? res?._source?.feature?.id;

			var showChartsNode = query('.' + this._showObservationsButtonClass, popupNode)[0];
			const showChartsNode = query('.' + this._showObservationsButtonClass, popupNode)[0];

			if (!showChartsNode) {
				return;