From 1884c7070eec3f994da1ddb36e7ce644771c8cb6 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Mon, 19 Dec 2016 16:55:11 -0500 Subject: [PATCH] Add a keybinding behavior for operations --- modules/behavior/index.js | 1 + modules/behavior/operation.js | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 modules/behavior/operation.js diff --git a/modules/behavior/index.js b/modules/behavior/index.js index da78fa23a..2dfb951bf 100644 --- a/modules/behavior/index.js +++ b/modules/behavior/index.js @@ -8,6 +8,7 @@ export { behaviorEdit } from './edit'; export { behaviorHash } from './hash'; export { behaviorHover } from './hover'; export { behaviorLasso } from './lasso'; +export { behaviorOperation } from './operation'; export { behaviorPaste } from './paste'; export { behaviorSelect } from './select'; export { behaviorTail } from './tail'; diff --git a/modules/behavior/operation.js b/modules/behavior/operation.js new file mode 100644 index 000000000..fa57ddc41 --- /dev/null +++ b/modules/behavior/operation.js @@ -0,0 +1,40 @@ +import * as d3 from 'd3'; +import { d3keybinding } from '../lib/d3.keybinding.js'; + + +/* Creates a keybinding behavior for an operation */ +export function behaviorOperation(context) { + var which, keybinding; + + + var behavior = function () { + if (which) { + keybinding = d3keybinding('behavior.key.' + which.id); + keybinding.on(which.keys, function() { + d3.event.preventDefault(); + if (!(context.inIntro() || which.disabled())) { + which(); + } + }); + d3.select(document).call(keybinding); + } + return behavior; + }; + + + behavior.off = function() { + if (keybinding) { + d3.select(document).call(keybinding.off); + } + }; + + + behavior.which = function (_) { + if (!arguments.length) return which; + which = _; + return behavior; + }; + + + return behavior; +}