Skip to content

Commit

Permalink
removed the dependency for jQuery UI and fixed the dependencies such …
Browse files Browse the repository at this point in the history
…that dragging works on mobile
  • Loading branch information
ksylvest committed Jun 29, 2013
1 parent e47b5c9 commit d6b0423
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 58 deletions.
114 changes: 92 additions & 22 deletions javascripts/jquery.gridly.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,77 @@ class Animation
transition = @transition($el)
if transition? then $el.one(transition, callback) else callback()

class Draggable

@settings:
delta: 64
distance: 16

constructor: ($container, selector, callbacks) ->
@$container = $container
@selector = selector
@callbacks = callbacks
@toggle()

bind: (method = 'on') =>
$(document)[method] 'mousemove touchmove', @moved
$(document)[method] 'mouseup touchend touchcancel', @ended

toggle: (method = 'on') =>
@$container[method] 'mousedown touchstart', @selector, @began
@$container[method] 'click', @selector, @click

coordinate: (event) =>
switch event.type
when 'touchstart','touchmove','touchend','touchcancel' then event.originalEvent.touches[0]
else event

began: (event) =>
return if @$target
event.preventDefault()
event.stopPropagation()
@bind('on')

@$target = $(event.target)
@$target.addClass('dragging')

@origin =
x: @coordinate(event).pageX - @$target.position().left
y: @coordinate(event).pageY - @$target.position().top

@callbacks?.began?(event)

ended: (event) =>
return unless @$target?
event.preventDefault()
event.stopPropagation()
@bind('off')

@$target.removeClass('dragging')

delete @$target
delete @origin

@callbacks?.ended?(event)

moved: (event) =>
return unless @$target?
event.preventDefault()
event.stopPropagation()

@$target.css
left: @coordinate(event).pageX - @origin.x
top: @coordinate(event).pageY - @origin.y

@dragged = @$target
@callbacks?.moved?(event)

click: (event) =>
return unless @dragged
event.preventDefault()
event.stopPropagation()
delete @dragged

class Gridly

@settings:
Expand All @@ -41,6 +112,8 @@ class Gridly
@$el = $el
@settings = $.extend {}, Gridly.settings, settings
@ordinalize(@$('> *'))
@draggable()
return @

ordinalize: ($elements) =>
for i in [0 .. $elements.length]
Expand All @@ -61,27 +134,10 @@ class Gridly
return 0

draggable: =>
callbacks =
drag: @drag
start: @start
stop: @stop

@$('> *').draggable($.extend(callbacks, @settings.draggable))

stationary: =>
@$('> *').draggable('destroy')

start: (event, ui) =>
$elements = @$sorted()
@ordinalize($elements)
setTimeout @layout, 0
@settings?.callbacks?.reordering?($elements)

stop: (event, ui) =>
$elements = @$sorted()
@ordinalize($elements)
setTimeout @layout, 0
@settings?.callbacks?.reordered?($elements)
@_draggable ?= new Draggable @$el, '> *',
began: @draggingBegan
ended: @draggingEnded
moved: @draggingMoved

$sorted: ($elements) =>
($elements || @$('> *')).sort (a,b) ->
Expand All @@ -99,7 +155,19 @@ class Gridly
return +1 if bPositionInt < aPositionInt
return 0

drag: (event, ui) =>
draggingBegan: (event) =>
$elements = @$sorted()
@ordinalize($elements)
setTimeout @layout, 0
@settings?.callbacks?.reordering?($elements)

draggingEnded: (event) =>
$elements = @$sorted()
@ordinalize($elements)
setTimeout @layout, 0
@settings?.callbacks?.reordered?($elements)

draggingMoved: (event) =>
$dragging = $(event.target)
$elements = @$sorted()
positions = @structure($elements).positions
Expand Down Expand Up @@ -169,6 +237,8 @@ class Gridly
$element = $($elements[index])
position = structure.positions[index]

continue if $element.is('.dragging')

$element.css
position: 'absolute'
left: position.x
Expand Down
171 changes: 138 additions & 33 deletions javascripts/jquery.gridly.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion stylesheets/jquery.gridly.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.gridly, .gridly > :not(.ui-draggable-dragging) {
.gridly, .gridly > :not(.dragging) {
-webkit-transition: all 0.4s ease-in-out;
-moz-transition: all 0.4s ease-in-out;
transition: all 0.4s ease-in-out; }
.gridly .dragging {
z-index: 800; }
6 changes: 4 additions & 2 deletions stylesheets/jquery.gridly.sass
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ $white: #FFF
$namespace: "gridly"

.gridly
&, > :not(.ui-draggable-dragging)
+transition(all 0.4s ease-in-out)
&, > :not(.dragging)
+transition(all 0.4s ease-in-out)
.dragging
z-index: 800

0 comments on commit d6b0423

Please sign in to comment.