forked from backface/turtlestitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.js
1241 lines (1075 loc) · 34.2 KB
/
tables.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
tables.js
basic spreadsheet elements for Snap!
written by Jens Mönig
Copyright (C) 2016 by Jens Mönig
This file is part of Snap!.
Snap! is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
prerequisites:
--------------
needs morphic.js, list.js, widgets.js, byob.js, threads
I. hierarchy
-------------
the following tree lists all constructors hierarchically,
indentation indicating inheritance. Refer to this list to get a
contextual overview:
DialogBoxMorph**
TableDialogMorph
Morph*
FrameMorph*
TableMorph
TableCellMorph
TableFrameMorph
Table
* from morphic.js
** from widgets.js
II. toc
-------
the following list shows the order in which all constructors are
defined. Use this list to locate code in this document:
Table
TableCellMorph
TableMorph
TableFrameMorph
TableDialogMorph
*/
// Global settings /////////////////////////////////////////////////////
/*global modules, Point, newCanvas, Morph, fontHeight, SliderMorph, List,
MorphicPreferences, FrameMorph, HandleMorph, DialogBoxMorph, isString,
SpriteMorph, Context, Costume, ArgMorph, BlockEditorMorph,
SyntaxElementMorph, MenuMorph, SpriteBubbleMorph, SpeechBubbleMorph,
CellMorph, ListWatcherMorph, isNil, BoxMorph, Variable, isSnapObject*/
modules.tables = '2016-May-02';
var Table;
var TableCellMorph;
var TableMorph;
var TableFrameMorph;
// Table /////////////////////////////////////////////////////////////
/*
Observable 2D data collections accessible by rows, columns and cells
with indices starting at 1.
currently only used for testing TableViews in Snap, because Snap
automatically displays 2D lists as tables.
*/
function Table(cols, rows) {
this.colCount = +cols;
this.rowCount = +rows;
this.colNames = [];
this.rowNames = [];
this.contents = new Array(+rows);
for (var i = 0; i < rows; i += 1) {
this.contents[i] = new Array(+cols);
}
this.lastChanged = Date.now();
}
// Table testing:
Table.prototype.demo = function(aWorld) {
// new Table(50, 10000).demo(world)
var dlg;
this.fillWithTestData();
dlg = new TableDialogMorph(this);
dlg.popUp (aWorld);
};
// Table updating:
Table.prototype.changed = function () {
this.lastChanged = Date.now();
};
// Table querying:
Table.prototype.get = function (col, row) {
if (!col) {
if (!row) {return [this.rowCount]; }
return this.rowName(row);
} else if (!row) {
return this.colName(col);
}
if (col > this.colCount || row > this.rowCount) {return null; }
return (this.contents[row - 1] || [])[col - 1];
};
Table.prototype.row = function(row) {
return this.contents[row - 1];
};
Table.prototype.col = function(col) {
var dta = [],
c = col - 1,
i;
for (i = 0; i < this.rowCount; i += 1) {
dta.push(this.contents[i][c]);
}
return dta;
};
Table.prototype.colName = function (col) {
// answer the specified name or a capital letter A-Z
// repeated accordingly
if (col > this.colCount) {return null; }
var name = this.colNames[col - 1];
if (name !== undefined) {return name; }
return String.fromCharCode(64 + ((col % 26) || 26)).repeat(
Math.floor((col - 1) / 26) + 1
);
};
Table.prototype.rowName = function (row) {
// answer the specified name or row number
if (row > this.rowCount) {return null; }
return this.rowNames[row - 1] || row;
};
Table.prototype.rows = function () {
return this.rowCount;
};
Table.prototype.cols = function () {
return this.colCount;
};
Table.prototype.columnNames = function () {
return this.colNames;
};
// Table setting:
Table.prototype.set = function (data, col, row) {
this.contents[row - 1][col - 1] = data;
this.changed();
};
Table.prototype.setRows = function (rowsArray, colNames, rowNames) {
this.contents = rowsArray;
if (colNames) {this.colNames = colNames; }
if (rowNames) {this.rowNames = rowNames; }
this.changed();
};
Table.prototype.setCols = function (colsArray, colNames, rowNames) {
var r, c;
for (c = 0; c < this.colCount; c += 1) {
for (r = 0; r < this.rowCount; r += 1) {
this.contents[r][c] = colsArray[c][r];
}
}
if (colNames) {this.colNames = colNames; }
if (rowNames) {this.rowNames = rowNames; }
this.changed();
};
Table.prototype.setColNames = function (array) {
this.colNames = array || [];
this.changed();
};
Table.prototype.setRowNames = function (array) {
this.rowNames = array || [];
this.changed();
};
Table.prototype.setColName = function (col, name) {
this.colNames[col + 1] = name;
this.changed();
};
Table.prototype.setRowName = function (row, name) {
this.rowNames[row + 1] = name;
this.changed();
};
// Table growing:
Table.prototype.addRow = function (array, name) {
if (array) {
this.contents[this.rowCount] = array;
} else {
this.contents[this.rowCount] = new Array(this.rowCount);
}
this.rowNames[this.rowCount] = name;
this.rowCount += 1;
this.changed();
};
Table.prototype.addCol = function (array, name) {
var i;
if (array) {
for (i = 0; i < this.col; i += 1) {
this.contents[i][this.colCount] = array[i];
}
}
this.colNames[this.colCount] = name;
this.colCount += 1;
this.changed();
};
// Table converting:
Table.prototype.toList = function () {
return new List(
this.contents.map(function (eachRow) {
return new List(eachRow);
})
);
};
// Table testing
Table.prototype.fillWithTestData = function () {
var c, r;
for (c = 1; c <= this.colCount; c += 1) {
for (r = 1; r <= this.rowCount; r += 1) {
this.set (this.colName(c) + this.rowName(r), c, r);
}
}
};
// TableCellMorph /////////////////////////////////////////////////////////
// basic fast data view, currently constrained to a single line of text
// TableCellMorph inherits from Morph:
TableCellMorph.prototype = new Morph();
TableCellMorph.prototype.constructor = TableCellMorph;
TableCellMorph.uber = Morph.prototype;
// TableCellMorph global setting:
TableCellMorph.prototype.listSymbol = ArgMorph.prototype.listIcon();
// TableCellMorph instance creation:
function TableCellMorph(data, extent, isLabel) {
this.init(data, extent, isLabel);
}
TableCellMorph.prototype.init = function (data, extent, isLabel) {
// additional properties:
this.data = data;
this.isLabel = isLabel || false;
// initialize inherited properties:
TableCellMorph.uber.init.call(this, true);
// override inherited properites:
this.noticesTransparentClick = true;
if (extent) {this.silentSetExtent(extent); }
this.drawNew();
};
TableCellMorph.prototype.setData = function (data, extent) {
this.data = data;
if (extent && (!extent.eq(this.extent()))) {
this.silentSetExtent(extent);
this.drawNew();
} else {
this.drawData();
}
// note: don't call changed(), let the TableMorph handle it instead
};
TableCellMorph.prototype.getData = function () {
return this.data instanceof Array ? this.data[0] : this.data;
};
TableCellMorph.prototype.drawNew = function () {
this.image = newCanvas(this.extent());
this.drawData();
};
TableCellMorph.prototype.drawData = function (lbl, bg) {
var dta = lbl || this.dataRepresentation(this.data),
context = this.image.getContext('2d'),
fontSize = SyntaxElementMorph.prototype.fontSize,
empty = TableMorph.prototype.highContrast ? 'rgb(220, 220, 220)'
: 'transparent',
orphaned = 'rgb(217, 77, 17)',
fontStyle = this.isLabel ?
(this.data instanceof Array ? 'italic' : '')
: this.shouldBeList() ? 'bold' : '',
font = fontStyle + ' ' + fontSize + 'px Helvetica, Arial, sans-serif',
background = bg || (this.isLabel ? empty
: (this.shouldBeList() ? orphaned
: (this.isOvershooting() ? 'white'
: (isNil(this.data) ? empty : 'white')))),
foreground = !this.isLabel && this.shouldBeList()? 'white' : 'black',
width = this.width(),
height = this.height(),
txtWidth,
txtHeight,
x,
y;
context.clearRect(0, 0, width, height);
context.fillStyle = background;
if (this.shouldBeList()) {
BoxMorph.prototype.outlinePath.call(
this, context, SyntaxElementMorph.prototype.corner + 1, 0
);
context.fill();
} else if (this.isOvershooting()) {
this.raggedBoxPath(context);
context.fill();
} else {
context.fillRect(0, 0, width, height);
}
if (!dta) {return; }
if (dta instanceof HTMLCanvasElement) {
x = Math.max((width - dta.width) / 2, 0);
y = Math.max((height - dta.height) / 2, 0);
context.shadowOffsetX = 4;
context.shadowOffsetY = 4;
context.shadowBlur = 4;
context.shadowColor = 'lightgray';
context.drawImage(dta, x, y);
} else { // text
context.font = font;
context.textAlign = 'left';
context.textBaseline = 'bottom';
txtWidth = context.measureText(dta).width;
txtHeight = fontHeight(fontSize);
context.fillStyle = foreground;
x = Math.max((width - txtWidth) / 2, 0);
y = Math.max((height - txtHeight) / 2, 0);
context.fillText(dta, x, txtHeight + y);
}
};
TableCellMorph.prototype.dataRepresentation = function (dta) {
if (dta instanceof Morph) {
if (isSnapObject(dta)) {
return dta.thumbnail(new Point(40, 40));
} else {
return dta.fullImageClassic();
}
} else if (isString(dta)) {
return dta.length > 100 ? dta.slice(0, 100) + '...' : dta;
} else if (typeof dta === 'number') {
return dta.toString();
} else if (typeof dta === 'boolean') {
return SpriteMorph.prototype.booleanMorph.call(
null,
dta
).fullImage();
} else if (dta instanceof Array) {
return this.dataRepresentation(dta[0]);
} else if (dta instanceof Variable) {
return this.dataRepresentation(dta.value);
} else if (dta instanceof HTMLCanvasElement) {
return dta;
} else if (dta instanceof Context) {
return dta.image();
} else if (dta instanceof Costume) {
return dta.thumbnail(new Point(40, 40));
} else if (dta instanceof List) {
return this.listSymbol;
// return new ListWatcherMorph(dta).fullImageClassic();
} else {
return dta ? dta.toString() : (dta === 0 ? '0' : null);
}
};
TableCellMorph.prototype.raggedBoxPath = function (context) {
var width = this.width(),
height = this.height(),
x = width * 0.75,
step = height / 6,
y = 0;
context.beginPath();
context.moveTo(0, 0);
context.lineTo(width, 0);
for (y = 0; y < height; y += (step * 2)) {
context.lineTo(x, y + step);
context.lineTo(width, y + (step * 2));
}
context.lineTo(width, height);
context.lineTo(0, height);
context.closePath();
};
TableCellMorph.prototype.shouldBeList = function () {
return this.data instanceof Array;
};
TableCellMorph.prototype.isOvershooting = function () {
return this.data instanceof Variable;
};
// TableCellMorph events:
TableCellMorph.prototype.mouseDoubleClick = function (pos) {
if (this.data instanceof Table || this.data instanceof List) {
new TableDialogMorph(this.data).popUp(this.world());
} else if (this.data instanceof Array && this.data[0] instanceof List) {
new TableDialogMorph(this.data[0]).popUp(this.world());
} else {
this.escalateEvent('mouseDoubleClick', pos);
}
};
TableCellMorph.prototype.mouseEnter = function () {
var tm, x, c;
if (this.isLabel) {
tm = this.parentThatIsA(TableMorph);
x = tm.world().hand.left() - tm.left();
c = tm.columnAt(x);
if (c > 0) {
this.drawData(c, 'rgb(220, 220, 250)');
this.changed();
}
}
};
TableCellMorph.prototype.mouseLeave = function () {
if (this.isLabel) {
this.drawData();
this.changed();
}
};
// TableMorph //////////////////////////////////////////////////////////
// TableMorph inherits from FrameMorph:
TableMorph.prototype = new FrameMorph();
TableMorph.prototype.constructor = TableMorph;
TableMorph.uber = FrameMorph.prototype;
// TableMorph preferences settings:
TableMorph.prototype.highContrast = false;
// TableMorph instance creation:
function TableMorph(
table,
// optional parameters below this line
scrollBarSize,
extent,
startRow,
startCol,
globalColWidth,
colWidths,
rowHeight,
colLabelHeight,
padding
) {
this.init(
table,
scrollBarSize,
extent,
startRow,
startCol,
globalColWidth,
colWidths,
rowHeight,
colLabelHeight,
padding
);
}
TableMorph.prototype.init = function (
table,
scrollBarSize,
extent,
startRow,
startCol,
globalColWidth,
colWidths,
rowHeight,
colLabelHeight,
padding
) {
// additional properties:
this.table = table;
this.scrollBarSize = scrollBarSize || MorphicPreferences.scrollBarSize;
this.startRow = startRow || 1;
this.startCol = startCol || 1;
this.textHeight = Math.ceil(
fontHeight(SyntaxElementMorph.prototype.fontSize) * 1.3
);
this.rowHeight = rowHeight || this.textHeight;
this.colWidths = colWidths || [];
this.globalColWidth = globalColWidth || Math.ceil(this.textHeight * 3.5);
this.colLabelHeight = colLabelHeight || this.textHeight;
this.padding = padding || SyntaxElementMorph.prototype.scale; //1;
this.tableVersion = this.table.lastChanged;
// scroll bars:
this.hBar = null;
this.vBar = null;
// cached properties (do not persist):
this.rowLabelWidth = 0;
this.columns = []; // relative left positions
this.rows = 0;
// cached properties for scrolling and resizing (do not persist):
this.maxStartRow = null;
this.maxStartCol = null;
this.dragAnchor = null;
this.resizeAnchor = null;
this.resizeCol = null;
this.resizeRow = null;
// cached property for updating (don not persist):
this.wantsUpdate = false;
// initialize inherited properties:
// make sure not to draw anything just yet
// therefore omit FrameMorph's properties (not needed here)
// and only initialize properties inherited from Morph:
Morph.prototype.init.call(this, true);
// override inherited properites:
// this.fps = 3; // this will slow down the sliders (!)
if (extent) {this.silentSetExtent(extent); }
this.initScrollBars();
this.drawNew();
};
TableMorph.prototype.initScrollBars = function () {
var myself = this;
// horizontal scroll bar - scrolls columns
this.hBar = new SliderMorph(
1, // start
null, // stop
null, // value
null, // size
'horizontal'
);
this.hBar.setHeight(this.scrollBarSize);
this.hBar.action = function (num) {
myself.showData(num, null, true);
};
this.hBar.isDraggable = false;
this.add(this.hBar);
// vertical scroll bar - scrolls rows
this.vBar = new SliderMorph(
1, // start
null, // stop
null, // value
null, // size
'vertical'
);
this.vBar.setWidth(this.scrollBarSize);
this.vBar.action = function (num) {
myself.showData(null, num, true);
};
this.vBar.isDraggable = false;
this.add(this.vBar);
};
TableMorph.prototype.updateScrollBars = function () {
if (this.maxStartCol === 1) {
this.hBar.hide();
} else {
this.hBar.show();
this.hBar.stop = this.maxStartCol;
this.hBar.value = this.startCol;
this.hBar.size = Math.max(
this.hBar.rangeSize() * this.columns.length / this.table.cols(),
this.hBar.rangeSize() / 10
);
this.hBar.drawNew();
}
this.vBar.stop = this.maxStartRow;
this.vBar.value = this.startRow;
if (this.maxStartRow === 1) {
this.vBar.hide();
} else {
this.vBar.show();
this.vBar.size = Math.max(
this.vBar.rangeSize() * this.rows / this.table.rows(),
this.vBar.rangeSize() / 10
);
this.vBar.drawNew();
}
};
TableMorph.prototype.drawNew = function () {
var context, w, i;
this.image = newCanvas(this.extent());
context = this.image.getContext('2d');
context.fillStyle = 'rgb(220, 220, 220)';
BoxMorph.prototype.outlinePath.call(
this, context, SyntaxElementMorph.prototype.corner + 1, 0
);
context.fill();
// determine and cache layout information
this.rowLabelWidth = this.rowLabelsWidth();
this.columns = this.columnsLayout();
this.rows = this.visibleRows();
// optionally draw grid
if (this.highContrast && this.table.cols() > 1) {
w = this.padding;
for (i = this.startCol; i <= this.table.cols(); i += 1) {
w += (this.colWidth(i) + this.padding);
}
context.fillStyle = 'darkGray';
context.fillRect(
this.padding + this.rowLabelWidth,
this.padding + this.colLabelHeight,
w,
(this.rowHeight + this.padding) *
(this.table.rows() + 1 - this.startRow) +
this.padding
);
}
this.buildCells();
// fix scroll bars layout
this.hBar.setWidth(this.width() - this.vBar.width());
this.hBar.setLeft(this.left());
this.hBar.setBottom(this.bottom());
this.vBar.setHeight(this.height() - this.hBar.height());
this.vBar.setRight(this.right());
this.vBar.setTop(this.top());
};
TableMorph.prototype.buildCells = function () {
// also populate cells with the correct data and
// arrange the layout of cells all in one pass
var cell, r, c,
pos = this.position();
// delete all existing cells
this.children = [];
// create cells
for (c = 0; c <= this.columns.length; c += 1) {
for (r = 0; r <= this.rows; r += 1) {
cell = new TableCellMorph(
this.table.get(
!c ? c : c + this.startCol - 1,
!r ? r : r + this.startRow - 1
),
new Point(
!c ? this.rowLabelWidth
: this.colWidth(c + this.startCol - 1),
!r ? this.colLabelHeight : this.rowHeight
),
!(r && c), // isLabel
false // should be list
);
cell.setPosition(
new Point(
!c ? this.padding
: this.columns[c - 1],
!r ? this.padding :
this.padding * 2 + this.colLabelHeight +
((r - 1) * (this.rowHeight + this.padding))
).add(pos)
);
this.add(cell);
if (isSnapObject(cell.getData())) {
this.wantsUpdate = true;
}
}
}
this.add(this.hBar);
this.add(this.vBar);
this.updateScrollBars();
this.changed();
};
TableMorph.prototype.drawData = function (noScrollUpdate) {
// redraw all cells with their current data or label
var cell, cellIdx = 0, r, c;
for (c = 0; c <= this.columns.length; c += 1) {
for (r = 0; r <= this.rows; r += 1) {
cell = this.children[cellIdx];
cellIdx += 1;
cell.setData(
this.table.get(
!c ? c : c + this.startCol - 1,
!r ? r : r + this.startRow - 1
)
);
if (isSnapObject(cell.getData())) {
this.wantsUpdate = true;
}
}
}
if (!noScrollUpdate) {this.updateScrollBars(); }
this.changed();
};
// TableMorph scrolling
TableMorph.prototype.scroll = function (xSteps, ySteps) {
this.showData(
Math.min(
this.maxStartCol,
Math.max(1, this.startCol + Math.round(xSteps))
),
Math.min(
this.maxStartRow,
Math.max(1, this.startRow + Math.round(ySteps))
)
);
this.updateScrollBars();
};
TableMorph.prototype.showData = function (startCol, startRow, noScrollUpdate) {
var c = startCol || this.startCol,
r = startRow || this.startRow;
if (c === this.startCol) {
if (r === this.startRow) {return; } // no change
this.startRow = r;
this.rows = this.visibleRows();
this.drawData(noScrollUpdate);
} else {
this.startCol = c;
this.startRow = r;
this.rows = this.visibleRows();
if (this.colWidths.length) {
this.columns = this.columnsLayout();
this.buildCells();
} else {
this.drawData(noScrollUpdate);
}
}
};
// TableMorph stepping
TableMorph.prototype.step = function () {
if (this.dragAnchor) {
this.shiftCells(this.world().hand.position());
} else if (this.resizeAnchor) {
this.resizeCells(this.world().hand.position());
}
this.update();
};
TableMorph.prototype.update = function () {
var oldCols, oldRows,
version = this.table instanceof List ?
this.table.version(this.startRow, this.rows)
: this.table.lastChanged;
if (this.tableVersion === version && !this.wantsUpdate) {
return;
}
this.wantsUpdate = false;
if (this.table instanceof List) {
oldCols = this.columns.length;
oldRows = this.rows;
this.rowLabelWidth = this.rowLabelsWidth();
this.columns = this.columnsLayout();
this.rows = this.visibleRows();
if (this.columns.length !== oldCols || (this.rows !== oldRows)) {
this.buildCells();
} else {
this.drawData();
}
} else { // Table
this.drawData();
}
this.tableVersion = version;
};
// TableMorph layout helpers (all private):
TableMorph.prototype.rowLabelsWidth = function () {
var ctx = newCanvas().getContext('2d');
ctx.font = 'italic ' + SyntaxElementMorph.prototype.fontSize +
'px Helvetica, Arial, sans-serif';
return Math.max(
0,
Math.max.apply(
null,
this.table.columnNames().map(function (name) {
return name ? ctx.measureText(name).width : 0;
})
)
) || ctx.measureText(this.table.rows().toString()).width +
(6 * SyntaxElementMorph.prototype.scale);
};
TableMorph.prototype.columnsLayout = function () {
// determines and maxStartCol and
// modifies startCol if needed
var c = [],
x = this.padding * 2 + this.rowLabelWidth,
colNum,
w;
// determine maxStartCol
colNum = this.table.cols();
w = x;
while (w < this.width() && colNum > 0) {
w += this.colWidth(colNum);
colNum -= 1;
}
if (colNum === 0 && (w < this.width())) {
this.maxStartCol = 1;
} else {
this.maxStartCol = Math.min(colNum + 2, this.table.cols());
}
// determine the left position of every shown column
this.startCol = Math.min(this.startCol, this.maxStartCol);
colNum = this.startCol;
while (x < this.width() &&
(colNum < (this.table.cols() + this.startCol))
) {
w = this.colWidth(colNum);
c.push(x);
x += w;
x += this.padding;
colNum += 1;
}
return c;
};
TableMorph.prototype.colWidth = function (col) {
return this.colWidths[col - 1] || this.globalColWidth;
};
TableMorph.prototype.visibleRows = function () {
// determines maxStartRow and
// modifies startRow if needed
var rest = this.height() - this.colLabelHeight - this.padding,
possible;
if (rest < 0) {return 0; }
possible = Math.ceil(rest / (this.rowHeight + this.padding));
this.maxStartRow = Math.max(1, this.table.rows() - possible + 2);
this.startRow = Math.min(this.startRow, this.maxStartRow);
return Math.min(this.table.rows(), possible);
};
TableMorph.prototype.globalExtent = function () {
var i,
w = this.rowLabelsWidth() + 2,
cols = this.table.cols();
for (i = 0; i < cols; i += 1) {
w += this.colWidth(i + 1);
w += this.padding;
}
if (cols === 1) {
w += this.scrollBarSize;
w += this.padding * 2;
}
return new Point(
w + this.padding,
this.colLabelHeight + (this.padding * 2) +
((this.rowHeight + this.padding) * this.table.rows())
);
};
// TableMorph events:
TableMorph.prototype.mouseScroll = function (y, x) {
this.scroll(
-(+x * MorphicPreferences.mouseScrollAmount / 4),
-(+y * MorphicPreferences.mouseScrollAmount)
);
};
TableMorph.prototype.mouseDownLeft = function (pos) {
var rel = pos.subtract(this.position());
if (rel.x <= this.rowLabelWidth || (rel.y <= this.colLabelHeight)) {
// resize cells
if (this.world().currentKey === 16) { // shiftClicked
this.resizeCol = 0;
} else {
this.resizeCol = this.columnAt(rel.x);
}
this.resizeRow = (rel.y > (this.colLabelHeight));
this.resizeAnchor = pos;
} else {
// shift the viewed portion
this.resizeRow = null;
this.dragAnchor = pos;
}
};
TableMorph.prototype.mouseClickLeft = function (pos) {
this.dragAnchor = null;
this.resizeAnchor = null;
this.resizeRow = null;
};
TableMorph.prototype.mouseLeaveDragging = function (pos) {
this.dragAnchor = null;
this.resizeAnchor = null;
this.resizeRow = null;
};
TableMorph.prototype.mouseDoubleClick = function (pos) {
if (this.parentThatIsA(TableDialogMorph)) {
this.escalateEvent('mouseDoubleClick', pos);
} else {
new TableDialogMorph(
this.table,
this.globalColWidth,
this.colWidths,
this.rowHeight
).popUp(this.world());
}
};
// TableMorph scrolling and resizing cells by "hand"
TableMorph.prototype.shiftCells = function (pos) {
var delta = this.dragAnchor.subtract(pos),
scrollX = Math.round(delta.x / this.globalColWidth),
scrollY = Math.round(delta.y / this.rowHeight);
if (scrollX || scrollY) {
this.scroll(scrollX, scrollY);
this.dragAnchor = pos;
}
};
TableMorph.prototype.resizeCells = function (pos) {
var delta = pos.subtract(this.resizeAnchor),
i;
if (this.resizeCol) {
this.colWidths[this.resizeCol - 1] = Math.max(
16,
(this.colWidths[this.resizeCol - 1] || this.globalColWidth) +
delta.x
);
} else if (this.resizeRow) {
this.rowHeight = Math.max(16, this.rowHeight + delta.y);
} else {
this.globalColWidth = Math.max(16, this.globalColWidth + delta.x);
for (i = 0; i < this.colWidths.length; i += 1) {
if (this.colWidths[i]) {
this.colWidths[i] = Math.max(
16,
this.colWidths[i] + delta.x
);
}
}
}
if (this.highContrast) {
this.drawNew();
} else {
this.rowLabelWidth = this.rowLabelsWidth();
this.columns = this.columnsLayout();
this.rows = this.visibleRows();
this.buildCells();