Skip to content

Commit

Permalink
Merge pull request #15 from bbc/types
Browse files Browse the repository at this point in the history
Types
  • Loading branch information
thomascgray authored Jun 5, 2019
2 parents bce632c + 734b4a5 commit d90a8e5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 58 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "3.1.0",
"description": "Left, Right, Up, Down. A spatial navigation library for devices with input via directional controls",
"main": "dist/index.min.js",
"types": "dist/index.d.ts",
"homepage": "https://github.com/bbc/lrud",
"license": "Apache-2.0",
"files": [
Expand Down Expand Up @@ -58,7 +59,7 @@
"minify": "uglifyjs dist/index.js --compress --mangle -o dist/index.min.js",
"prebuild": "npm run clean",
"build": "rollup -c",
"postbuild": "npm run minify",
"postbuild": "npx tsc && npm run minify",
"watch": "npm run build -- -w"
}
}
25 changes: 6 additions & 19 deletions src/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('event scenarios', () => {
// only called once, as `left` is already the activeChild of `root`
expect(activeSpy).toHaveBeenCalledWith({
parent: 'left',
parents: ['left', 'root'],
isFocusable: true,
id: 'b',
index: 1
Expand Down Expand Up @@ -58,28 +57,24 @@ describe('event scenarios', () => {
expect(activeSpy.mock.calls).toEqual([
[expect.objectContaining({
parent: 'right',
parents: ['right', 'root'],
isFocusable: true,
id: 'd',
index: 1
})],
[expect.objectContaining({
id: 'right',
parent: 'root',
parents: ['root'],
index: 1,
activeChild: 'd',
children: {
c: {
parent: 'right',
parents: ['right', 'root'],
isFocusable: true,
id: 'c',
index: 0
},
d: {
parent: 'right',
parents: ['right', 'root'],
isFocusable: true,
id: 'd',
index: 1
Expand All @@ -91,28 +86,24 @@ describe('event scenarios', () => {
expect(inactiveSpy.mock.calls).toEqual([
[expect.objectContaining({
parent: 'right',
parents: ['right', 'root'],
isFocusable: true,
id: 'c',
index: 0
})],
[expect.objectContaining({
id: 'left',
parents: ['root'],
parent: 'root',
index: 0,
activeChild: 'a',
children: {
a: {
parent: 'left',
parents: ['left', 'root'],
isFocusable: true,
id: 'a',
index: 0
},
b: {
parent: 'left',
parents: ['left', 'root'],
isFocusable: true,
id: 'b',
index: 1
Expand Down Expand Up @@ -145,14 +136,12 @@ describe('event scenarios', () => {
id: 'a',
index: 0,
parent: 'root',
parents: ['root'],
isFocusable: true
},
enter: {
id: 'b',
index: 1,
parent: 'root',
parents: ['root'],
isFocusable: true
},
direction: 'RIGHT',
Expand Down Expand Up @@ -183,14 +172,12 @@ describe('event scenarios', () => {
id: 'b',
index: 1,
parent: 'root',
parents: ['root'],
isFocusable: true
},
enter: {
id: 'a',
index: 0,
parent: 'root',
parents: ['root'],
isFocusable: true
},
direction: 'LEFT',
Expand All @@ -215,11 +202,11 @@ describe('event scenarios', () => {
.registerNode('d', { isFocusable: true })

navigation.assignFocus('a')
expect(focusSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'a', parent: 'root', parents: ['root'], index: 0 })
expect(focusSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'a', parent: 'root', index: 0 })

navigation.assignFocus('b')
expect(blurSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'a', parent: 'root', parents: ['root'], index: 0 })
expect(focusSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'b', parent: 'root', parents: ['root'], index: 1 })
expect(blurSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'a', parent: 'root', index: 0 })
expect(focusSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'b', parent: 'root', index: 1 })
})

test('standard blur and focus should fire after doing a move', () => {
Expand All @@ -239,12 +226,12 @@ describe('event scenarios', () => {
.registerNode('d', { isFocusable: true })

navigation.assignFocus('a')
expect(focusSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'a', parent: 'root', parents: ['root'], index: 0 })
expect(focusSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'a', parent: 'root', index: 0 })

navigation.handleKeyEvent({ direction: 'right' })

expect(blurSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'a', parent: 'root', parents: ['root'], index: 0 })
expect(focusSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'b', parent: 'root', parents: ['root'], index: 1 })
expect(blurSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'a', parent: 'root', index: 0 })
expect(focusSpy).toHaveBeenCalledWith({ isFocusable: true, id: 'b', parent: 'root', index: 1 })
})

test('`onLeave` and `onEnter` functions should fire on a node', () => {
Expand Down
4 changes: 0 additions & 4 deletions src/handle-key-event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ describe('handleKeyEvent()', () => {

expect(spy).toHaveBeenCalledWith({
parent: 'root',
parents: ['root'],
index: 1,
id: 'child_2',
isFocusable: true
Expand All @@ -92,7 +91,6 @@ describe('handleKeyEvent()', () => {

expect(spy).toHaveBeenCalledWith({
parent: 'root',
parents: ['root'],
id: 'child_3',
index: 2,
isFocusable: true
Expand All @@ -116,7 +114,6 @@ describe('handleKeyEvent()', () => {

expect(spy).toHaveBeenCalledWith({
parent: 'root',
parents: ['root'],
id: 'child_2',
index: 1,
isFocusable: true
Expand All @@ -128,7 +125,6 @@ describe('handleKeyEvent()', () => {

expect(spy).toHaveBeenCalledWith({
parent: 'root',
parents: ['root'],
id: 'child_1',
index: 0,
isFocusable: true
Expand Down
47 changes: 21 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Get } from './get'
import { Set } from './set'
import { Node, Override, KeyEvent } from './interfaces'

import {
isNodeFocusable,
Expand Down Expand Up @@ -48,7 +49,7 @@ export class Lrud {
*
* @param {object} node
*/
reindexChildrenOfNode(node) {
reindexChildrenOfNode(node: Node) {
if (!node.children) {
return
}
Expand Down Expand Up @@ -99,7 +100,7 @@ export class Lrud {
*
* @param {string} nodeId
*/
getPathForNodeId(nodeId) {
getPathForNodeId(nodeId: string) {
if (nodeId === this.rootNodeId) {
return this.rootNodeId
}
Expand All @@ -123,7 +124,7 @@ export class Lrud {
* @param {function} [node.onLeave] if a node has an `onLeave` function, it will be run when a move event leaves this node
* @param {function} [node.onEnter] if a node has an `onEnter` function, it will be run when a move event enters this node
*/
registerNode(nodeId, node: any = {}) {
registerNode(nodeId: string, node: Node = { id: null }) {
if (!node.id) {
node.id = nodeId
}
Expand All @@ -145,12 +146,6 @@ export class Lrud {
node.parent = this.rootNodeId
}

// if we have a parent but no parents, work out the parents
if (node.parent && node.parents == null) {
const list = this.nodePathList.find(path => path.includes(node.parent));
node.parents = list.split('.').filter(i => i != 'children').reverse();
}

// if this node is the first child of its parent, we need to set its parent's `activeChild`
// to it so that the parent always has an `activeChild` value
// we can tell if its parent has any children by checking the nodePathList for
Expand Down Expand Up @@ -191,7 +186,7 @@ export class Lrud {
/**
* maintained for legacy API reasons
*/
register(nodeId, node: any = {}) {
register(nodeId: string, node: any = {}) {
return this.registerNode(nodeId, node)
}

Expand All @@ -201,7 +196,7 @@ export class Lrud {
*
* @param {string} nodeId
*/
unregister(nodeId) {
unregister(nodeId: string) {
this.unregisterNode(nodeId);
}

Expand All @@ -210,7 +205,7 @@ export class Lrud {
*
* @param {string} nodeId
*/
unregisterNode(nodeId) {
unregisterNode(nodeId: string) {
if (nodeId === this.rootNodeId) {
this.tree = {}
this.overrides = {};
Expand Down Expand Up @@ -290,7 +285,7 @@ export class Lrud {
* @param {string} override.direction
* @param {string} override.target
*/
registerOverride(overrideId, override) {
registerOverride(overrideId: string, override: Override) {
if (!overrideId) {
throw new Error('need an ID to register an override')
}
Expand All @@ -316,7 +311,7 @@ export class Lrud {
*
* @param {string} overrideId
*/
unregisterOverride(overrideId) {
unregisterOverride(overrideId: string) {
delete this.overrides[overrideId]

return this
Expand All @@ -327,7 +322,7 @@ export class Lrud {
*
* @param {string} nodeId node id
*/
getNode(nodeId) {
getNode(nodeId: string) {
return Get(this.tree, (this.getPathForNodeId(nodeId)))
}

Expand All @@ -336,7 +331,7 @@ export class Lrud {
*
* @param {string} nodeId node id
*/
pickNode(nodeId) {
pickNode(nodeId: string) {
const node = this.getNode(nodeId)

if (!node) {
Expand All @@ -356,7 +351,7 @@ export class Lrud {
* @param {object} node
* @param {string} direction
*/
climbUp(node, direction) {
climbUp(node: Node, direction: string) {
if (!node) {
return null
}
Expand Down Expand Up @@ -414,7 +409,7 @@ export class Lrud {
*
* @param {object} node
*/
digDown(node, direction = null) {
digDown(node: Node, direction: string = null) {
// if the active child is focusable, return it
if (isNodeFocusable(node)) {
return node
Expand Down Expand Up @@ -491,7 +486,7 @@ export class Lrud {
* @param {object} node
* @param {string} direction
*/
getNextChildInDirection(node, direction) {
getNextChildInDirection(node: Node, direction: string) {
direction = direction.toUpperCase()

if (node.orientation === 'horizontal' && direction === 'RIGHT') {
Expand All @@ -515,7 +510,7 @@ export class Lrud {
*
* @param {object} node
*/
getNextChild(node) {
getNextChild(node: Node) {
if (!node.activeChild) {
node.activeChild = this.getNodeFirstChild(node).id
}
Expand All @@ -539,7 +534,7 @@ export class Lrud {
* get the semantic "previous" child for a node
* @param {object} node
*/
getPrevChild(node) {
getPrevChild(node: Node) {
if (!node.activeChild) {
node.activeChild = this.getNodeFirstChild(node).id
}
Expand All @@ -564,7 +559,7 @@ export class Lrud {
* get the first child of a node, based on index
* @param {object} node
*/
getNodeFirstChild(node) {
getNodeFirstChild(node: Node) {
if (!node.children) {
return undefined
}
Expand All @@ -578,7 +573,7 @@ export class Lrud {
* get the last child of a node, based on index
* @param {object} node
*/
getNodeLastChild(node) {
getNodeLastChild(node: Node) {
if (!node.children) {
return undefined
}
Expand All @@ -596,7 +591,7 @@ export class Lrud {
* @param {string} [event.keyCode]
* @param {string} [event.direction]
*/
handleKeyEvent(event) {
handleKeyEvent(event: KeyEvent) {
const direction = (event.keyCode) ? getDirectionForKeyCode(event.keyCode) : event.direction.toUpperCase()
const currentFocusNode = this.getNode(this.currentFocusNodeId)

Expand Down Expand Up @@ -662,7 +657,7 @@ export class Lrud {
* @param {string} parentId
* @param {string} childId
*/
setActiveChild(parentId, childId) {
setActiveChild(parentId: string, childId: string) {
const child = this.getNode(childId)
const parent = this.getNode(parentId)
if (!child) {
Expand Down Expand Up @@ -705,7 +700,7 @@ export class Lrud {
*
* @param {string} nodeId
*/
assignFocus(nodeId) {
assignFocus(nodeId: string) {
let node = this.getNode(nodeId)

if (!isNodeFocusable(node)) {
Expand Down
Loading

0 comments on commit d90a8e5

Please sign in to comment.