Skip to content

Commit 78cb5ec

Browse files
committed
moving functionality from plot.js to the data split file
1 parent 309c8bb commit 78cb5ec

File tree

2 files changed

+84
-71
lines changed

2 files changed

+84
-71
lines changed
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var extendFlat = require('../../lib/extend').extendFlat;
12+
13+
// pure functions, don't alter but passes on `gd` and parts of `trace` without deep copying
14+
15+
exports.splitToPanels = function(d) {
16+
var prevPages = [0, 0];
17+
var headerPanel = extendFlat({}, d, {
18+
key: 'header',
19+
type: 'header',
20+
page: 0,
21+
prevPages: prevPages,
22+
currentRepaint: [null, null],
23+
dragHandle: true,
24+
values: d.calcdata.headerCells.values[d.specIndex],
25+
rowBlocks: d.calcdata.headerRowBlocks,
26+
calcdata: extendFlat({}, d.calcdata, {cells: d.calcdata.headerCells})
27+
});
28+
var revolverPanel1 = extendFlat({}, d, {
29+
key: 'cells1',
30+
type: 'cells',
31+
page: 0,
32+
prevPages: prevPages,
33+
currentRepaint: [null, null],
34+
dragHandle: false,
35+
values: d.calcdata.cells.values[d.specIndex],
36+
rowBlocks: d.calcdata.rowBlocks
37+
});
38+
var revolverPanel2 = extendFlat({}, d, {
39+
key: 'cells2',
40+
type: 'cells',
41+
page: 1,
42+
prevPages: prevPages,
43+
currentRepaint: [null, null],
44+
dragHandle: false,
45+
values: d.calcdata.cells.values[d.specIndex],
46+
rowBlocks: d.calcdata.rowBlocks
47+
});
48+
// order due to SVG using painter's algo:
49+
return [revolverPanel1, revolverPanel2, headerPanel];
50+
};
51+
52+
exports.splitToCells = function(d) {
53+
var fromTo = rowFromTo(d);
54+
return d.values.slice(fromTo[0], fromTo[1]).map(function(v, i) {
55+
// By keeping identical key, a DOM node removal, creation and addition is spared, important when visible
56+
// grid has a lot of elements (quadratic with xcol/ycol count).
57+
// But it has to be busted when `svgUtil.convertToTspans` is used as it reshapes cell subtrees asynchronously,
58+
// and by that time the user may have scrolled away, resulting in stale overwrites. The real solution will be
59+
// to turn `svgUtil.convertToTspans` into a cancelable request, in which case no key busting is needed.
60+
var buster = (typeof v === 'string') && v.match(/[<$&> ]/) ? '_keybuster_' + Math.random() : '';
61+
return {
62+
// keyWithinBlock: /*fromTo[0] + */i, // optimized future version - no busting
63+
// keyWithinBlock: fromTo[0] + i, // initial always-unoptimized version - janky scrolling with 5+ columns
64+
keyWithinBlock: i + buster, // current compromise: regular content is very fast; async content is possible
65+
key: fromTo[0] + i,
66+
column: d,
67+
calcdata: d.calcdata,
68+
page: d.page,
69+
rowBlocks: d.rowBlocks,
70+
value: v
71+
};
72+
});
73+
};
74+
75+
function rowFromTo(d) {
76+
var rowBlock = d.rowBlocks[d.page];
77+
// fixme rowBlock truthiness check is due to ugly hack of placing 2nd panel as d.page = -1
78+
var rowFrom = rowBlock ? rowBlock.rows[0].rowIndex : 0;
79+
var rowTo = rowBlock ? rowFrom + rowBlock.rows.length : 0;
80+
return [rowFrom, rowTo];
81+
}

src/traces/table/plot.js

+3-71
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ var c = require('./constants');
1212
var d3 = require('d3');
1313
var gup = require('../../lib/gup');
1414
var Drawing = require('../../components/drawing');
15-
var extendFlat = require('../../lib/extend').extendFlat;
1615
var svgUtil = require('../../lib/svg_text_utils');
1716
var raiseToTop = require('../../lib').raiseToTop;
1817
var cancelEeaseColumn = require('../../lib').cancelTransition;
1918
var prepareData = require('./data_preparation_helper');
19+
var splitData = require('./data_split_helpers');
2020

