ec9db96e653a72e84d874e93ba8182b094b88be8ad0cab84164b46ef0b531e5496138631e32467c2accc439ec9c0d802746d203d241d44ec846faf285ab362 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. 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; }; }();
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. import { getScrollableElement, getTrimmingContainer } from './../../../../helpers/dom/element';
  4. import { defineGetter } from './../../../../helpers/object';
  5. import { arrayEach } from './../../../../helpers/array';
  6. import EventManager from './../../../../eventManager';
  7. import Walkontable from './../core';
  8. var registeredOverlays = {};
  9. /**
  10. * Creates an overlay over the original Walkontable instance. The overlay renders the clone of the original Walkontable
  11. * and (optionally) implements behavior needed for native horizontal and vertical scrolling.
  12. *
  13. * @class Overlay
  14. */
  15. var Overlay = function () {
  16. _createClass(Overlay, null, [{
  17. key: 'registerOverlay',
  18. /**
  19. * Register overlay class.
  20. *
  21. * @param {String} type Overlay type, one of the CLONE_TYPES value
  22. * @param {Overlay} overlayClass Overlay class extended from base overlay class {@link Overlay}
  23. */
  24. value: function registerOverlay(type, overlayClass) {
  25. if (Overlay.CLONE_TYPES.indexOf(type) === -1) {
  26. throw new Error('Unsupported overlay (' + type + ').');
  27. }
  28. registeredOverlays[type] = overlayClass;
  29. }
  30. /**
  31. * Create new instance of overlay type.
  32. *
  33. * @param {String} type Overlay type, one of the CLONE_TYPES value
  34. * @param {Walkontable} wot Walkontable instance
  35. */
  36. }, {
  37. key: 'createOverlay',
  38. value: function createOverlay(type, wot) {
  39. return new registeredOverlays[type](wot);
  40. }
  41. /**
  42. * Check if specified overlay was registered.
  43. *
  44. * @param {String} type Overlay type, one of the CLONE_TYPES value
  45. * @returns {Boolean}
  46. */
  47. }, {
  48. key: 'hasOverlay',
  49. value: function hasOverlay(type) {
  50. return registeredOverlays[type] !== void 0;
  51. }
  52. /**
  53. * Checks if overlay object (`overlay`) is instance of overlay type (`type`).
  54. *
  55. * @param {Overlay} overlay Overlay object
  56. * @param {String} type Overlay type, one of the CLONE_TYPES value
  57. * @returns {Boolean}
  58. */
  59. }, {
  60. key: 'isOverlayTypeOf',
  61. value: function isOverlayTypeOf(overlay, type) {
  62. if (!overlay || !registeredOverlays[type]) {
  63. return false;
  64. }
  65. return overlay instanceof registeredOverlays[type];
  66. }
  67. /**
  68. * @param {Walkontable} wotInstance
  69. */
  70. }, {
  71. key: 'CLONE_TOP',
  72. /**
  73. * @type {String}
  74. */
  75. get: function get() {
  76. return 'top';
  77. }
  78. /**
  79. * @type {String}
  80. */
  81. }, {
  82. key: 'CLONE_BOTTOM',
  83. get: function get() {
  84. return 'bottom';
  85. }
  86. /**
  87. * @type {String}
  88. */
  89. }, {
  90. key: 'CLONE_LEFT',
  91. get: function get() {
  92. return 'left';
  93. }
  94. /**
  95. * @type {String}
  96. */
  97. }, {
  98. key: 'CLONE_TOP_LEFT_CORNER',
  99. get: function get() {
  100. return 'top_left_corner';
  101. }
  102. /**
  103. * @type {String}
  104. */
  105. }, {
  106. key: 'CLONE_BOTTOM_LEFT_CORNER',
  107. get: function get() {
  108. return 'bottom_left_corner';
  109. }
  110. /**
  111. * @type {String}
  112. */
  113. }, {
  114. key: 'CLONE_DEBUG',
  115. get: function get() {
  116. return 'debug';
  117. }
  118. /**
  119. * List of all availables clone types
  120. *
  121. * @type {Array}
  122. */
  123. }, {
  124. key: 'CLONE_TYPES',
  125. get: function get() {
  126. return [Overlay.CLONE_TOP, Overlay.CLONE_BOTTOM, Overlay.CLONE_LEFT, Overlay.CLONE_TOP_LEFT_CORNER, Overlay.CLONE_BOTTOM_LEFT_CORNER, Overlay.CLONE_DEBUG];
  127. }
  128. }]);
  129. function Overlay(wotInstance) {
  130. _classCallCheck(this, Overlay);
  131. defineGetter(this, 'wot', wotInstance, {
  132. writable: false
  133. });
  134. // legacy support, deprecated in the future
  135. this.instance = this.wot;
  136. this.type = '';
  137. this.mainTableScrollableElement = null;
  138. this.TABLE = this.wot.wtTable.TABLE;
  139. this.hider = this.wot.wtTable.hider;
  140. this.spreader = this.wot.wtTable.spreader;
  141. this.holder = this.wot.wtTable.holder;
  142. this.wtRootElement = this.wot.wtTable.wtRootElement;
  143. this.trimmingContainer = getTrimmingContainer(this.hider.parentNode.parentNode);
  144. this.areElementSizesAdjusted = false;
  145. this.updateStateOfRendering();
  146. }
  147. /**
  148. * Update internal state of object with an information about the need of full rendering of the overlay.
  149. *
  150. * @returns {Boolean} Returns `true` if the state has changed since the last check.
  151. */
  152. _createClass(Overlay, [{
  153. key: 'updateStateOfRendering',
  154. value: function updateStateOfRendering() {
  155. var previousState = this.needFullRender;
  156. this.needFullRender = this.shouldBeRendered();
  157. var changed = previousState !== this.needFullRender;
  158. if (changed && !this.needFullRender) {
  159. this.reset();
  160. }
  161. return changed;
  162. }
  163. /**
  164. * Checks if overlay should be fully rendered
  165. *
  166. * @returns {Boolean}
  167. */
  168. }, {
  169. key: 'shouldBeRendered',
  170. value: function shouldBeRendered() {
  171. return true;
  172. }
  173. /**
  174. * Update the trimming container.
  175. */
  176. }, {
  177. key: 'updateTrimmingContainer',
  178. value: function updateTrimmingContainer() {
  179. this.trimmingContainer = getTrimmingContainer(this.hider.parentNode.parentNode);
  180. }
  181. /**
  182. * Update the main scrollable element.
  183. */
  184. }, {
  185. key: 'updateMainScrollableElement',
  186. value: function updateMainScrollableElement() {
  187. this.mainTableScrollableElement = getScrollableElement(this.wot.wtTable.TABLE);
  188. }
  189. /**
  190. * Make a clone of table for overlay
  191. *
  192. * @param {String} direction Can be `Overlay.CLONE_TOP`, `Overlay.CLONE_LEFT`,
  193. * `Overlay.CLONE_TOP_LEFT_CORNER`, `Overlay.CLONE_DEBUG`
  194. * @returns {Walkontable}
  195. */
  196. }, {
  197. key: 'makeClone',
  198. value: function makeClone(direction) {
  199. if (Overlay.CLONE_TYPES.indexOf(direction) === -1) {
  200. throw new Error('Clone type "' + direction + '" is not supported.');
  201. }
  202. var clone = document.createElement('DIV');
  203. var clonedTable = document.createElement('TABLE');
  204. clone.className = 'ht_clone_' + direction + ' handsontable';
  205. clone.style.position = 'absolute';
  206. clone.style.top = 0;
  207. clone.style.left = 0;
  208. clone.style.overflow = 'hidden';
  209. clonedTable.className = this.wot.wtTable.TABLE.className;
  210. clone.appendChild(clonedTable);
  211. this.type = direction;
  212. this.wot.wtTable.wtRootElement.parentNode.appendChild(clone);
  213. var preventOverflow = this.wot.getSetting('preventOverflow');
  214. if (preventOverflow === true || preventOverflow === 'horizontal' && this.type === Overlay.CLONE_TOP || preventOverflow === 'vertical' && this.type === Overlay.CLONE_LEFT) {
  215. this.mainTableScrollableElement = window;
  216. } else {
  217. this.mainTableScrollableElement = getScrollableElement(this.wot.wtTable.TABLE);
  218. }
  219. return new Walkontable({
  220. cloneSource: this.wot,
  221. cloneOverlay: this,
  222. table: clonedTable
  223. });
  224. }
  225. /**
  226. * Refresh/Redraw overlay
  227. *
  228. * @param {Boolean} [fastDraw=false]
  229. */
  230. }, {
  231. key: 'refresh',
  232. value: function refresh() {
  233. var fastDraw = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  234. // When hot settings are changed we allow to refresh overlay once before blocking
  235. var nextCycleRenderFlag = this.shouldBeRendered();
  236. if (this.clone && (this.needFullRender || nextCycleRenderFlag)) {
  237. this.clone.draw(fastDraw);
  238. }
  239. this.needFullRender = nextCycleRenderFlag;
  240. }
  241. /**
  242. * Reset overlay styles to initial values.
  243. */
  244. }, {
  245. key: 'reset',
  246. value: function reset() {
  247. if (!this.clone) {
  248. return;
  249. }
  250. var holder = this.clone.wtTable.holder;
  251. var hider = this.clone.wtTable.hider;
  252. var holderStyle = holder.style;
  253. var hidderStyle = hider.style;
  254. var rootStyle = holder.parentNode.style;
  255. arrayEach([holderStyle, hidderStyle, rootStyle], function (style) {
  256. style.width = '';
  257. style.height = '';
  258. });
  259. }
  260. /**
  261. * Destroy overlay instance
  262. */
  263. }, {
  264. key: 'destroy',
  265. value: function destroy() {
  266. new EventManager(this.clone).destroy();
  267. }
  268. }]);
  269. return Overlay;
  270. }();
  271. export default Overlay;