Show:

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.

_getSortComparator

(
  • node
  • [options]
)
Function protected

Returns a sort comparator function derived from the given node and options, and bound to the correct thisObj based on where it was found.

Parameters:

  • node Tree.Node

    Node on which to look for a sortComparator function.

  • [options] Object optional

    Options object on which to look for a sortComparator function.

Returns:

Function:

Properly bound sort comparator function.

_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.

sort

(
  • [options]
)
chainable

Sorts the children of every node in this tree.

A sort event will be fired for each node whose children are sorted, which can get very noisy. If this is a large tree, you may want to set the silent option to true to suppress these events.

Parameters:

  • [options] Object optional

    Options.

    • [silent] Boolean optional

      If true, no sort events will be fired.

    • [sortComparator] Function optional

      Custom comparator function to use. If specified, this will become the new comparator function for each node, 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 each 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.

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.

By default, only the node's direct children are sorted. To sort all nodes in the hierarchy (children, children's children, etc.), set the deep option to true. If this is a very deep hierarchy, you may also want to set silent to true to avoid generating a flood of sort events.

Parameters:

  • node Tree.Node

    Node whose children should be sorted.

  • [options] Object optional

    Options.

    • [deep=false] Boolean optional

      If true, all of this node's children (and their children, and so on) will be traversed and re-sorted as well.

    • [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.