f2faa70ecd71d55ac83cd2b4b16e03dbeaaf6f5503a6e07c57c5768ae2112924bc1c37447c797cd1f062ae9c76213a635c323975a12ef25e02266a7697ca33 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {arrayEach} from './../../helpers/array';
  2. import {hasClass} from './../../helpers/dom/element';
  3. import {KEY as SEPARATOR} from './predefinedItems/separator';
  4. export function normalizeSelection(selRange) {
  5. return {
  6. start: selRange.getTopLeftCorner(),
  7. end: selRange.getBottomRightCorner()
  8. };
  9. }
  10. export function isSeparator(cell) {
  11. return hasClass(cell, 'htSeparator');
  12. }
  13. export function hasSubMenu(cell) {
  14. return hasClass(cell, 'htSubmenu');
  15. }
  16. export function isDisabled(cell) {
  17. return hasClass(cell, 'htDisabled');
  18. }
  19. export function isSelectionDisabled(cell) {
  20. return hasClass(cell, 'htSelectionDisabled');
  21. }
  22. export function getValidSelection(hot) {
  23. let selected = hot.getSelected();
  24. if (!selected) {
  25. return null;
  26. }
  27. if (selected[0] < 0) {
  28. return null;
  29. }
  30. return selected;
  31. }
  32. export function prepareVerticalAlignClass(className, alignment) {
  33. if (className.indexOf(alignment) != -1) {
  34. return className;
  35. }
  36. className = className
  37. .replace('htTop', '')
  38. .replace('htMiddle', '')
  39. .replace('htBottom', '')
  40. .replace(' ', '');
  41. className += ` ${alignment}`;
  42. return className;
  43. }
  44. export function prepareHorizontalAlignClass(className, alignment) {
  45. if (className.indexOf(alignment) != -1) {
  46. return className;
  47. }
  48. className = className
  49. .replace('htLeft', '')
  50. .replace('htCenter', '')
  51. .replace('htRight', '')
  52. .replace('htJustify', '')
  53. .replace(' ', '');
  54. className += ` ${alignment}`;
  55. return className;
  56. }
  57. export function getAlignmentClasses(range, callback) {
  58. const classes = {};
  59. for (let row = range.from.row; row <= range.to.row; row++) {
  60. for (let col = range.from.col; col <= range.to.col; col++) {
  61. if (!classes[row]) {
  62. classes[row] = [];
  63. }
  64. classes[row][col] = callback(row, col);
  65. }
  66. }
  67. return classes;
  68. }
  69. export function align(range, type, alignment, cellDescriptor, propertySetter) {
  70. if (range.from.row == range.to.row && range.from.col == range.to.col) {
  71. applyAlignClassName(range.from.row, range.from.col, type, alignment, cellDescriptor, propertySetter);
  72. } else {
  73. for (let row = range.from.row; row <= range.to.row; row++) {
  74. for (let col = range.from.col; col <= range.to.col; col++) {
  75. applyAlignClassName(row, col, type, alignment, cellDescriptor, propertySetter);
  76. }
  77. }
  78. }
  79. }
  80. function applyAlignClassName(row, col, type, alignment, cellDescriptor, propertySetter) {
  81. let cellMeta = cellDescriptor(row, col);
  82. let className = alignment;
  83. if (cellMeta.className) {
  84. if (type === 'vertical') {
  85. className = prepareVerticalAlignClass(cellMeta.className, alignment);
  86. } else {
  87. className = prepareHorizontalAlignClass(cellMeta.className, alignment);
  88. }
  89. }
  90. propertySetter(row, col, 'className', className);
  91. }
  92. export function checkSelectionConsistency(range, comparator) {
  93. let result = false;
  94. if (range) {
  95. range.forAll((row, col) => {
  96. if (comparator(row, col)) {
  97. result = true;
  98. return false;
  99. }
  100. });
  101. }
  102. return result;
  103. }
  104. export function markLabelAsSelected(label) {
  105. // workaround for https://github.com/handsontable/handsontable/issues/1946
  106. return `<span class="selected">${String.fromCharCode(10003)}</span>${label}`;
  107. }
  108. export function isItemHidden(item, instance) {
  109. return !item.hidden || !(typeof item.hidden == 'function' && item.hidden.call(instance));
  110. }
  111. function shiftSeparators(items, separator) {
  112. let result = items.slice(0);
  113. for (let i = 0; i < result.length;) {
  114. if (result[i].name === separator) {
  115. result.shift();
  116. } else {
  117. break;
  118. }
  119. }
  120. return result;
  121. }
  122. function popSeparators(items, separator) {
  123. let result = items.slice(0);
  124. result.reverse();
  125. result = shiftSeparators(result, separator);
  126. result.reverse();
  127. return result;
  128. }
  129. function removeDuplicatedSeparators(items) {
  130. let result = [];
  131. arrayEach(items, (value, index) => {
  132. if (index > 0) {
  133. if (result[result.length - 1].name !== value.name) {
  134. result.push(value);
  135. }
  136. } else {
  137. result.push(value);
  138. }
  139. });
  140. return result;
  141. }
  142. export function filterSeparators(items, separator = SEPARATOR) {
  143. let result = items.slice(0);
  144. result = shiftSeparators(result, separator);
  145. result = popSeparators(result, separator);
  146. result = removeDuplicatedSeparators(result);
  147. return result;
  148. }