Sha256: f84ec904eb37d690d9d13ab778e6c6a0c13631740328b3f122a7bf6022c93434

Contents?: true

Size: 1.36 KB

Versions: 29

Compression:

Stored size: 1.36 KB

Contents

class Para.ResourceTree
  constructor: (@$el) ->
    @initializeTree()

  initializeTree: ->
    @orderUrl = @$el.data('url')
    @maxDepth = parseInt @$el.data('max-depth')

    $(".tree")
      .sortable
        handle: ".handle"
        items: ".node"
        connectWith: ".tree"
      .on('sortupdate', $.proxy(@sortUpdate, this))

  sortUpdate: (e, data) ->
    @handlePlaceholder($el) for $el in [data.endparent, data.startparent, data.item.find('.tree')]
    @updateOrder()

  handlePlaceholder: ($el) ->
    $placeholder = $el.find("> .placeholder")
    if $el.parents('.tree').length - 1 >= @maxDepth or $el.find('> .node').length
      $placeholder.hide()
      $el.children("> .tree").each (index, el) => @handlePlaceholder $(el)
    else
      $placeholder.show()

  updateOrder: ->
    Para.ajax(
      url: @orderUrl
      method: 'patch'
      data:
        resources: @buildOrderedData()
      success: $.proxy(@orderUpdated, this)
    )

  buildOrderedData: ->
    data = {}
    $(".node").each (index) ->
      $this = $(this)
      data[index] = {
        id: $this.data("id"),
        position: index,
        parent_id: $this.parent().parent().data("id")
      }
    data

  orderUpdated: ->
    # TODO: Add flash message to display ordering success

$(document).on 'page:change turbolinks:load', ->
  $('.root-tree').each (i, el) ->
    new Para.ResourceTree($(el))

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
para-0.8.7 app/assets/javascripts/para/admin/tree.coffee
para-0.8.5 app/assets/javascripts/para/admin/tree.coffee
para-0.8.3.3 app/assets/javascripts/para/admin/tree.coffee
para-0.8.3.2 app/assets/javascripts/para/admin/tree.coffee
para-0.8.3.1 app/assets/javascripts/para/admin/tree.coffee
para-0.8.3 app/assets/javascripts/para/admin/tree.coffee
para-0.8.2.3 app/assets/javascripts/para/admin/tree.coffee
para-0.8.2.2 app/assets/javascripts/para/admin/tree.coffee
para-0.8.2.1 app/assets/javascripts/para/admin/tree.coffee
para-0.8.2 app/assets/javascripts/para/admin/tree.coffee
para-0.8.1 app/assets/javascripts/para/admin/tree.coffee
para-0.8.0 app/assets/javascripts/para/admin/tree.coffee
para-0.7.4.p1 app/assets/javascripts/para/admin/tree.coffee
para-0.7.4 app/assets/javascripts/para/admin/tree.coffee
para-0.7.3.p6 app/assets/javascripts/para/admin/tree.coffee
para-0.7.3.p5 app/assets/javascripts/para/admin/tree.coffee
para-0.7.3.p4 app/assets/javascripts/para/admin/tree.coffee
para-0.7.3.p3 app/assets/javascripts/para/admin/tree.coffee
para-0.7.3.p2 app/assets/javascripts/para/admin/tree.coffee
para-0.7.3.p1 app/assets/javascripts/para/admin/tree.coffee