1
|
|
|
/** |
2
|
|
|
* Utility functions. |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
jsondash = jsondash || {util: {}}; |
|
|
|
|
6
|
|
|
|
7
|
|
|
jsondash.util.getValidParamString = function(arr) { |
8
|
|
|
// Jquery $.serialize and $.serializeArray will |
9
|
|
|
// return empty query parameters, which is undesirable and can |
10
|
|
|
// be error prone for RESTFUL endpoints. |
11
|
|
|
// e.g. `foo=bar&bar=` becomes `foo=bar` |
12
|
|
|
var param_str = ''; |
13
|
|
|
arr = arr.filter(function(param, i){return param.value !== '';}); |
|
|
|
|
14
|
|
|
$.each(arr, function(i, param){ |
15
|
|
|
param_str += (param.name + '=' + param.value); |
16
|
|
|
if(i < arr.length - 1 && arr.length > 1) param_str += '&'; |
|
|
|
|
17
|
|
|
}); |
18
|
|
|
return param_str; |
19
|
|
|
}; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* [intervalStrToMS Convert a string formatted to indicate] |
23
|
|
|
* @param {[String]} ival_fmt [The interval format string e.g. "1d", "2h"] |
24
|
|
|
* @return {[Number]} [The number of milliseconds] |
25
|
|
|
*/ |
26
|
|
|
jsondash.util.intervalStrToMS = function(ival_fmt) { |
27
|
|
|
// Just return number if it's a regular integer. |
28
|
|
|
if(!isNaN(ival_fmt)) { |
29
|
|
|
return ival_fmt; |
30
|
|
|
} |
31
|
|
|
var pieces = ival_fmt.split('-'); |
32
|
|
|
var amt = parseInt(pieces[0], 10); |
33
|
|
|
if(pieces.length !== 2 || isNaN(amt) || amt === 0) { |
34
|
|
|
// Force NO value if the format is invalid. |
35
|
|
|
// This would be used to ensure the interval |
36
|
|
|
// is not set in the first place. |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
var ival = pieces[1].toLowerCase(); |
40
|
|
|
var ms2s = 1000; |
41
|
|
|
var ms2min = 60 * ms2s; |
42
|
|
|
var ms2hr = 60 * ms2min; |
43
|
|
|
var ms2day = 24 * ms2hr; |
44
|
|
|
|
45
|
|
|
// Seconds |
46
|
|
|
if(ival === 's') { |
47
|
|
|
return amt * ms2s; |
48
|
|
|
} |
49
|
|
|
// Minutes |
50
|
|
|
if(ival === 'm') { |
51
|
|
|
return amt * ms2min; |
52
|
|
|
} |
53
|
|
|
// Hours |
54
|
|
|
if(ival === 'h') { |
55
|
|
|
return amt * ms2hr; |
56
|
|
|
} |
57
|
|
|
// Days |
58
|
|
|
if(ival === 'd') { |
59
|
|
|
return amt * ms2day; |
60
|
|
|
} |
61
|
|
|
// Anything else is invalid. |
62
|
|
|
return null; |
63
|
|
|
}; |
64
|
|
|
|
65
|
|
|
jsondash.util.serializeToJSON = function(arr) { |
66
|
|
|
// Convert form data to a proper json value |
67
|
|
|
var json = {}; |
68
|
|
|
$.each(arr, function(_, pair){ |
69
|
|
|
json[pair.name] = pair.value; |
70
|
|
|
}); |
71
|
|
|
return json; |
72
|
|
|
}; |
73
|
|
|
|
74
|
|
|
jsondash.util.isOverride = function(config) { |
75
|
|
|
return config.override && config.override === true; |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
|
// Credit: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript |
79
|
|
|
jsondash.util.s4 = function() { |
80
|
|
|
return Math.floor((1 + Math.random()) * 0x10000) |
81
|
|
|
.toString(16) |
82
|
|
|
.substring(1); |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
jsondash.util.guid = function() { |
86
|
|
|
var s4 = jsondash.util.s4; |
87
|
|
|
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + |
88
|
|
|
s4() + '-' + s4() + s4() + s4(); |
89
|
|
|
}; |
90
|
|
|
|
91
|
|
|
jsondash.util.polygon = function(d) { |
92
|
|
|
return "M" + d.join("L") + "Z"; |
93
|
|
|
}; |
94
|
|
|
|
95
|
|
|
jsondash.util.scaleStr = function(x, y) { |
96
|
|
|
return 'scale(' + x + ',' + y + ')'; |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
jsondash.util.translateStr = function(x, y) { |
100
|
|
|
return 'translate(' + x + ',' + y + ')'; |
101
|
|
|
}; |
102
|
|
|
|
103
|
|
|
jsondash.util.isD3Subtype = function(config) { |
104
|
|
|
// Handle specific D3 types that aren't necessarily referenced under |
105
|
|
|
// the D3 namespace in a select field. |
106
|
|
|
if(config.type === 'dendrogram') return true; |
|
|
|
|
107
|
|
|
if(config.type === 'voronoi') return true; |
|
|
|
|
108
|
|
|
if(config.type === 'circlepack') return true; |
|
|
|
|
109
|
|
|
if(config.type === 'treemap') return true; |
|
|
|
|
110
|
|
|
if(config.type === 'radial-dendrogram') return true; |
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
}; |
113
|
|
|
|
114
|
|
|
jsondash.util.isSparkline = function(type) { |
115
|
|
|
return type.substr(0, 10) === 'sparklines'; |
116
|
|
|
}; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* [getDigitSize return a d3 scale for adjusting font size based |
120
|
|
|
* on digits and width of container.] |
121
|
|
|
*/ |
122
|
|
|
jsondash.util.getDigitSize = function() { |
123
|
|
|
var BOX_PADDING = 20; |
|
|
|
|
124
|
|
|
// scale value is reversed, since we want |
125
|
|
|
// the font-size to get smaller as the number gets longer. |
126
|
|
|
var scale = d3.scale.linear() |
|
|
|
|
127
|
|
|
.clamp(true) |
128
|
|
|
.domain([2, 14]) // min/max digits length: $0 - $999,999,999.00 |
129
|
|
|
.range([90, 30]); // max/min font-size |
130
|
|
|
window.scale = scale; |
131
|
|
|
return scale; |
132
|
|
|
}; |
133
|
|
|
|
134
|
|
|
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
135
|
|
|
module.exports = jsondash; |
136
|
|
|
} |
137
|
|
|
|