Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 94 lines (82 sloc) 2.082 kb
59216df7 »
2011-11-23 Migration to Coffee
1
2 class Sortable
3
59162237 »
2011-11-23 stylistic change
4 @defaultOptions:
5 itemSelector: 'li'
6 triggerSelector: ''
7 browserPrefix: '-webkit-'
8 transitionDuration: '0.1'
9
59216df7 »
2011-11-23 Migration to Coffee
10 constructor: (el, options) ->
11 $.extend @, Sortable.defaultOptions, options
12 @$list = $(el).first()
13 $(document).bind
14 mousemove: @onMouseMove
15 mouseup: @onMouseUp
16 @$list.delegate "#{@itemSelector} #{@triggerSelector}", 'mousedown', @onMouseDown
17
18 refreshItems: ->
19 @$items = @$list.find @itemSelector
20 if @$items.length > 1
21 @height = @$items.get(1).offsetTop - @$items.get(0).offsetTop
22 else
23 @height = 0
24
25 onMouseDown: (e) =>
26 if @triggerSelector
27 @$target = $(e.target).parent @itemSelector
28 else
29 @$target = $(e.target)
30 @x1 = e.pageX
31 @y1 = e.pageY
32 @start()
33 e.preventDefault()
34 e.stopPropagation()
35
36 onMouseMove: (e) =>
37 return unless @$target
38 @move e.pageX - @x1, e.pageY - @y1
39
40 onMouseUp: (e) =>
41 return unless @$target
42 @end()
43 delete @$target
44 delete @x1
45 delete @y1
46
47 start: ->
48 @refreshItems()
49 # set items styles
50 @$items.css 'position', 'relative'
51 @$items.not(@$target).css "#{@browserPrefix}transition", "top #{@transitionDuration}s"
52 @$target.css 'z-index', 1
53 # store target index
54 @ti = @$items.index @$target
55
56 move: (dx, dy) ->
57 @$target.css
58 top: dy
59 left: dx
60 # index delta
61 di = Math.round dy / @height
62 return if di is @di
63 @di = di
64 # current index
65 ci = @ti + @di
66 # adjust positions
67 @$items.each (i, item) =>
68 return if i is @ti
69 switch yes
70 when i >= ci and i < @ti then top = @height
71 when i <= ci and i > @ti then top = -@height
72 else top = ''
73 $(item).css 'top', top
74
75 end: ->
76 # reset items styles
77 @$items.css("#{@browserPrefix}transition", '').css
78 position: ''
79 top: ''
80 left: ''
81 'z-index': ''
82 # current index
83 ci = @ti + @di
84 maxi = @$items.length - 1
85 ci = 0 if ci < 0
86 ci = maxi if ci > maxi
223f8498 »
2011-11-23 comments
87 # move item
59216df7 »
2011-11-23 Migration to Coffee
88 if @di < 0 then func = 'before' else func = 'after'
89 $(@$items.get ci)[func] @$target unless ci is @ti
90 delete @di
91 delete @ti
92
93
94 if module? then module.exports = Sortable else @Sortable = Sortable
Something went wrong with that request. Please try again.