meshviewer/lib/utils/router.js

151 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-03-05 11:29:21 +00:00
define(['Navigo'], function (Navigo) {
'use strict';
return function (language) {
var init = false;
2017-10-31 12:32:39 +00:00
var objects = {};
2017-03-05 11:29:21 +00:00
var targets = [];
var views = {};
var current = {};
var state = { lang: language.getLocale(), view: 'map' };
function resetView() {
targets.forEach(function (t) {
t.resetView();
});
}
function gotoNode(d) {
2017-10-31 12:32:39 +00:00
if (objects.nodeDict[d.nodeId]) {
2017-03-05 11:29:21 +00:00
targets.forEach(function (t) {
2017-10-31 12:32:39 +00:00
t.gotoNode(objects.nodeDict[d.nodeId], objects.nodeDict);
2017-03-05 11:29:21 +00:00
});
}
}
function gotoLink(d) {
var link = objects.links.filter(function (value) {
2017-10-31 12:32:39 +00:00
return value.id === d.linkId;
});
if (link) {
2017-03-05 11:29:21 +00:00
targets.forEach(function (t) {
2017-10-31 12:32:39 +00:00
t.gotoLink(link);
2017-03-05 11:29:21 +00:00
});
}
}
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)) {
2018-08-01 23:58:41 +00:00
location.reload();
2017-03-05 11:29:21 +00:00
}
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();
}
}
2018-07-22 13:30:09 +00:00
var router = new Navigo(null, true, '#!');
2017-03-05 11:29:21 +00:00
router
2018-07-22 13:30:09 +00:00
.on(/^\/?#?!?\/([\w]{2})?\/?(map|graph)?\/?([a-f\d]{12})?([a-f\d\-]{25})?\/?(?:(\d+)\/(-?[\d.]+)\/(-?[\d.]+))?$/, customRoute)
2017-03-05 11:29:21 +00:00
.on({
'*': function () {
router.fullUrl();
}
});
router.generateLink = function generateLink(data, full, deep) {
2018-07-22 13:30:09 +00:00
var result = '#!';
2017-03-05 11:29:21 +00:00
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() {
2018-07-22 13:30:09 +00:00
var lang = location.hash.match(/^\/?#!?\/([\w]{2})\//);
2017-03-05 11:29:21 +00:00
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) {
2017-10-31 12:32:39 +00:00
objects = data;
2017-03-05 11:29:21 +00:00
};
return router;
};
});