1+ const HAS_ANY_VALUE = '__anyvalue__' ;
2+ const HAS_NO_VALUE = '__novalue__' ;
3+
14let specialSearchInput ;
25
36/**
@@ -52,9 +55,9 @@ function onFacetsInput( event ) {
5255
5356 // TODO: Clean up the facet type detection logic after MVP or when we have more facet types
5457 if ( target . classList . contains ( 'wikibase-faceted-search__facet-item-checkbox' ) ) {
55- onListFacetInput ( facet , propertyId ) ;
56- } else if ( target . classList . contains ( 'wikibase-faceted-search__facet-toggle-button' ) ) {
5758 onListFacetInput ( facet , propertyId , target . value ) ;
59+ } else if ( target . classList . contains ( 'wikibase-faceted-search__facet-toggle-button' ) ) {
60+ onToggleInput ( facet , propertyId , target . value ) ;
5861 } else if ( target . classList . contains ( 'wikibase-faceted-search__facet-item-input' ) ) {
5962 onRangeFacetInput ( facet , propertyId ) ;
6063 }
@@ -81,9 +84,28 @@ function onInstancesClick( event, instanceId ) {
8184 *
8285 * @param {HTMLDivElement } facet
8386 * @param {string } propertyId
84- * @param {? string } mode
87+ * @param {string } value
8588 */
86- function onListFacetInput ( facet , propertyId , mode ) {
89+ function onListFacetInput ( facet , propertyId , value ) {
90+ let selectedValues = getListFacetSelectedValues ( facet ) ;
91+ if ( value === HAS_ANY_VALUE || value === HAS_NO_VALUE ) {
92+ selectedValues = selectedValues . indexOf ( value ) !== - 1 ? [ value ] : [ ] ;
93+ } else {
94+ selectedValues = selectedValues . filter ( ( v ) => v !== HAS_ANY_VALUE && v !== HAS_NO_VALUE ) ;
95+ }
96+
97+ const newQueries = getListFacetQuerySegments ( selectedValues , propertyId , getListFacetQueryMode ( facet ) ) ;
98+ submitSearchForm ( buildQueryString ( specialSearchInput . value , newQueries , propertyId ) ) ;
99+ }
100+
101+ /**
102+ * Handles the input event for the toggle button.
103+ *
104+ * @param {HTMLDivElement } facet
105+ * @param {string } propertyId
106+ * @param {string } mode
107+ */
108+ function onToggleInput ( facet , propertyId , mode ) {
87109 const selectedValues = getListFacetSelectedValues ( facet ) ;
88110 mode = mode || getListFacetQueryMode ( facet ) ;
89111 const newQueries = getListFacetQuerySegments ( selectedValues , propertyId , mode ) ;
@@ -192,6 +214,15 @@ function getListFacetSelectedValues( facet ) {
192214 * @return {string[] }
193215 */
194216function getListFacetQuerySegments ( selectedValues , propertyId , mode ) {
217+ if ( selectedValues . length === 1 ) {
218+ if ( selectedValues [ 0 ] === HAS_ANY_VALUE ) {
219+ return [ `haswbfacet:${ propertyId } ` ] ;
220+ }
221+ if ( selectedValues [ 0 ] === HAS_NO_VALUE ) {
222+ return [ `-haswbfacet:${ propertyId } ` ] ;
223+ }
224+ }
225+
195226 const segments = [ ] ;
196227 if ( mode === 'AND' ) {
197228 if ( selectedValues . length === 0 ) {
@@ -255,7 +286,7 @@ function buildQueryString( oldQuery, newQueries, propertyId ) {
255286function getFilteredQueries ( query , propertyId ) {
256287 const propertyIdPattern = propertyId || 'P\\d+' ;
257288 return query . split ( / \s + / ) . filter (
258- ( item ) => ! ( new RegExp ( `^(haswbfacet|\\-haswbfacet):${ propertyIdPattern } (=|>=|<=)` ) ) . test ( item )
289+ ( item ) => ! ( new RegExp ( `^(haswbfacet|\\-haswbfacet):${ propertyIdPattern } (=|>=|<=)?\\b ` ) ) . test ( item )
259290 ) ;
260291}
261292
0 commit comments