UNPKG

molstar

Version:

A comprehensive macromolecular library.

116 lines (115 loc) 4.21 kB
/** * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { ButtonsType, ModifiersKeys } from './input/input-observer'; import { interpolate, stringToWords } from './string'; export { Binding }; function Binding(triggers, action, description) { if (action === void 0) { action = ''; } if (description === void 0) { description = ''; } return Binding.create(triggers, action, description); } (function (Binding) { function create(triggers, action, description) { if (action === void 0) { action = ''; } if (description === void 0) { description = ''; } return { triggers: triggers, action: action, description: description }; } Binding.create = create; function isBinding(x) { return !!x && Array.isArray(x.triggers) && typeof x.action === 'string'; } Binding.isBinding = isBinding; Binding.Empty = { triggers: [], action: '', description: '' }; function isEmpty(binding) { return binding.triggers.length === 0 || binding.triggers.every(function (t) { return t.buttons === undefined && t.modifiers === undefined; }); } Binding.isEmpty = isEmpty; function match(binding, buttons, modifiers) { return binding.triggers.some(function (t) { return Trigger.match(t, buttons, modifiers); }); } Binding.match = match; function formatTriggers(binding) { return binding.triggers.map(Trigger.format).join(' or '); } Binding.formatTriggers = formatTriggers; function format(binding, name) { if (name === void 0) { name = ''; } var help = binding.description || stringToWords(name); return interpolate(help, { triggers: '<i>' + formatTriggers(binding) + '</i>' }); } Binding.format = format; function Trigger(buttons, modifiers) { return Trigger.create(buttons, modifiers); } Binding.Trigger = Trigger; (function (Trigger) { function create(buttons, modifiers) { return { buttons: buttons, modifiers: modifiers }; } Trigger.create = create; Trigger.Empty = {}; function match(trigger, buttons, modifiers) { var b = trigger.buttons, m = trigger.modifiers; return b !== undefined && (b === buttons || ButtonsType.has(b, buttons)) && (!m || ModifiersKeys.areEqual(m, modifiers)); } Trigger.match = match; function format(trigger) { var s = []; var b = formatButtons(trigger.buttons); if (b) s.push(b); var m = formatModifiers(trigger.modifiers); if (m) s.push(m); return s.join(' + '); } Trigger.format = format; })(Trigger = Binding.Trigger || (Binding.Trigger = {})); })(Binding || (Binding = {})); var B = ButtonsType; function formatButtons(buttons) { var s = []; if (buttons === undefined) { s.push('any mouse button'); } else if (buttons === 0) { s.push('mouse hover'); } else { if (B.has(buttons, 1 /* B.Flag.Primary */)) s.push('left mouse button'); if (B.has(buttons, 2 /* B.Flag.Secondary */)) s.push('right mouse button'); if (B.has(buttons, 4 /* B.Flag.Auxilary */)) s.push('wheel/middle mouse button'); if (B.has(buttons, 8 /* B.Flag.Forth */)) s.push('three fingers'); } return s.join(' + '); } function formatModifiers(modifiers, verbose) { var s = []; if (modifiers) { if (modifiers.alt) s.push('alt key'); if (modifiers.control) s.push('control key'); if (modifiers.meta) s.push('meta/command key'); if (modifiers.shift) s.push('shift key'); if (verbose && s.length === 0) s.push('no key'); } else { if (verbose) s.push('any key'); } return s.join(' + '); }