utils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. var 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.replace('htTop', '').replace('htMiddle', '').replace('htBottom', '').replace(' ', '');
  37. className += ' ' + alignment;
  38. return className;
  39. }
  40. export function prepareHorizontalAlignClass(className, alignment) {
  41. if (className.indexOf(alignment) != -1) {
  42. return className;
  43. }
  44. className = className.replace('htLeft', '').replace('htCenter', '').replace('htRight', '').replace('htJustify', '').replace(' ', '');
  45. className += ' ' + alignment;
  46. return className;
  47. }
  48. export function getAlignmentClasses(range, callback) {
  49. var classes = {};
  50. for (var row = range.from.row; row <= range.to.row; row++) {
  51. for (var col = range.from.col; col <= range.to.col; col++) {
  52. if (!classes[row]) {
  53. classes[row] = [];
  54. }
  55. classes[row][col] = callback(row, col);
  56. }
  57. }
  58. return classes;
  59. }
  60. export function align(range, type, alignment, cellDescriptor, propertySetter) {
  61. if (range.from.row == range.to.row && range.from.col == range.to.col) {
  62. applyAlignClassName(range.from.row, range.from.col, type, alignment, cellDescriptor, propertySetter);
  63. } else {
  64. for (var row = range.from.row; row <= range.to.row; row++) {
  65. for (var col = range.from.col; col <= range.to.col; col++) {
  66. applyAlignClassName(row, col, type, alignment, cellDescriptor, propertySetter);
  67. }
  68. }
  69. }
  70. }
  71. function applyAlignClassName(row, col, type, alignment, cellDescriptor, propertySetter) {
  72. var cellMeta = cellDescriptor(row, col);
  73. var className = alignment;
  74. if (cellMeta.className) {
  75. if (type === 'vertical') {
  76. className = prepareVerticalAlignClass(cellMeta.className, alignment);
  77. } else {
  78. className = prepareHorizontalAlignClass(cellMeta.className, alignment);
  79. }
  80. }
  81. propertySetter(row, col, 'className', className);
  82. }
  83. export function checkSelectionConsistency(range, comparator) {
  84. var result = false;
  85. if (range) {
  86. range.forAll(function (row, col) {
  87. if (comparator(row, col)) {
  88. result = true;
  89. return false;
  90. }
  91. });
  92. }
  93. return result;
  94. }
  95. export function markLabelAsSelected(label) {
  96. // workaround for https://github.com/handsontable/handsontable/issues/1946
  97. return '<span class="selected">' + String.fromCharCode(10003) + '</span>' + label;
  98. }
  99. export function isItemHidden(item, instance) {
  100. return !item.hidden || !(typeof item.hidden == 'function' && item.hidden.call(instance));
  101. }
  102. function shiftSeparators(items, separator) {
  103. var result = items.slice(0);
  104. for (var i = 0; i < result.length;) {
  105. if (result[i].name === separator) {
  106. result.shift();
  107. } else {
  108. break;
  109. }
  110. }
  111. return result;
  112. }
  113. function popSeparators(items, separator) {
  114. var result = items.slice(0);
  115. result.reverse();
  116. result = shiftSeparators(result, separator);
  117. result.reverse();
  118. return result;
  119. }
  120. function removeDuplicatedSeparators(items) {
  121. var result = [];
  122. arrayEach(items, function (value, index) {
  123. if (index > 0) {
  124. if (result[result.length - 1].name !== value.name) {
  125. result.push(value);
  126. }
  127. } else {
  128. result.push(value);
  129. }
  130. });
  131. return result;
  132. }
  133. export function filterSeparators(items) {
  134. var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : SEPARATOR;
  135. var result = items.slice(0);
  136. result = shiftSeparators(result, separator);
  137. result = popSeparators(result, separator);
  138. result = removeDuplicatedSeparators(result);
  139. return result;
  140. }