new Scroller( what, how )

Description

An ordered collection of HTMLElements for a user to continuously scroll through.

The dispatcher can be used the handle the following events:

  • 'out-of-range' - Scrolling went past one end of the collection. This is NOT an error condition, but rather a design feature.
  • 'change' - The value of item has changed.
  • 'activate' - The Scroller was activated.
  • 'deactivate' - The Scroller was deactivated.
  • 'focus' - Before the focus is set.
  • 'focused' - After the focus is set.
Parameters
Name Type Description
what What

What we want to scroll.

how How

How we want to scroll.

Throws

On many construction problems.

Members


dispatcher :NexusHoratio.base.Dispatcher


item :external:Element


itemUid :string

Details
string

logger :NexusHoratio.base.Logger


name :string

Details
string

Methods


defaultUid( element ) → {string}

Description

Return normalized text for an element.

Like HTMLElement.innerText, but cleaner and mostly deduped.

Parameters
Name Type Description
element external:Element

Element to examine.

Returns

The normalized text.


click()

Description

Click either the current item OR document.activeElement.


next()

Description

Move to the next item in the collection.


prev()

Description

Move to the previous item in the collection.


first()

Description

Jump to the first item in the collection.


last()

Description

Jump to last item in the collection.


goto( item )

Description

Move to a specific item if possible.

Parameters
Name Type Description
item external:Element

Item to go to.


gotoUid( uid ) → {boolean}

Description

Move to a specific item if possible, by uid.

Parameters
Name Type Description
uid string

The uid of a specific item.

Returns

Was able to goto the item.


shine()

Description

Adds the registered CSS classes to the current element.


dull()

Description

Removes the registered CSS classes from the current element.


show()

Description

Bring current item back into view.


focus()

Description

Focus on current item.

Fires

<async> activate()

Description

Activate the scroller.

Fires

deactivate()

Description

Deactivate the scroller (but do not destroy it).

Fires

destroy()

Description

Mark instance as inactive and do any internal cleanup.


<private> attributesHandler( type, records )

Description

Currently removes scrollerId at the drop of a hat.

XXX: This was originally intended to clear scrollerId before duplications were detected. But such detection happens inside #getItems(), so this does not help with that. Still, might be useful in cases where the uid depends on attributes, even if duplicates are not involved.

Parameters
Name Type Description
type string

Event type.

records Array.<MutationRecords>

Standard MutationRecords.


<private> isItemViewable( item ) → {boolean}

Description

Determine if the item can be viewed.

Often this means the content is being loaded lazily and is not ready yet.

Parameters
Name Type Description
item external:Element

The item to inspect.

Returns

Whether the item has viewable content.


<private> onClick( evt )

Description

If an item is clicked, switch to it.

Parameters
Name Type Description
evt Event

Standard 'click' event.


<private> realHeight( element ) → {number}

Description

Return the computed height of an element.

The usual element.clientHeight is too unpredictable.

Parameters
Name Type Description
element external:Element

Element to examine.

Returns

The height of the element.


<private> containersMutationHandler( records )

Parameters
Name Type Description
records Array.<MutationRecord>

Standard mutation records.

Fires

<private> bottomHalf( val )

Description

Since the getter will try to validate the current item (since it could have changed out from under us), it too can update information.

Parameters
Name Type Description
val external:Element

Element to make current.

Fires

<private> getItems() → {Array.<Elements>}

Description

Builds the list of elements using the registered CSS selectors.

Returns

Items to scroll through.


<private> postProcessItems( items ) → {Array.<Element>}

Description

Log items and do any fixups on them.

Parameters
Name Type Description
items Array.<Element>

Elements in the Scroller.

Returns

Post processed items.


<private> uid( element ) → {string}

Description

Returns the uid for the current element. Will use the registered uidCallback function for this.

Parameters
Name Type Description
element external:Element

Element to identify.

Returns

Computed uid for element.


<private> matchItem( element ) → {boolean}

Description

Checks if the element is the current one. Useful as a callback to Array.find.

Parameters
Name Type Description
element external:Element

Element to check.

Returns

Whether or not element is the current one.


<private> gentlyScrollIntoView( item )

Description

If necessary, scroll the bottom into view, then same for top.

Parameters
Name Type Description
item external:Element

The item to scroll into view.


<private> scrollToCurrentItem()

