1<!DOCTYPE html> 2<!-- 3Copyright (c) 2015 The Chromium Authors. All rights reserved. 4Use of this source code is governed by a BSD-style license that can be 5found in the LICENSE file. 6--> 7 8<link rel="import" href="/tracing/base/rect.html"> 9 10<script> 11'use strict'; 12 13tr.exportTo('tr.ui.b', function() { 14 function instantiateTemplate(selector, doc) { 15 doc = doc || document; 16 var el = doc.querySelector(selector); 17 if (!el) 18 throw new Error('Element not found'); 19 return el.createInstance(); 20 } 21 22 function windowRectForElement(element) { 23 var position = [element.offsetLeft, element.offsetTop]; 24 var size = [element.offsetWidth, element.offsetHeight]; 25 var node = element.offsetParent; 26 while (node) { 27 position[0] += node.offsetLeft; 28 position[1] += node.offsetTop; 29 node = node.offsetParent; 30 } 31 return tr.b.Rect.fromXYWH(position[0], position[1], size[0], size[1]); 32 } 33 34 function scrollIntoViewIfNeeded(el) { 35 var pr = el.parentElement.getBoundingClientRect(); 36 var cr = el.getBoundingClientRect(); 37 if (cr.top < pr.top) { 38 el.scrollIntoView(true); 39 } else if (cr.bottom > pr.bottom) { 40 el.scrollIntoView(false); 41 } 42 } 43 44 function extractUrlString(url) { 45 var extracted = url.replace(/url\((.*)\)/, '$1'); 46 47 // In newer versions of chrome, the contents of url() will be quoted. Remove 48 // these quotes as well. If quotes are not present, match will fail and this 49 // becomes a no-op. 50 extracted = extracted.replace(/\"(.*)\"/, '$1'); 51 52 return extracted; 53 } 54 55 function toThreeDigitLocaleString(value) { 56 return value.toLocaleString( 57 undefined, {minimumFractionDigits: 3, maximumFractionDigits: 3}); 58 } 59 60 return { 61 toThreeDigitLocaleString: toThreeDigitLocaleString, 62 instantiateTemplate: instantiateTemplate, 63 windowRectForElement: windowRectForElement, 64 scrollIntoViewIfNeeded: scrollIntoViewIfNeeded, 65 extractUrlString: extractUrlString 66 }; 67}); 68</script> 69