2121
module.exports = function plot(gd, wrappedTraceHolders) {
2222

@@ -144,7 +144,7 @@ module.exports = function plot(gd, wrappedTraceHolders) {
144144
}
145145

146146
var columnBlock = yColumn.selectAll('.columnBlock')
147-
.data(splitToPanels, gup.keyFun);
147+
.data(splitData.splitToPanels, gup.keyFun);
148148

149149
columnBlock.enter()
150150
.append('g')
@@ -404,7 +404,7 @@ function renderColumnCells(columnBlock) {
404404
function renderColumnCell(columnCells) {
405405

406406
var columnCell = columnCells.selectAll('.columnCell')
407-
.data(splitToCells, function(d) {return d.keyWithinBlock;});
407+
.data(splitData.splitToCells, function(d) {return d.keyWithinBlock;});
408408

409409
columnCell.enter()
410410
.append('g')
@@ -601,74 +601,6 @@ function headerBlock(d) {return d.type === 'header';}
601601
* Revolver panel and cell contents layouting
602602
*/
603603

604-
function splitToPanels(d) {
605-
var prevPages = [0, 0];
606-
var headerPanel = extendFlat({}, d, {
607-
key: 'header',
608-
type: 'header',
609-
page: 0,
610-
prevPages: prevPages,
611-
currentRepaint: [null, null],
612-
dragHandle: true,
613-
values: d.calcdata.headerCells.values[d.specIndex],
614-
rowBlocks: d.calcdata.headerRowBlocks,
615-
calcdata: extendFlat({}, d.calcdata, {cells: d.calcdata.headerCells})
616-
});
617-
var revolverPanel1 = extendFlat({}, d, {
618-
key: 'cells1',
619-
type: 'cells',
620-
page: 0,
621-
prevPages: prevPages,
622-
currentRepaint: [null, null],
623-
dragHandle: false,
624-
values: d.calcdata.cells.values[d.specIndex],
625-
rowBlocks: d.calcdata.rowBlocks
626-
});
627-
var revolverPanel2 = extendFlat({}, d, {
628-
key: 'cells2',
629-
type: 'cells',
630-
page: 1,
631-
prevPages: prevPages,
632-
currentRepaint: [null, null],
633-
dragHandle: false,
634-
values: d.calcdata.cells.values[d.specIndex],
635-
rowBlocks: d.calcdata.rowBlocks
636-
});
637-
// order due to SVG using painter's algo:
638-
return [revolverPanel1, revolverPanel2, headerPanel];
639-
}
640-
641-
function splitToCells(d) {
642-
var fromTo = rowFromTo(d);
643-
return d.values.slice(fromTo[0], fromTo[1]).map(function(v, i) {
644-
// By keeping identical key, a DOM node removal, creation and addition is spared, important when visible
645-
// grid has a lot of elements (quadratic with xcol/ycol count).
646-
// But it has to be busted when `svgUtil.convertToTspans` is used as it reshapes cell subtrees asynchronously,
647-
// and by that time the user may have scrolled away, resulting in stale overwrites. The real solution will be
648-
// to turn `svgUtil.convertToTspans` into a cancelable request, in which case no key busting is needed.
649-
var buster = (typeof v === 'string') && v.match(/[<$&> ]/) ? '_keybuster_' + Math.random() : '';
650-
return {
651-
// keyWithinBlock: /*fromTo[0] + */i, // optimized future version - no busting
652-
// keyWithinBlock: fromTo[0] + i, // initial always-unoptimized version - janky scrolling with 5+ columns
653-
keyWithinBlock: i + buster, // current compromise: regular content is very fast; async content is possible
654-
key: fromTo[0] + i,
655-
column: d,
656-
calcdata: d.calcdata,
657-
page: d.page,
658-
rowBlocks: d.rowBlocks,
659-
value: v
660-
};
661-
});
662-
}
663-
664-
function rowFromTo(d) {
665-
var rowBlock = d.rowBlocks[d.page];
666-
// fixme rowBlock truthiness check is due to ugly hack of placing 2nd panel as d.page = -1
667-
var rowFrom = rowBlock ? rowBlock.rows[0].rowIndex : 0;
668-
var rowTo = rowBlock ? rowFrom + rowBlock.rows.length : 0;
669-
return [rowFrom, rowTo];
670-
}
671-
672604
function headerHeight(d) {
673605
var headerBlocks = d.rowBlocks[0].auxiliaryBlocks;
674606
return headerBlocks.reduce(function(p, n) {return p + rowsHeight(n, Infinity);}, 0);

0 commit comments

Comments
 (0)