Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

dragndrop.table.columns.js 3.7KB

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