API Docs for: 3.10.3
Show:

Tree.Sortable Class

Module: tree-sortable
Parent Module: tree

Extension for Tree that makes nodes sortable.

Constructor

Tree.Sortable

(
  • [config]
)

Parameters:

  • [config] Object optional

    Configuration options.

    • [sortComparator] Function optional

      Default comparator function to use when sorting a node's children if the node itself doesn't have a custom comparator function. If not specified, insertion order will be used by default.

    • [sortReverse=false] Boolean optional

      If true, node children will be sorted in reverse (descending) order by default. Otherwise they'll be sorted in ascending order.

Methods

_compare

(
  • a
  • b
)
Number protected

Compares value a to value b for sorting purposes.

Values are assumed to be the result of calling a sortComparator function.

Parameters:

  • a Mixed

    First value to compare.

  • b Mixed

    Second value to compare.

Returns:

Number: -1 if a should come before b, 0 if they're equivalent, 1 if a should come after b.

_compareReverse

(
  • a
  • b
)
Number protected

Compares value a to value b for sorting purposes, but sorts them in reverse (descending) order.

Values are assumed to be the result of calling a sortComparator function.

Parameters:

  • a Mixed

    First value to compare.

  • b Mixed

    Second value to compare.

Returns:

Number: -1 if a should come before b, 0 if they're equivalent, 1 if a should come after b.

_getDefaultNodeIndex

(
  • parent
  • node
  • [options]
)
Number protected

Overrides Tree#_getDefaultNodeIndex() to provide insertion-time sorting for nodes inserted without an explicit index.

Parameters:

  • parent Tree.Node

    Parent node.

  • node Tree.Node

    Node being inserted.

  • [options] Object optional

    Options passed to insertNode().

Returns:

Number: Index at which node should be inserted into parent's children array.

_sort

(
  • a
  • b
  • comparator
  • [reverse=false]
)
Number protected

Array sort function used by sortNode() to re-sort a node's children.

Parameters:

  • a Tree.Node

    First node to compare.

  • b Tree.Node

    Second node to compare.

  • comparator Function

    Comparator function.

  • [reverse=false] Boolean optional

    If true, this will be a reverse (descending) comparison.

Returns:

Number: -1 if a is less than b, 0 if equal, 1 if greater.

sortComparator

(
  • node
)
Number | String

Default comparator function to use when sorting a node's children if the node itself doesn't have a custom comparator function.

If not specified, insertion order will be used by default.

Parameters:

Returns:

Number | String: Value by which the node should be sorted relative to its siblings.

sortNode

(
  • node
  • [options]
)
chainable

Sorts the children of the specified node.

Parameters:

  • node Tree.Node

    Node whose children should be sorted.

  • [options] Object optional

    Options.

    • [silent] Boolean optional

      If true, no sort event will be fired.

    • [sortComparator] Function optional

      Custom comparator function to use. If specified, this will become the node's new comparator function, overwriting any previous comparator function that was set for the node.

    • [sortReverse] Boolean optional

      If true, children will be sorted in reverse (descending) order. Otherwise they'll be sorted in ascending order. This will become the node's new sort order, overwriting any previous sort order that was set for the node.

    • [src] String optional

      Source of the sort operation. Will be passed along to the sort event facade.

Properties

sortReverse

Boolean

If true, node children will be sorted in reverse (descending) order by default. Otherwise they'll be sorted in ascending order.

Default: false

Events

sort

Fired after a node's children are re-sorted.

Event Payload:

  • node Tree.Node

    Node whose children were sorted.

  • reverse Boolean

    true if the children were sorted in reverse (descending) order, false otherwise.

  • src String

    Source of the event.