metamaps--metamaps/frontend/src/Metamaps/Control.js

456 lines
12 KiB
JavaScript
Raw Normal View History

2016-04-15 01:11:50 +00:00
/* global Metamaps, $ */
2016-09-22 15:51:33 +00:00
import _ from 'lodash'
2016-09-22 09:36:47 +00:00
import Active from './Active'
import Filter from './Filter'
2016-09-22 10:31:56 +00:00
import GlobalUI from './GlobalUI'
2016-09-22 09:36:47 +00:00
import JIT from './JIT'
import Mouse from './Mouse'
import Selected from './Selected'
import Settings from './Settings'
import Visualize from './Visualize'
2016-04-15 01:11:50 +00:00
/*
* Metamaps.Control.js
2016-04-15 01:11:50 +00:00
*
* Dependencies:
* - Metamaps.Mappings
* - Metamaps.Metacodes
* - Metamaps.Synapses
* - Metamaps.Topics
*/
const Control = {
2016-04-15 01:11:50 +00:00
init: function () {},
selectNode: function (node, e) {
var filtered = node.getData('alpha') === 0
2016-09-22 09:36:47 +00:00
if (filtered || Selected.Nodes.indexOf(node) != -1) return
2016-04-15 01:11:50 +00:00
node.selected = true
node.setData('dim', 30, 'current')
2016-09-22 09:36:47 +00:00
Selected.Nodes.push(node)
2016-04-15 01:11:50 +00:00
},
deselectAllNodes: function () {
2016-09-22 09:36:47 +00:00
var l = Selected.Nodes.length
2016-04-15 01:11:50 +00:00
for (var i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
var node = Selected.Nodes[i]
Control.deselectNode(node)
2016-04-15 01:11:50 +00:00
}
2016-09-22 09:36:47 +00:00
Visualize.mGraph.plot()
2016-04-15 01:11:50 +00:00
},
deselectNode: function (node) {
delete node.selected
node.setData('dim', 25, 'current')
// remove the node
2016-09-22 09:36:47 +00:00
Selected.Nodes.splice(
Selected.Nodes.indexOf(node), 1)
2016-04-15 01:11:50 +00:00
},
deleteSelected: function () {
2016-09-22 09:36:47 +00:00
if (!Active.Map) return
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
var n = Selected.Nodes.length
var e = Selected.Edges.length
2016-04-15 01:11:50 +00:00
var ntext = n == 1 ? '1 topic' : n + ' topics'
var etext = e == 1 ? '1 synapse' : e + ' synapses'
var text = 'You have ' + ntext + ' and ' + etext + ' selected. '
2016-09-22 09:36:47 +00:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 01:11:50 +00:00
if (!authorized) {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 01:11:50 +00:00
return
}
var r = confirm(text + 'Are you sure you want to permanently delete them all? This will remove them from all maps they appear on.')
if (r == true) {
Control.deleteSelectedEdges()
Control.deleteSelectedNodes()
2016-04-15 01:11:50 +00:00
}
},
deleteSelectedNodes: function () { // refers to deleting topics permanently
2016-09-22 09:36:47 +00:00
if (!Active.Map) return
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 01:11:50 +00:00
if (!authorized) {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 01:11:50 +00:00
return
}
2016-09-22 09:36:47 +00:00
var l = Selected.Nodes.length
2016-04-15 01:11:50 +00:00
for (var i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
var node = Selected.Nodes[i]
Control.deleteNode(node.id)
2016-04-15 01:11:50 +00:00
}
},
deleteNode: function (nodeid) { // refers to deleting topics permanently
2016-09-22 09:36:47 +00:00
if (!Active.Map) return
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 01:11:50 +00:00
if (!authorized) {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 01:11:50 +00:00
return
}
2016-09-22 09:36:47 +00:00
var node = Visualize.mGraph.graph.getNode(nodeid)
2016-04-15 01:11:50 +00:00
var topic = node.getData('topic')
2016-09-22 09:36:47 +00:00
var permToDelete = Active.Mapper.id === topic.get('user_id') || Active.Mapper.get('admin')
2016-04-15 01:11:50 +00:00
if (permToDelete) {
var mappableid = topic.id
var mapping = node.getData('mapping')
topic.destroy()
Metamaps.Mappings.remove(mapping)
2016-09-22 09:36:47 +00:00
$(document).trigger(JIT.events.deleteTopic, [{
2016-04-15 01:11:50 +00:00
mappableid: mappableid
}])
Control.hideNode(nodeid)
2016-04-15 01:11:50 +00:00
} else {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Only topics you created can be deleted')
2016-04-15 01:11:50 +00:00
}
},
removeSelectedNodes: function () { // refers to removing topics permanently from a map
2016-09-22 09:36:47 +00:00
if (Active.Topic) {
// hideNode will handle synapses as well
2016-09-22 09:36:47 +00:00
var nodeids = _.map(Selected.Nodes, function(node) {
return node.id
})
_.each(nodeids, function(nodeid) {
2016-09-22 09:36:47 +00:00
if (Active.Topic.id !== nodeid) {
Metamaps.Topics.remove(nodeid)
Control.hideNode(nodeid)
}
})
return
}
2016-09-22 09:36:47 +00:00
if (!Active.Map) return
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
var l = Selected.Nodes.length,
2016-04-15 01:11:50 +00:00
i,
node,
2016-09-22 09:36:47 +00:00
authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 01:11:50 +00:00
if (!authorized) {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 01:11:50 +00:00
return
}
for (i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
node = Selected.Nodes[i]
Control.removeNode(node.id)
2016-04-15 01:11:50 +00:00
}
},
removeNode: function (nodeid) { // refers to removing topics permanently from a map
2016-09-22 09:36:47 +00:00
if (!Active.Map) return
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
var node = Visualize.mGraph.graph.getNode(nodeid)
2016-04-15 01:11:50 +00:00
if (!authorized) {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 01:11:50 +00:00
return
}
var topic = node.getData('topic')
var mappableid = topic.id
var mapping = node.getData('mapping')
mapping.destroy()
Metamaps.Topics.remove(topic)
2016-09-22 09:36:47 +00:00
$(document).trigger(JIT.events.removeTopic, [{
2016-04-15 01:11:50 +00:00
mappableid: mappableid
}])
Control.hideNode(nodeid)
2016-04-15 01:11:50 +00:00
},
hideSelectedNodes: function () {
2016-09-22 09:36:47 +00:00
var l = Selected.Nodes.length,
2016-04-15 01:11:50 +00:00
i,
node
for (i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
node = Selected.Nodes[i]
Control.hideNode(node.id)
2016-04-15 01:11:50 +00:00
}
},
hideNode: function (nodeid) {
2016-09-22 09:36:47 +00:00
var node = Visualize.mGraph.graph.getNode(nodeid)
var graph = Visualize.mGraph
2016-04-15 01:11:50 +00:00
Control.deselectNode(node)
2016-04-15 01:11:50 +00:00
node.setData('alpha', 0, 'end')
node.eachAdjacency(function (adj) {
adj.setData('alpha', 0, 'end')
})
2016-09-22 09:36:47 +00:00
Visualize.mGraph.fx.animate({
2016-04-15 01:11:50 +00:00
modes: ['node-property:alpha',
'edge-property:alpha'
],
duration: 500
})
setTimeout(function () {
2016-09-22 09:36:47 +00:00
if (nodeid == Visualize.mGraph.root) { // && Visualize.type === "RGraph"
2016-04-15 01:11:50 +00:00
var newroot = _.find(graph.graph.nodes, function (n) { return n.id !== nodeid; })
graph.root = newroot ? newroot.id : null
}
2016-09-22 09:36:47 +00:00
Visualize.mGraph.graph.removeNode(nodeid)
2016-04-15 01:11:50 +00:00
}, 500)
2016-09-22 09:36:47 +00:00
Filter.checkMetacodes()
Filter.checkMappers()
2016-04-15 01:11:50 +00:00
},
selectEdge: function (edge) {
var filtered = edge.getData('alpha') === 0; // don't select if the edge is filtered
2016-09-22 09:36:47 +00:00
if (filtered || Selected.Edges.indexOf(edge) != -1) return
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
var width = Mouse.edgeHoveringOver === edge ? 4 : 2
2016-04-15 01:11:50 +00:00
edge.setDataset('current', {
showDesc: true,
lineWidth: width,
2016-09-22 09:36:47 +00:00
color: Settings.colors.synapses.selected
2016-04-15 01:11:50 +00:00
})
2016-09-22 09:36:47 +00:00
Visualize.mGraph.plot()
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
Selected.Edges.push(edge)
2016-04-15 01:11:50 +00:00
},
deselectAllEdges: function () {
2016-09-22 09:36:47 +00:00
var l = Selected.Edges.length
2016-04-15 01:11:50 +00:00
for (var i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
var edge = Selected.Edges[i]
Control.deselectEdge(edge)
2016-04-15 01:11:50 +00:00
}
2016-09-22 09:36:47 +00:00
Visualize.mGraph.plot()
2016-04-15 01:11:50 +00:00
},
deselectEdge: function (edge) {
edge.setData('showDesc', false, 'current')
edge.setDataset('current', {
lineWidth: 2,
2016-09-22 09:36:47 +00:00
color: Settings.colors.synapses.normal
2016-04-15 01:11:50 +00:00
})
2016-09-22 09:36:47 +00:00
if (Mouse.edgeHoveringOver == edge) {
2016-04-15 01:11:50 +00:00
edge.setDataset('current', {
showDesc: true,
lineWidth: 4
})
}
2016-09-22 09:36:47 +00:00
Visualize.mGraph.plot()
2016-04-15 01:11:50 +00:00
// remove the edge
2016-09-22 09:36:47 +00:00
Selected.Edges.splice(
Selected.Edges.indexOf(edge), 1)
2016-04-15 01:11:50 +00:00
},
deleteSelectedEdges: function () { // refers to deleting topics permanently
var edge,
2016-09-22 09:36:47 +00:00
l = Selected.Edges.length
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
if (!Active.Map) return
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 01:11:50 +00:00
if (!authorized) {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 01:11:50 +00:00
return
}
for (var i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
edge = Selected.Edges[i]
Control.deleteEdge(edge)
2016-04-15 01:11:50 +00:00
}
},
deleteEdge: function (edge) {
2016-09-22 09:36:47 +00:00
if (!Active.Map) return
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 01:11:50 +00:00
if (!authorized) {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 01:11:50 +00:00
return
}
var index = edge.getData('displayIndex') ? edge.getData('displayIndex') : 0
var synapse = edge.getData('synapses')[index]
var mapping = edge.getData('mappings')[index]
2016-09-22 09:36:47 +00:00
var permToDelete = Active.Mapper.id === synapse.get('user_id') || Active.Mapper.get('admin')
2016-04-15 01:11:50 +00:00
if (permToDelete) {
if (edge.getData('synapses').length - 1 === 0) {
Control.hideEdge(edge)
2016-04-15 01:11:50 +00:00
}
var mappableid = synapse.id
synapse.destroy()
// the server will destroy the mapping, we just need to remove it here
Metamaps.Mappings.remove(mapping)
edge.getData('mappings').splice(index, 1)
edge.getData('synapses').splice(index, 1)
if (edge.getData('displayIndex')) {
delete edge.data.$displayIndex
}
2016-09-22 09:36:47 +00:00
$(document).trigger(JIT.events.deleteSynapse, [{
2016-04-15 01:11:50 +00:00
mappableid: mappableid
}])
} else {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Only synapses you created can be deleted')
2016-04-15 01:11:50 +00:00
}
},
removeSelectedEdges: function () {
// Topic view is handled by removeSelectedNodes
2016-09-22 09:36:47 +00:00
if (!Active.Map) return
2016-09-22 09:36:47 +00:00
var l = Selected.Edges.length,
2016-04-15 01:11:50 +00:00
i,
edge
2016-09-22 09:36:47 +00:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 01:11:50 +00:00
if (!authorized) {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 01:11:50 +00:00
return
}
for (i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
edge = Selected.Edges[i]
Control.removeEdge(edge)
2016-04-15 01:11:50 +00:00
}
2016-09-22 09:36:47 +00:00
Selected.Edges = [ ]
2016-04-15 01:11:50 +00:00
},
removeEdge: function (edge) {
2016-09-22 09:36:47 +00:00
if (!Active.Map) return
2016-04-15 01:11:50 +00:00
2016-09-22 09:36:47 +00:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 01:11:50 +00:00
if (!authorized) {
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 01:11:50 +00:00
return
}
if (edge.getData('mappings').length - 1 === 0) {
Control.hideEdge(edge)
2016-04-15 01:11:50 +00:00
}
var index = edge.getData('displayIndex') ? edge.getData('displayIndex') : 0
var synapse = edge.getData('synapses')[index]
var mapping = edge.getData('mappings')[index]
var mappableid = synapse.id
mapping.destroy()
Metamaps.Synapses.remove(synapse)
edge.getData('mappings').splice(index, 1)
edge.getData('synapses').splice(index, 1)
if (edge.getData('displayIndex')) {
delete edge.data.$displayIndex
}
2016-09-22 09:36:47 +00:00
$(document).trigger(JIT.events.removeSynapse, [{
2016-04-15 01:11:50 +00:00
mappableid: mappableid
}])
},
hideSelectedEdges: function () {
var edge,
2016-09-22 09:36:47 +00:00
l = Selected.Edges.length,
2016-04-15 01:11:50 +00:00
i
for (i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
edge = Selected.Edges[i]
Control.hideEdge(edge)
2016-04-15 01:11:50 +00:00
}
2016-09-22 09:36:47 +00:00
Selected.Edges = [ ]
2016-04-15 01:11:50 +00:00
},
hideEdge: function (edge) {
var from = edge.nodeFrom.id
var to = edge.nodeTo.id
edge.setData('alpha', 0, 'end')
Control.deselectEdge(edge)
2016-09-22 09:36:47 +00:00
Visualize.mGraph.fx.animate({
2016-04-15 01:11:50 +00:00
modes: ['edge-property:alpha'],
duration: 500
})
setTimeout(function () {
2016-09-22 09:36:47 +00:00
Visualize.mGraph.graph.removeAdjacence(from, to)
2016-04-15 01:11:50 +00:00
}, 500)
2016-09-22 09:36:47 +00:00
Filter.checkSynapses()
Filter.checkMappers()
2016-04-15 01:11:50 +00:00
},
updateSelectedPermissions: function (permission) {
var edge, synapse, node, topic
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Working...')
2016-04-15 01:11:50 +00:00
// variables to keep track of how many nodes and synapses you had the ability to change the permission of
var nCount = 0,
sCount = 0
// change the permission of the selected synapses, if logged in user is the original creator
2016-09-22 09:36:47 +00:00
var l = Selected.Edges.length
2016-04-15 01:11:50 +00:00
for (var i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
edge = Selected.Edges[i]
2016-04-15 01:11:50 +00:00
synapse = edge.getData('synapses')[0]
2016-09-22 09:36:47 +00:00
if (synapse.authorizePermissionChange(Active.Mapper)) {
2016-04-15 01:11:50 +00:00
synapse.save({
permission: permission
})
sCount++
}
}
// change the permission of the selected topics, if logged in user is the original creator
2016-09-22 09:36:47 +00:00
var l = Selected.Nodes.length
2016-04-15 01:11:50 +00:00
for (var i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
node = Selected.Nodes[i]
2016-04-15 01:11:50 +00:00
topic = node.getData('topic')
2016-09-22 09:36:47 +00:00
if (topic.authorizePermissionChange(Active.Mapper)) {
2016-04-15 01:11:50 +00:00
topic.save({
permission: permission
})
nCount++
}
}
var nString = nCount == 1 ? (nCount.toString() + ' topic and ') : (nCount.toString() + ' topics and ')
var sString = sCount == 1 ? (sCount.toString() + ' synapse') : (sCount.toString() + ' synapses')
var message = nString + sString + ' you created updated to ' + permission
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser(message)
2016-04-15 01:11:50 +00:00
},
updateSelectedMetacodes: function (metacode_id) {
var node, topic
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Working...')
2016-04-15 01:11:50 +00:00
var metacode = Metamaps.Metacodes.get(metacode_id)
// variables to keep track of how many nodes and synapses you had the ability to change the permission of
var nCount = 0
// change the permission of the selected topics, if logged in user is the original creator
2016-09-22 09:36:47 +00:00
var l = Selected.Nodes.length
2016-04-15 01:11:50 +00:00
for (var i = l - 1; i >= 0; i -= 1) {
2016-09-22 09:36:47 +00:00
node = Selected.Nodes[i]
2016-04-15 01:11:50 +00:00
topic = node.getData('topic')
2016-09-22 09:36:47 +00:00
if (topic.authorizeToEdit(Active.Mapper)) {
2016-04-15 01:11:50 +00:00
topic.save({
'metacode_id': metacode_id
})
nCount++
}
}
var nString = nCount == 1 ? (nCount.toString() + ' topic') : (nCount.toString() + ' topics')
var message = nString + ' you can edit updated to ' + metacode.get('name')
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser(message)
2016-09-22 09:36:47 +00:00
Visualize.mGraph.plot()
2016-04-15 01:11:50 +00:00
},
}
export default Control