1// Copyright 2015 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5export function anyToString(x: any): string { 6 return "" + x; 7} 8 9function computeScrollTop(container, element) { 10 const height = container.offsetHeight; 11 const margin = Math.floor(height / 4); 12 const pos = element.offsetTop; 13 const currentScrollTop = container.scrollTop; 14 if (pos < currentScrollTop + margin) { 15 return Math.max(0, pos - margin); 16 } else if (pos > (currentScrollTop + 3 * margin)) { 17 return Math.max(0, pos - 3 * margin); 18 } 19 return pos; 20} 21 22export class ViewElements { 23 container: HTMLElement; 24 scrollTop: number; 25 26 constructor(container: HTMLElement) { 27 this.container = container; 28 this.scrollTop = undefined; 29 } 30 31 consider(element, doConsider) { 32 if (!doConsider) return; 33 const newScrollTop = computeScrollTop(this.container, element); 34 if (isNaN(newScrollTop)) { 35 console.log("NOO"); 36 } 37 if (this.scrollTop === undefined) { 38 this.scrollTop = newScrollTop; 39 } else { 40 this.scrollTop = Math.min(this.scrollTop, newScrollTop); 41 } 42 } 43 44 apply(doApply) { 45 if (!doApply || this.scrollTop === undefined) return; 46 this.container.scrollTop = this.scrollTop; 47 } 48} 49 50export 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 const ret = [arr[0]]; 54 for (let i = 1; i < arr.length; i++) { 55 if (!equal(arr[i - 1], arr[i])) { 56 ret.push(arr[i]); 57 } 58 } 59 return ret; 60} 61 62// Partial application without binding the receiver 63export function partial(f: any, ...arguments1: Array<any>) { 64 return function (this: any, ...arguments2: Array<any>) { 65 f.apply(this, [...arguments1, ...arguments2]); 66 }; 67} 68 69export function isIterable(obj: any): obj is Iterable<any> { 70 return obj != null && obj != undefined 71 && typeof obj != 'string' && typeof obj[Symbol.iterator] === 'function'; 72} 73 74export function alignUp(raw: number, multiple: number): number { 75 return Math.floor((raw + multiple - 1) / multiple) * multiple; 76} 77 78export function measureText(text: string) { 79 const textMeasure = document.getElementById('text-measure'); 80 if (textMeasure instanceof SVGTSpanElement) { 81 textMeasure.textContent = text; 82 return { 83 width: textMeasure.getBBox().width, 84 height: textMeasure.getBBox().height, 85 }; 86 } 87 return { width: 0, height: 0 }; 88} 89 90// Interpolate between the given start and end values by a fraction of val/max. 91export function interpolate(val: number, max: number, start: number, end: number) { 92 return start + (end - start) * (val / max); 93} 94 95export function createElement(tag: string, cls: string, content?: string) { 96 const el = document.createElement(tag); 97 el.className = cls; 98 if (content != undefined) el.innerText = content; 99 return el; 100} 101