Improve this doc View source

# `FacetHandler`
service in module `seco.facetedSearch`

## Description

Service for mediating the communication between facets.

When created, the FacetHandler object broadcasts a `sf-initial-constraints` event on the given scope, with the following structure:

- constraint - `{Array}` - The constraints as generated by all the facets, plus any initial constraints defined in the init configuration.
- facets - `{Object}` - A collection of facet states where the key is the `facetId` of the facet, and value is the state as emitted by that facet.
- config - `{Object}` - The configuration given when instantiating the FacetHandler.

The FacetHandler object listens for two events:

- `sf-request-constraints` - In response will broadcast a `sf-initial-constraints` event with the same object as above as the argument.
- `sf-facets-changed` - In response will broadcast a `sf-facet-constraints` event on the scope with the same object as above, except omitting `config`.

The event names are injectable as constants:

- `sf-initial-constraints`: `EVENT_INITIAL_CONSTRAINTS`
- `sf-facet-constraints`: `EVENT_FACET_CONSTRAINTS`
- `sf-request-constraints`: `EVENT_REQUEST_CONSTRAINTS`
- `sf-facet-changed`: `EVENT_FACET_CHANGED`

## Usage

    new FacetHandler(config);

#### Parameters

Param | Type | Details
--- | --- | ---
config | Object | Configuration object broadcast to all facets at init. The object has the following properties: scope - `{Object}` - The facets' parent scope. This is used for broadcasting and listening for events. [endpointUrl] - `{string}` - The SPARQL endpoint URL. Optional, as it is just sent to all listening facets at init, and can also be given to facets individually. [headers] `{Object}` - Additional HTTP headers. [constraint] - `{string}` - A SPARQL triple pattern that selects all the resources that are being faceted. [rdfClass] - `{string}` - The `rdf:type` of the resources being faceted. A shorthand for `constraint: '?id a <class> .'`. [preferredLang] - `{string|Array}` - The language tag that is preferred when getting labels for facet values, in case the value is a resource. If the value is an Array, the specified languages are tried in order. The default is 'en'. This argument can also be given directly to the individual facets. [initialState] - `{Object}` - The initial state of the facets. Used when loading the state from URL parameters, for example. See `facetUrlStateHandlerService`. The object is sent to facets as is, and so any extra fields will be included, although the above fields are the only ones used by FacetHandler and the built-in facets.

## Methods

- ### removeListeners()

  Remove event listeners from the scope. Probably only needed if you, for some reason, need to use `$rootScope` as the communication scope.