forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectionManager.js
More file actions
154 lines (114 loc) · 6.85 KB
/
SectionManager.js
File metadata and controls
154 lines (114 loc) · 6.85 KB
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
* Window Sections are used to group nearby cells.
* This enables us to more quickly determine which cells to display in a given region of the Window.
*
*/
var _Section = require('./Section');
var _Section2 = _interopRequireDefault(_Section);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var SECTION_SIZE = 100;
/**
* Contains 0 to many Sections.
* Grows (and adds Sections) dynamically as cells are registered.
* Automatically adds cells to the appropriate Section(s).
*/
var SectionManager = function () {
function SectionManager() {
var sectionSize = arguments.length <= 0 || arguments[0] === undefined ? SECTION_SIZE : arguments[0];
_classCallCheck(this, SectionManager);
this._sectionSize = sectionSize;
this._cellMetadata = [];
this._sections = {};
}
/**
* Gets all cell indices contained in the specified region.
* A region may encompass 1 or more Sections.
*/
_createClass(SectionManager, [{
key: 'getCellIndices',
value: function getCellIndices(_ref) {
var height = _ref.height;
var width = _ref.width;
var x = _ref.x;
var y = _ref.y;
var indices = {};
this.getSections({ height: height, width: width, x: x, y: y }).forEach(function (section) {
return section.getCellIndices().forEach(function (index) {
return indices[index] = index;
});
});
// Object keys are strings; this function returns numbers
return Object.keys(indices).map(function (index) {
return indices[index];
});
}
/** Get size and position information for the cell specified. */
}, {
key: 'getCellMetadata',
value: function getCellMetadata(index) {
return this._cellMetadata[index];
}
/** Get all Sections overlapping the specified region. */
}, {
key: 'getSections',
value: function getSections(_ref2) {
var height = _ref2.height;
var width = _ref2.width;
var x = _ref2.x;
var y = _ref2.y;
var sectionXStart = Math.floor(x / this._sectionSize);
var sectionXStop = Math.floor((x + width - 1) / this._sectionSize);
var sectionYStart = Math.floor(y / this._sectionSize);
var sectionYStop = Math.floor((y + height - 1) / this._sectionSize);
var sections = [];
for (var sectionX = sectionXStart; sectionX <= sectionXStop; sectionX++) {
for (var sectionY = sectionYStart; sectionY <= sectionYStop; sectionY++) {
var key = sectionX + '.' + sectionY;
if (!this._sections[key]) {
this._sections[key] = new _Section2.default({
height: this._sectionSize,
width: this._sectionSize,
x: sectionX * this._sectionSize,
y: sectionY * this._sectionSize
});
}
sections.push(this._sections[key]);
}
}
return sections;
}
/** Total number of Sections based on the currently registered cells. */
}, {
key: 'getTotalSectionCount',
value: function getTotalSectionCount() {
return Object.keys(this._sections).length;
}
/** Intended for debugger/test purposes only */
}, {
key: 'toString',
value: function toString() {
var _this = this;
return Object.keys(this._sections).map(function (index) {
return _this._sections[index].toString();
});
}
/** Adds a cell to the appropriate Sections and registers it metadata for later retrievable. */
}, {
key: 'registerCell',
value: function registerCell(_ref3) {
var cellMetadatum = _ref3.cellMetadatum;
var index = _ref3.index;
this._cellMetadata[index] = cellMetadatum;
this.getSections(cellMetadatum).forEach(function (section) {
return section.addCellIndex(index);
});
}
}]);
return SectionManager;
}();
exports.default = SectionManager;