util.ts 2.87 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
export function anyToString(x: any): string {
  return "" + x;
}
8

9 10 11 12 13 14 15 16 17
function computeScrollTop(container, element) {
  const height = container.offsetHeight;
  const margin = Math.floor(height / 4);
  const pos = element.offsetTop;
  const currentScrollTop = container.scrollTop;
  if (pos < currentScrollTop + margin) {
    return Math.max(0, pos - margin);
  } else if (pos > (currentScrollTop + 3 * margin)) {
    return Math.max(0, pos - 3 * margin);
18
  }
19 20 21
  return pos;
}

22
export class ViewElements {
23 24 25 26
  container: HTMLElement;
  scrollTop: number;

  constructor(container: HTMLElement) {
27 28 29 30 31 32 33 34
    this.container = container;
    this.scrollTop = undefined;
  }

  consider(element, doConsider) {
    if (!doConsider) return;
    const newScrollTop = computeScrollTop(this.container, element);
    if (isNaN(newScrollTop)) {
35
      console.log("NOO");
36 37 38 39 40 41 42 43 44 45 46
    }
    if (this.scrollTop === undefined) {
      this.scrollTop = newScrollTop;
    } else {
      this.scrollTop = Math.min(this.scrollTop, newScrollTop);
    }
  }

  apply(doApply) {
    if (!doApply || this.scrollTop === undefined) return;
    this.container.scrollTop = this.scrollTop;
47 48 49
  }
}

50
export function sortUnique<T>(arr: Array<T>, f: (a: T, b: T) => number, equal: (a: T, b: T) => boolean) {
51
  if (arr.length == 0) return arr;
52
  arr = arr.sort(f);
53 54
  const ret = [arr[0]];
  for (let i = 1; i < arr.length; i++) {
55
    if (!equal(arr[i - 1], arr[i])) {
56 57 58 59 60
      ret.push(arr[i]);
    }
  }
  return ret;
}
61 62

// Partial application without binding the receiver
63 64
export function partial(f: any, ...arguments1: Array<any>) {
  return function (this: any, ...arguments2: Array<any>) {
65
    f.apply(this, [...arguments1, ...arguments2]);
66
  };
67
}
68

69
export function isIterable(obj: any): obj is Iterable<any> {
70 71 72
  return obj != null && obj != undefined
    && typeof obj != 'string' && typeof obj[Symbol.iterator] === 'function';
}
73

74
export function alignUp(raw: number, multiple: number): number {
75 76
  return Math.floor((raw + multiple - 1) / multiple) * multiple;
}
77 78 79 80 81 82 83 84 85 86

export function measureText(text: string) {
  const textMeasure = document.getElementById('text-measure');
  if (textMeasure instanceof SVGTSpanElement) {
    textMeasure.textContent = text;
    return {
      width: textMeasure.getBBox().width,
      height: textMeasure.getBBox().height,
    };
  }
87
  return { width: 0, height: 0 };
88 89 90 91 92
}

// Interpolate between the given start and end values by a fraction of val/max.
export function interpolate(val: number, max: number, start: number, end: number) {
  return start + (end - start) * (val / max);
93
}
94 95 96 97 98 99 100

export function createElement(tag: string, cls: string, content?: string) {
  const el = document.createElement(tag);
  el.className = cls;
  if (content != undefined) el.innerText = content;
  return el;
}