From 6ece282ff0a152cbdf363972019876f83b0f3ce8 Mon Sep 17 00:00:00 2001 From: Tom Gray Date: Wed, 5 Jun 2019 13:10:49 +0100 Subject: [PATCH 1/5] wiring up types --- src/index.ts | 45 ++++++++++++++++++++------------------------- src/interfaces.ts | 22 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 src/interfaces.ts diff --git a/src/index.ts b/src/index.ts index 38cd025..f358107 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { Get } from './get' import { Set } from './set' +import { Node, Override } from './interfaces' import { isNodeFocusable, @@ -48,7 +49,7 @@ export class Lrud { * * @param {object} node */ - reindexChildrenOfNode(node) { + reindexChildrenOfNode(node: Node) { if (!node.children) { return } @@ -99,7 +100,7 @@ export class Lrud { * * @param {string} nodeId */ - getPathForNodeId(nodeId) { + getPathForNodeId(nodeId: string) { if (nodeId === this.rootNodeId) { return this.rootNodeId } @@ -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 } @@ -141,12 +142,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 @@ -187,7 +182,7 @@ export class Lrud { /** * maintained for legacy API reasons */ - register(nodeId, node: any = {}) { + register(nodeId: string, node: any = {}) { return this.registerNode(nodeId, node) } @@ -197,7 +192,7 @@ export class Lrud { * * @param {string} nodeId */ - unregister(nodeId) { + unregister(nodeId: string) { this.unregisterNode(nodeId); } @@ -206,7 +201,7 @@ export class Lrud { * * @param {string} nodeId */ - unregisterNode(nodeId) { + unregisterNode(nodeId: string) { if (nodeId === this.rootNodeId) { this.tree = {} this.overrides = {}; @@ -284,7 +279,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') } @@ -310,7 +305,7 @@ export class Lrud { * * @param {string} overrideId */ - unregisterOverride(overrideId) { + unregisterOverride(overrideId: string) { delete this.overrides[overrideId] return this @@ -321,7 +316,7 @@ export class Lrud { * * @param {string} nodeId node id */ - getNode(nodeId) { + getNode(nodeId: string) { return Get(this.tree, (this.getPathForNodeId(nodeId))) } @@ -330,7 +325,7 @@ export class Lrud { * * @param {string} nodeId node id */ - pickNode(nodeId) { + pickNode(nodeId: string) { const node = this.getNode(nodeId) if (!node) { @@ -350,7 +345,7 @@ export class Lrud { * @param {object} node * @param {string} direction */ - climbUp(node, direction) { + climbUp(node: Node, direction: string) { if (!node) { return null } @@ -408,7 +403,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 @@ -485,7 +480,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') { @@ -509,7 +504,7 @@ export class Lrud { * * @param {object} node */ - getNextChild(node) { + getNextChild(node: Node) { if (!node.activeChild) { node.activeChild = this.getNodeFirstChild(node).id } @@ -533,7 +528,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 } @@ -558,7 +553,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 } @@ -572,7 +567,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 } @@ -656,7 +651,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) { @@ -699,7 +694,7 @@ export class Lrud { * * @param {string} nodeId */ - assignFocus(nodeId) { + assignFocus(nodeId: string) { let node = this.getNode(nodeId) if (!isNodeFocusable(node)) { diff --git a/src/interfaces.ts b/src/interfaces.ts new file mode 100644 index 0000000..ac27de4 --- /dev/null +++ b/src/interfaces.ts @@ -0,0 +1,22 @@ +export interface Node { + id: string; + parent?: string; + parents?: string; + index?: number; + indexRange?: number[]; + selectAction?: Function; + isFocusable?: boolean; + isWrapping?: boolean; + orientation?: string; + isIndexAlign?: boolean; + onLeave?: Function; + onEnter?: Function; + activeChild?: string; + children?: any; +} + +export interface Override { + id: string; + direction: string; + target: string; +} \ No newline at end of file From e8aae4d4dd1f2c7ef649c0a9453481dc484fa672 Mon Sep 17 00:00:00 2001 From: Tom Gray Date: Wed, 5 Jun 2019 13:15:58 +0100 Subject: [PATCH 2/5] updated tests to remove reference to parents --- src/events.test.js | 25 ++++++------------------- src/handle-key-event.test.js | 4 ---- src/index.ts | 4 ++-- src/interfaces.ts | 6 +++++- src/lrud.test.js | 1 - src/unregister.test.js | 10 +++------- 6 files changed, 16 insertions(+), 34 deletions(-) diff --git a/src/events.test.js b/src/events.test.js index b8711f3..7af5370 100644 --- a/src/events.test.js +++ b/src/events.test.js @@ -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 @@ -58,7 +57,6 @@ describe('event scenarios', () => { expect(activeSpy.mock.calls).toEqual([ [expect.objectContaining({ parent: 'right', - parents: ['right', 'root'], isFocusable: true, id: 'd', index: 1 @@ -66,20 +64,17 @@ describe('event scenarios', () => { [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 @@ -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 @@ -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', @@ -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', @@ -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', () => { @@ -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', () => { diff --git a/src/handle-key-event.test.js b/src/handle-key-event.test.js index ac9c31d..ca50947 100644 --- a/src/handle-key-event.test.js +++ b/src/handle-key-event.test.js @@ -80,7 +80,6 @@ describe('handleKeyEvent()', () => { expect(spy).toHaveBeenCalledWith({ parent: 'root', - parents: ['root'], index: 1, id: 'child_2', isFocusable: true @@ -92,7 +91,6 @@ describe('handleKeyEvent()', () => { expect(spy).toHaveBeenCalledWith({ parent: 'root', - parents: ['root'], id: 'child_3', index: 2, isFocusable: true @@ -116,7 +114,6 @@ describe('handleKeyEvent()', () => { expect(spy).toHaveBeenCalledWith({ parent: 'root', - parents: ['root'], id: 'child_2', index: 1, isFocusable: true @@ -128,7 +125,6 @@ describe('handleKeyEvent()', () => { expect(spy).toHaveBeenCalledWith({ parent: 'root', - parents: ['root'], id: 'child_1', index: 0, isFocusable: true diff --git a/src/index.ts b/src/index.ts index f358107..ea1e699 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { Get } from './get' import { Set } from './set' -import { Node, Override } from './interfaces' +import { Node, Override, KeyEvent } from './interfaces' import { isNodeFocusable, @@ -585,7 +585,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) diff --git a/src/interfaces.ts b/src/interfaces.ts index ac27de4..f36209e 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,7 +1,6 @@ export interface Node { id: string; parent?: string; - parents?: string; index?: number; indexRange?: number[]; selectAction?: Function; @@ -19,4 +18,9 @@ export interface Override { id: string; direction: string; target: string; +} + +export interface KeyEvent { + keyCode: number; + direction: string; } \ No newline at end of file diff --git a/src/lrud.test.js b/src/lrud.test.js index 69c1d8e..3ad1e08 100644 --- a/src/lrud.test.js +++ b/src/lrud.test.js @@ -69,7 +69,6 @@ describe('lrud', () => { expect(node2.selectAction).toEqual(12) expect(node2.parent).toEqual('BOX_A') - expect(node2.parents).toEqual(['BOX_A', 'root']) expect(navigation.tree.root.children['BOX_A'].children['NODE_2']).toBeUndefined() }) }) diff --git a/src/unregister.test.js b/src/unregister.test.js index ef7ca76..6337126 100644 --- a/src/unregister.test.js +++ b/src/unregister.test.js @@ -116,7 +116,6 @@ describe('unregisterNode()', () => { // should trigger with the details of BOX_B expect(spy).toHaveBeenCalledWith({ parent: 'root', - parents: ['root'], id: 'BOX_B', index: 1, activeChild: 'NODE_4', @@ -124,20 +123,17 @@ describe('unregisterNode()', () => { NODE_4: { id: 'NODE_4', index: 0, - parent: 'BOX_B', - parents: ['BOX_B', 'root'] + parent: 'BOX_B' }, NODE_5: { id: 'NODE_5', index: 1, - parent: 'BOX_B', - parents: ['BOX_B', 'root'] + parent: 'BOX_B' }, NODE_6: { id: 'NODE_6', index: 2, - parent: 'BOX_B', - parents: ['BOX_B', 'root'] + parent: 'BOX_B' } } }) From e1a77b7665fc86a9ba71d2b77b57e0d367153d5d Mon Sep 17 00:00:00 2001 From: Tom Gray Date: Wed, 5 Jun 2019 13:28:18 +0100 Subject: [PATCH 3/5] generating files, and put them in the dist --- tsconfig.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tsconfig.json b/tsconfig.json index 8bb66b7..3dfc269 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,8 @@ { "compilerOptions": { + "declaration": true, + "declarationDir": "dist", + "outDir": "dist", "moduleResolution": "node", "target": "es5", "esModuleInterop": true, From aa04e2a043f7a4abc106acef9e8a550b38747e19 Mon Sep 17 00:00:00 2001 From: Tom Gray Date: Wed, 5 Jun 2019 13:28:24 +0100 Subject: [PATCH 4/5] run compile before rollup --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f7d4b57..3d6f5b5 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,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" } } From 734b4a5a9fb51ac934be21dcc335aad10c66d000 Mon Sep 17 00:00:00 2001 From: Tom Gray Date: Wed, 5 Jun 2019 13:33:42 +0100 Subject: [PATCH 5/5] define types output --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 3d6f5b5..28f7a1d 100644 --- a/package.json +++ b/package.json @@ -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": [