123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- 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; }; }();
- var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
- import BasePlugin from './../_base';
- import { registerPlugin } from './../../plugins';
- import { arrayEach } from './../../helpers/array';
- import freezeColumnItem from './contextMenuItem/freezeColumn';
- import unfreezeColumnItem from './contextMenuItem/unfreezeColumn';
- var privatePool = new WeakMap();
- /**
- * This plugin allows to manually "freeze" and "unfreeze" a column using an entry in the Context Menu.
- * You can turn it on by setting a `manualColumnFreeze` property to `true`.
- *
- * @plugin ManualColumnFreeze
- * @dependencies ManualColumnMove
- */
- var ManualColumnFreeze = function (_BasePlugin) {
- _inherits(ManualColumnFreeze, _BasePlugin);
- function ManualColumnFreeze(hotInstance) {
- _classCallCheck(this, ManualColumnFreeze);
- var _this = _possibleConstructorReturn(this, (ManualColumnFreeze.__proto__ || Object.getPrototypeOf(ManualColumnFreeze)).call(this, hotInstance));
- privatePool.set(_this, {
- moveByFreeze: false,
- afterFirstUse: false
- });
- /**
- * Original column positions
- *
- * @type {Array}
- */
- _this.frozenColumnsBasePositions = [];
- /**
- * Reference to the `ManualColumnMove` plugin.
- */
- _this.manualColumnMovePlugin = void 0;
- return _this;
- }
- /**
- * Check if the plugin is enabled in the Handsontable settings.
- *
- * @returns {Boolean}
- */
- _createClass(ManualColumnFreeze, [{
- key: 'isEnabled',
- value: function isEnabled() {
- return !!this.hot.getSettings().manualColumnFreeze;
- }
- /**
- * Enable plugin for this Handsontable instance.
- */
- }, {
- key: 'enablePlugin',
- value: function enablePlugin() {
- var _this2 = this;
- if (this.enabled) {
- return;
- }
- this.addHook('afterContextMenuDefaultOptions', function (options) {
- return _this2.addContextMenuEntry(options);
- });
- this.addHook('afterInit', function () {
- return _this2.onAfterInit();
- });
- this.addHook('beforeColumnMove', function (rows, target) {
- return _this2.onBeforeColumnMove(rows, target);
- });
- _get(ManualColumnFreeze.prototype.__proto__ || Object.getPrototypeOf(ManualColumnFreeze.prototype), 'enablePlugin', this).call(this);
- }
- /**
- * Disable plugin for this Handsontable instance.
- */
- }, {
- key: 'disablePlugin',
- value: function disablePlugin() {
- var priv = privatePool.get(this);
- priv.afterFirstUse = false;
- priv.moveByFreeze = false;
- _get(ManualColumnFreeze.prototype.__proto__ || Object.getPrototypeOf(ManualColumnFreeze.prototype), 'disablePlugin', this).call(this);
- }
- /**
- * Updates the plugin to use the latest options you have specified.
- */
- }, {
- key: 'updatePlugin',
- value: function updatePlugin() {
- this.disablePlugin();
- this.enablePlugin();
- _get(ManualColumnFreeze.prototype.__proto__ || Object.getPrototypeOf(ManualColumnFreeze.prototype), 'updatePlugin', this).call(this);
- }
- /**
- * Freeze the given column (add it to fixed columns).
- *
- * @param {Number} column Column index.
- */
- }, {
- key: 'freezeColumn',
- value: function freezeColumn(column) {
- var priv = privatePool.get(this);
- var settings = this.hot.getSettings();
- if (!priv.afterFirstUse) {
- priv.afterFirstUse = true;
- }
- if (settings.fixedColumnsLeft === this.hot.countCols() || column <= settings.fixedColumnsLeft - 1) {
- return; // already fixed
- }
- priv.moveByFreeze = true;
- if (column !== this.getMovePlugin().columnsMapper.getValueByIndex(column)) {
- this.frozenColumnsBasePositions[settings.fixedColumnsLeft] = column;
- }
- this.getMovePlugin().moveColumn(column, settings.fixedColumnsLeft++);
- }
- /**
- * Unfreeze the given column (remove it from fixed columns and bring to it's previous position).
- *
- * @param {Number} column Column index.
- */
- }, {
- key: 'unfreezeColumn',
- value: function unfreezeColumn(column) {
- var priv = privatePool.get(this);
- var settings = this.hot.getSettings();
- if (!priv.afterFirstUse) {
- priv.afterFirstUse = true;
- }
- if (settings.fixedColumnsLeft <= 0 || column > settings.fixedColumnsLeft - 1) {
- return; // not fixed
- }
- var returnCol = this.getBestColumnReturnPosition(column);
- priv.moveByFreeze = true;
- settings.fixedColumnsLeft--;
- this.getMovePlugin().moveColumn(column, returnCol + 1);
- }
- /**
- * Get the reference to the ManualColumnMove plugin.
- *
- * @private
- * @returns {Object}
- */
- }, {
- key: 'getMovePlugin',
- value: function getMovePlugin() {
- if (!this.manualColumnMovePlugin) {
- this.manualColumnMovePlugin = this.hot.getPlugin('manualColumnMove');
- }
- return this.manualColumnMovePlugin;
- }
- /**
- * Estimates the most fitting return position for unfrozen column.
- *
- * @private
- * @param {Number} column Column index.
- */
- }, {
- key: 'getBestColumnReturnPosition',
- value: function getBestColumnReturnPosition(column) {
- var movePlugin = this.getMovePlugin();
- var settings = this.hot.getSettings();
- var i = settings.fixedColumnsLeft;
- var j = movePlugin.columnsMapper.getValueByIndex(i);
- var initialCol = void 0;
- if (this.frozenColumnsBasePositions[column] == null) {
- initialCol = movePlugin.columnsMapper.getValueByIndex(column);
- while (j < initialCol) {
- i++;
- j = movePlugin.columnsMapper.getValueByIndex(i);
- }
- } else {
- initialCol = this.frozenColumnsBasePositions[column];
- this.frozenColumnsBasePositions[column] = void 0;
- while (j <= initialCol) {
- i++;
- j = movePlugin.columnsMapper.getValueByIndex(i);
- }
- i = j;
- }
- return i - 1;
- }
- /**
- * Add the manualColumnFreeze context menu entries.
- *
- * @private
- * @param {Object} options Context menu options.
- */
- }, {
- key: 'addContextMenuEntry',
- value: function addContextMenuEntry(options) {
- options.items.push({ name: '---------' }, freezeColumnItem(this), unfreezeColumnItem(this));
- }
- /**
- * Enabling `manualColumnMove` plugin on `afterInit` hook.
- *
- * @private
- */
- }, {
- key: 'onAfterInit',
- value: function onAfterInit() {
- if (!this.getMovePlugin().isEnabled()) {
- this.getMovePlugin().enablePlugin();
- }
- }
- /**
- * Prevent moving the rows from/to fixed area.
- *
- * @private
- * @param {Array} rows
- * @param {Number} target
- */
- }, {
- key: 'onBeforeColumnMove',
- value: function onBeforeColumnMove(rows, target) {
- var priv = privatePool.get(this);
- if (priv.afterFirstUse && !priv.moveByFreeze) {
- var frozenLen = this.hot.getSettings().fixedColumnsLeft;
- var disallowMoving = target < frozenLen;
- if (!disallowMoving) {
- arrayEach(rows, function (value, index, array) {
- if (value < frozenLen) {
- disallowMoving = true;
- return false;
- }
- });
- }
- if (disallowMoving) {
- return false;
- }
- }
- if (priv.moveByFreeze) {
- priv.moveByFreeze = false;
- }
- }
- /**
- * Destroy plugin instance.
- */
- }, {
- key: 'destroy',
- value: function destroy() {
- _get(ManualColumnFreeze.prototype.__proto__ || Object.getPrototypeOf(ManualColumnFreeze.prototype), 'destroy', this).call(this);
- }
- }]);
- return ManualColumnFreeze;
- }(BasePlugin);
- registerPlugin('manualColumnFreeze', ManualColumnFreeze);
- export default ManualColumnFreeze;
|