meshviewer/lib/utils/router.js

157 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

2017-03-05 11:29:21 +00:00
define(['Navigo'], function (Navigo) {
'use strict';
return function (language) {
var init = false;
var objects = { nodes: {}, links: {} };
var targets = [];
var views = {};
var current = {};
var state = { lang: language.getLocale(), view: 'map' };
function resetView() {
targets.forEach(function (t) {
t.resetView();
});
}
function gotoNode(d) {
if (d.nodeId in objects.nodes) {
targets.forEach(function (t) {
t.gotoNode(objects.nodes[d.nodeId]);
});
}
}
function gotoLink(d) {
if (d.linkId in objects.links) {
targets.forEach(function (t) {
t.gotoLink(objects.links[d.linkId]);
});
}
}
function view(d) {
if (d.view in views) {
views[d.view]();
state.view = d.view;
resetView();
}
}
function customRoute(lang, viewValue, node, link, zoom, lat, lng) {
current = {
lang: lang,
view: viewValue,
node: node,
link: link,
zoom: zoom,
lat: lat,
lng: lng
};
if (lang && lang !== state.lang && lang === language.getLocale(lang)) {
language.setLocale(lang);
}
if (!init || viewValue && viewValue !== state.view) {
if (!viewValue) {
viewValue = state.view;
}
view({ view: viewValue });
init = true;
}
if (node) {
gotoNode({ nodeId: node });
} else if (link) {
gotoLink({ linkId: link });
} else if (lat) {
targets.forEach(function (t) {
t.gotoLocation({
zoom: parseInt(zoom, 10),
lat: parseFloat(lat),
lng: parseFloat(lng)
});
});
} else {
resetView();
}
}
var router = new Navigo(null, true);
router
.on(/^\/?#?\/([\w]{2})?\/?(map|graph)?\/?([a-f\d]{12})?([a-f\d\-]{25})?\/?(?:(\d+)\/([\d.]+)\/([\d.]+))?$/, customRoute)
.on({
'*': function () {
router.fullUrl();
}
});
router.generateLink = function generateLink(data, full, deep) {
var result = '#';
if (full) {
data = Object.assign({}, state, data);
} else if (deep) {
data = Object.assign({}, current, data);
}
for (var key in data) {
if (!data.hasOwnProperty(key) || data[key] === undefined) {
continue;
}
result += '/' + data[key];
}
return result;
};
router.fullUrl = function fullUrl(data, e, deep) {
if (e) {
e.preventDefault();
}
router.navigate(router.generateLink(data, !deep, deep));
};
router.getLang = function getLang() {
var lang = location.hash.match(/^\/?#\/([\w]{2})\//);
if (lang) {
state.lang = language.getLocale(lang[1]);
return lang[1];
}
return null;
};
router.addTarget = function addTarget(d) {
targets.push(d);
};
router.removeTarget = function removeTarget(d) {
targets = targets.filter(function (e) {
return d !== e;
});
};
router.addView = function addView(k, d) {
views[k] = d;
};
router.setData = function setData(data) {
objects.nodes = {};
objects.links = {};
data.nodes.all.forEach(function (d) {
objects.nodes[d.nodeinfo.node_id] = d;
});
data.graph.links.forEach(function (d) {
objects.links[d.id] = d;
});
};
return router;
};
});