Skip to content

Commit d565f28

Browse files
committed
Added insertZonePlus option, fixed little bug with ol lists.
1 parent c4b3c52 commit d565f28

File tree

4 files changed

+180
-31
lines changed

4 files changed

+180
-31
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<h1><a href="http://camohub.github.io/jquery-sortable-lists/index.html">jquery-sortable-lists</a></h1>
22
<h2 style="font-size:17px">Changelog</h2>
33

4+
<h3>v1.3.0</h3>
5+
<p>Added insertZonePlus option. Fixed bug with ol lists.</p>
6+
47
<h3>v1.2.0</h3>
58
<p>Added opener.as option to opener. Now is possible to use opener.as html or class option.</p>
69

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"dependencies": {
55
"jquery": ">=1 <3.0.0"
66
},
7-
"version": "1.2.0",
7+
"version": "1.2.1",
88
"homepage": "[email protected]:camohub/jquery-sortable-lists.git",
99
"authors": [
1010
"Vladimír Čamaj"

jquery-sortable-lists.js

Lines changed: 156 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
$.fn.sortableLists = function( options )
1616
{
1717
// Local variables. This scope is available for all the functions in this closure.
18-
var jQBody = $( 'body' )
19-
.css( 'position', 'relative' ),
18+
var jQBody = $( 'body' ).css( 'position', 'relative' ),
2019

2120
defaults = {
2221
currElClass: '',
@@ -58,6 +57,7 @@
5857
listsClass: '', // Used for hintWrapper and baseElement
5958
listsCss: {},
6059
insertZone: 50,
60+
insertZonePlus: false,
6161
scroll: 20,
6262
ignoreClass: '',
6363
isAllowed: function( cEl, hint, target ) { return true; }, // Params: current el., hint el.
@@ -184,7 +184,7 @@
184184
e.preventDefault();
185185

186186
// El must be li in jQuery object
187-
var el = target.is( 'li' ) ? target : target.closest( 'li' ),
187+
var el = target.closest( 'li' ),
188188
rEl = $( this );
189189

190190
// Check if el is not empty
@@ -477,7 +477,7 @@
477477
state.upScroll = state.downScroll = false;
478478
}
479479

480-
/////// Scroll handlers end ///////////////////////////////////////////////////////////////////
480+
/////// End of Scroll handlers //////////////////////////////////////////////////////////////
481481

482482
/**
483483
* @desc Sets the position of dragged element
@@ -555,6 +555,8 @@
555555

556556
}
557557

558+
//////// Show hint handlers //////////////////////////////////////////////////////
559+
558560
/**
559561
* @desc Shows or hides or does not show hint element
560562
* @param e event
@@ -571,15 +573,28 @@
571573
var oElH = oEl.outerHeight( false ),
572574
relY = e.pageY - oEl.offset().top;
573575

574-
if ( 5 > relY ) // Inserting before
576+
if ( setting.insertZonePlus )
575577
{
576-
showHintBefore( e, oEl );
578+
if ( 14 > relY ) // Inserting on top
579+
{
580+
showOnTopPlus( e, oEl, 7 > relY ); // Last bool param express if hint insert outside/inside
581+
}
582+
else if ( oElH - 14 < relY ) // Inserting on bottom
583+
{
584+
showOnBottomPlus( e, oEl, oElH - 7 < relY );
585+
}
577586
}
578-
else if ( oElH - 5 < relY ) // Inserting after
587+
else
579588
{
580-
showHintAfter( e, oEl );
589+
if ( 5 > relY ) // Inserting on top
590+
{
591+
showOnTop( e, oEl );
592+
}
593+
else if ( oElH - 5 < relY ) // Inserting on bottom
594+
{
595+
showOnBottom( e, oEl );
596+
}
581597
}
582-
583598
}
584599

585600
/**
@@ -588,7 +603,7 @@
588603
* @param oEl oElement
589604
* @return No value
590605
*/
591-
function showHintBefore( e, oEl )
606+
function showOnTop( e, oEl )
592607
{
593608
if ( $( '#sortableListsHintWrapper', state.rootEl.el ).length )
594609
{
@@ -610,7 +625,7 @@
610625
else
611626
{
612627
var children = oEl.children(),
613-
list = oEl.children( 'ul' ).first();
628+
list = oEl.children( setting.listSelector ).first();
614629

615630
if ( list.children().first().is( '#sortableListsPlaceholder' ) )
616631
{
@@ -633,6 +648,68 @@
633648
{
634649
open( oEl ); // TODO:animation??? .children('ul,ol').css('display', 'block');
635650
}
651+
652+
}
653+
654+
hint.css( 'display', 'block' );
655+
// Ensures posible formating of elements. Second call is in the endDrag method.
656+
state.isAllowed = setting.isAllowed( state.cEl.el, hint, hint.parents( 'li' ).first() );
657+
658+
}
659+
660+
/**
661+
* @desc Called from showHint method. Displays or hides hint element
662+
* @param e event
663+
* @param oEl oElement
664+
* @param outside bool
665+
* @return No value
666+
*/
667+
function showOnTopPlus( e, oEl, outside )
668+
{
669+
if ( $( '#sortableListsHintWrapper', state.rootEl.el ).length )
670+
{
671+
hint.unwrap(); // If hint is wrapped by ul/ol #sortableListsHintWrapper
672+
}
673+
674+
// Hint inside the oEl
675+
if ( ! outside && e.pageX - oEl.offset().left > setting.insertZone )
676+
{
677+
var children = oEl.children(),
678+
list = oEl.children( setting.listSelector ).first();
679+
680+
if ( list.children().first().is( '#sortableListsPlaceholder' ) )
681+
{
682+
hint.css( 'display', 'none' );
683+
return;
684+
}
685+
686+
// Find out if is necessary to wrap hint by hintWrapper
687+
if ( ! list.length )
688+
{
689+
children.first().after( hint );
690+
hint.wrap( hintWrapper );
691+
}
692+
else
693+
{
694+
list.prepend( hint );
695+
}
696+
697+
if ( state.oEl )
698+
{
699+
open( oEl ); // TODO:animation??? .children('ul,ol').css('display', 'block');
700+
}
701+
}
702+
// Hint outside the oEl
703+
else
704+
{
705+
// Ensure display:none if hint will be next to the placeholder
706+
if ( oEl.prev( '#sortableListsPlaceholder' ).length )
707+
{
708+
hint.css( 'display', 'none' );
709+
return;
710+
}
711+
oEl.before( hint );
712+
636713
}
637714

638715
hint.css( 'display', 'block' );
@@ -647,7 +724,7 @@
647724
* @param oEl oElement
648725
* @return No value
649726
*/
650-
function showHintAfter( e, oEl )
727+
function showOnBottom( e, oEl )
651728
{
652729
if ( $( '#sortableListsHintWrapper', state.rootEl.el ).length )
653730
{
@@ -701,6 +778,71 @@
701778

702779
}
703780

781+
/**
782+
* @desc Called from showHint function. Displays or hides hint element.
783+
* @param e event
784+
* @param oEl oElement
785+
* @param outside bool
786+
* @return No value
787+
*/
788+
function showOnBottomPlus( e, oEl, outside )
789+
{
790+
if ( $( '#sortableListsHintWrapper', state.rootEl.el ).length )
791+
{
792+
hint.unwrap(); // If hint is wrapped by ul/ol sortableListsHintWrapper
793+
}
794+
795+
// Hint inside the oEl
796+
if ( ! outside && e.pageX - oEl.offset().left > setting.insertZone )
797+
{
798+
var children = oEl.children(),
799+
list = oEl.children( setting.listSelector ).last(); // ul/ol || empty jQuery obj
800+
801+
if ( list.children().last().is( '#sortableListsPlaceholder' ) )
802+
{
803+
hint.css( 'display', 'none' );
804+
return;
805+
}
806+
807+
// Find out if is necessary to wrap hint by hintWrapper
808+
if ( list.length )
809+
{
810+
children.last().append( hint );
811+
}
812+
else
813+
{
814+
oEl.append( hint );
815+
hint.wrap( hintWrapper );
816+
}
817+
818+
if ( state.oEl )
819+
{
820+
open( oEl ); // TODO: animation???
821+
}
822+
823+
}
824+
// Hint outside the oEl
825+
else
826+
{
827+
// Ensure display:none if hint will be next to the placeholder
828+
if ( oEl.next( '#sortableListsPlaceholder' ).length )
829+
{
830+
hint.css( 'display', 'none' );
831+
return;
832+
}
833+
oEl.after( hint );
834+
835+
}
836+
837+
hint.css( 'display', 'block' );
838+
// Ensures posible formating of elements. Second call is in the endDrag method.
839+
state.isAllowed = setting.isAllowed( state.cEl.el, hint, hint.parents( 'li' ).first() );
840+
841+
}
842+
843+
//////// End of show hint handlers ////////////////////////////////////////////////////
844+
//////// Open/close handlers //////////////////////////////////////////////////////////
845+
704846
/**
705847
* @desc Handles opening nested lists
706848
* @param li
@@ -752,6 +894,8 @@
752894

753895
}
754896

897+
/////// Enf of open/close handlers //////////////////////////////////////////////
898+
755899
/**
756900
* @desc Places the currEl to the target place
757901
* @param cEl

0 commit comments

Comments
 (0)