manualColumnFreeze.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. 'use strict';
  2. exports.__esModule = true;
  3. 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; }; }();
  4. 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); } };
  5. var _base = require('./../_base');
  6. var _base2 = _interopRequireDefault(_base);
  7. var _plugins = require('./../../plugins');
  8. var _array = require('./../../helpers/array');
  9. var _freezeColumn = require('./contextMenuItem/freezeColumn');
  10. var _freezeColumn2 = _interopRequireDefault(_freezeColumn);
  11. var _unfreezeColumn = require('./contextMenuItem/unfreezeColumn');
  12. var _unfreezeColumn2 = _interopRequireDefault(_unfreezeColumn);
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  15. 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; }
  16. 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; }
  17. var privatePool = new WeakMap();
  18. /**
  19. * This plugin allows to manually "freeze" and "unfreeze" a column using an entry in the Context Menu.
  20. * You can turn it on by setting a `manualColumnFreeze` property to `true`.
  21. *
  22. * @plugin ManualColumnFreeze
  23. * @dependencies ManualColumnMove
  24. */
  25. var ManualColumnFreeze = function (_BasePlugin) {
  26. _inherits(ManualColumnFreeze, _BasePlugin);
  27. function ManualColumnFreeze(hotInstance) {
  28. _classCallCheck(this, ManualColumnFreeze);
  29. var _this = _possibleConstructorReturn(this, (ManualColumnFreeze.__proto__ || Object.getPrototypeOf(ManualColumnFreeze)).call(this, hotInstance));
  30. privatePool.set(_this, {
  31. moveByFreeze: false,
  32. afterFirstUse: false
  33. });
  34. /**
  35. * Original column positions
  36. *
  37. * @type {Array}
  38. */
  39. _this.frozenColumnsBasePositions = [];
  40. /**
  41. * Reference to the `ManualColumnMove` plugin.
  42. */
  43. _this.manualColumnMovePlugin = void 0;
  44. return _this;
  45. }
  46. /**
  47. * Check if the plugin is enabled in the Handsontable settings.
  48. *
  49. * @returns {Boolean}
  50. */
  51. _createClass(ManualColumnFreeze, [{
  52. key: 'isEnabled',
  53. value: function isEnabled() {
  54. return !!this.hot.getSettings().manualColumnFreeze;
  55. }
  56. /**
  57. * Enable plugin for this Handsontable instance.
  58. */
  59. }, {
  60. key: 'enablePlugin',
  61. value: function enablePlugin() {
  62. var _this2 = this;
  63. if (this.enabled) {
  64. return;
  65. }
  66. this.addHook('afterContextMenuDefaultOptions', function (options) {
  67. return _this2.addContextMenuEntry(options);
  68. });
  69. this.addHook('afterInit', function () {
  70. return _this2.onAfterInit();
  71. });
  72. this.addHook('beforeColumnMove', function (rows, target) {
  73. return _this2.onBeforeColumnMove(rows, target);
  74. });
  75. _get(ManualColumnFreeze.prototype.__proto__ || Object.getPrototypeOf(ManualColumnFreeze.prototype), 'enablePlugin', this).call(this);
  76. }
  77. /**
  78. * Disable plugin for this Handsontable instance.
  79. */
  80. }, {
  81. key: 'disablePlugin',
  82. value: function disablePlugin() {
  83. var priv = privatePool.get(this);
  84. priv.afterFirstUse = false;
  85. priv.moveByFreeze = false;
  86. _get(ManualColumnFreeze.prototype.__proto__ || Object.getPrototypeOf(ManualColumnFreeze.prototype), 'disablePlugin', this).call(this);
  87. }
  88. /**
  89. * Updates the plugin to use the latest options you have specified.
  90. */
  91. }, {
  92. key: 'updatePlugin',
  93. value: function updatePlugin() {
  94. this.disablePlugin();
  95. this.enablePlugin();
  96. _get(ManualColumnFreeze.prototype.__proto__ || Object.getPrototypeOf(ManualColumnFreeze.prototype), 'updatePlugin', this).call(this);
  97. }
  98. /**
  99. * Freeze the given column (add it to fixed columns).
  100. *
  101. * @param {Number} column Column index.
  102. */
  103. }, {
  104. key: 'freezeColumn',
  105. value: function freezeColumn(column) {
  106. var priv = privatePool.get(this);
  107. var settings = this.hot.getSettings();
  108. if (!priv.afterFirstUse) {
  109. priv.afterFirstUse = true;
  110. }
  111. if (settings.fixedColumnsLeft === this.hot.countCols() || column <= settings.fixedColumnsLeft - 1) {
  112. return; // already fixed
  113. }
  114. priv.moveByFreeze = true;
  115. if (column !== this.getMovePlugin().columnsMapper.getValueByIndex(column)) {
  116. this.frozenColumnsBasePositions[settings.fixedColumnsLeft] = column;
  117. }
  118. this.getMovePlugin().moveColumn(column, settings.fixedColumnsLeft++);
  119. }
  120. /**
  121. * Unfreeze the given column (remove it from fixed columns and bring to it's previous position).
  122. *
  123. * @param {Number} column Column index.
  124. */
  125. }, {
  126. key: 'unfreezeColumn',
  127. value: function unfreezeColumn(column) {
  128. var priv = privatePool.get(this);
  129. var settings = this.hot.getSettings();
  130. if (!priv.afterFirstUse) {
  131. priv.afterFirstUse = true;
  132. }
  133. if (settings.fixedColumnsLeft <= 0 || column > settings.fixedColumnsLeft - 1) {
  134. return; // not fixed
  135. }
  136. var returnCol = this.getBestColumnReturnPosition(column);
  137. priv.moveByFreeze = true;
  138. settings.fixedColumnsLeft--;
  139. this.getMovePlugin().moveColumn(column, returnCol + 1);
  140. }
  141. /**
  142. * Get the reference to the ManualColumnMove plugin.
  143. *
  144. * @private
  145. * @returns {Object}
  146. */
  147. }, {
  148. key: 'getMovePlugin',
  149. value: function getMovePlugin() {
  150. if (!this.manualColumnMovePlugin) {
  151. this.manualColumnMovePlugin = this.hot.getPlugin('manualColumnMove');
  152. }
  153. return this.manualColumnMovePlugin;
  154. }
  155. /**
  156. * Estimates the most fitting return position for unfrozen column.
  157. *
  158. * @private
  159. * @param {Number} column Column index.
  160. */
  161. }, {
  162. key: 'getBestColumnReturnPosition',
  163. value: function getBestColumnReturnPosition(column) {
  164. var movePlugin = this.getMovePlugin();
  165. var settings = this.hot.getSettings();
  166. var i = settings.fixedColumnsLeft;
  167. var j = movePlugin.columnsMapper.getValueByIndex(i);
  168. var initialCol = void 0;
  169. if (this.frozenColumnsBasePositions[column] == null) {
  170. initialCol = movePlugin.columnsMapper.getValueByIndex(column);
  171. while (j < initialCol) {
  172. i++;
  173. j = movePlugin.columnsMapper.getValueByIndex(i);
  174. }
  175. } else {
  176. initialCol = this.frozenColumnsBasePositions[column];
  177. this.frozenColumnsBasePositions[column] = void 0;
  178. while (j <= initialCol) {
  179. i++;
  180. j = movePlugin.columnsMapper.getValueByIndex(i);
  181. }
  182. i = j;
  183. }
  184. return i - 1;
  185. }
  186. /**
  187. * Add the manualColumnFreeze context menu entries.
  188. *
  189. * @private
  190. * @param {Object} options Context menu options.
  191. */
  192. }, {
  193. key: 'addContextMenuEntry',
  194. value: function addContextMenuEntry(options) {
  195. options.items.push({ name: '---------' }, (0, _freezeColumn2.default)(this), (0, _unfreezeColumn2.default)(this));
  196. }
  197. /**
  198. * Enabling `manualColumnMove` plugin on `afterInit` hook.
  199. *
  200. * @private
  201. */
  202. }, {
  203. key: 'onAfterInit',
  204. value: function onAfterInit() {
  205. if (!this.getMovePlugin().isEnabled()) {
  206. this.getMovePlugin().enablePlugin();
  207. }
  208. }
  209. /**
  210. * Prevent moving the rows from/to fixed area.
  211. *
  212. * @private
  213. * @param {Array} rows
  214. * @param {Number} target
  215. */
  216. }, {
  217. key: 'onBeforeColumnMove',
  218. value: function onBeforeColumnMove(rows, target) {
  219. var priv = privatePool.get(this);
  220. if (priv.afterFirstUse && !priv.moveByFreeze) {
  221. var frozenLen = this.hot.getSettings().fixedColumnsLeft;
  222. var disallowMoving = target < frozenLen;
  223. if (!disallowMoving) {
  224. (0, _array.arrayEach)(rows, function (value, index, array) {
  225. if (value < frozenLen) {
  226. disallowMoving = true;
  227. return false;
  228. }
  229. });
  230. }
  231. if (disallowMoving) {
  232. return false;
  233. }
  234. }
  235. if (priv.moveByFreeze) {
  236. priv.moveByFreeze = false;
  237. }
  238. }
  239. /**
  240. * Destroy plugin instance.
  241. */
  242. }, {
  243. key: 'destroy',
  244. value: function destroy() {
  245. _get(ManualColumnFreeze.prototype.__proto__ || Object.getPrototypeOf(ManualColumnFreeze.prototype), 'destroy', this).call(this);
  246. }
  247. }]);
  248. return ManualColumnFreeze;
  249. }(_base2.default);
  250. (0, _plugins.registerPlugin)('manualColumnFreeze', ManualColumnFreeze);
  251. exports.default = ManualColumnFreeze;