Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / base / utils.html
1 <!DOCTYPE html>
2 <!--
3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
6 -->
7 <link rel="import" href="/base/iteration_helpers.html">
8 <link rel="import" href="/base/rect.html">
9 <script>
10 'use strict';
11
12 tv.exportTo('tv', function() {
13   /**
14    * Adds a {@code getInstance} static method that always return the same
15    * instance object.
16    * @param {!Function} ctor The constructor for the class to add the static
17    *     method to.
18    */
19   function addSingletonGetter(ctor) {
20     ctor.getInstance = function() {
21       return ctor.instance_ || (ctor.instance_ = new ctor());
22     };
23   }
24
25   function instantiateTemplate(selector, doc) {
26     doc = doc || document;
27     var el = doc.querySelector(selector);
28     if (!el)
29       throw new Error('Element not found');
30     return el.createInstance();
31   }
32
33   function tracedFunction(fn, name, opt_this) {
34     function F() {
35       console.time(name);
36       try {
37         fn.apply(opt_this, arguments);
38       } finally {
39         console.timeEnd(name);
40       }
41     }
42     return F;
43   }
44
45   function normalizeException(e) {
46     if (typeof(e) == 'string') {
47       return {
48         message: e,
49         stack: ['<unknown>']
50       };
51     }
52
53     return {
54       message: e.message,
55       stack: e.stack ? e.stack : ['<unknown>']
56     };
57   }
58
59   function stackTrace() {
60     var stack = new Error().stack + '';
61     stack = stack.split('\n');
62     return stack.slice(2);
63   }
64
65   function windowRectForElement(element) {
66     var position = [element.offsetLeft, element.offsetTop];
67     var size = [element.offsetWidth, element.offsetHeight];
68     var node = element.offsetParent;
69     while (node) {
70       position[0] += node.offsetLeft;
71       position[1] += node.offsetTop;
72       node = node.offsetParent;
73     }
74     return tv.Rect.fromXYWH(position[0], position[1], size[0], size[1]);
75   }
76
77   function clamp(x, lo, hi) {
78     return Math.min(Math.max(x, lo), hi);
79   }
80
81   function lerp(percentage, lo, hi) {
82     var range = hi - lo;
83     return lo + percentage * range;
84   }
85
86   function deg2rad(deg) {
87     return (Math.PI * deg) / 180.0;
88   }
89
90   function scrollIntoViewIfNeeded(el) {
91     var pr = el.parentElement.getBoundingClientRect();
92     var cr = el.getBoundingClientRect();
93     if (cr.top < pr.top) {
94       el.scrollIntoView(true);
95     } else if (cr.bottom > pr.bottom) {
96       el.scrollIntoView(false);
97     }
98   }
99
100   function getUsingPath(path, from_dict) {
101     var parts = path.split('.');
102     var cur = from_dict;
103
104     for (var part; parts.length && (part = parts.shift());) {
105       if (!parts.length) {
106         return cur[part];
107       } else if (part in cur) {
108         cur = cur[part];
109       } else {
110         return undefined;
111       }
112     }
113     return undefined;
114   }
115
116   return {
117     addSingletonGetter: addSingletonGetter,
118
119     tracedFunction: tracedFunction,
120     normalizeException: normalizeException,
121     instantiateTemplate: instantiateTemplate,
122     stackTrace: stackTrace,
123
124     windowRectForElement: windowRectForElement,
125
126     scrollIntoViewIfNeeded: scrollIntoViewIfNeeded,
127
128     clamp: clamp,
129     lerp: lerp,
130     deg2rad: deg2rad,
131
132     getUsingPath: getUsingPath
133   };
134 });
135 </script>