fb1acdfa0c1407dec94f047b413c482dd7cf1b01e7d47e775bcfebde86f00be8f1b43ca0efc8a7b82c853b5e2f11b0a5302926ed76228cdc8c7e8fe8923f87 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import {fastInnerText} from './../../../helpers/dom/element';
  2. import {hasOwnProperty} from './../../../helpers/object';
  3. /**
  4. * @class Settings
  5. */
  6. class Settings {
  7. /**
  8. * @param {Walkontable} wotInstance
  9. * @param {Object} settings
  10. */
  11. constructor(wotInstance, settings) {
  12. this.wot = wotInstance;
  13. // legacy support
  14. this.instance = wotInstance;
  15. // default settings. void 0 means it is required, null means it can be empty
  16. this.defaults = {
  17. table: void 0,
  18. debug: false, // shows WalkontableDebugOverlay
  19. // presentation mode
  20. externalRowCalculator: false,
  21. stretchH: 'none', // values: all, last, none
  22. currentRowClassName: null,
  23. currentColumnClassName: null,
  24. preventOverflow() {
  25. return false;
  26. },
  27. // data source
  28. data: void 0,
  29. freezeOverlays: false,
  30. fixedColumnsLeft: 0,
  31. fixedRowsTop: 0,
  32. fixedRowsBottom: 0,
  33. minSpareRows: 0,
  34. // this must be array of functions: [function (row, TH) {}]
  35. rowHeaders() {
  36. return [];
  37. },
  38. // this must be array of functions: [function (column, TH) {}]
  39. columnHeaders() {
  40. return [];
  41. },
  42. totalRows: void 0,
  43. totalColumns: void 0,
  44. cellRenderer: (row, column, TD) => {
  45. let cellData = this.getSetting('data', row, column);
  46. fastInnerText(TD, cellData === void 0 || cellData === null ? '' : cellData);
  47. },
  48. // columnWidth: 50,
  49. columnWidth(col) {
  50. // return undefined means use default size for the rendered cell content
  51. },
  52. rowHeight(row) {
  53. // return undefined means use default size for the rendered cell content
  54. },
  55. defaultRowHeight: 23,
  56. defaultColumnWidth: 50,
  57. selections: null,
  58. hideBorderOnMouseDownOver: false,
  59. viewportRowCalculatorOverride: null,
  60. viewportColumnCalculatorOverride: null,
  61. // callbacks
  62. onCellMouseDown: null,
  63. onCellMouseOver: null,
  64. onCellMouseOut: null,
  65. onCellMouseUp: null,
  66. // onCellMouseOut: null,
  67. onCellDblClick: null,
  68. onCellCornerMouseDown: null,
  69. onCellCornerDblClick: null,
  70. beforeDraw: null,
  71. onDraw: null,
  72. onBeforeDrawBorders: null,
  73. onScrollVertically: null,
  74. onScrollHorizontally: null,
  75. onBeforeTouchScroll: null,
  76. onAfterMomentumScroll: null,
  77. onBeforeStretchingColumnWidth: (width) => width,
  78. onModifyRowHeaderWidth: null,
  79. // constants
  80. scrollbarWidth: 10,
  81. scrollbarHeight: 10,
  82. renderAllRows: false,
  83. groups: false,
  84. rowHeaderWidth: null,
  85. columnHeaderHeight: null,
  86. headerClassName: null
  87. };
  88. // reference to settings
  89. this.settings = {};
  90. for (let i in this.defaults) {
  91. if (hasOwnProperty(this.defaults, i)) {
  92. if (settings[i] !== void 0) {
  93. this.settings[i] = settings[i];
  94. } else if (this.defaults[i] === void 0) {
  95. throw new Error(`A required setting "${i}" was not provided`);
  96. } else {
  97. this.settings[i] = this.defaults[i];
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Update settings
  104. *
  105. * @param {Object} settings
  106. * @param {*} value
  107. * @returns {Walkontable}
  108. */
  109. update(settings, value) {
  110. if (value === void 0) { // settings is object
  111. for (let i in settings) {
  112. if (hasOwnProperty(settings, i)) {
  113. this.settings[i] = settings[i];
  114. }
  115. }
  116. } else { // if value is defined then settings is the key
  117. this.settings[settings] = value;
  118. }
  119. return this.wot;
  120. }
  121. /**
  122. * Get setting by name
  123. *
  124. * @param {String} key
  125. * @param {*} param1
  126. * @param {*} param2
  127. * @param {*} param3
  128. * @param {*} param4
  129. * @returns {*}
  130. */
  131. getSetting(key, param1, param2, param3, param4) {
  132. if (typeof this.settings[key] === 'function') {
  133. // this is faster than .apply - https://github.com/handsontable/handsontable/wiki/JavaScript-&-DOM-performance-tips
  134. return this.settings[key](param1, param2, param3, param4);
  135. } else if (param1 !== void 0 && Array.isArray(this.settings[key])) {
  136. // perhaps this can be removed, it is only used in tests
  137. return this.settings[key][param1];
  138. }
  139. return this.settings[key];
  140. }
  141. /**
  142. * Checks if setting exists
  143. *
  144. * @param {Boolean} key
  145. * @returns {Boolean}
  146. */
  147. has(key) {
  148. return !!this.settings[key];
  149. }
  150. }
  151. export default Settings;