UNPKG

@swrve/smarttv-sdk

Version:

Swrve marketing engagement platform SDK for SmartTV OTT devices

82 lines (81 loc) 2.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const keymapHorizontal = { primaryNext: 'Right', primaryPrev: 'Left', }; const keymapVertical = { primaryNext: 'Down', primaryPrev: 'Up', }; const keymap = { horizontal: keymapHorizontal, vertical: keymapVertical, bidirectional: null, }; class SwrveFocusManager { constructor(items, props) { this.index = 0; this.props = props; this.items = items; } onFocus() { const child = this.items[this.index]; if (child) { this.props.onFocus(child); } } onBlur() { const child = this.items[this.index]; if (child) { this.props.onBlur(child); } } onKeyPress(key) { const child = this.items[this.index]; if (this.props.onKeyPress && child && this.props.onKeyPress(child, key)) { return true; } const { direction } = this.props; if (direction === "bidirectional") { return this.handleDirection(keymapVertical, key) || this.handleDirection(keymapHorizontal, key); } else { return this.handleDirection(keymap[direction || "vertical"] || keymapVertical, key); } } setItems(items) { this.items = items; } getItems() { return this.items; } getCurrentIndex() { return this.index; } setActiveItem(index) { if (index >= 0 && index < this.items.length) { this.props.onBlur(this.items[this.index]); this.index = index; this.props.onFocus(this.items[this.index]); } return true; } setActiveFirst() { this.setActiveItem(0); } setActiveLast() { this.setActiveItem(this.items.length - 1); } handleDirection(keymap, key) { const { primaryNext, primaryPrev } = keymap; if (key === primaryPrev) { return this.setActiveItem(this.index - 1); } if (key === primaryNext) { return this.setActiveItem(this.index + 1); } return false; } } exports.default = SwrveFocusManager;