From 1611ecdf4811bc06fd05fdb1a084d1aad74e28a9 Mon Sep 17 00:00:00 2001 From: Mike A Date: Fri, 11 Aug 2023 20:41:59 +0200 Subject: [PATCH] Add ability to change subcircuit buttons --- src/index.mjs | 72 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index 2d95c94..a4015fa 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -74,16 +74,51 @@ export const paperOptions = { } }; +const defaultSubcircuitButtons = [ + { + id: "zoomOut", + hidden: false, + buttonText: "–", + callback: ({circuit, model, paper}) => { + const newZoom = model.get('zoomLevel') - 1; + circuit.scaleAndRefreshPaper(paper, newZoom); + model.set("zoomLevel", newZoom); + } + }, + { + id: "zoomIn", + hidden: false, + buttonText: "+", + callback: ({circuit, model, paper}) => { + const newZoom = model.get('zoomLevel') + 1; + circuit.scaleAndRefreshPaper(paper, newZoom); + model.set("zoomLevel", newZoom); + } + } +]; + export class Circuit extends HeadlessCircuit { - constructor(data, { windowCallback = Circuit.prototype._defaultWindowCallback, layoutEngine = "elkjs", ...options } = {}) { + constructor(data, { windowCallback = Circuit.prototype._defaultWindowCallback, layoutEngine = "elkjs", subcircuitButtons = [], ...options } = {}) { if (!options.engine) options.engine = BrowserSynchEngine; super(data, options); this._layoutEngine = layoutEngine this._windowCallback = windowCallback; + this._subcircuitButtons = this._mergeSubcircuitButtons(subcircuitButtons); this.listenTo(this._engine, 'changeRunning', () => { this.trigger('changeRunning'); }); } + _mergeSubcircuitButtons(buttons = []) { + const res = new Map(); + for (const button of defaultSubcircuitButtons.concat(buttons)) { + if (button?.hidden) { + res.delete(button.id); + } else { + res.set(button.id, button); + } + } + return Array.from(res.values()); + } _defaultWindowCallback(type, div, closingCallback) { const maxWidth = () => $(window).width() * 0.9; const maxHeight = () => $(window).height() * 0.9; @@ -168,17 +203,13 @@ export class Circuit extends HeadlessCircuit { }); paper.unfreeze(); // subcircuit display + const circuit = this; this.listenTo(paper, 'open:subcircuit', (model) => { const subcircuitModal = $('
', { title: model.get('celltype') + ' ' + model.get('label') }).appendTo('html > body'); - const btnId = model.get('label'); - $( - `
- - -
` - ).appendTo(subcircuitModal); + + // Create and set up paper const pdiv = $('
').appendTo(subcircuitModal); const graph = model.get('graph'); const paper = this._makePaper(pdiv, graph); @@ -190,21 +221,16 @@ export class Circuit extends HeadlessCircuit { }); }); - // Since ids are generated dynamically, we should ensure, - // that we use a selector that escapes possible special characters - const btnIdSelector = btnId.replace(/[^A-Za-z0-9\s]/g, '\\$&'); - const zoomInBtn = $(`#${btnIdSelector}_zoomIn`); - const zoomOutBtn = $(`#${btnIdSelector}_zoomOut`); - - let scaleIndex = 0; - zoomInBtn.click(() => { - scaleIndex++; - this.scaleAndRefreshPaper(paper, scaleIndex); - }); - zoomOutBtn.click(() => { - scaleIndex--; - this.scaleAndRefreshPaper(paper, scaleIndex); - }); + // Create buttons + model.set("zoomLevel", 0); + const buttonGroup = $('
') + for (const button of this._subcircuitButtons) { + $('') + .append($('').text(button.buttonText)) + .on('click', {circuit, model, paper}, (event) => button.callback(event.data)) + .appendTo(buttonGroup); + } + buttonGroup.prependTo(subcircuitModal); }); this.listenTo(paper, 'open:memorycontent', (subcircuitModal, closeCallback) => { this._windowCallback('Memory', subcircuitModal, closeCallback);