Conditions | 1 |
Paths | 4 |
Total Lines | 155 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | define(['Navigo'], function (Navigo) { |
||
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 |