summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/tracing/tracing/ui/base/utils.html
blob: cca1003cc061ca1213cbe4fdbbee62a0066c7f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/base.html">
<link rel="import" href="/tracing/base/math/rect.html">

<script>
'use strict';

tr.exportTo('tr.ui.b', function() {
  function instantiateTemplate(selector, doc) {
    doc = doc || document;
    const el = Polymer.dom(doc).querySelector(selector);
    if (!el) {
      throw new Error('Element not found: ' + selector);
    }
    return doc.importNode(el.content, true);
    //    return el.createInstance();
  }

  function windowRectForElement(element) {
    const position = [element.offsetLeft, element.offsetTop];
    const size = [element.offsetWidth, element.offsetHeight];
    let node = element.offsetParent;
    while (node) {
      position[0] += node.offsetLeft;
      position[1] += node.offsetTop;
      node = node.offsetParent;
    }
    return tr.b.math.Rect.fromXYWH(position[0], position[1], size[0], size[1]);
  }

  function scrollIntoViewIfNeeded(el) {
    const pr = el.parentElement.getBoundingClientRect();
    const cr = el.getBoundingClientRect();
    if (cr.top < pr.top) {
      el.scrollIntoView(true);
    } else if (cr.bottom > pr.bottom) {
      el.scrollIntoView(false);
    }
  }

  function extractUrlString(url) {
    let extracted = url.replace(/url\((.*)\)/, '$1');

    // In newer versions of chrome, the contents of url() will be quoted. Remove
    // these quotes as well. If quotes are not present, match will fail and this
    // becomes a no-op.
    extracted = extracted.replace(/\"(.*)\"/, '$1');

    return extracted;
  }

  function toThreeDigitLocaleString(value) {
    return value.toLocaleString(
        undefined, {minimumFractionDigits: 3, maximumFractionDigits: 3});
  }

  /**
   * Returns true if |name| is the name of an unknown HTML element.  Registered
   * polymer elements are known, so this returns false.  Typos of registered
   * polymer element names are unknown, so this returns true for typos.
   *
   * @return {boolean}
   */
  function isUnknownElementName(name) {
    return document.createElement(name) instanceof HTMLUnknownElement;
  }

  return {
    isUnknownElementName,
    toThreeDigitLocaleString,
    instantiateTemplate,
    windowRectForElement,
    scrollIntoViewIfNeeded,
    extractUrlString,
  };
});
</script>