prepare router, forcegraph, map for destroy/reload

This commit is contained in:
Nils Schneider 2015-03-31 23:24:24 +02:00
parent 625e7ba880
commit 473734f2bb
3 changed files with 40 additions and 8 deletions

View File

@ -1,7 +1,7 @@
define(["d3"], function (d3) { define(["d3"], function (d3) {
return function (linkScale, sidebar, router) { return function (linkScale, sidebar, router) {
var self = this var self = this
var vis, link, node, label var svg, vis, link, node, label
var nodesDict, linksDict var nodesDict, linksDict
var zoomBehavior var zoomBehavior
var force var force
@ -72,8 +72,6 @@ define(["d3"], function (d3) {
var height = el.offsetHeight var height = el.offsetHeight
force.size([width, height]) force.size([width, height])
force.start()
panzoom()
} }
function tickEvent() { function tickEvent() {
@ -99,11 +97,13 @@ define(["d3"], function (d3) {
zoomBehavior = d3.behavior.zoom() zoomBehavior = d3.behavior.zoom()
.scaleExtent([1 / 3, 3]) .scaleExtent([1 / 3, 3])
.on("zoom", panzoom) .on("zoom", panzoom)
.translate([sidebar.getWidth(), 0])
vis = d3.select(el).append("svg") svg = d3.select(el).append("svg")
.attr("pointer-events", "all") .attr("pointer-events", "all")
.call(zoomBehavior) .call(zoomBehavior)
.append("g")
vis = svg.append("g")
vis.append("g").attr("class", "links") vis.append("g").attr("class", "links")
vis.append("g").attr("class", "nodes") vis.append("g").attr("class", "nodes")
@ -115,6 +115,8 @@ define(["d3"], function (d3) {
.linkDistance(70) .linkDistance(70)
.on("tick", tickEvent) .on("tick", tickEvent)
panzoom()
var draggableNode = d3.behavior.drag() var draggableNode = d3.behavior.drag()
.on("dragstart", dragstart) .on("dragstart", dragstart)
.on("drag", dragmove) .on("drag", dragmove)
@ -263,6 +265,21 @@ define(["d3"], function (d3) {
} }
} }
self.destroy = function () {
window.removeEventListener("resize", resize)
force.stop()
node.remove()
link.remove()
svg.remove()
force = null
svg = null
vis = null
link = null
node = null
label = null
}
return self return self
} }
}) })

View File

@ -182,6 +182,10 @@ define(["leaflet", "moment", "leaflet.label"], function (L, moment) {
resetView() resetView()
} }
self.destroy = function () {
map.remove()
}
return self return self
} }
}) })

View File

@ -1,8 +1,9 @@
define(function () { define(function () {
return function () { return function () {
var self = this
var objects = { nodes: {}, links: {} } var objects = { nodes: {}, links: {} }
var targets = [] var targets = []
var self = this var running = false
function saveState(d) { function saveState(d) {
var s = "#!" var s = "#!"
@ -79,6 +80,8 @@ define(function () {
} }
self.start = function () { self.start = function () {
running = true
if (!loadState(window.location.hash)) if (!loadState(window.location.hash))
resetView(false) resetView(false)
@ -134,6 +137,14 @@ define(function () {
}) })
} }
self.reload = function () {
if (!running)
return
if (!loadState(window.history.state))
resetView(false)
}
return self return self
} }
}) })