Completed
Pull Request — develop (#123)
by Xaver
01:08
created

router.js ➔ ... ➔   B

Complexity

Conditions 1
Paths 4

Size

Total Lines 155

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 4
dl 0
loc 155
rs 8.2857
nop 2

14 Functions

Rating   Name   Duplication   Size   Complexity  
A �� resetView 0 5 1
A �� gotoNode 0 7 2
A �� view 0 7 2
A �� gotoLink 0 7 2
B �� generateLink 0 18 7
A �� setData 0 12 1
A �� reset 0 3 1
A �� router.on.* 0 3 1
A �� addView 0 3 1
A �� fullUrl 0 6 2
A �� removeTarget 0 5 1
A �� gotoLocation 0 5 1
A �� addTarget 0 3 1
C �� customRoute 0 39 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
define(['Navigo'], function (Navigo) {
2
  'use strict';
3
4
  return function (config, language) {
5
    var init = false;
6
    var objects = { nodes: {}, links: {} };
7
    var targets = [];
8
    var views = {};
9
    var current = {};
10
    var state = { language: _.locale(), view: 'map' };
11
12
    function resetView() {
13
      targets.forEach(function (t) {
14
        t.resetView();
15
      });
16
    }
17
18
    function gotoNode(d, update) {
19
      if (d.nodeId in objects.nodes) {
20
        targets.forEach(function (t) {
21
          t.gotoNode(objects.nodes[d.nodeId], update);
22
        });
23
      }
24
    }
25
26
    function gotoLink(d, update) {
27
      if (d.linkId in objects.links) {
28
        targets.forEach(function (t) {
29
          t.gotoLink(objects.links[d.linkId], update);
30
        });
31
      }
32
    }
33
34
    function view(d) {
35
      if (d.view in views) {
36
        views[d.view]();
37
        state.view = d.view;
38
        resetView();
39
      }
40
    }
41
42
    function customRoute(lang, viewValue, node, link, zoom, lat, lng) {
43
      current = {
44
        lang: lang,
45
        view: viewValue,
46
        node: node,
47
        link: link,
48
        zoom: zoom,
49
        lat: lat,
50
        lng: lng
51
      };
52
53
      if (state.language !== lang) {
54
        language.setLocale(lang);
55
      }
56
57
      if (!init || state.view !== viewValue) {
58
        if (!viewValue) {
59
          viewValue = state.view;
60
        }
61
        view({ view: viewValue });
62
        init = true;
63
      }
64
65
      if (node) {
66
        gotoNode({ nodeId: node });
67
      } else if (link) {
68
        gotoLink({ linkId: link });
69
      } else if (lat) {
70
        targets.forEach(function (t) {
71
          t.gotoLocation({
72
            zoom: parseInt(zoom, 10),
73
            lat: parseFloat(lat),
74
            lng: parseFloat(lng)
75
          });
76
        });
77
      } else {
78
        resetView();
79
      }
80
    }
81
82
    var root = null;
83
    var useHash = true;
84
    const router = new Navigo(root, useHash);
85
86
    router
87
      .on(/^\/?#?\/([\w]{2})?\/?(map|graph)?\/?([a-f\d]{12})?([a-f\d\-]{25})?\/?(?:(\d+)\/([\d.]+)\/([\d.]+))?$/, customRoute)
88
      .on({
89
        '*': function () {
90
          router.fullUrl();
91
        }
92
      });
93
94
    router.generateLink = function generateLink(data, full, deep) {
95
      var result = '';
96
97
      if (full) {
98
        data = Object.assign({}, state, data);
99
      } else if (deep) {
100
        data = Object.assign({}, current, data);
101
      }
102
103
      for (var key in data) {
104
        if (!data.hasOwnProperty(key) || data[key] === undefined) {
105
          continue;
106
        }
107
        result += '/' + data[key];
108
      }
109
110
      return useHash ? '#' + result : result;
111
    };
112
113
    router.fullUrl = function fullUrl(data, e) {
114
      if (e) {
115
        e.preventDefault();
116
      }
117
      router.navigate(router.generateLink(data, true, true));
118
    };
119
120
    router.gotoLocation = function gotoLocation(d) {
121
      targets.forEach(function (t) {
122
        t.gotoLocation(d);
123
      });
124
    };
125
126
    router.reset = function reset() {
127
      resetView();
128
    };
129
130
    router.addTarget = function addTarget(d) {
131
      targets.push(d);
132
    };
133
134
    router.removeTarget = function removeTarget(d) {
135
      targets = targets.filter(function (e) {
136
        return d !== e;
137
      });
138
    };
139
140
    router.addView = function addView(k, d) {
141
      views[k] = d;
142
    };
143
144
    router.setData = function setData(data) {
145
      objects.nodes = {};
146
      objects.links = {};
147
148
      data.nodes.all.forEach(function (d) {
149
        objects.nodes[d.nodeinfo.node_id] = d;
150
      });
151
152
      data.graph.links.forEach(function (d) {
153
        objects.links[d.id] = d;
154
      });
155
    };
156
157
    return router;
158
  };
159
});
160