Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / fusion / external / d3 / js / core.js
1
2 var nv = window.nv || {};
3
4
5 nv.version = '1.1.13b';
6 nv.dev = true //set false when in production
7
8 window.nv = nv;
9
10 nv.tooltip = {}; // For the tooltip system
11 nv.utils = nv.utils || {}; // Utility subsystem
12 nv.models = {}; //stores all the possible models/components
13 nv.charts = {}; //stores all the ready to use charts
14 nv.graphs = []; //stores all the graphs currently on the page
15 nv.logs = {}; //stores some statistics and potential error messages
16
17 nv.dispatch = d3.dispatch('render_start', 'render_end');
18
19 // *************************************************************************
20 //  Development render timers - disabled if dev = false
21
22 if (nv.dev) {
23   nv.dispatch.on('render_start', function(e) {
24     nv.logs.startTime = +new Date();
25   });
26
27   nv.dispatch.on('render_end', function(e) {
28     nv.logs.endTime = +new Date();
29     nv.logs.totalTime = nv.logs.endTime - nv.logs.startTime;
30     nv.log('total', nv.logs.totalTime); // used for development, to keep track of graph generation times
31   });
32 }
33
34 // ********************************************
35 //  Public Core NV functions
36
37 // Logs all arguments, and returns the last so you can test things in place
38 // Note: in IE8 console.log is an object not a function, and if modernizr is used
39 // then calling Function.prototype.bind with with anything other than a function
40 // causes a TypeError to be thrown.
41 nv.log = function() {
42   if (nv.dev && console.log && console.log.apply)
43     console.log.apply(console, arguments)
44   else if (nv.dev && typeof console.log == "function" && Function.prototype.bind) {
45     var log = Function.prototype.bind.call(console.log, console);
46     log.apply(console, arguments);
47   }
48   return arguments[arguments.length - 1];
49 };
50
51
52 nv.render = function render(step) {
53   step = step || 1; // number of graphs to generate in each timeout loop
54
55   nv.render.active = true;
56   nv.dispatch.render_start();
57
58   setTimeout(function() {
59     var chart, graph;
60
61     for (var i = 0; i < step && (graph = nv.render.queue[i]); i++) {
62       chart = graph.generate();
63       if (typeof graph.callback == typeof(Function)) graph.callback(chart);
64       nv.graphs.push(chart);
65     }
66
67     nv.render.queue.splice(0, i);
68
69     if (nv.render.queue.length) setTimeout(arguments.callee, 0);
70     else { nv.render.active = false; nv.dispatch.render_end(); }
71   }, 0);
72 };
73
74 nv.render.active = false;
75 nv.render.queue = [];
76
77 nv.addGraph = function(obj) {
78   if (typeof arguments[0] === typeof(Function))
79     obj = {generate: arguments[0], callback: arguments[1]};
80
81   nv.render.queue.push(obj);
82
83   if (!nv.render.active) nv.render();
84 };
85
86 nv.identity = function(d) { return d; };
87
88 nv.strip = function(s) { return s.replace(/(\s|&)/g,''); };
89
90 function daysInMonth(month,year) {
91   return (new Date(year, month+1, 0)).getDate();
92 }
93
94 function d3_time_range(floor, step, number) {
95   return function(t0, t1, dt) {
96     var time = floor(t0), times = [];
97     if (time < t0) step(time);
98     if (dt > 1) {
99       while (time < t1) {
100         var date = new Date(+time);
101         if ((number(date) % dt === 0)) times.push(date);
102         step(time);
103       }
104     } else {
105       while (time < t1) { times.push(new Date(+time)); step(time); }
106     }
107     return times;
108   };
109 }
110
111 d3.time.monthEnd = function(date) {
112   return new Date(date.getFullYear(), date.getMonth(), 0);
113 };
114
115 d3.time.monthEnds = d3_time_range(d3.time.monthEnd, function(date) {
116     date.setUTCDate(date.getUTCDate() + 1);
117     date.setDate(daysInMonth(date.getMonth() + 1, date.getFullYear()));
118   }, function(date) {
119     return date.getMonth();
120   }
121 );
122