/
scrollgrid.v0.2.1.js
2071 lines (1705 loc) · 69.5 KB
/
scrollgrid.v0.2.1.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
(function (context, library) {
"use strict";
if (typeof exports === "object") {
// CommonJS
module.exports = library(require('d3'));
} else {
if (typeof define === "function" && define.amd) {
// RequireJS | AMD
define(["d3"], function (d3) {
// publish scrollgrid to the global namespace for backwards compatibility
// and define it as an AMD module
context.Scrollgrid = library(d3);
return context.Scrollgrid;
});
} else {
// No AMD, expect d3 to exist in the current context and publish
// scrollgrid to the global namespace
if (!context.d3) {
if (console && console.warn) {
console.warn("scrollgrid requires d3 to run. Are you missing a reference to the d3 library?");
} else {
throw "scrollgrid requires d3 to run. Are you missing a reference to the d3 library?";
}
} else {
context.Scrollgrid = library(context.d3);
}
}
}
}(this, function (d3) {
"use strict";
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
var Scrollgrid = function (options) {
var int = this.internal,
sizes = int.sizes,
interaction = int.interaction,
render = int.render,
dom = int.dom,
virtual = sizes.virtual,
physical = sizes.physical;
options = options || {};
if (!options.target) {
int.raise('target is a required field, please provide a selector string for the container. ' +
'The selector string should be a d3/jquery selector identifying a single div. E.g. "#tableContainer" ' +
'where you have a div with id "tableContainer".');
} else if (d3.select(options.target).length !== 1) {
int.raise('target should be a d3/jquery selector identifying a single div. E.g. "#tableContainer" ' +
'where you have a div with id "tableContainer". Please check that your selector is matching 1 and only 1 div.');
} else {
// Set the display options
physical.rowHeight = options.rowHeight || 30;
physical.dragHandleWidth = options.dragHandleWidth || 8;
physical.headerRowHeight = options.headerRowHeight || physical.rowHeight;
physical.footerRowHeight = options.footerRowHeight || physical.rowHeight;
physical.defaultColumnWidth = options.defaultColumnWidth || 100;
physical.cellPadding = options.cellPadding || 6;
// Set the interaction options
interaction.allowColumnResizing = options.allowColumnResizing || true;
interaction.allowSorting = options.allowSorting || true;
// Set the number of header or footer rows or columns
virtual.top = options.headerRows || 0;
virtual.bottom = options.footerRows || 0;
virtual.left = options.headerColumns || 0;
virtual.right = options.footerColumns || 0;
// Set a reference to the parent object
this.target = options.target;
render.setDefaultStyles.call(this);
render.formatRules = options.formatRules || [];
render.cellWaitText = options.cellWaitText || "loading...";
render.sortIconSize = options.sortIconSize || 7;
// Create the DOM shapes required
dom.populateDOM.call(this);
// Pass the data or adapter through to setData
this.data(options.data || options.adapter);
if (options.autoResize) {
dom.setAutoResize.call(this);
}
}
};
Scrollgrid.init = function (target, options) {
options = options || {};
options.target = target;
var scrollgrid = new Scrollgrid(options);
return scrollgrid;
};
// Build namespaces
Scrollgrid.adapters = {};
Scrollgrid.prototype.internal = {
eventHandlers: [],
sizes: {
virtual: {},
physical: {}
},
events: {},
interaction: {},
dom: {},
render: {}
};
return Scrollgrid;
}));
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/dom/getTopMargin.js
Scrollgrid.prototype.internal.dom.getTopMargin = function (containerSize, parent) {
"use strict";
var int = this.internal,
sizes = int.sizes,
physical = sizes.physical,
topMargin = 0,
parentHeight;
if (containerSize && containerSize.height && parent) {
parentHeight = parent.node().offsetHeight;
if (physical.verticalAlignment === 'middle') {
topMargin = ((parentHeight - containerSize.height) / 2);
} else if (physical.verticalAlignment === 'bottom') {
topMargin = parentHeight - containerSize.height - 1;
}
}
return topMargin;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/dom/layoutDOM.js
Scrollgrid.prototype.internal.dom.layoutDOM = function (fixedSize) {
"use strict";
var self = this,
int = self.internal,
dom = int.dom,
sizes = int.sizes,
render = int.render,
physical = sizes.physical,
topMargin;
// This is required so content can size relative to it
dom.parent
.style('position', 'relative');
topMargin = dom.getTopMargin.call(self, fixedSize, dom.parent);
dom.container
.style('position', 'relative')
.style('width', (fixedSize && fixedSize.width ? fixedSize.width + 'px' : '100%'))
.style('height', (fixedSize && fixedSize.height ? (fixedSize.height) + 'px' : '100%'))
.style('padding-top', topMargin + 'px')
.style('font-size', 0);
// If the fixed size is too great, reset to 100%, this gives the effect of
// pinning the edges when they reach the limit of available space
if (dom.container.node().offsetWidth > dom.parent.node().offsetWidth) {
dom.container.style('width', '100%');
}
if (dom.container.node().offsetHeight > dom.parent.node().offsetHeight) {
dom.container
.style('margin-top', '0px')
.style('height', '100%');
}
// Set the physical dimensions of the various data elements in memory
sizes.calculatePhysicalBounds.call(self, topMargin);
// Set all panels
dom.setAbsolutePosition.call(self, dom.left.svg, 0, physical.top + topMargin, physical.left, physical.visibleInnerHeight);
dom.setRelativePosition.call(self, dom.top.svg, physical.left, physical.visibleInnerWidth, physical.top, 'hidden');
dom.setRelativePosition.call(self, dom.main.viewport, physical.left, physical.visibleInnerWidth, physical.visibleInnerHeight, 'auto');
dom.setAbsolutePosition.call(self, dom.right.svg, physical.left + physical.visibleInnerWidth, physical.top + topMargin, physical.right, physical.visibleInnerHeight);
dom.setRelativePosition.call(self, dom.bottom.svg, physical.left, physical.visibleInnerWidth, physical.bottom, 'hidden');
dom.setAbsolutePosition.call(self, dom.top.left.svg, 0, topMargin, physical.left + physical.dragHandleWidth / 2, physical.top);
dom.setAbsolutePosition.call(self, dom.top.right.svg, physical.left + physical.visibleInnerWidth - physical.dragHandleWidth / 2, topMargin, physical.right + physical.dragHandleWidth / 2, physical.top);
dom.setAbsolutePosition.call(self, dom.bottom.left.svg, 0, physical.top + physical.visibleInnerHeight + topMargin, physical.left, physical.bottom);
dom.setAbsolutePosition.call(self, dom.bottom.right.svg, physical.left + physical.visibleInnerWidth, physical.top + physical.visibleInnerHeight + topMargin, physical.right, physical.bottom);
dom.setAbsolutePosition.call(self, dom.main.svg, physical.left, physical.top + topMargin, physical.visibleInnerWidth, physical.visibleInnerHeight);
// Style all panels
dom.stylePanels.call(this, this.style);
// Top right panel needs a small offset for the handle
dom.top.right.transform.attr('transform', 'translate(' + physical.dragHandleWidth / 2 + ', 0)');
// Invoke draw on scroll
dom.main.viewport.on('scroll', function () { render.draw.call(self, false); });
// Invoke eventHandlers of the target group behind the main viewport
dom.redirectViewportEvents.call(self);
// Set the scrollable area
dom.setScrollerSize.call(self);
// Get the scroll bar bounds
physical.verticalScrollbarWidth = dom.main.viewport.node().offsetWidth - dom.main.viewport.node().clientWidth;
physical.horizontalScrollbarHeight = dom.main.viewport.node().offsetHeight - dom.main.viewport.node().clientHeight;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/dom/populateDOM.js
Scrollgrid.prototype.internal.dom.populateDOM = function () {
"use strict";
var int = this.internal,
dom = int.dom;
// Get the parent container
dom.parent = d3.select(this.target);
// Add a container to the target which will house everything
dom.container = dom.parent.append('div');
// Populate the 5 regions of the control
dom.left = dom.populatePanel.call(this);
dom.top = dom.populatePanel.call(this);
dom.top.left = dom.populatePanel.call(this);
dom.top.right = dom.populatePanel.call(this);
dom.main = dom.populatePanel.call(this);
// Add the viewport which is the fixed area with scroll bars
dom.main.viewport = dom.container.append('div');
dom.right = dom.populatePanel.call(this);
dom.bottom = dom.populatePanel.call(this);
dom.bottom.left = dom.populatePanel.call(this);
dom.bottom.right = dom.populatePanel.call(this);
// The scroller is going to be as large as the virtual size of
// the data (as if it had all been rendered) this is so that
// the scroll bars behave as expected
dom.main.scroller = dom.main.viewport.append('div');
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/dom/populatePanel.js
Scrollgrid.prototype.internal.dom.populatePanel = function () {
"use strict";
var dom = this.internal.dom,
panel = {};
panel.svg = dom.container.append('svg');
panel.transform = panel.svg.append('g');
panel.content = panel.transform.append('g');
return panel;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/dom/redirectViewportEvents.js
Scrollgrid.prototype.internal.dom.redirectViewportEvents = function () {
"use strict";
var self = this,
int = self.internal,
dom = int.dom,
viewport = dom.main.viewport,
eventHandlers = int.eventHandlers,
n = eventHandlers.length,
j,
eventHandler,
getRedirectHandler;
getRedirectHandler = function (dom, eventType) {
return function () {
var mouse,
svg,
rpos,
list,
target,
g,
targetEventHandler;
mouse = d3.mouse(this);
svg = dom.main.svg.node();
rpos = svg.createSVGRect();
rpos.x = mouse[0];
rpos.y = mouse[1];
rpos.width = 1;
rpos.height = 1;
list = svg.getIntersectionList(rpos, null);
if (list.length) {
target = list[0].parentNode;
g = d3.select(target);
targetEventHandler = g.on(eventType);
if (targetEventHandler) {
targetEventHandler.call(target, g.datum());
}
}
};
};
for (j = 0; j < n; j += 1) {
eventHandler = eventHandlers[j];
viewport.on(eventHandler.type, getRedirectHandler(dom, eventHandler.type));
}
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/dom/setAbsolutePosition.js
Scrollgrid.prototype.internal.dom.setAbsolutePosition = function (element, x, y, width, height) {
"use strict";
return element
.style('position', 'absolute')
.style('overflow', 'hidden')
.style('left', x + 'px')
.style('top', y + 'px')
.style('width', width + 'px')
.style('height', height + 'px');
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/dom/setAutoResize.js
Scrollgrid.prototype.internal.dom.setAutoResize = function () {
"use strict";
// Pick up any existing resize handlers
var self = this,
existingHandler = window.onresize;
// Add a new handler
window.onresize = function () {
// Invoke the non-scrollgrid resize handlers
if (existingHandler) {
existingHandler();
}
// Call the instantiated layout refresh
self.refresh(true);
};
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/dom/setRelativePosition.js
Scrollgrid.prototype.internal.dom.setRelativePosition = function (element, x, width, height, overflow) {
"use strict";
return element
.style('overflow', overflow)
.style('position', 'relative')
.style('margin-left', x + 'px')
.style('width', width + 'px')
.style('height', height + 'px');
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/dom/setScrollerSize.js
Scrollgrid.prototype.internal.dom.setScrollerSize = function () {
"use strict";
var int = this.internal,
dom = int.dom,
sizes = int.sizes,
physical = sizes.physical;
dom.main.scroller
.style('width', physical.totalInnerWidth + 'px')
.style('height', physical.totalInnerHeight + 'px');
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/dom/stylePanels.js
Scrollgrid.prototype.internal.dom.stylePanels = function (style) {
"use strict";
var int = this.internal,
dom = int.dom;
this.style = style || this.style;
dom.left.svg.attr('class', this.style.left.panel);
dom.top.svg.attr('class', this.style.top.panel);
dom.right.svg.attr('class', this.style.right.panel);
dom.bottom.svg.attr('class', this.style.bottom.panel);
dom.top.left.svg.attr('class', this.style.top.left.panel);
dom.top.right.svg.attr('class', this.style.top.right.panel);
dom.bottom.left.svg.attr('class', this.style.bottom.left.panel);
dom.bottom.right.svg.attr('class', this.style.bottom.right.panel);
dom.main.svg.attr('class', this.style.main.panel);
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/render/renderBackground.js
Scrollgrid.prototype.internal.events.addEventHandlers = function (g, viewData) {
"use strict";
var self = this,
int = self.internal,
eventHandlers = int.eventHandlers,
n = eventHandlers.length,
i,
eventHandler;
g.attr("data-row", viewData.rowIndex)
.attr("data-col", viewData.columnIndex);
for (i = 0; i < n; i += 1) {
eventHandler = eventHandlers[i];
g.on(eventHandler.type, eventHandler.listener, eventHandler.capture);
}
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/interaction/addResizeHandles.js
Scrollgrid.prototype.internal.interaction.addResizeHandles = function (target, bounds, startX) {
"use strict";
var self = this,
int = self.internal,
style = self.style,
sizes = int.sizes,
interaction = int.interaction,
physical = sizes.physical,
runningTotal = startX || 0;
target.content
.selectAll(".sg-no-style--handle-selector")
.remove();
target.content
.selectAll(".sg-no-style--handle-selector")
.data(this.columns.slice(bounds.left, bounds.right))
.enter()
.append("rect")
.attr("class", "sg-no-style--handle-selector " + style.resizeHandle)
.attr("transform", "translate(" + (-1 * physical.dragHandleWidth / 2) + ", 0)")
.attr("x", function (c) {
runningTotal += c.width;
c.x = runningTotal;
return c.x;
})
.attr("y", 0)
.attr("width", physical.dragHandleWidth)
.attr("height", physical.top)
.on("dblclick", function (c) { interaction.autoResizeColumn.call(self, c); })
.call(interaction.getColumnResizer.call(self));
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/interaction/addSortButtons.js
Scrollgrid.prototype.internal.interaction.addSortButtons = function (g, viewData) {
"use strict";
var self = this,
int = self.internal,
interaction = int.interaction;
g.append("rect")
.attr("width", viewData.boxWidth)
.attr("height", viewData.boxHeight)
.style("opacity", 0)
.style("cursor", "pointer")
.on("click", function () { return interaction.sortColumn.call(self, viewData.columnIndex, true); });
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/interaction/autoResizeColumn.js
Scrollgrid.prototype.internal.interaction.autoResizeColumn = function (column) {
"use strict";
var int = this.internal,
dom = int.dom,
sizes = int.sizes,
panels = [dom.top.left, dom.top, dom.top.right, dom.left, dom.main, dom.right, dom.bottom.left, dom.bottom, dom.bottom.right],
i;
// Do not allow the width to be less than 0
column.width = 0;
// Get the widest from the various panels (some panels may not apply to the given cell but those panels will return zero anyway)
for (i = 0; i < panels.length; i += 1) {
column.width = Math.max(column.width, sizes.getExistingTextBound.call(this, panels[i].svg, column.index).width);
}
// Update the container size because the width will have changed
this.refresh(true);
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/interaction/columnResizeEnd.js
Scrollgrid.prototype.internal.interaction.columnResizeEnd = function (shape) {
"use strict";
shape.classed('dragging', false);
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/interaction/columnResizeStart.js
Scrollgrid.prototype.internal.interaction.columnResizeStart = function (shape) {
"use strict";
d3.event.sourceEvent.stopPropagation();
shape.classed('dragging', true);
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/interaction/columnResizing.js
Scrollgrid.prototype.internal.interaction.columnResizing = function (shape, column) {
"use strict";
// Some resize handle should be inverted
column.width -= column.x - d3.event.x;
// Update the x coordinate for the drag
column.x = d3.event.x;
// If the column width is below 0 reset it, negative widths cause problems
if (column.width < 0) {
column.width = 0;
}
// Move the drag handle itself
shape.attr('x', column.x);
// Redraw
this.refresh(true);
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/interaction/defaultComparer.js
Scrollgrid.prototype.internal.interaction.defaultComparer = function (a, b) {
"use strict";
var order;
if (isNaN(a) || isNaN(b)) {
order = (new Date(a)) - (new Date(b));
} else {
order = parseFloat(a) - parseFloat(b);
}
if (isNaN(order)) {
order = (a < b ? -1 : (a > b ? 1 : 0));
}
return order;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/interaction/getColumnResizer.js
Scrollgrid.prototype.internal.interaction.getColumnResizer = function () {
"use strict";
var int = this.internal,
interaction = int.interaction,
self = this;
return d3.behavior.drag()
.origin(function (c) { return c; })
.on('dragstart', function () { interaction.columnResizeStart.call(self, d3.select(this)); })
.on('drag', function (c) { interaction.columnResizing.call(self, d3.select(this), c); })
.on('dragend', function () { interaction.columnResizeEnd.call(self, d3.select(this)); });
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/interaction/sortColumn.js
Scrollgrid.prototype.internal.interaction.sortColumn = function (index, toggle) {
"use strict";
var int = this.internal,
interaction = int.interaction,
sizes = int.sizes,
virtual = sizes.virtual,
c;
// Clear existing sorts and set the new one
for (c = 0; c < this.columns.length; c += 1) {
if (c !== index) {
delete this.columns[c].sort;
} else if (toggle) {
this.columns[c].sort = (this.columns[c].sort === 'desc' ? 'asc' : 'desc');
}
}
// Instruct the adapter to perform a sort
this.adapter.sort(index, virtual.top, virtual.bottom, this.columns[index].sort === 'desc', this.columns[index].compareFunction || interaction.defaultComparer);
this.refresh(false);
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/raise.js
Scrollgrid.prototype.internal.raise = function (err) {
"use strict";
var log = this.reporter || console;
if (log && log.error) {
log.error(err);
} else {
throw err;
}
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/render/applyRules.js
Scrollgrid.prototype.internal.render.applyRules = function (data) {
"use strict";
var int = this.internal,
render = int.render,
sizes = int.sizes,
virtual = sizes.virtual,
rule,
key,
ruleDefinition = {},
i,
k,
r,
c;
if (render.formatRules) {
// Iterate the focus data
for (i = 0; i < data.length; i += 1) {
ruleDefinition = {};
// Rules use 1 based indices for rows and columns, this is because they use negative
// notation to refer to elements from the end e.g. row: -1 = last row. This would be
// inconsistent if 0 was the first row.
r = data[i].rowIndex + 1;
c = data[i].columnIndex + 1;
for (k = 0; k < render.formatRules.length; k += 1) {
rule = render.formatRules[k];
if (render.matchRule.call(this, rule.row, r, virtual.outerHeight) && render.matchRule.call(this, rule.column, c, virtual.outerWidth)) {
// Iterate the rule properties and apply them to the object
for (key in rule) {
if (rule.hasOwnProperty(key) && key !== "row" && key !== "column") {
ruleDefinition[key] = rule[key];
}
}
}
}
// Apply the combined rules
if (ruleDefinition.formatter) {
data[i].formatter = ruleDefinition.formatter;
}
if (ruleDefinition.alignment) {
data[i].alignment = ruleDefinition.alignment;
}
if (ruleDefinition.cellPadding) {
data[i].cellPadding = ruleDefinition.cellPadding;
}
if (ruleDefinition.backgroundStyle) {
data[i].backgroundStyle += " " + ruleDefinition.backgroundStyle;
}
if (ruleDefinition.foregroundStyle) {
data[i].foregroundStyle += " " + ruleDefinition.foregroundStyle;
}
if (ruleDefinition.renderBackground) {
data[i].renderBackground = ruleDefinition.renderBackground;
}
if (ruleDefinition.renderBetween) {
data[i].renderBetween = ruleDefinition.renderBetween;
}
if (ruleDefinition.renderForeground) {
data[i].renderForeground = ruleDefinition.renderForeground;
}
}
}
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/render/calculateCellAdjustments.js
Scrollgrid.prototype.internal.render.calculateCellAdjustments = function (row, column) {
"use strict";
var int = this.internal,
sizes = int.sizes,
virtual = sizes.virtual,
physical = sizes.physical,
extension = {
x: 0,
y: 0,
boxHeight: 0,
boxWidth: 0,
textHeight: 0,
textWidth: 0
};
// If the cell is a columns header or footer and the column is the last in the dataset we need to extend the width
// to remove the gap for the scrollbar
if ((row < virtual.top || row >= virtual.outerHeight - virtual.bottom) && column === virtual.outerWidth - virtual.right - 1) {
extension.boxWidth += physical.verticalScrollbarWidth;
}
// If the cell is a row header or footer and the row is the last in the dataset we need to extend the height to
// remove the gap for the scrollbar
if ((column < virtual.left || column >= virtual.outerWidth - virtual.right) && row === virtual.outerHeight - virtual.bottom - 1) {
extension.boxHeight += physical.horizontalScrollbarHeight;
}
// If the cell is the last column header reduce height by 1 to show the bottom gridline
if (row === virtual.top - 1) {
extension.boxHeight -= 1;
}
// If the cell is the first row after a column header and there is a column header extend it up to hide the top line
if (row === virtual.top && row > 0) {
extension.boxHeight += 1;
extension.y -= 1;
}
// If the cell is the last row header reduce width by 1 to show the right gridline
if (column === virtual.left - 1) {
extension.boxWidth -= 1;
}
// If the cell is the first column after a row header and there is a row header extend it left to hide the top line
if (column === virtual.left && column > 0) {
extension.boxWidth += 1;
extension.x -= 1;
}
// If the cell is in the last row shrink it to show the bottom line
if (row === virtual.outerHeight - 1) {
extension.boxHeight -= 1;
}
// If the cell is in the last column shrink it to show the right line
if (column === virtual.outerWidth - 1) {
extension.boxWidth -= 1;
}
// If the cell is in the last row of the column headers and the column is being sorted
if (row === virtual.top - 1) {
// Set the sort icon to that of the column
extension.sortIcon = (this.columns[column] ? this.columns[column].sort : undefined);
}
return extension;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/render/cropText.js
Scrollgrid.prototype.internal.render.cropText = function (textShape, width) {
"use strict";
var textWidth = textShape.node().getBBox().width,
text = textShape.text(),
avgChar = textWidth / text.length,
// Deliberately overestimate to start with and reduce toward target
chars = Math.ceil(width / avgChar) + 1;
// Store the unabbreviated text
textShape.datum().originalText = text;
// Handle cases where chars is < 0 (negative width) so it never enters while loop
if (chars <= 0) {
textShape.text("");
}
while (textWidth > width && chars >= 0) {
if (chars === 0) {
textShape.text("");
} else {
textShape.text(text.substring(0, chars));
}
textWidth = textShape.node().getBBox().width;
chars -= 1;
}
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/render/draw.js
Scrollgrid.prototype.internal.render.draw = function (clearCache) {
"use strict";
var int = this.internal,
render = int.render,
interaction = int.interaction,
sizes = int.sizes,
dom = int.dom,
virtual = sizes.virtual,
physical = sizes.physical,
physicalViewArea = render.getVisibleRegion.call(this),
viewArea = render.getDataBounds.call(this, physicalViewArea),
totalWidth,
totalHeight,
fixedSize = {},
p = viewArea.physical,
v = viewArea.virtual,
y = {
top: { top: 0, bottom: virtual.top },
middle: { top: virtual.top + v.top, bottom: virtual.top + v.bottom },
bottom: { top: virtual.outerHeight - virtual.bottom, bottom: virtual.outerHeight }
},
x = {
left: { left: 0, right: virtual.left },
middle: { left: virtual.left + v.left, right: virtual.left + v.right },
right: { left: virtual.outerWidth - virtual.right, right: virtual.outerWidth }
};
// Draw the separate regions
render.renderRegion.call(this, dom.top.left, {}, x.left, y.top, clearCache);
render.renderRegion.call(this, dom.top, { x: p.x }, x.middle, y.top, clearCache);
render.renderRegion.call(this, dom.top.right, {}, x.right, y.top, clearCache);
render.renderRegion.call(this, dom.left, { y: p.y }, x.left, y.middle, clearCache);
render.renderRegion.call(this, dom.main, { x: p.x, y: p.y }, x.middle, y.middle, clearCache);
render.renderRegion.call(this, dom.right, { y: p.y }, x.right, y.middle, clearCache);
render.renderRegion.call(this, dom.bottom.left, {}, x.left, y.bottom, clearCache);
render.renderRegion.call(this, dom.bottom, { x: p.x }, x.middle, y.bottom, clearCache);
render.renderRegion.call(this, dom.bottom.right, {}, x.right, y.bottom, clearCache);
// Add resize handles
if (interaction.allowColumnResizing) {
interaction.addResizeHandles.call(this, dom.top.left, x.left);
interaction.addResizeHandles.call(this, dom.top, x.middle, p.x);
interaction.addResizeHandles.call(this, dom.top.right, x.right);
}
// Calculate if the rendering means that the width of the
// whole table should change and layout accordingly
totalWidth = (physical.left + physical.totalInnerWidth + physical.right + physical.verticalScrollbarWidth);
fixedSize.width = (totalWidth < dom.parent.node().offsetWidth ? totalWidth : null);
totalHeight = (physical.top + physical.totalInnerHeight + physical.bottom + physical.horizontalScrollbarHeight);
fixedSize.height = (totalHeight < dom.parent.node().offsetHeight ? totalHeight : null);
dom.layoutDOM.call(this, fixedSize);
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/render/getDataBounds.js
Scrollgrid.prototype.internal.render.getDataBounds = function (physicalViewArea) {
"use strict";
var i,
int = this.internal,
cols = this.columns,
sizes = int.sizes,
virtual = sizes.virtual,
physical = sizes.physical,
runningX = 0,
columnWidth,
left,
right,
bounds = {
physical: {
x: 0,
y: 0
},
virtual: {
top: Math.max(Math.floor(virtual.innerHeight * (physicalViewArea.top / physical.totalInnerHeight) - 1), 0),
bottom: Math.min(Math.ceil(virtual.innerHeight * (physicalViewArea.bottom / physical.totalInnerHeight) + 1), virtual.innerHeight)
}
};
bounds.physical.y = bounds.virtual.top * physical.rowHeight - physicalViewArea.top;
for (i = 0; i < virtual.innerWidth; i += 1) {
columnWidth = cols[i + virtual.left].width;
if (left === undefined && (i === virtual.innerWidth - 1 || runningX + columnWidth > physicalViewArea.left)) {
left = i;
bounds.physical.x = runningX - physicalViewArea.left;
}
if (right === undefined && (i === virtual.innerWidth - 1 || runningX + columnWidth > physicalViewArea.right)) {
right = i + 1;
break;
}
runningX += columnWidth;
}
bounds.virtual.left = Math.max(Math.floor(left), 0);
bounds.virtual.right = Math.min(Math.ceil(right + 1), virtual.innerWidth);
return bounds;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/scrollgrid/blob/master/MIT-LICENSE.txt"
// Source: /src/internal/render/getDataInBounds.js
Scrollgrid.prototype.internal.render.getDataInBounds = function (viewArea) {
"use strict";
var i, r, c, x, vc, vr = 0,
int = this.internal,
sizes = int.sizes,
render = int.render,
physical = sizes.physical,
cols = this.columns,
column,
runningX,
runningY,
rowHeight = 0,
visibleData = [],
adjustments;
runningY = viewArea.startY;
for (r = viewArea.top || 0, i = 0; r < viewArea.bottom || 0; r += 1) {
rowHeight = physical.getRowHeight.call(this, r);
runningX = viewArea.startX || 0;
vc = 0;
for (c = viewArea.left || 0; c < viewArea.right || 0; c += 1, i += 1) {
// Get any measurement modifiers based on cell position
adjustments = render.calculateCellAdjustments.call(this, r, c);
// Get the column definition
column = cols[c];
// Get the x position of the cell
x = Math.floor(runningX) + adjustments.x + 0.5;
// Using direct assignment for speed
visibleData[i] = {
x: x,
y: Math.floor(runningY) + adjustments.y + 0.5,
visibleRow: vr,
visibleColumn: vc,
boxWidth: Math.ceil(column.width) + adjustments.boxWidth,
boxHeight: Math.ceil(rowHeight) + adjustments.boxHeight,
textWidth: Math.ceil(column.width) + adjustments.textWidth,
textHeight: Math.ceil(rowHeight) + adjustments.textHeight,
backgroundStyle: this.style.cellBackgroundPrefix + 'r' + (r + 1) + ' ' + this.style.cellBackgroundPrefix + 'c' + (c + 1),
foregroundStyle: this.style.cellForegroundPrefix + 'r' + (r + 1) + ' ' + this.style.cellForegroundPrefix + 'c' + (c + 1),
sortIcon: adjustments.sortIcon || 'none',
cellPadding: physical.cellPadding,
alignment: 'left',
rowIndex: r,
columnIndex: c,
column: column,
formatter: null,
renderForeground: render.renderForeground,
renderBetween: null,
renderBackground: render.renderBackground
};
// We abuse the key here, cells will be rendered on enter only, we therefore
// want to key by any value which should result in a redraw of a particular cell,
// this has huge performance benefits. The
visibleData[i].key = visibleData[i].columnIndex + '_' + visibleData[i].rowIndex + "_" + visibleData[i].boxHeight + "_" + visibleData[i].boxWidth + "_" + visibleData[i].sortIcon;
vc += 1;
runningX += column.width;
}
vr += 1;
runningY += rowHeight;
}
// Modify the data based on the user rules
render.applyRules.call(this, visibleData);
return visibleData;