Sha256: d9cedd43c4037b0a1430bb097d4c4a46a0d16b65c4d763831791eba3ed493276

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

# ---------------------------------------------------------
# REORDER LIST ITEMS
# ---------------------------------------------------------

@Character.Generic.Helpers.stopReorder = (el) ->
  el.sortable( "destroy" )

@Character.Generic.Helpers.startReorder = (el, collection) ->
  updateModelPosition = ($el, position) ->
    object_id = $el.attr('data-id')
    object    = collection.get(object_id)
    object.save({ _position: position }, { patch: true })

  options =
    delay: 150
    placeholder: 'placeholder'
    update: (e, ui) =>
      prev = ui.item.prev()
      next = ui.item.next()

      if prev.length > 0 and next.length > 0
        prevPosition = parseFloat prev.attr('data-position')
        nextPosition = parseFloat next.attr('data-position')
        newPosition  = (prevPosition + nextPosition) / 2

      else if prev.length > 0 # bottom of the list
        lastPosition = parseFloat prev.attr('data-position')
        newPosition  = lastPosition - 10

      else if next.length > 0 # top of the list
        firstPosition = parseFloat next.attr('data-position')
        newPosition   = firstPosition + 10

      updateModelPosition(ui.item, newPosition)

  el.sortable(options).disableSelection()

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
character-1.0.6 app/assets/javascripts/character/generic/helpers/reorder.coffee
character-1.0.5 app/assets/javascripts/character/generic/helpers/reorder.coffee
character-1.0.4 app/assets/javascripts/character/generic/helpers/reorder.coffee
character-1.0.3 app/assets/javascripts/character/generic/helpers/reorder.coffee
character-1.0.2 app/assets/javascripts/character/generic/helpers/reorder.coffee
character-1.0.1 app/assets/javascripts/character/generic/helpers/reorder.coffee
character-1.0.0 app/assets/javascripts/character/generic/helpers/reorder.coffee