@teachinglab/omd 0.3.8 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,359 @@
1
+
2
+
3
+
4
+ // ================ jsvgButton ================================= //
5
+
6
+ export class jsvgButton extends jsvgGroup
7
+ {
8
+ constructor()
9
+ {
10
+ super();
11
+
12
+ this.callback = null;
13
+ this.fontSize = 12;
14
+ this.text = 'button';
15
+
16
+ // add rect
17
+ this.backRect = new jsvgRect();
18
+ this.backRect.setWidthAndHeight( 100,30 );
19
+ this.backRect.setFillColor( "white" );
20
+ this.backRect.setStrokeWidth(0);
21
+ this.backRect.setCornerRadius( 15 );
22
+ this.backRect.svgObject.style.cursor = "pointer";
23
+ this.addChild( this.backRect );
24
+
25
+ // add text
26
+ this.buttonText = new jsvgTextLine();
27
+ this.buttonText.setPosition(50,20);
28
+ this.buttonText.setFontFamily( "Arial" );
29
+ this.buttonText.setFontSize( 12 );
30
+ this.buttonText.setText("button");
31
+ this.buttonText.setTextAnchor("middle");
32
+ this.buttonText.svgObject.style.cursor = "pointer";
33
+
34
+ this.addChild( this.buttonText );
35
+ }
36
+
37
+ setText( T ) { this.text = T; this.buttonText.setText(T) }
38
+ setFontFamily( F ) { this.buttonText.setFontFamily(F) }
39
+ setFontSize( S ) { this.fontSize=S; this.buttonText.setFontSize(S) }
40
+ setFontColor( C ) { this.buttonText.setFontColor(C) }
41
+ setFillColor( C ) { this.backRect.setFillColor( C ) };
42
+ setStrokeColor( C ) { this.backRect.setStrokeColor( C ) };
43
+ setStrokeWidth( C ) { this.backRect.setStrokeWidth( C ) };
44
+ setCornerRadius( R ) { this.backRect.setCornerRadius( R ) };
45
+
46
+ addImage( imageURL, imageWidth=30, imageHeight=30 )
47
+ {
48
+ this.image = new jsvgImage();
49
+ this.image.setImageURL( imageURL );
50
+ this.image.setWidthAndHeight( imageWidth, imageHeight );
51
+ this.image.setPosition( this.backRect.width/2-imageWidth/2, this.backRect.height/2-imageHeight/2 );
52
+ this.image.svgObject.style.cursor = "pointer";
53
+ this.addChild( this.image );
54
+ }
55
+
56
+ setWidthAndHeight( W, H )
57
+ {
58
+ this.width = W;
59
+ this.height = H;
60
+ this.backRect.setWidthAndHeight( W, H );
61
+ this.buttonText.setPosition( W/2, H*0.50+2 + this.fontSize*0.25 );
62
+ if ( this.image )
63
+ {
64
+ this.image.setPosition( this.backRect.width/2-this.image.width/2,
65
+ this.backRect.height/2-this.image.height/2 );
66
+ }
67
+ }
68
+ }
69
+
70
+
71
+ // ================ jsvgLayoutGroup ================================= //
72
+
73
+ export class jsvgLayoutGroup extends jsvgGroup
74
+ {
75
+ constructor()
76
+ {
77
+ super();
78
+
79
+ this.spacer = 10;
80
+ }
81
+
82
+ setSpacer( S )
83
+ {
84
+ this.spacer = S;
85
+ }
86
+
87
+ updateLayout()
88
+ {
89
+ }
90
+
91
+ showAll()
92
+ {
93
+ var N = this.getNumberOfChildren();
94
+ for ( var i=0; i<N; i++)
95
+ {
96
+ var C = this.getChild(i);
97
+ C.show();
98
+ }
99
+ }
100
+
101
+ showOnly( indexToShow )
102
+ {
103
+ var N = this.getNumberOfChildren();
104
+ for ( var i=0; i<N; i++)
105
+ {
106
+ var C = this.getChild(i);
107
+ if ( i == indexToShow )
108
+ C.show();
109
+ else
110
+ C.hide();
111
+ }
112
+ }
113
+
114
+ doVerticalLayout()
115
+ {
116
+ var N = this.getNumberOfChildren();
117
+
118
+ // layout children from top to bottom
119
+ var pX = 0;
120
+ var pY = 0;
121
+ var minW = 0;
122
+ for ( var i=0; i<N; i++)
123
+ {
124
+ var C = this.getChild(i);
125
+ if ( ! C.visible )
126
+ continue;
127
+ C.setPosition( pX,pY );
128
+ pY += C.height;
129
+ pY += this.spacer;
130
+
131
+ if ( C.width > minW )
132
+ minW = C.width;
133
+ }
134
+
135
+ // set width and height
136
+ this.height = pY - this.spacer;
137
+ this.width = minW;
138
+
139
+ // horizontal center all items
140
+ for ( var i=0; i<N; i++)
141
+ {
142
+ var C = this.getChild(i);
143
+ var pX = this.width/2-C.width/2;
144
+ C.setPosition( pX, C.ypos );
145
+ }
146
+ }
147
+
148
+ doHorizontalLayout()
149
+ {
150
+ var N = this.getNumberOfChildren();
151
+
152
+ // layout children from left to right
153
+ var pX = 0;
154
+ var pY = 0;
155
+ var minH = 0;
156
+ for ( var i=0; i<N; i++)
157
+ {
158
+ var C = this.getChild(i);
159
+ if ( ! C.visible )
160
+ continue;
161
+ C.setPosition( pX,pY );
162
+ pX += C.width;
163
+ pX += this.spacer;
164
+
165
+ if ( C.height > minH )
166
+ minH = C.height;
167
+ }
168
+
169
+ // set width and height
170
+ this.width = pX - this.spacer;
171
+ this.height = minH;
172
+
173
+ // vertically center all items
174
+ for ( var i=0; i<N; i++)
175
+ {
176
+ var C = this.getChild(i);
177
+ var pY = this.height/2-C.height/2;
178
+ C.setPosition( C.xpos, pY );
179
+ }
180
+ }
181
+ }
182
+
183
+
184
+ // ================ jsvgScrollbox ================================= //
185
+
186
+ export class jsvgScrollbox extends jsvgGroup
187
+ {
188
+ constructor()
189
+ {
190
+ super();
191
+
192
+ this.scrollPercent = 0;
193
+ this.scrollOffset = 0;
194
+ this.contentHeight = 0;
195
+
196
+ this.oldMouseX = 0;
197
+ this.oldMouseY = 0;
198
+
199
+ // make clip mask
200
+ this.clipMask = new jsvgClipMask( 200,400, 10 );
201
+ super.addChild( this.clipMask ); // use super to avoid addChild recursion
202
+
203
+ // make background rect
204
+ this.backRect = new jsvgRect();
205
+ this.backRect.setFillColor("white");
206
+ this.clipMask.addChild( this.backRect );
207
+
208
+ // make scroll group
209
+ this.scrollGroup = new jsvgGroup();
210
+ this.clipMask.addChild( this.scrollGroup );
211
+
212
+ // make scroll bar
213
+ this.scrollBar = new jsvgRect();
214
+ this.scrollBar.setWidthAndHeight( 10,50 );
215
+ this.scrollBar.setCornerRadius( 5 );
216
+ this.scrollBar.setFillColor( "darkgray" );
217
+ this.scrollBar.setPosition( 180, 10 );
218
+ this.scrollBar.svgObject.onmousedown = this.handleMouseDown.bind(this);
219
+ this.scrollBar.svgObject.style.cursor = "pointer";
220
+ this.clipMask.addChild( this.scrollBar );
221
+
222
+ this.setWidthAndHeight( 200,500 );
223
+
224
+ this.updateLayout();
225
+ }
226
+
227
+ setWidth( W )
228
+ {
229
+ super.setWidth( W );
230
+ this.clipMask.setWidth( W );
231
+ this.backRect.setWidth( W );
232
+ }
233
+
234
+ setHeight( H )
235
+ {
236
+ super.setWidth( H );
237
+ this.clipMask.setHeight( H );
238
+ this.backRect.setHeight( H );
239
+ }
240
+
241
+ setCornerRadius( R )
242
+ {
243
+ this.clipMask.setCornerRadius( R );
244
+ }
245
+
246
+ addChild( C )
247
+ {
248
+ this.scrollGroup.addChild( C );
249
+ }
250
+
251
+ handleMouseDown( event )
252
+ {
253
+ if ( ! this.visible )
254
+ return;
255
+
256
+ // handle dragging
257
+ function handleMouseMove(event)
258
+ {
259
+ event.preventDefault();
260
+ // console.log("handleMouseMove");
261
+
262
+ // get mouseX and mouseY
263
+ var mouseX, mouseY;
264
+ if ( event.touches && event.touches.length >= 1 )
265
+ {
266
+ mouseX = event.touches[0].clientX;
267
+ mouseY = event.touches[0].clientY;
268
+ }
269
+ else
270
+ {
271
+ mouseX = event.clientX;
272
+ mouseY = event.clientY;
273
+ }
274
+
275
+ // calculate dX and dY
276
+ if ( this.oldMouseX && this.oldMouseY )
277
+ {
278
+ var dX = mouseX - this.oldMouseX;
279
+ var dY = mouseY - this.oldMouseY;
280
+
281
+ var pY = this.scrollBar.ypos + dY;
282
+
283
+ // enforce slider bounds
284
+ var minY = 10;
285
+ var maxY = this.backRect.height - this.scrollBar.height - 10;
286
+ if ( pY < minY )
287
+ pY = minY;
288
+ if ( pY > maxY )
289
+ pY = maxY;
290
+
291
+ // move the circle
292
+ this.scrollBar.setPosition( this.scrollBar.xpos, pY );
293
+
294
+ this.scrollPercent = (pY-minY) / (maxY-minY);
295
+ // console.log( this.scrollPercent.toFixed(2) );
296
+
297
+ // update layout
298
+ this.updateLayout();
299
+ }
300
+
301
+ // set oldMouseX and oldMouseY
302
+ this.oldMouseX = mouseX;
303
+ this.oldMouseY = mouseY;
304
+ }
305
+
306
+ // on mouse up
307
+ function handleMouseUp(event)
308
+ {
309
+ // console.log("handleMouseUp");
310
+ this.activeBubble = null;
311
+ window.onmousemove = null;
312
+ window.ontouchmove = null;
313
+ window.onmouseup = null;
314
+ window.ontouchend = null;
315
+ this.oldMouseX = 0;
316
+ this.oldMouseY = 0;
317
+ return;
318
+ }
319
+
320
+ // set up events
321
+ window.onmousemove = handleMouseMove.bind(this);
322
+ window.ontouchmove = handleMouseMove.bind(this);;
323
+ window.onmouseup = handleMouseUp.bind(this);
324
+ window.ontouchend = handleMouseUp.bind(this);;
325
+ }
326
+
327
+ computeContentBounds()
328
+ {
329
+ var maxY = 0;
330
+ var N = this.scrollGroup.getNumberOfChildren();
331
+
332
+ // find bottom most point of all child objects
333
+ for ( var i=0; i<N; i++ )
334
+ {
335
+ var C = this.scrollGroup.getChild(i);
336
+ var bottomY = C.ypos + C.height;
337
+ if ( bottomY > maxY )
338
+ maxY = bottomY;
339
+ }
340
+
341
+ this.contentHeight = maxY;
342
+ }
343
+
344
+ updateLayout()
345
+ {
346
+ var W = this.backRect.width;
347
+ var H = this.backRect.height;
348
+
349
+ this.computeContentBounds();
350
+
351
+ if ( this.contentHeight > this.backRect.height )
352
+ {
353
+ var maxShift = this.contentHeight - this.backRect.height;
354
+ var scrollOffset = -1.0 * this.scrollPercent * maxShift;
355
+ this.scrollGroup.setPosition( 0, scrollOffset );
356
+ }
357
+ }
358
+
359
+ }