| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- import BasePlugin from '../_base';
- import { arrayEach } from '../../helpers/array';
- import { rangeEach } from '../../helpers/number';
- import { registerPlugin } from '../../plugins';
- import { mixin } from '../../helpers/object';
- import arrayMapper from '../../mixins/arrayMapper';
- class Mapper {
- constructor(shieldRows) {
- this.shieldRows = shieldRows;
- }
- createMap(length) {
- let rowOffset = 0;
- const originLength = length ? this._arrayMap.length : length;
- this._arrayMap.length = 0;
- rangeEach(originLength - 1, (itemIndex) => {
- if (this.shieldRows.isShield(itemIndex)) {
- rowOffset += 1;
- } else {
- this._arrayMap[itemIndex - rowOffset] = itemIndex;
- }
- });
- }
- destroy() {
- this._arrayMap = null;
- }
- }
- mixin(Mapper, arrayMapper);
- class TrimRows extends BasePlugin {
- constructor(hotInstance) {
- super(hotInstance);
- this.trimmedRows = [];
- this.removedRows = [];
- this.rowsMapper = new Mapper(this);
- }
- isEnabled() {
- return !!this.hot.getSettings().trimRows;
- }
- enablePlugin() {
- if (this.enabled) {
- return;
- }
- const settings = this.hot.getSettings().trimRows;
- if (Array.isArray(settings)) {
- this.trimmedRows = settings;
- }
- this.rowsMapper.createMap(this.hot.countSourceRows());
- this.addHook('modifyRow', (row, source) => this.onModifyRow(row, source));
- this.addHook('unmodifyRow', (row, source) => this.onUnmodifyRow(row, source));
- this.addHook('beforeCreateRow', (index, amount, source) => this.onBeforeCreateRow(index, amount, source));
- this.addHook('afterCreateRow', (index, amount) => this.onAfterCreateRow(index, amount));
- this.addHook('beforeRemoveRow', (index, amount) => this.onBeforeRemoveRow(index, amount));
- this.addHook('afterRemoveRow', () => this.onAfterRemoveRow());
- this.addHook('afterLoadData', firstRun => this.onAfterLoadData(firstRun));
- super.enablePlugin();
- }
- updatePlugin() {
- const settings = this.hot.getSettings().trimRows;
- if (Array.isArray(settings)) {
- this.disablePlugin();
- this.enablePlugin();
- }
- super.updatePlugin();
- }
- disablePlugin() {
- this.trimmedRows = [];
- this.removedRows.length = 0;
- this.rowsMapper.clearMap();
- super.disablePlugin();
- }
- trimRows(rows) {
- arrayEach(rows, (row) => {
- const physicalRow = parseInt(row, 10);
- if (!this.isShield(physicalRow)) {
- this.trimmedRows.push(physicalRow);
- }
- });
- this.hot.runHooks('skipLengthCache', 100);
- this.rowsMapper.createMap(this.hot.countSourceRows());
- this.hot.runHooks('afterTrimRow', rows);
- }
- trimRow(...row) {
- this.trimRows(row);
- }
- untrimRows(rows) {
- arrayEach(rows, (row) => {
- const physicalRow = parseInt(row, 10);
- if (this.isShield(physicalRow)) {
- this.trimmedRows.splice(this.trimmedRows.indexOf(physicalRow), 1);
- }
- });
- this.hot.runHooks('skipLengthCache', 100);
- this.rowsMapper.createMap(this.hot.countSourceRows());
- this.hot.runHooks('afterUntrimRow', rows);
- }
- untrimRow(...row) {
- this.untrimRows(row);
- }
- isShield(row) {
- return this.trimmedRows.indexOf(row) > -1;
- }
- untrimAll() {
- this.untrimRows([].concat(this.trimmedRows));
- }
- onModifyRow(row, source) {
- let physicalRow = row;
- if (source !== this.pluginName) {
- physicalRow = this.rowsMapper.getValueByIndex(physicalRow);
- }
- return physicalRow;
- }
- onUnmodifyRow(row, source) {
- let visualRow = row;
- if (source !== this.pluginName) {
- visualRow = this.rowsMapper.getIndexByValue(visualRow);
- }
- return visualRow;
- }
- onBeforeCreateRow(index, amount, source) {
- return !(this.isEnabled() && this.trimmedRows.length > 0 && source === 'auto');
- }
- onAfterCreateRow(index, amount) {
- this.rowsMapper.shiftItems(index, amount);
- }
- onBeforeRemoveRow(index, amount) {
- this.removedRows.length = 0;
- if (index !== false) {
- // Collect physical row index.
- rangeEach(index, index + amount - 1, (removedIndex) => {
- this.removedRows.push(this.hot.runHooks('modifyRow', removedIndex, this.pluginName));
- });
- }
- }
- onAfterRemoveRow() {
- this.rowsMapper.unshiftItems(this.removedRows);
- }
- onAfterLoadData(firstRun) {
- if (!firstRun) {
- this.rowsMapper.createMap(this.hot.countSourceRows());
- }
- }
- destroy() {
- this.rowsMapper.destroy();
- super.destroy();
- }
- }
- registerPlugin('trimRows', TrimRows);
- export default TrimRows;
|