@@ -74,16 +74,51 @@ export const paperOptions = {
74
74
}
75
75
} ;
76
76
77
+ const defaultSubcircuitButtons = [
78
+ {
79
+ id : "zoomOut" ,
80
+ hidden : false ,
81
+ buttonText : "–" ,
82
+ callback : ( { circuit, model, paper} ) => {
83
+ const newZoom = model . get ( 'zoomLevel' ) - 1 ;
84
+ circuit . scaleAndRefreshPaper ( paper , newZoom ) ;
85
+ model . set ( "zoomLevel" , newZoom ) ;
86
+ }
87
+ } ,
88
+ {
89
+ id : "zoomIn" ,
90
+ hidden : false ,
91
+ buttonText : "+" ,
92
+ callback : ( { circuit, model, paper} ) => {
93
+ const newZoom = model . get ( 'zoomLevel' ) + 1 ;
94
+ circuit . scaleAndRefreshPaper ( paper , newZoom ) ;
95
+ model . set ( "zoomLevel" , newZoom ) ;
96
+ }
97
+ }
98
+ ] ;
99
+
77
100
export class Circuit extends HeadlessCircuit {
78
- constructor ( data , { windowCallback = Circuit . prototype . _defaultWindowCallback , layoutEngine = "elkjs" , ...options } = { } ) {
101
+ constructor ( data , { windowCallback = Circuit . prototype . _defaultWindowCallback , layoutEngine = "elkjs" , subcircuitButtons = [ ] , ...options } = { } ) {
79
102
if ( ! options . engine ) options . engine = BrowserSynchEngine ;
80
103
super ( data , options ) ;
81
104
this . _layoutEngine = layoutEngine
82
105
this . _windowCallback = windowCallback ;
106
+ this . _subcircuitButtons = this . _mergeSubcircuitButtons ( subcircuitButtons ) ;
83
107
this . listenTo ( this . _engine , 'changeRunning' , ( ) => {
84
108
this . trigger ( 'changeRunning' ) ;
85
109
} ) ;
86
110
}
111
+ _mergeSubcircuitButtons ( buttons = [ ] ) {
112
+ const res = new Map ( ) ;
113
+ for ( const button of defaultSubcircuitButtons . concat ( buttons ) ) {
114
+ if ( button ?. hidden ) {
115
+ res . delete ( button . id ) ;
116
+ } else {
117
+ res . set ( button . id , button ) ;
118
+ }
119
+ }
120
+ return Array . from ( res . values ( ) ) ;
121
+ }
87
122
_defaultWindowCallback ( type , div , closingCallback ) {
88
123
const maxWidth = ( ) => $ ( window ) . width ( ) * 0.9 ;
89
124
const maxHeight = ( ) => $ ( window ) . height ( ) * 0.9 ;
@@ -168,17 +203,13 @@ export class Circuit extends HeadlessCircuit {
168
203
} ) ;
169
204
paper . unfreeze ( ) ;
170
205
// subcircuit display
206
+ const circuit = this ;
171
207
this . listenTo ( paper , 'open:subcircuit' , ( model ) => {
172
208
const subcircuitModal = $ ( '<div>' , {
173
209
title : model . get ( 'celltype' ) + ' ' + model . get ( 'label' )
174
210
} ) . appendTo ( 'html > body' ) ;
175
- const btnId = model . get ( 'label' ) ;
176
- $ (
177
- `<div class="btn-group">
178
- <button id="${ btnId } _zoomOut" class="btn btn-secondary"><strong>–</strong></button>
179
- <button id="${ btnId } _zoomIn" class="btn btn-secondary"><strong>+</strong></button>
180
- </div>`
181
- ) . appendTo ( subcircuitModal ) ;
211
+
212
+ // Create and set up paper
182
213
const pdiv = $ ( '<div>' ) . appendTo ( subcircuitModal ) ;
183
214
const graph = model . get ( 'graph' ) ;
184
215
const paper = this . _makePaper ( pdiv , graph ) ;
@@ -190,21 +221,16 @@ export class Circuit extends HeadlessCircuit {
190
221
} ) ;
191
222
} ) ;
192
223
193
- // Since ids are generated dynamically, we should ensure,
194
- // that we use a selector that escapes possible special characters
195
- const btnIdSelector = btnId . replace ( / [ ^ A - Z a - z 0 - 9 \s ] / g, '\\$&' ) ;
196
- const zoomInBtn = $ ( `#${ btnIdSelector } _zoomIn` ) ;
197
- const zoomOutBtn = $ ( `#${ btnIdSelector } _zoomOut` ) ;
198
-
199
- let scaleIndex = 0 ;
200
- zoomInBtn . click ( ( ) => {
201
- scaleIndex ++ ;
202
- this . scaleAndRefreshPaper ( paper , scaleIndex ) ;
203
- } ) ;
204
- zoomOutBtn . click ( ( ) => {
205
- scaleIndex -- ;
206
- this . scaleAndRefreshPaper ( paper , scaleIndex ) ;
207
- } ) ;
224
+ // Create buttons
225
+ model . set ( "zoomLevel" , 0 ) ;
226
+ const buttonGroup = $ ( '<div class="btn-group"></div>' )
227
+ for ( const button of this . _subcircuitButtons ) {
228
+ $ ( '<button class="btn btn-secondary"></button>' )
229
+ . append ( $ ( '<strong></strong>' ) . text ( button . buttonText ) )
230
+ . on ( 'click' , { circuit, model, paper} , ( event ) => button . callback ( event . data ) )
231
+ . appendTo ( buttonGroup ) ;
232
+ }
233
+ buttonGroup . prependTo ( subcircuitModal ) ;
208
234
} ) ;
209
235
this . listenTo ( paper , 'open:memorycontent' , ( subcircuitModal , closeCallback ) => {
210
236
this . _windowCallback ( 'Memory' , subcircuitModal , closeCallback ) ;
0 commit comments