Advertisement
transpalette

textarea drag n' drop

Nov 7th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function() {
  2.    
  3.     var lineHeight = parseInt($('#textarea').css('line-height').replace('px',''));
  4.     var previousLineNo = 0;
  5.     var content = $('#textarea').val();
  6.     var linesArray = content.length > 0 ? content.split('\n') : [];
  7.     var lineNo = 0;
  8.     var emptyLineAdded = false;
  9.  
  10.     $('.draggable').draggable({
  11.         revert: function(is_valid_drop) {
  12.             if (!is_valid_drop) {
  13.                 $('#textarea').val(content);
  14.  
  15.                 return true;
  16.             }
  17.         },
  18.         drag: function(event, ui) {
  19.             lineNo = getLineNo(ui, lineHeight);
  20.  
  21.             if (linesArray.length > 0 && previousLineNo != lineNo) {
  22.                 insertWhiteLine(lineNo, linesArray);
  23.             }
  24.            
  25.             previousLineNo = lineNo;
  26.         }
  27.     });
  28.  
  29.     $("#textarea").droppable({
  30.         accept: ".draggable",
  31.         drop: function( event, ui ) {
  32.             appendAtLine(lineNo, linesArray, ui.draggable.text());
  33.             $(ui.draggable).remove();
  34.             content = $('#textarea').val();
  35.             linesArray = content.split('\n');
  36.  
  37.             if (linesArray[linesArray.length - 1] == '')
  38.                 linesArray.pop(); //remove empty line
  39.         }
  40.     });
  41.  
  42.     $('#textarea').on('input', function() {
  43.         if (!emptyLineAdded) {
  44.             console.log('input !');
  45.             console.log($('#textarea').val());
  46.             content = $('#textarea').val();
  47.             linesArray = content.split('\n');
  48.  
  49.             if (linesArray[linesArray.length - 1] == '')
  50.                 linesArray.pop(); //remove empty line
  51.         }
  52.     });
  53.  
  54. });
  55.  
  56.  
  57. //Returns the top position of a draggable element,
  58. //relative to the textarea. (0 means at the very top of the textarea)
  59. function getYPosition(element, lineHeight) {
  60.     var participantIndex = $(element.helper.context).index();
  61.     var initPos = participantIndex * lineHeight;
  62.     var actualPos = initPos + element.position.top;
  63.  
  64.     return actualPos;
  65. }
  66.  
  67.  
  68. //Returns the line number corresponding to where the element
  69. //would be dropped
  70. function getLineNo(element, lineHeight) {
  71.     return Math.round(getYPosition(element, lineHeight) / lineHeight);
  72. }
  73.  
  74.  
  75. //Inserts a white line at the given line number,
  76. //to show where the element would be dropped in the textarea
  77. function insertWhiteLine(lineNo, linesArray) {
  78.     $('#textarea').val('');
  79.     $(linesArray).each(function(index, value) {
  80.         if (index < lineNo)
  81.             $('#textarea').val($('#textarea').val() + value + '\n');
  82.     });
  83.  
  84.     emptyLineAdded = true;
  85.     $('#textarea').val($('#textarea').val() + '_____________\n'); //white line
  86.     emptyLineAdded = false;
  87.    
  88.     $(linesArray).each(function(index, value) {
  89.         if (index >= lineNo)
  90.             $('#textarea').val($('#textarea').val() + value + '\n');
  91.     });
  92. }
  93.  
  94.  
  95. //Inserts content of draggable at the given line number
  96. function appendAtLine(lineNo, linesArray, content) {
  97.     $('#textarea').val('');
  98.     $(linesArray).each(function(index, value) {
  99.         if (index < lineNo)
  100.             $('#textarea').val($('#textarea').val() + value + '\n');
  101.     });
  102.  
  103.     $('#textarea').val($('#textarea').val() + content + '\n'); //content to be added
  104.  
  105.     $(linesArray).each(function(index, value) {
  106.         if (index >= lineNo)
  107.             $('#textarea').val($('#textarea').val() + value + '\n');
  108.     });
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement