Completed
Push — master ( a10da3...25c806 )
by Chris
01:12
created

jsondash.util.translateStr   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
nc 1
nop 2
1
/**
2
 * Utility functions.
3
 */
4
5
jsondash = jsondash || {util: {}};
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable jsondash is declared in the current environment, consider using typeof jsondash === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Bug introduced by
The variable jsondash seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.jsondash.
Loading history...
6
7
jsondash.util.serializeToJSON = function(arr) {
8
    // Convert form data to a proper json value
9
    var json = {};
10
    $.each(arr, function(_, pair){
11
        json[pair.name] = pair.value;
12
    });
13
    return json;
14
};
15
16
jsondash.util.isOverride = function(config) {
17
    return config.override && config.override === true;
18
};
19
20
// Credit: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
21
jsondash.util.s4 = function() {
22
    return Math.floor((1 + Math.random()) * 0x10000)
23
    .toString(16)
24
    .substring(1);
25
};
26
27
jsondash.util.guid = function() {
28
    var s4 = jsondash.util.s4;
29
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
30
    s4() + '-' + s4() + s4() + s4();
31
};
32
33
jsondash.util.polygon = function(d) {
34
    return "M" + d.join("L") + "Z";
35
};
36
37
jsondash.util.scaleStr = function(x, y) {
38
    return 'scale(' + x + ',' + y + ')';
39
};
40
41
jsondash.util.translateStr = function(x, y) {
42
    return 'translate(' + x + ',' + y + ')';
43
};
44
45
jsondash.util.isD3Subtype = function(config) {
46
    // Handle specific D3 types that aren't necessarily referenced under
47
    // the D3 namespace in a select field.
48
    if(config.type === 'dendrogram') return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
49
    if(config.type === 'voronoi') return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
50
    if(config.type === 'circlepack') return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
51
    if(config.type === 'treemap') return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
52
    if(config.type === 'radial-dendrogram') return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
53
    return false;
54
};
55
56
jsondash.util.isSparkline = function(type) {
57
    return type.substr(0, 10) === 'sparklines';
58
};
59
60
/**
61
 * [getDigitSize return a d3 scale for adjusting font size based
62
 *     on digits and width of container.]
63
 */
64
jsondash.util.getDigitSize = function() {
65
    var BOX_PADDING = 20;
0 ignored issues
show
Unused Code introduced by
The variable BOX_PADDING seems to be never used. Consider removing it.
Loading history...
66
    // scale value is reversed, since we want
67
    // the font-size to get smaller as the number gets longer.
68
    var scale = d3.scale.linear()
0 ignored issues
show
Bug introduced by
The variable d3 seems to be never declared. If this is a global, consider adding a /** global: d3 */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
69
        .clamp(true)
70
        .domain([2, 14]) // min/max digits length: $0 - $999,999,999.00
71
        .range([90, 30]); // max/min font-size
72
    window.scale = scale;
73
    return scale;
74
};
75
76
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
77
    module.exports = jsondash;
78
}
79