87041ae689d0382315d19b0d0a04fbb325193752aa3afa5a589cf5595b69d23128b3061cf343f7cb3188a1bb3cabeb59c59c469cb2077e119ae131c52c564f 6.7 KB

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