react-virtualized
Version:
React components for efficiently rendering large, scrollable lists and tabular data
162 lines (120 loc) • 4.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _Section = require('./Section');
var _Section2 = _interopRequireDefault(_Section);
var _types = require('./types');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var SECTION_SIZE = 100; /**
* 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.
*
*/
/**
* 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 ? arguments[0] : SECTION_SIZE;
(0, _classCallCheck3.default)(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.
*/
(0, _createClass3.default)(SectionManager, [{
key: 'getCellIndices',
value: function getCellIndices(_ref) {
var height = _ref.height,
width = _ref.width,
x = _ref.x,
y = _ref.y;
var indices = {};
this.getSections({ height: height, width: width, x: x, y: y }).forEach(function (section) {
return section.getCellIndices().forEach(function (index) {
indices[index] = index;
});
});
// Object keys are strings; this function returns numbers
return (0, _keys2.default)(indices).map(function (index) {
return indices[index];
});
}
/** Get size and position information for the cell specified. */
}, {
key: 'getCellMetadata',
value: function getCellMetadata(_ref2) {
var index = _ref2.index;
return this._cellMetadata[index];
}
/** Get all Sections overlapping the specified region. */
}, {
key: 'getSections',
value: function getSections(_ref3) {
var height = _ref3.height,
width = _ref3.width,
x = _ref3.x,
y = _ref3.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 (0, _keys2.default)(this._sections).length;
}
/** Intended for debugger/test purposes only */
}, {
key: 'toString',
value: function toString() {
var _this = this;
return (0, _keys2.default)(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(_ref4) {
var cellMetadatum = _ref4.cellMetadatum,
index = _ref4.index;
this._cellMetadata[index] = cellMetadatum;
this.getSections(cellMetadatum).forEach(function (section) {
return section.addCellIndex({ index: index });
});
}
}]);
return SectionManager;
}();
exports.default = SectionManager;