Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / fusion / external / d3 / js / utils.js
1
2 nv.utils.windowSize = function() {
3     // Sane defaults
4     var size = {width: 640, height: 480};
5
6     // Earlier IE uses Doc.body
7     if (document.body && document.body.offsetWidth) {
8         size.width = document.body.offsetWidth;
9         size.height = document.body.offsetHeight;
10     }
11
12     // IE can use depending on mode it is in
13     if (document.compatMode=='CSS1Compat' &&
14         document.documentElement &&
15         document.documentElement.offsetWidth ) {
16         size.width = document.documentElement.offsetWidth;
17         size.height = document.documentElement.offsetHeight;
18     }
19
20     // Most recent browsers use
21     if (window.innerWidth && window.innerHeight) {
22         size.width = window.innerWidth;
23         size.height = window.innerHeight;
24     }
25     return (size);
26 };
27
28
29
30 // Easy way to bind multiple functions to window.onresize
31 // TODO: give a way to remove a function after its bound, other than removing all of them
32 nv.utils.windowResize = function(fun){
33   if (fun === undefined) return;
34   var oldresize = window.onresize;
35
36   window.onresize = function(e) {
37     if (typeof oldresize == 'function') oldresize(e);
38     fun(e);
39   }
40 }
41
42 // Backwards compatible way to implement more d3-like coloring of graphs.
43 // If passed an array, wrap it in a function which implements the old default
44 // behavior
45 nv.utils.getColor = function(color) {
46     if (!arguments.length) return nv.utils.defaultColor(); //if you pass in nothing, get default colors back
47
48     if( Object.prototype.toString.call( color ) === '[object Array]' )
49         return function(d, i) { return d.color || color[i % color.length]; };
50     else
51         return color;
52         //can't really help it if someone passes rubbish as color
53 }
54
55 // Default color chooser uses the index of an object as before.
56 nv.utils.defaultColor = function() {
57     var colors = d3.scale.category20().range();
58     return function(d, i) { return d.color || colors[i % colors.length] };
59 }
60
61
62 // Returns a color function that takes the result of 'getKey' for each series and
63 // looks for a corresponding color from the dictionary,
64 nv.utils.customTheme = function(dictionary, getKey, defaultColors) {
65   getKey = getKey || function(series) { return series.key }; // use default series.key if getKey is undefined
66   defaultColors = defaultColors || d3.scale.category20().range(); //default color function
67
68   var defIndex = defaultColors.length; //current default color (going in reverse)
69
70   return function(series, index) {
71     var key = getKey(series);
72
73     if (!defIndex) defIndex = defaultColors.length; //used all the default colors, start over
74
75     if (typeof dictionary[key] !== "undefined")
76       return (typeof dictionary[key] === "function") ? dictionary[key]() : dictionary[key];
77     else
78       return defaultColors[--defIndex]; // no match in dictionary, use default color
79   }
80 }
81
82
83
84 // From the PJAX example on d3js.org, while this is not really directly needed
85 // it's a very cool method for doing pjax, I may expand upon it a little bit,
86 // open to suggestions on anything that may be useful
87 nv.utils.pjax = function(links, content) {
88   d3.selectAll(links).on("click", function() {
89     history.pushState(this.href, this.textContent, this.href);
90     load(this.href);
91     d3.event.preventDefault();
92   });
93
94   function load(href) {
95     d3.html(href, function(fragment) {
96       var target = d3.select(content).node();
97       target.parentNode.replaceChild(d3.select(fragment).select(content).node(), target);
98       nv.utils.pjax(links, content);
99     });
100   }
101
102   d3.select(window).on("popstate", function() {
103     if (d3.event.state) load(d3.event.state);
104   });
105 }
106
107 /* For situations where we want to approximate the width in pixels for an SVG:text element.
108 Most common instance is when the element is in a display:none; container. 
109 Forumla is : text.length * font-size * constant_factor
110 */
111 nv.utils.calcApproxTextWidth = function (svgTextElem) {
112     if (svgTextElem instanceof d3.selection) {
113         var fontSize = parseInt(svgTextElem.style("font-size").replace("px",""));
114         var textLength = svgTextElem.text().length;
115
116         return textLength * fontSize * 0.5; 
117     }
118     return 0;
119 };
120
121 /* Numbers that are undefined, null or NaN, convert them to zeros.
122 */
123 nv.utils.NaNtoZero = function(n) {
124     if (typeof n !== 'number' 
125         || isNaN(n) 
126         || n === null
127         || n === Infinity) return 0;
128
129     return n;
130 };
131
132 /*
133 Snippet of code you can insert into each nv.models.* to give you the ability to
134 do things like:
135 chart.options({
136   showXAxis: true,
137   tooltips: true
138 });
139
140 To enable in the chart:
141 chart.options = nv.utils.optionsFunc.bind(chart);
142 */
143 nv.utils.optionsFunc = function(args) {
144     if (args) {
145       d3.map(args).forEach((function(key,value) {
146         if (typeof this[key] === "function") {
147            this[key](value);
148         }
149       }).bind(this));
150     }
151     return this;
152 };