UNPKG

@lhs7/wheel-selector

Version:
86 lines (85 loc) 2.89 kB
import { makeCanvas, removeCanvas, drawItems, } from "../util/canvas"; export class WheelSelector { constructor(options) { var _a; this.isActive = false; this.isLeftClicked = false; this.position = null; this.items = []; this.selectedItemNo = null; this.outerDistance = 200; this.innerDistance = 100; this.cursorCanvas = null; this.theme = { defaultColor: "rgba(0, 0, 0, 0.7)", selectedColor: "rgba(125, 220, 0, 0.7)", }; if (options !== undefined) { let { outerDistance, innerDistance } = options; if (outerDistance === undefined) { if (innerDistance !== undefined) throw new Error("give outerDistance value"); else { outerDistance = this.outerDistance; innerDistance = this.innerDistance; } } else { if (innerDistance === undefined) { innerDistance = outerDistance / 2; } else if (innerDistance >= outerDistance) { throw new Error("innerDistance should be smaller than outerDistance"); } } options.outerDistance = outerDistance; options.innerDistance = innerDistance; let { theme } = options; if (theme) { Object.assign(this.theme, theme); } } options = Object.assign({ items: [], }, options); this.updateItems((_a = options.items) !== null && _a !== void 0 ? _a : []); this.outerDistance = options.outerDistance; this.innerDistance = options.innerDistance; } activateSelector(pos) { this.position = pos; WheelSelector.prototype.deactivateSelector.call(this); this.cursorCanvas = makeCanvas(this.position, this.outerDistance * 2); this.redraw(); this.isActive = true; } deactivateSelector() { if (this.cursorCanvas !== null) removeCanvas(this.cursorCanvas); this.isActive = false; } triggerSelected() { if (typeof this.selectedItemNo !== "number") return; const item = this.items[this.selectedItemNo]; this.selectedItemNo = null; item.callback(item); } redraw() { if (this.position === null) return; drawItems(this); } selectItem(itemno) { if (itemno !== null && this.items[itemno] === undefined) throw Error("invalid item number"); this.selectedItemNo = itemno; this.redraw(); } updateItems(items) { this.selectedItemNo = null; this.items = items ? items : []; this.redraw(); return this.items; } }