1 | /** |
||
2 | * Utility functions. |
||
3 | */ |
||
4 | |||
5 | jsondash = jsondash || {util: {}}; |
||
6 | |||
7 | jsondash.util.getCSSClasses = function(conf, defaults) { |
||
8 | var classes = {}; |
||
9 | if(conf.classes === undefined && defaults !== undefined) { |
||
10 | $.each(defaults, function(i, klass){ |
||
11 | classes[klass] = true; |
||
12 | }); |
||
13 | return classes; |
||
14 | } |
||
15 | if(conf.classes !== undefined) { |
||
16 | $.each(conf.classes, function(i, klass){ |
||
17 | classes[klass] = true; |
||
18 | }); |
||
19 | } |
||
20 | return classes; |
||
21 | }; |
||
22 | |||
23 | jsondash.util.getValidParamString = function(arr) { |
||
24 | // Jquery $.serialize and $.serializeArray will |
||
25 | // return empty query parameters, which is undesirable and can |
||
26 | // be error prone for RESTFUL endpoints. |
||
27 | // e.g. `foo=bar&bar=` becomes `foo=bar` |
||
28 | var param_str = ''; |
||
29 | arr = arr.filter(function(param, i){return param.value !== '';}); |
||
30 | $.each(arr, function(i, param){ |
||
31 | param_str += (param.name + '=' + param.value); |
||
32 | if(i < arr.length - 1 && arr.length > 1) param_str += '&'; |
||
33 | }); |
||
34 | return param_str; |
||
35 | }; |
||
36 | |||
37 | /** |
||
38 | * [reformatQueryParams Reformat params into a query string.] |
||
39 | * @param {[type]} old [List of query params] |
||
40 | * @param {[type]} new [List of query params] |
||
41 | * @return {[type]} [The new string (e.g. 'foo=bar&baz=1')] |
||
42 | For example: |
||
43 | old: foo=1&baz=1 |
||
44 | new: foo=2&quux=1 |
||
45 | expected: foo=2&quux=1&baz=1 |
||
46 | */ |
||
47 | jsondash.util.reformatQueryParams = function(oldp, newp) { |
||
48 | var _combined = {}; |
||
49 | var combined = ''; |
||
50 | var oldparams = {}; |
||
51 | var newparams = {}; |
||
52 | $.each(oldp ? oldp.split('&'): [], function(i, param){ |
||
53 | param = param.split('='); |
||
54 | oldparams[param[0]] = param[1]; |
||
55 | }); |
||
56 | $.each(newp ? newp.split('&'): [], function(i, param){ |
||
57 | param = param.split('='); |
||
58 | newparams[param[0]] = param[1]; |
||
59 | }); |
||
60 | _combined = $.extend(oldparams, newparams); |
||
61 | $.each(_combined, function(k, v){ |
||
62 | if(v !== undefined) { |
||
63 | combined += k + '=' + v + '&'; |
||
64 | } |
||
65 | }); |
||
66 | // Replace last ampersan if it exists. |
||
67 | if(combined.charAt(combined.length - 1) === '&') { |
||
68 | return combined.substring(0, combined.length - 1); |
||
69 | } |
||
70 | return combined; |
||
71 | }; |
||
72 | |||
73 | /** |
||
74 | * [isInDemoMode Check if app is in demo mode.] |
||
75 | */ |
||
76 | jsondash.util.isInDemoMode = function() { |
||
77 | var parts = window.location.href.split('?'); |
||
78 | var matches = parts.filter(function(part, _){ |
||
79 | return part === 'jsondash_demo_mode=1' || part === 'jsondash_demo_mode=true'; |
||
80 | }); |
||
81 | return matches.length > 0; |
||
82 | }; |
||
83 | |||
84 | /** |
||
85 | * [intervalStrToMS Convert a string formatted to indicate an interval to milliseconds] |
||
86 | * @param {[String]} ival_fmt [The interval format string e.g. "1-d", "2-h"] |
||
87 | * @return {[Number]} [The number of milliseconds] |
||
88 | */ |
||
89 | jsondash.util.intervalStrToMS = function(ival_fmt) { |
||
90 | if(ival_fmt === undefined || ival_fmt === '') { |
||
91 | return null; |
||
92 | } |
||
93 | // Just return number if it's a regular integer. |
||
94 | if(!isNaN(ival_fmt)) { |
||
95 | return ival_fmt; |
||
96 | } |
||
97 | var pieces = ival_fmt.split('-'); |
||
98 | var amt = parseInt(pieces[0], 10); |
||
99 | if(pieces.length !== 2 || isNaN(amt) || amt === 0) { |
||
100 | // Force NO value if the format is invalid. |
||
101 | // This would be used to ensure the interval |
||
102 | // is not set in the first place. |
||
103 | return null; |
||
104 | } |
||
105 | var ival = pieces[1].toLowerCase(); |
||
106 | var ms2s = 1000; |
||
107 | var ms2min = 60 * ms2s; |
||
108 | var ms2hr = 60 * ms2min; |
||
109 | var ms2day = 24 * ms2hr; |
||
110 | |||
111 | // Seconds |
||
112 | if(ival === 's') { |
||
113 | return amt * ms2s; |
||
114 | } |
||
115 | // Minutes |
||
116 | if(ival === 'm') { |
||
117 | return amt * ms2min; |
||
118 | } |
||
119 | // Hours |
||
120 | if(ival === 'h') { |
||
121 | return amt * ms2hr; |
||
122 | } |
||
123 | // Days |
||
124 | if(ival === 'd') { |
||
125 | return amt * ms2day; |
||
126 | } |
||
127 | // Anything else is invalid. |
||
128 | return null; |
||
129 | }; |
||
130 | |||
131 | jsondash.util.serializeToJSON = function(arr) { |
||
132 | // Convert form data to a proper json value |
||
133 | var json = {}; |
||
134 | $.each(arr, function(_, pair){ |
||
135 | json[pair.name] = pair.value; |
||
136 | }); |
||
137 | return json; |
||
138 | }; |
||
139 | |||
140 | jsondash.util.isOverride = function(config) { |
||
141 | return config.override && config.override === true; |
||
142 | }; |
||
143 | |||
144 | // Credit: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript |
||
145 | jsondash.util.s4 = function() { |
||
146 | return Math.floor((1 + Math.random()) * 0x10000) |
||
147 | .toString(16) |
||
148 | .substring(1); |
||
149 | }; |
||
150 | |||
151 | jsondash.util.guid = function() { |
||
152 | var s4 = jsondash.util.s4; |
||
153 | return s4() + s4() + '-' + s4() + '-' + s4() + '-' + |
||
154 | s4() + '-' + s4() + s4() + s4(); |
||
155 | }; |
||
156 | |||
157 | jsondash.util.polygon = function(d) { |
||
158 | return "M" + d.join("L") + "Z"; |
||
159 | }; |
||
160 | |||
161 | jsondash.util.scaleStr = function(x, y) { |
||
162 | return 'scale(' + x + ',' + y + ')'; |
||
163 | }; |
||
164 | |||
165 | jsondash.util.translateStr = function(x, y) { |
||
166 | return 'translate(' + x + ',' + y + ')'; |
||
167 | }; |
||
168 | |||
169 | /** |
||
170 | * [getDigitSize return a d3 scale for adjusting font size based |
||
171 | * on digits and width of container.] |
||
172 | */ |
||
173 | jsondash.util.getDigitSize = function() { |
||
174 | var BOX_PADDING = 20; |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
175 | // scale value is reversed, since we want |
||
176 | // the font-size to get smaller as the number gets longer. |
||
177 | var scale = d3.scale.linear() |
||
178 | .clamp(true) |
||
179 | .domain([2, 14]) // min/max digits length: $0 - $999,999,999.00 |
||
180 | .range([90, 30]); // max/min font-size |
||
181 | window.scale = scale; |
||
182 | return scale; |
||
183 | }; |
||
184 | |||
185 | if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
||
186 | module.exports = jsondash; |
||
187 | } |
||
188 |