You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.9 KiB

4 years ago
  1. /**
  2. * @preserve jQuery Column cell selector v1.0
  3. *
  4. * Licensed under the new BSD License.
  5. * Copyright 2009-2012, Bram Stein
  6. * All rights reserved.
  7. */
  8. /*global jQuery*/
  9. (function ($) {
  10. var equationRegExp = /([\+\-]?\d*)[nN]([\+\-]?\d*)/,
  11. cache, equation,
  12. pseudoSelector = jQuery.expr.filter.PSEUDO;
  13. function parseEquation(str) {
  14. var tmp = [],
  15. result = {
  16. multiplier: 0,
  17. offset: 0
  18. };
  19. if (str === 'even') {
  20. str = '2n';
  21. } else if (str === 'odd') {
  22. str = '2n+1';
  23. } else if (/^\d*$/.test(str)) {
  24. str = '0n+' + str;
  25. }
  26. tmp = equationRegExp.exec(str);
  27. if (tmp !== null) {
  28. result.multiplier = tmp[1] - 0;
  29. result.offset = tmp[2] - 0;
  30. }
  31. return result;
  32. }
  33. function generateCache(cells, equation) {
  34. var currentRow, currentSection, matrix = [], first = 0, lookup = [];
  35. $.each(cells, function (k, cell) {
  36. var i = 0, j = 0,
  37. rowSpan = cell.rowSpan || 1,
  38. colSpan = cell.colSpan || 1;
  39. if (cell.parentNode !== currentRow) {
  40. currentRow = cell.parentNode;
  41. if (currentRow.parentNode !== currentSection) {
  42. currentSection = currentRow.parentNode;
  43. matrix = [];
  44. }
  45. first = 0;
  46. if (matrix[currentRow.rowIndex] === undefined) {
  47. matrix[currentRow.rowIndex] = [];
  48. }
  49. }
  50. for (i = 0; i < matrix[currentRow.rowIndex].length + 1; i += 1) {
  51. if (matrix[currentRow.rowIndex][i] === undefined) {
  52. first = i;
  53. break;
  54. }
  55. }
  56. lookup[k] = first;
  57. for (i = currentRow.rowIndex; i < currentRow.rowIndex + rowSpan; i += 1) {
  58. if (matrix[i] === undefined) {
  59. matrix[i] = [];
  60. }
  61. for (j = first; j < first + colSpan; j += 1) {
  62. matrix[i][j] = true;
  63. }
  64. }
  65. });
  66. return lookup;
  67. }
  68. function nthCol(element, match, index) {
  69. var difference = cache[index] - (equation.offset - 1);
  70. if (equation.multiplier === 0) {
  71. return difference === 0;
  72. } else {
  73. return (difference % equation.multiplier === 0 && difference / equation.multiplier >= 0);
  74. }
  75. }
  76. $.extend(jQuery.fn, {
  77. nthCol: function (e) {
  78. equation = parseEquation(e);
  79. cache = generateCache(this);
  80. return $(this).filter(function (i) {
  81. return nthCol(this, undefined, i);
  82. });
  83. }
  84. });
  85. $.extend(jQuery.expr.match, {
  86. COLUMN: new RegExp(":nth-col\\((even|odd|[\\dnN\\+\\-]*)\\)(?![\\^\\[]*\\])(?![\\^\\(]*\\))")
  87. });
  88. $.extend(jQuery.expr.leftMatch, {
  89. COLUMN: new RegExp("(^(?:.|\\r|\\n)*?):nth-col\\((even|odd|[\\dnN\\+\\-]*)\\)(?![\\^\\[]*\\])(?![\\^\\(]*\\))")
  90. });
  91. $.extend(jQuery.expr.preFilter, {
  92. COLUMN: function (match, items) {
  93. equation = parseEquation(match[1]);
  94. cache = generateCache(items);
  95. return match;
  96. }
  97. });
  98. $.extend(jQuery.expr.filter, {
  99. // Override the pseudo selector and go past the "unrecognized
  100. // expression" error message if nth-col is part of the
  101. // expression. This is only necessary on jQuery >= 1.4.x.
  102. PSEUDO: function (elem, match, i, array) {
  103. var name = match[1];
  104. if (name !== 'nth-col') {
  105. return pseudoSelector(elem, match, i, array);
  106. }
  107. }
  108. });
  109. $.extend(jQuery.expr.filter, {
  110. COLUMN: nthCol
  111. });
  112. }(jQuery));