Description

Scroll the current item into the view port. Depending on the instance configuration, this could snap to the top, snap to the bottom, or be a no-op.


<private> jumpToEndItem( first )

Description

Jump an item on an end of the collection.

Parameters
Name Type Description
first boolean

If true, the first item in the collection, else, the last.


<private> scrollBy( n )

Description

Move forward or backwards in the collection by at least n.

Parameters
Name Type Description
n number

How many items to move and the intended direction.

Fires

<private> validateInstance()

Throws

On many validation issues.


<private> validateWhat()

Throws

On many validation issues.


<private> validateHow()

Throws

On many validation issues.


<private> waitForContainers() → {Promise.<Array.<Element>>}

Description

The page may still be loading, so wait for many things to settle.

Returns

All the new base elements.


<private> currentItemWatcher() → {Promise.<string>}

Description

Watches for the current item, if there was one, to return.

Used during activation to deal with items still being loaded.

Returns

Wait on this to finish with something useful to log.

Type Definitions


uidCallback( scroller, element ) → {string}

Description

Function that generates a, preferably, reproducible unique identifier for an Element.

The method defaultUid exists to both provide an example and fallback implementation. However, it may not always be reproducible (consider items that consist of counts for reads and likes). It may also not be unique within a particular instance.

It is a good practice to verify the stability and uniqueness of callbacks across page reloads. Built in logging will identify duplicates.

Parameters
Name Type Description
scroller Scroller

The calling Scroller instance.

element external:Element

Element to examine.

Returns

A value unique to this element.

Details
function

ContainerItemsSelector

Description

Contains CSS selectors to first find a base element, then items that it contains.

Properties
Name Type Description
container string

CSS selector to find the container element.

items string

CSS selector to find the items inside the container.

Details
object

ElementFinder( element ) → {external:Element}

Description

Function that finds a DOM element based upon another one.

Useful for cases where CSS selectors are not sufficient.

Parameters
Name Type Description
element external:Element

Starting point.

Returns

Found element.

Details
function

ClickConfig

Description

Common config for finding a clickable element inside the current item.

Use only one of selectorArray or finder.

Properties
Name Type Attributes Default Description
selectorArray Array.<string> <optional>

CSS selectors to use to find an element, passed to NexusHoratio.web.clickElement.

matchSelf boolean <optional>
false

If a CSS selector would match base, then use it, NexusHoratio.web.clickElement.

finder ElementFinder <optional>

Function to find the appropriate clickable element, when a selectorArray is too simplistic.

Details
object

What

Description

There are two ways to describe what elements go into a Scroller:

  1. An explicit container (base) element and selectors stemming from it.
  2. An array of ContainerItemsSelector that can allow for multiple containers with items. This approach will also allow the Scroller to automatically wait for all container elements to exist during activation.
Properties
Name Type Description
name string

Name for this Scroller, used for logging.

base external:Element

The container to use as a base for selecting elements.

selectors Array.<string>

Array of CSS selectors to find elements to collect, calling base.querySelectorAll().

containerItems Array.<ContainerItemsSelector>

Array of ContainerItemsSelectors.

Details
object

How

Properties
Name Type Attributes Default Description
uidCallback uidCallback

Callback to generate a uid.

maxUidLength number <optional>
20

Max length for default uid text.

classes Array.<string> <optional>
[]

Array of CSS classes to add/remove from an element as it becomes current.

watchForClicks boolean <optional>
true

Whether the Scroller should watch for clicks and if one is inside an item, select it.

autoActivate boolean <optional>
false

Whether to call the activate method at the end of construction.

observeAttributes boolean <optional>
false

Whether the built in external:MutationObserver should also observer node attributes (useful if the uid depends on attributes).

snapToTop boolean <optional>
false

Whether items should snap to the top of the window when coming into view. should happen when {snapToTop} is false.

waitForItemTimeout number <optional>
3000

Time to wait, in milliseconds, for existing item to reappear upon reactivation.

containerTimeout number <optional>
0

Time to wait, in milliseconds, for a {ContainerItemsSelector.container} to show up. Some pages may not always provide all identified containers. The default of 0 disables timing out. NB: Any containers that timeout will not handle further activate() processing, such as watchForClicks.

clickConfig ClickConfig <optional>
{}

Configures how the click() method operates.

Details
object