Skip to content

Commit

Permalink
Merge pull request #2 from metrica-sports/fran/add-order-field-to-item
Browse files Browse the repository at this point in the history
Add order field to item
  • Loading branch information
FranCarrascosaMS authored Jul 1, 2023
2 parents 3c060fd + 7cef635 commit 2a92b9a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
27 changes: 22 additions & 5 deletions lib/timeline/Stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ const EPSILON = 0.001; // used when checking collisions, to prevent round-off er
* @param {Item[]} items
*/
export function orderByStart(items) {
items.sort((a, b) => a.data.start - b.data.start);
items.sort((a, b) => {
if (a.data.order !== b.data.order) {
return a.data.order - b.data.order;
}

return a.data.start - b.data.start;
});
}

/**
Expand All @@ -16,6 +22,10 @@ export function orderByStart(items) {
*/
export function orderByEnd(items) {
items.sort((a, b) => {
if (a.data.order !== b.data.order) {
return a.data.order - b.data.order;
}

const aTime = ('end' in a.data) ? a.data.end : a.data.start;
const bTime = ('end' in b.data) ? b.data.end : b.data.start;

Expand Down Expand Up @@ -311,15 +321,22 @@ function performStacking(items, margins, compareTimes, shouldStack, shouldOthers
const horizontallyCollidingItems = itemsAlreadyPositioned
.slice(horizontalOverlapStartIndex, horizontalOverlapEndIndex)
.filter(i => itemStart < getItemEnd(i) - EPSILON && itemEnd - EPSILON > getItemStart(i))
.sort((a, b) => a.top - b.top);
.sort((a, b) => {
if (a.data.order !== b.data.order) {
return a.data.order - b.data.order;
}

return a.top - b.top;
});

// Keep moving the item down until it stops colliding with any other items
for(let i2 = 0; i2 < horizontallyCollidingItems.length; i2++) {
const otherItem = horizontallyCollidingItems[i2];

if(checkVerticalSpatialCollision(item, otherItem, margins)) {

// We won't check vertical collisions because don't want to share lanes with previous items.
//if(checkVerticalSpatialCollision(item, otherItem, margins)) {
item.top = otherItem.top + otherItem.height + margins.vertical;
}
//}
}

if(shouldOthersStack(item)) {
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metrica-sports/vis-timeline",
"version": "1.0.0",
"version": "1.1.0",
"description": "Create a fully customizable, interactive timeline with items and ranges.",
"homepage": "https://visjs.github.io/vis-timeline/",
"license": "(Apache-2.0 OR MIT)",
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface LegendOptions {
export interface DataItem {
className?: string;
content: string;
order: number;
end?: DateType;
group?: any;
id?: IdType;
Expand Down

0 comments on commit 2a92b9a

Please sign in to comment.