Advertisement
transpalette

Textarea drag'n drop no reorder

Nov 14th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function() {
  2.     var lineHeight = parseInt($('#personer').css('line-height').replace('px',''));
  3.  
  4.     if (/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor)) {
  5.         lineHeight -= 0.3; //Chrome adds 0.3px to the lineHeight for some reason ...
  6.     }
  7.  
  8.     var previousLineNo = 0;
  9.     var content = $('#personer').val();
  10.     var linesArray = content.length > 0 ? content.split('\n') : [];
  11.     var lineNo = 0;
  12.     var emptyLineAdded = false;
  13.  
  14.     $('.draggable').draggable({
  15.         helper:'clone',
  16.         revert: function(is_valid_drop) {
  17.             if (!is_valid_drop) {
  18.                 $('#personer').val(content);
  19.                 $('#replace-warn').css('opacity', 0);
  20.  
  21.                 return true;
  22.             }
  23.         },
  24.         drag: function(event, ui) {
  25.             lineNo = getLineNo(ui, lineHeight);
  26.  
  27.             if (linesArray.length > 0 && previousLineNo != lineNo) {
  28.                 insertIndicator(lineNo, linesArray);
  29.             }
  30.            
  31.             previousLineNo = lineNo;
  32.         }
  33.     });
  34.  
  35.     $("#personer").droppable({
  36.         accept: ".draggable",
  37.         drop: function( event, ui ) {
  38.             appendAtLine(lineNo, linesArray, ui.draggable.text());
  39.             $(this).append($(ui.draggable).clone());
  40.             content = $('#personer').val();
  41.             linesArray = content.split('\n');
  42.  
  43.             if (linesArray[linesArray.length - 1] == '')
  44.                 linesArray.pop(); //remove empty line
  45.         }
  46.     });
  47.  
  48.     $('#personer').on('input', function() {
  49.         if (!emptyLineAdded) {
  50.             content = $('#personer').val();
  51.             linesArray = content.split('\n');
  52.  
  53.             if (linesArray[linesArray.length - 1] == '')
  54.                 linesArray.pop(); //remove empty line
  55.         }
  56.     });
  57.  
  58.     $('#button-populate').click(function() {
  59.         $('#personer').val("vl1:\nvl1:\nvl1:\nvl1:\nvl1:\nvl1:\nvl1:\n");
  60.         $('#personer').val($('#personer').val() + "vl2:\nvl2:\nvl2:\nvl2:\nvl2:\nvl2:\nvl2:\n");
  61.         $('#personer').val($('#personer').val() + "synth1:\nsynth1:\nsynth1:\nsynth1:\nsynth1:\nsynth1:\nsynth1:\n");
  62.         content = $('#personer').val();
  63.         linesArray = content.split('\n');
  64.     });
  65. });
  66.  
  67.  
  68. //Returns the top position of a draggable element,
  69. //relative to the textarea. (0 means at the very top of the textarea)
  70. function getYPosition(element, lineHeight) {
  71.     var participantIndex = $(element.helper.context).index();
  72.     var initPos = participantIndex * lineHeight;
  73.     var actualPos = initPos + element.position.top;
  74.  
  75.     return element.position.top + $('#personer').scrollTop();
  76. }
  77.  
  78.  
  79. //Returns the line number corresponding to where the element
  80. //would be dropped
  81. function getLineNo(element, lineHeight) {
  82.     return Math.round(getYPosition(element, lineHeight) / lineHeight);
  83. }
  84.  
  85.  
  86. //Inserts an indicator at the given line number,
  87. //to show where the element would be dropped in the textarea
  88. function insertIndicator(lineNo, linesArray) {
  89.     var lineHeight = parseInt($('#personer').css('line-height').replace('px',''));
  90.     var scrollTop = $('#personer').scrollTop(); //Has to be here, otherwise causes bugs ...
  91.  
  92.     if (/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor)) {
  93.         lineHeight -= 0.3; //Chrome adds 0.3px to the lineHeight for some reason ...
  94.     }
  95.  
  96.     $('#personer').val('');
  97.     $(linesArray).each(function(index, value) {
  98.         if (index < lineNo)
  99.             $('#personer').val($('#personer').val() + value + '\n');
  100.     });
  101.  
  102.     if (linesArray[lineNo] != null && linesArray[lineNo] != 'undefined') {
  103.         if (linesArray[lineNo].split(':')[1].length > 0) { //If there's already a participant on that role
  104.             $('#replace-warn').css('top', (lineNo * lineHeight) - scrollTop);
  105.             $('#replace-warn').css('opacity', 0.5);
  106.             $('#personer').val($('#personer').val() + linesArray[lineNo].split(':')[0] + ': --> ' + linesArray[lineNo].split(':')[1] + '\n');
  107.         } else {
  108.             $('#replace-warn').css('opacity', 0);
  109.             $('#personer').val($('#personer').val() + linesArray[lineNo] + ' <--\n');
  110.         }
  111.     }
  112.    
  113.     $(linesArray).each(function(index, value) {
  114.         if (index > lineNo)
  115.             $('#personer').val($('#personer').val() + value + '\n');
  116.     });
  117. }
  118.  
  119.  
  120. //Inserts content of draggable at the given line number
  121. function appendAtLine(lineNo, linesArray, content) {
  122.     $('#personer').val('');
  123.     $(linesArray).each(function(index, value) {
  124.         if (index < lineNo)
  125.             $('#personer').val($('#personer').val() + value + '\n');
  126.     });
  127.  
  128.     if (linesArray[lineNo].split(':')[1].length > 0) {
  129.         $('#replace-warn').css('opacity', 0);
  130.         $('#personer').val($('#personer').val() + linesArray[lineNo].split(':')[0] + ': ' + content + '\n');
  131.     }
  132.     else
  133.         $('#personer').val($('#personer').val() + linesArray[lineNo] + ' ' + content + '\n'); //content to be added
  134.  
  135.     $(linesArray).each(function(index, value) {
  136.         if (index > lineNo)
  137.             $('#personer').val($('#personer').val() + value + '\n');
  138.     });
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement