change code style to ffrgb/meshviewer fork
This commit is contained in:
parent
59ba0ba29e
commit
418b630e02
42 changed files with 3505 additions and 3154 deletions
258
lib/router.js
258
lib/router.js
|
|
@ -1,214 +1,230 @@
|
|||
define(function () {
|
||||
return function () {
|
||||
var self = this
|
||||
var objects = { nodes: {}, links: {} }
|
||||
var targets = []
|
||||
var views = {}
|
||||
var currentView
|
||||
var currentObject
|
||||
var running = false
|
||||
var self = this;
|
||||
var objects = {nodes: {}, links: {}};
|
||||
var targets = [];
|
||||
var views = {};
|
||||
var currentView;
|
||||
var currentObject;
|
||||
var running = false;
|
||||
|
||||
function saveState() {
|
||||
var e = []
|
||||
var e = [];
|
||||
|
||||
if (currentView)
|
||||
e.push("v:" + currentView)
|
||||
|
||||
if (currentObject) {
|
||||
if ("node" in currentObject)
|
||||
e.push("n:" + encodeURIComponent(currentObject.node.nodeinfo.node_id))
|
||||
|
||||
if ("link" in currentObject)
|
||||
e.push("l:" + encodeURIComponent(currentObject.link.id))
|
||||
if (currentView) {
|
||||
e.push("v:" + currentView);
|
||||
}
|
||||
|
||||
var s = "#!" + e.join(";")
|
||||
if (currentObject) {
|
||||
if ("node" in currentObject) {
|
||||
e.push("n:" + encodeURIComponent(currentObject.node.nodeinfo.node_id));
|
||||
}
|
||||
|
||||
window.history.pushState(s, undefined, s)
|
||||
if ("link" in currentObject) {
|
||||
e.push("l:" + encodeURIComponent(currentObject.link.id));
|
||||
}
|
||||
}
|
||||
|
||||
var s = "#!" + e.join(";");
|
||||
|
||||
window.history.pushState(s, undefined, s);
|
||||
}
|
||||
|
||||
function resetView(push) {
|
||||
push = trueDefault(push)
|
||||
push = trueDefault(push);
|
||||
|
||||
targets.forEach( function (t) {
|
||||
t.resetView()
|
||||
})
|
||||
targets.forEach(function (t) {
|
||||
t.resetView();
|
||||
});
|
||||
|
||||
if (push) {
|
||||
currentObject = undefined
|
||||
saveState()
|
||||
currentObject = undefined;
|
||||
saveState();
|
||||
}
|
||||
}
|
||||
|
||||
function gotoNode(d) {
|
||||
if (!d)
|
||||
return false
|
||||
if (!d) {
|
||||
return false;
|
||||
}
|
||||
|
||||
targets.forEach( function (t) {
|
||||
t.gotoNode(d)
|
||||
})
|
||||
targets.forEach(function (t) {
|
||||
t.gotoNode(d);
|
||||
});
|
||||
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
|
||||
function gotoLink(d) {
|
||||
if (!d)
|
||||
return false
|
||||
if (!d) {
|
||||
return false;
|
||||
}
|
||||
|
||||
targets.forEach( function (t) {
|
||||
t.gotoLink(d)
|
||||
})
|
||||
targets.forEach(function (t) {
|
||||
t.gotoLink(d);
|
||||
});
|
||||
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
|
||||
function gotoLocation(d) {
|
||||
if (!d)
|
||||
return false
|
||||
if (!d) {
|
||||
return false;
|
||||
}
|
||||
|
||||
targets.forEach( function (t) {
|
||||
if(!t.gotoLocation)console.warn("has no gotoLocation", t)
|
||||
t.gotoLocation(d)
|
||||
})
|
||||
targets.forEach(function (t) {
|
||||
if (!t.gotoLocation) {
|
||||
console.warn("has no gotoLocation", t);
|
||||
}
|
||||
t.gotoLocation(d);
|
||||
});
|
||||
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
|
||||
function loadState(s) {
|
||||
if (!s)
|
||||
return false
|
||||
if (!s) {
|
||||
return false;
|
||||
}
|
||||
|
||||
s = decodeURIComponent(s)
|
||||
s = decodeURIComponent(s);
|
||||
|
||||
if (!s.startsWith("#!"))
|
||||
return false
|
||||
if (!s.startsWith("#!")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var targetSet = false
|
||||
var targetSet = false;
|
||||
|
||||
s.slice(2).split(";").forEach(function (d) {
|
||||
var args = d.split(":")
|
||||
var args = d.split(":");
|
||||
|
||||
if (args[0] === "v" && args[1] in views) {
|
||||
currentView = args[1]
|
||||
views[args[1]]()
|
||||
currentView = args[1];
|
||||
views[args[1]]();
|
||||
}
|
||||
|
||||
var id
|
||||
var id;
|
||||
|
||||
if (args[0] === "n") {
|
||||
id = args[1]
|
||||
id = args[1];
|
||||
if (id in objects.nodes) {
|
||||
currentObject = { node: objects.nodes[id] }
|
||||
gotoNode(objects.nodes[id])
|
||||
targetSet = true
|
||||
currentObject = {node: objects.nodes[id]};
|
||||
gotoNode(objects.nodes[id]);
|
||||
targetSet = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (args[0] === "l") {
|
||||
id = args[1]
|
||||
id = args[1];
|
||||
if (id in objects.links) {
|
||||
currentObject = { link: objects.links[id] }
|
||||
gotoLink(objects.links[id])
|
||||
targetSet = true
|
||||
currentObject = {link: objects.links[id]};
|
||||
gotoLink(objects.links[id]);
|
||||
targetSet = true;
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
return targetSet
|
||||
return targetSet;
|
||||
}
|
||||
|
||||
self.start = function () {
|
||||
running = true
|
||||
running = true;
|
||||
|
||||
if (!loadState(window.location.hash))
|
||||
resetView(false)
|
||||
if (!loadState(window.location.hash)) {
|
||||
resetView(false);
|
||||
}
|
||||
|
||||
window.onpopstate = function (d) {
|
||||
if (!loadState(d.state))
|
||||
resetView(false)
|
||||
}
|
||||
}
|
||||
if (!loadState(d.state)) {
|
||||
resetView(false);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
self.view = function (d) {
|
||||
if (d in views) {
|
||||
views[d]()
|
||||
views[d]();
|
||||
|
||||
if (!currentView || running)
|
||||
currentView = d
|
||||
|
||||
if (!running)
|
||||
return
|
||||
|
||||
saveState()
|
||||
|
||||
if (!currentObject) {
|
||||
resetView(false)
|
||||
return
|
||||
if (!currentView || running) {
|
||||
currentView = d;
|
||||
}
|
||||
|
||||
if ("node" in currentObject)
|
||||
gotoNode(currentObject.node)
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ("link" in currentObject)
|
||||
gotoLink(currentObject.link)
|
||||
saveState();
|
||||
|
||||
if (!currentObject) {
|
||||
resetView(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if ("node" in currentObject) {
|
||||
gotoNode(currentObject.node);
|
||||
}
|
||||
|
||||
if ("link" in currentObject) {
|
||||
gotoLink(currentObject.link);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
self.node = function (d) {
|
||||
return function () {
|
||||
if (gotoNode(d)) {
|
||||
currentObject = { node: d }
|
||||
saveState()
|
||||
currentObject = {node: d};
|
||||
saveState();
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
};
|
||||
|
||||
self.link = function (d) {
|
||||
return function () {
|
||||
if (gotoLink(d)) {
|
||||
currentObject = { link: d }
|
||||
saveState()
|
||||
currentObject = {link: d};
|
||||
saveState();
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
};
|
||||
|
||||
self.gotoLocation = gotoLocation
|
||||
self.gotoLocation = gotoLocation;
|
||||
|
||||
self.reset = function () {
|
||||
resetView()
|
||||
}
|
||||
resetView();
|
||||
};
|
||||
|
||||
self.addTarget = function (d) {
|
||||
targets.push(d)
|
||||
}
|
||||
targets.push(d);
|
||||
};
|
||||
|
||||
self.removeTarget = function (d) {
|
||||
targets = targets.filter( function (e) {
|
||||
return d !== e
|
||||
})
|
||||
}
|
||||
targets = targets.filter(function (e) {
|
||||
return d !== e;
|
||||
});
|
||||
};
|
||||
|
||||
self.addView = function (k, d) {
|
||||
views[k] = d
|
||||
}
|
||||
views[k] = d;
|
||||
};
|
||||
|
||||
self.setData = function (data) {
|
||||
objects.nodes = {}
|
||||
objects.links = {}
|
||||
objects.nodes = {};
|
||||
objects.links = {};
|
||||
|
||||
data.nodes.all.forEach( function (d) {
|
||||
objects.nodes[d.nodeinfo.node_id] = d
|
||||
})
|
||||
data.nodes.all.forEach(function (d) {
|
||||
objects.nodes[d.nodeinfo.node_id] = d;
|
||||
});
|
||||
|
||||
data.graph.links.forEach( function (d) {
|
||||
objects.links[d.id] = d
|
||||
})
|
||||
}
|
||||
data.graph.links.forEach(function (d) {
|
||||
objects.links[d.id] = d;
|
||||
});
|
||||
};
|
||||
|
||||
return self
|
||||
}
|
||||
})
|
||||
return self;
|
||||
};
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue