Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

dragndrop.table.columns.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* global $:false, jQuery:false */
  2. /*
  3. Drag & Drop Table Columns v.3.1.5
  4. for jQuery 3.x
  5. by Oleksandr Nikitin (a.nikitin@i.ua)
  6. https://github.com/alexshnur/drag-n-drop-table-columns
  7. */
  8. (function ($, window) {
  9. let cols;
  10. let dragSrcEl = null;
  11. let dragSrcEnter = null;
  12. let dragableColumns;
  13. let
  14. _this;
  15. function insertAfter(elem, refElem) {
  16. return refElem.parentNode.insertBefore(elem, refElem.nextSibling);
  17. }
  18. function isIE() {
  19. const nav = navigator.userAgent.toLowerCase();
  20. return (nav.indexOf('msie') !== -1) ? parseInt(nav.split('msie')[1]) : false;
  21. }
  22. dragableColumns = (function () {
  23. let $table;
  24. function dragColumns(table, options) {
  25. _this = this;
  26. $table = table;
  27. _this.options = $.extend({}, _this.options, options);
  28. if (_this.options.drag) {
  29. if (isIE() === 9) {
  30. $table.find('thead tr th').each(function () {
  31. if ($(this).find('.drag-ie').length === 0) {
  32. $(this).html($('<a>').html($(this).html()).attr('href', '#').addClass(
  33. 'drag-ie',
  34. ));
  35. }
  36. });
  37. }
  38. cols = $table.find('thead tr th');
  39. jQuery.event.addProp('dataTransfer');
  40. [].forEach.call(cols, (col) => {
  41. col.setAttribute('draggable', true);
  42. $(col).on('dragstart', _this.handleDragStart);
  43. $(col).on('dragenter', _this.handleDragEnter);
  44. $(col).on('dragover', _this.handleDragOver);
  45. $(col).on('dragleave', _this.handleDragLeave);
  46. $(col).on('drop', _this.handleDrop);
  47. $(col).on('dragend', _this.handleDragEnd);
  48. });
  49. }
  50. }
  51. dragColumns.prototype = {
  52. options: {
  53. drag: true,
  54. dragClass: 'drag',
  55. overClass: 'over',
  56. movedContainerSelector: '.dnd-moved',
  57. },
  58. handleDragStart(e) {
  59. $(this).addClass(_this.options.dragClass);
  60. dragSrcEl = this;
  61. e.dataTransfer.effectAllowed = 'copy';
  62. e.dataTransfer.setData('text/html', this.id);
  63. },
  64. handleDragOver(e) {
  65. if (e.preventDefault) {
  66. e.preventDefault();
  67. }
  68. e.dataTransfer.dropEffect = 'copy';
  69. },
  70. handleDragEnter(e) {
  71. dragSrcEnter = this;
  72. [].forEach.call(cols, (col) => {
  73. $(col).removeClass(_this.options.overClass);
  74. });
  75. $(this).addClass(_this.options.overClass);
  76. },
  77. handleDragLeave(e) {
  78. if (dragSrcEnter !== e) {
  79. // this.classList.remove(_this.options.overClass);
  80. }
  81. },
  82. handleDrop(e) {
  83. if (e.stopPropagation) {
  84. e.stopPropagation();
  85. }
  86. if (dragSrcEl !== e) {
  87. _this.moveColumns($(dragSrcEl).index(), $(this).index());
  88. }
  89. },
  90. handleDragEnd(e) {
  91. const colPositions = {
  92. array: [],
  93. object: {},
  94. };
  95. [].forEach.call(cols, (col) => {
  96. const name = $(col).attr('data-name') || $(col).index();
  97. $(col).removeClass(_this.options.overClass);
  98. colPositions.object[name] = $(col).index();
  99. colPositions.array.push($(col).index());
  100. });
  101. if (typeof _this.options.onDragEnd === 'function') {
  102. _this.options.onDragEnd(colPositions);
  103. }
  104. $(dragSrcEl).removeClass(_this.options.dragClass);
  105. },
  106. moveColumns(fromIndex, toIndex) {
  107. const rows = $table.find(_this.options.movedContainerSelector);
  108. for (let i = 0; i < rows.length; i++) {
  109. if (toIndex > fromIndex) {
  110. insertAfter(rows[i].children[fromIndex], rows[i].children[toIndex]);
  111. } else if (toIndex < $table.find('thead tr th').length - 1) {
  112. rows[i].insertBefore(rows[i].children[fromIndex], rows[i].children[toIndex]);
  113. }
  114. }
  115. },
  116. };
  117. return dragColumns;
  118. }());
  119. return $.fn.extend({
  120. dragableColumns() {
  121. const option = (arguments[0]);
  122. return this.each(function () {
  123. const $table = $(this);
  124. new dragableColumns($table, option);
  125. });
  126. },
  127. });
  128. }(window.jQuery, window));