Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Network: force array order when sorting hierarchical levels #3576

Merged
merged 5 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/network/modules/LayoutEngine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
/**
* There's a mix-up with terms in the code. Following are the formal definitions:
*
Expand Down Expand Up @@ -30,6 +29,8 @@
* The layout is a way to arrange the nodes in the view; this can be done
* on non-hierarchical networks as well. The converse is also possible.
*/
'use strict';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No special reason, other than to keep the javascript code together. You'd rather have it on top?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine as is, just curious

var TimSort = require('timsort');
let util = require('../../util');
var NetworkUtil = require('../NetworkUtil').default;
var {HorizontalStrategy, VerticalStrategy} = require('./components/DirectionStrategy.js');
Expand Down Expand Up @@ -1422,7 +1423,7 @@ class LayoutEngine {
result.push(Number(size));
});

result.sort(function(a, b) {
TimSort.sort(result, function(a, b) {
return b - a;
});

Expand Down
18 changes: 12 additions & 6 deletions lib/network/modules/components/DirectionStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Strategy pattern for usage of direction methods for hierarchical layouts.
*/
var TimSort = require('timsort');


/**
Expand Down Expand Up @@ -87,6 +88,15 @@ class DirectionInterface {
/**
* Sort array of nodes on the unfixed coordinates.
*
* **Note:** chrome has non-stable sorting implementation, which
* has a tendency to change the order of the array items,
* even if the custom sort function returns 0.
*
* For this reason, an external sort implementation is used,
* which has the added benefit of being faster than the standard
* platforms implementation. This has been verified on `node.js`,
* `firefox` and `chrome` (all linux).
*
* @param {Array.<Node>} nodeArray array of nodes to sort
*/
sort(nodeArray) { this.fake_use(nodeArray); this.abstract(); }
Expand Down Expand Up @@ -156,9 +166,7 @@ class VerticalStrategy extends DirectionInterface {

/** @inheritdoc */
sort(nodeArray) {
nodeArray.sort(function(a, b) {
// Test on 'undefined' takes care of divergent behaviour in chrome
if (a.x === undefined || b.x === undefined) return 0; // THIS HAPPENS
TimSort.sort(nodeArray, function(a, b) {
return a.x - b.x;
});
}
Expand Down Expand Up @@ -221,9 +229,7 @@ class HorizontalStrategy extends DirectionInterface {

/** @inheritdoc */
sort(nodeArray) {
nodeArray.sort(function(a, b) {
// Test on 'undefined' takes care of divergent behaviour in chrome
if (a.y === undefined || b.y === undefined) return 0; // THIS HAPPENS
TimSort.sort(nodeArray, function(a, b) {
return a.y - b.y;
});
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"hammerjs": "^2.0.8",
"keycharm": "^0.2.0",
"moment": "^2.18.1",
"propagating-hammerjs": "^1.4.6"
"propagating-hammerjs": "^1.4.6",
"timsort": "^0.3.0"
},
"devDependencies": {
"async": "^2.5.0",
Expand Down