Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery ui sortable hold and drag for mobile

I have a jQuery UI sortable list. I am trying to make it work on mobile devices. I used the touch punch workaround. In order to scroll I have to deactivate the sortable function, I have done that and on taphold of a list element activate the sortable function. That is working ok, but the problem is that I want that while on taphold to allow the sorting of the element. Now it only works like this: taphold the element (taphold stops) and then I have to again tap it in order to sort.

HTML Code:

<ul class="list-group" id="wrapper">
 <li class="list-group-item">Block 1</li>
 <li class="list-group-item">Block 2</li>
 <li class="list-group-item">Block 3</li>
 <li class="list-group-item">Block 4</li>
 <li class="list-group-item">Block 5</li>
 <li class="list-group-item">Block 6</li>
 <li class="list-group-item">Block 7</li>
 <li class="list-group-item">Block 8</li>
 <li class="list-group-item">Block 9</li>
 <li class="list-group-item">Block 10</li>
 <li class="list-group-item">Block 11</li>
 <li class="list-group-item">Block 12</li>
 <li class="list-group-item">Block 13</li>
 <li class="list-group-item">Block 14</li>
 <li class="list-group-item">Block 15</li>
 <li class="list-group-item">Block 16</li>
 <li class="list-group-item">Block 17</li>
 <li class="list-group-item">Block 18</li>
 <li class="list-group-item">Block 19</li>
 <li class="list-group-item">Block 20</li>
 <li class="list-group-item">Block 21</li>
 <li class="list-group-item">Block 22</li>
 <li class="list-group-item">Block 23</li>
</ul>

JS Code:

$('#wrapper li').on('taphold', function(event, ui) {
    $( "#wrapper li" ).removeClass('selected');
    $( "#wrapper" ).sortable({disabled:false});
    $(this).addClass('selected');
});

$( "#wrapper" ).sortable({disabled:true,containment: "parent"});

$( "#wrapper" ).on( "sortupdate", function( event, ui ) {
    $( "#wrapper" ).sortable({disabled:true});
} );

Here is a jsfiddle(https://jsfiddle.net/3cygah12/) of the example.

Anyone know how to solve this issue?

like image 702
Shile Avatar asked Dec 01 '15 18:12

Shile


2 Answers

I managed to do it.

I modified a part of touch punch( http://touchpunch.furf.com/ ) code like this to bind touchstart to taphold -

mouseProto._mouseInit = function () {

var self = this;

// Delegate the touch handlers to the widget's element
self.element
    .bind('taphold', $.proxy(self, '_touchStart'))   // IMPORTANT!MOD FOR TAPHOLD TO START SORTABLE
    .bind('touchmove', $.proxy(self, '_touchMove'))
    .bind('touchend', $.proxy(self, '_touchEnd'));

// Call the original $.ui.mouse init method
_mouseInit.call(self);
};  

And also used https://github.com/benmajor/jQuery-Touch-Events . It works now!

like image 84
Shile Avatar answered Nov 19 '22 19:11

Shile


I addressed the same issue with this tiny plugin:

http://touchpunch.furf.com/

Add this library to your to your scripts.

Another way to do it, is to download the whole mobile jquery library (here) and work like this

$('#yourSelector').on('touchstart mousedown', function(event){
  event.preventDefault();
  var touch = event.touches[0];
 if(touch){
   //Do some stuff
 }
 else {
  //Do some other stuff
 }
});
like image 34
Sidius Avatar answered Nov 19 '22 19:11

Sidius