Passed
Push — master ( cddcf6...1d3855 )
by Alexander
01:59
created

tcms/static/js/jsonrpc.js   A

Complexity

Total Complexity 14
Complexity/F 1.4

Size

Lines of Code 72
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
eloc 40
nc 1
dl 0
loc 72
rs 10
c 1
b 0
f 0
wmc 14
mnd 2
bc 15
fnc 10
bpm 1.5
cpm 1.4
noi 3

3 Functions

Rating   Name   Duplication   Size   Complexity  
A jsonrpc.js ➔ addResourceToData 0 16 1
A jsonrpc.js ➔ renderFromCache 0 8 1
A jsonrpc.js ➔ dataTableJsonRPC 0 11 1
1
// JSON-RPC client inspired by
2
// https://stackoverflow.com/questions/8147211/jquery-jsonrpc-2-0-call-via-ajax-gets-correct-response-but-does-not-work
3
function jsonRPC(rpc_method, rpc_params, callback, is_sync) {
4
   // .filter() args are passed as dictionary but other args,
5
   // e.g. for .add_tag() are passed as a list of positional values
6
   if (!Array.isArray(rpc_params)) {
7
      rpc_params = [rpc_params]
8
   }
9
10
   $.ajax({
11
      url: '/json-rpc/',
12
      async: is_sync !== true,
13
      data: JSON.stringify({jsonrpc: '2.0',
14
                            method: rpc_method,
15
                            params: rpc_params,
16
                            id:"jsonrpc"}),  // id is needed !!
17
      // see "Request object" at https://www.jsonrpc.org/specification
18
      type:"POST",
19
      dataType:"json",
20
      contentType: "application/json",
21
      success: function (result) {
22
            callback(result.result);
23
      },
24
      error: function (err,status,thrown) {
25
             console.log("*** jsonRPC ERROR: " + err + " STATUS: " + status + " " + thrown );
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
26
      },
27
   });
28
}
29
30
31
// used by DataTables to convert a list of objects to a dict
32
// suitable for loading data into the table
33
function dataTableJsonRPC(rpc_method, rpc_params, callback, pre_process_data) {
34
    var internal_callback = function(data) {
35
        // used to collect additional information about columns via ForeignKeys
36
        if (pre_process_data !== undefined) {
37
            pre_process_data(data);
38
        }
39
        callback({'data': data})
40
    };
41
42
    jsonRPC(rpc_method, rpc_params, internal_callback);
43
}
44
45
46
// called from pre_process_data to fill local cache with values
47
function addResourceToData(element, key, resource, cache) {
48
    var data = [];
49
    element[key].forEach(function(id) {
50
        if (id in cache) {
51
            data.push(cache[id]);
52
        } else {
53
            jsonRPC(resource, {pk: id}, function (result) {
54
                if (result) {
55
                    data.push(result[0]);
56
                    cache[id] = result[0];
57
                }
58
            }, true);
59
        }
60
    });
61
    element[key] = data;
62
}
63
64
65
// renders values from the local cache
66
// for example tags or components
67
function renderFromCache(data) {
68
    result = '';
0 ignored issues
show
Bug introduced by
The variable result 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.result.
Loading history...
69
    data.forEach(function (el) {
70
        result += el.name + ', ';
0 ignored issues
show
Bug introduced by
The variable result 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.result.
Loading history...
71
    });
72
    // remove trailing coma
73
    return result.slice(0, result.lastIndexOf(','));
74
}
75