| Conditions | 1 |
| Paths | 16 |
| Total Lines | 176 |
| 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) { |
||
| 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 (lang && _.locale() !== lang) { |
||
| 54 | language.setLocale(lang); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!init) { |
||
| 58 | if (viewValue) { |
||
| 59 | state.view = viewValue; |
||
| 60 | } |
||
| 61 | view({ view: state.view }); |
||
| 62 | init = true; |
||
| 63 | } |
||
| 64 | |||
| 65 | if (viewValue && state.view !== viewValue) { |
||
| 66 | view({ view: viewValue }); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (node) { |
||
| 70 | gotoNode({ nodeId: node }); |
||
| 71 | } else if (link) { |
||
| 72 | gotoLink({ linkId: link }); |
||
| 73 | } else if (lat) { |
||
| 74 | targets.forEach(function (t) { |
||
| 75 | t.gotoLocation({ |
||
| 76 | zoom: parseInt(zoom, 10), |
||
| 77 | lat: parseFloat(lat), |
||
| 78 | lng: parseFloat(lng) |
||
| 79 | }); |
||
| 80 | }); |
||
| 81 | } else { |
||
| 82 | resetView(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | var root = null; |
||
| 87 | var useHash = true; |
||
| 88 | const router = new Navigo(root, useHash); |
||
| 89 | |||
| 90 | router |
||
| 91 | .on(/^\/?#?\/([\w]{2})?\/?(map|graph)?\/?([a-f\d]{12})?([a-f\d\-]{25})?\/?(?:(\d+)\/([\d.]+)\/([\d.]+))?$/, customRoute) |
||
| 92 | .on({ |
||
| 93 | '*': function () { |
||
| 94 | router.fullUrl(); |
||
| 95 | } |
||
| 96 | }); |
||
| 97 | |||
| 98 | router.generateLink = function generateLink(data, full, deep, navigate) { |
||
| 99 | var result = ''; |
||
| 100 | |||
| 101 | if (full) { |
||
| 102 | data = Object.assign({}, state, data); |
||
| 103 | } else if (deep) { |
||
| 104 | data = Object.assign({}, current, data); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (data) { |
||
| 108 | for (var key in data) { |
||
| 109 | if (!data.hasOwnProperty(key)) { |
||
| 110 | continue; |
||
| 111 | } |
||
| 112 | if (data[key] !== undefined) { |
||
| 113 | result += '/' + data[key]; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return useHash && !navigate ? '#' + result : result; |
||
| 119 | }; |
||
| 120 | |||
| 121 | router.fullUrl = function fullUrl(data, e) { |
||
| 122 | if (e) { |
||
| 123 | e.preventDefault(); |
||
| 124 | } |
||
| 125 | router.navigate(router.generateLink(data, true, true)); |
||
| 126 | }; |
||
| 127 | |||
| 128 | router.gotoLocation = function gotoLocation(d) { |
||
| 129 | if (!d) { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | targets.forEach(function (t) { |
||
| 134 | if (!t.gotoLocation) { |
||
| 135 | console.warn('has no gotoLocation', t); |
||
| 136 | } |
||
| 137 | t.gotoLocation(d); |
||
| 138 | }); |
||
| 139 | |||
| 140 | return true; |
||
| 141 | }; |
||
| 142 | |||
| 143 | router.reset = function reset() { |
||
| 144 | resetView(); |
||
| 145 | }; |
||
| 146 | |||
| 147 | router.addTarget = function addTarget(d) { |
||
| 148 | targets.push(d); |
||
| 149 | }; |
||
| 150 | |||
| 151 | router.removeTarget = function removeTarget(d) { |
||
| 152 | targets = targets.filter(function (e) { |
||
| 153 | return d !== e; |
||
| 154 | }); |
||
| 155 | }; |
||
| 156 | |||
| 157 | router.addView = function addView(k, d) { |
||
| 158 | views[k] = d; |
||
| 159 | }; |
||
| 160 | |||
| 161 | router.setData = function setData(data) { |
||
| 162 | objects.nodes = {}; |
||
| 163 | objects.links = {}; |
||
| 164 | |||
| 165 | data.nodes.all.forEach(function (d) { |
||
| 166 | objects.nodes[d.nodeinfo.node_id] = d; |
||
| 167 | }); |
||
| 168 | |||
| 169 | data.graph.links.forEach(function (d) { |
||
| 170 | objects.links[d.id] = d; |
||
| 171 | }); |
||
| 172 | }; |
||
| 173 | |||
| 174 | return router; |
||
| 175 | }; |
||
| 176 | }); |
||
| 177 |