scroller: use binary search for moveHoverCursor

old-stable24
Varun Patil 2022-10-15 11:00:24 -07:00
parent ec4a11dae8
commit 7bc8f944a7
3 changed files with 40 additions and 8 deletions

View File

@ -206,7 +206,7 @@ export default class ScrollerManager extends Mixins(GlobalMixin) {
// Itearte over rows
for (const row of this.rows) {
if (row.type === IRowType.HEAD) {
if (Object.values(this.TagDayID).includes(row.dayId)) {
if (this.TagDayIDValueSet.has(row.dayId)) {
// Blank tick
this.ticks.push(getTick(row.dayId));
} else {
@ -234,22 +234,20 @@ export default class ScrollerManager extends Mixins(GlobalMixin) {
this.hoverCursorY = y;
// Get index of previous tick
let idx = this.ticks.findIndex(t => t.top >= y);
let idx = utils.binarySearch(this.ticks, y, 'top');
if (idx === 0) {
// use this tick
} else if (idx >= 1) {
} else if (idx >= 1 && idx <= this.ticks.length) {
idx = idx - 1;
} else if (idx === -1 && this.ticks.length > 0) {
idx = this.ticks.length - 1;
} else {
return;
}
// DayId of current hover
const dayId = this.ticks[idx].dayId
const dayId = this.ticks[idx]?.dayId
// Special days
if (Object.values(this.TagDayID).includes(dayId)) {
if (dayId === undefined || this.TagDayIDValueSet.has(dayId)) {
this.hoverCursorText = "";
return;
}

View File

@ -9,4 +9,5 @@ export default class GlobalMixin extends Vue {
public readonly c = constants.c;
public readonly TagDayID = constants.TagDayID;
public readonly TagDayIDValueSet = new Set(Object.values(this.TagDayID));
}

View File

@ -43,6 +43,39 @@ export function getLongDateStr(date: Date, skipYear=false, time=false) {
return hash;
}
/**
* Search for elem in a sorted array of objects
* If the object is not found, return the index where it should be inserted
*
* @param arr Array of objects to search
* @param elem Element to search for
* @param key Key to use for comparison
*/
export function binarySearch(arr: any, elem: any, key?: string) {
let minIndex = 0;
let maxIndex = arr.length - 1;
let currentIndex: number;
let currentElement: any;
while (minIndex <= maxIndex) {
currentIndex = (minIndex + maxIndex) / 2 | 0;
currentElement = key ? arr[currentIndex][key] : arr[currentIndex];
if (currentElement < elem) {
minIndex = currentIndex + 1;
}
else if (currentElement > elem) {
maxIndex = currentIndex - 1;
}
else {
return currentIndex;
}
}
return minIndex;
}
/** Global constants */
export const constants = {
c: {
FLAG_PLACEHOLDER: 1 << 0,