Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / src / base / utils.js
1 // Copyright (c) 2013 The Chromium 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
5 'use strict';
6
7 base.require('base.iteration_helpers');
8 base.require('base.rect');
9
10 base.exportTo('base', function() {
11   /**
12    * Adds a {@code getInstance} static method that always return the same
13    * instance object.
14    * @param {!Function} ctor The constructor for the class to add the static
15    *     method to.
16    */
17   function addSingletonGetter(ctor) {
18     ctor.getInstance = function() {
19       return ctor.instance_ || (ctor.instance_ = new ctor());
20     };
21   }
22
23   function instantiateTemplate(selector) {
24     return document.querySelector(selector).content.cloneNode(true);
25   }
26
27   function tracedFunction(fn, name, opt_this) {
28     function F() {
29       console.time(name);
30       try {
31         fn.apply(opt_this, arguments);
32       } finally {
33         console.timeEnd(name);
34       }
35     }
36     return F;
37   }
38
39   function normalizeException(e) {
40     if (typeof(e) == 'string') {
41       return {
42         message: e,
43         stack: ['<unknown>']
44       };
45     }
46
47     return {
48       message: e.message,
49       stack: e.stack ? e.stack : ['<unknown>']
50     };
51   }
52
53   function stackTrace() {
54     var stack = new Error().stack + '';
55     stack = stack.split('\n');
56     return stack.slice(2);
57   }
58
59   function windowRectForElement(element) {
60     var position = [element.offsetLeft, element.offsetTop];
61     var size = [element.offsetWidth, element.offsetHeight];
62     var node = element.offsetParent;
63     while (node) {
64       position[0] += node.offsetLeft;
65       position[1] += node.offsetTop;
66       node = node.offsetParent;
67     }
68     return base.Rect.fromXYWH(position[0], position[1], size[0], size[1]);
69   }
70
71   function clamp(x, lo, hi) {
72     return Math.min(Math.max(x, lo), hi);
73   }
74
75   function lerp(percentage, lo, hi) {
76     var range = hi - lo;
77     return lo + percentage * range;
78   }
79
80   function deg2rad(deg) {
81     return (Math.PI * deg) / 180.0;
82   }
83
84   function scrollIntoViewIfNeeded(el) {
85     var pr = el.parentElement.getBoundingClientRect();
86     var cr = el.getBoundingClientRect();
87     if (cr.top < pr.top) {
88       el.scrollIntoView(true);
89     } else if (cr.bottom > pr.bottom) {
90       el.scrollIntoView(false);
91     }
92   }
93
94   return {
95     addSingletonGetter: addSingletonGetter,
96
97     tracedFunction: tracedFunction,
98     normalizeException: normalizeException,
99     instantiateTemplate: instantiateTemplate,
100     stackTrace: stackTrace,
101
102     windowRectForElement: windowRectForElement,
103
104     scrollIntoViewIfNeeded: scrollIntoViewIfNeeded,
105
106     clamp: clamp,
107     lerp: lerp,
108     deg2rad: deg2rad
109   };
110 });