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

443 lines
12 KiB
JavaScript
Raw Normal View History

/* global $ */
2016-04-15 01:11:50 +00:00
2016-09-22 15:51:33 +00:00
import _ from 'lodash'
import outdent from 'outdent'
2016-09-22 15:51:33 +00:00
2016-09-22 09:36:47 +00:00
import Active from './Active'
import DataModel from './DataModel'
2016-09-22 09:36:47 +00:00
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'
const Control = {
2016-11-07 20:25:08 +00:00
init: function() {},
selectNode: function(node, e) {
2016-04-15 01:11:50 +00:00
var filtered = node.getData('alpha') === 0
2016-11-07 20:25:08 +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
},
2016-11-07 20:25:08 +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
},
2016-11-07 20:25:08 +00:00
deselectNode: function(node) {
2016-04-15 01:11:50 +00:00
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
},
2016-11-07 20:25:08 +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
var ntext = n === 1 ? '1 topic' : n + ' topics'
var etext = e === 1 ? '1 synapse' : e + ' synapses'
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 r = window.confirm(outdent`
You have ${ntext} and ${etext} selected. Are you sure you want
to permanently delete them all? This will remove them from all
maps they appear on.`)
if (r) {
Control.deleteSelectedEdges()
Control.deleteSelectedNodes()
2016-04-15 01:11:50 +00:00
}
if (DataModel.Topics.length === 0) {
GlobalUI.showDiv('#instructions')
}
2016-04-15 01:11:50 +00:00
},
2016-11-07 20:25:08 +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
}
},
2016-11-07 20:25:08 +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()
DataModel.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
}
},
2016-11-07 20:25:08 +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) {
DataModel.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-11-07 20:25:08 +00:00
const l = Selected.Nodes.length
const 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-11-07 20:25:08 +00:00
for (let i = l - 1; i >= 0; i -= 1) {
const node = Selected.Nodes[i]
Control.removeNode(node.id)
2016-04-15 01:11:50 +00:00
}
},
2016-11-07 20:25:08 +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()
DataModel.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
},
2016-11-07 20:25:08 +00:00
hideSelectedNodes: function() {
const l = Selected.Nodes.length
for (let i = l - 1; i >= 0; i -= 1) {
const node = Selected.Nodes[i]
Control.hideNode(node.id)
2016-04-15 01:11:50 +00:00
}
},
2016-11-07 20:25:08 +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')
2016-11-07 20:25:08 +00:00
node.eachAdjacency(function(adj) {
2016-04-15 01:11:50 +00:00
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
})
2016-11-07 20:25:08 +00:00
setTimeout(function() {
if (nodeid === Visualize.mGraph.root) { // && Visualize.type === "RGraph"
2016-11-07 20:25:08 +00:00
var newroot = _.find(graph.graph.nodes, function(n) { return n.id !== nodeid })
2016-04-15 01:11:50 +00:00
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
},
2016-11-07 20:25:08 +00:00
selectEdge: function(edge) {
var filtered = edge.getData('alpha') === 0 // don't select if the edge is filtered
2016-04-15 01:11:50 +00:00
2016-11-07 20:25:08 +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
},
2016-11-07 20:25:08 +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
},
2016-11-07 20:25:08 +00:00
deselectEdge: function(edge) {
2016-04-15 01:11:50 +00:00
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
})
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
},
2016-11-07 20:25:08 +00:00
deleteSelectedEdges: 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-11-07 20:25:08 +00:00
const l = Selected.Edges.length
for (let i = l - 1; i >= 0; i -= 1) {
const edge = Selected.Edges[i]
Control.deleteEdge(edge)
2016-04-15 01:11:50 +00:00
}
},
2016-11-07 20:25:08 +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
DataModel.Mappings.remove(mapping)
2016-04-15 01:11:50 +00:00
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
}
},
2016-11-07 20:25:08 +00:00
removeSelectedEdges: function() {
// Topic view is handled by removeSelectedNodes
2016-09-22 09:36:47 +00:00
if (!Active.Map) return
2016-11-07 20:25:08 +00:00
const l = Selected.Edges.length
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-11-07 20:25:08 +00:00
for (let i = l - 1; i >= 0; i -= 1) {
const 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
},
2016-11-07 20:25:08 +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()
DataModel.Synapses.remove(synapse)
2016-04-15 01:11:50 +00:00
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
}])
},
2016-11-07 20:25:08 +00:00
hideSelectedEdges: function() {
const l = Selected.Edges.length
for (let i = l - 1; i >= 0; i -= 1) {
const 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
},
2016-11-07 20:25:08 +00:00
hideEdge: function(edge) {
2016-04-15 01:11:50 +00:00
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
})
2016-11-07 20:25:08 +00:00
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
},
2016-11-07 20:25:08 +00:00
updateSelectedPermissions: function(permission) {
2016-04-15 01:11:50 +00:00
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
2016-11-07 20:25:08 +00:00
var nCount = 0
var sCount = 0
2016-04-15 01:11:50 +00:00
// change the permission of the selected synapses, if logged in user is the original creator
2016-11-07 20:25:08 +00:00
const edgesLength = Selected.Edges.length
for (let i = edgesLength - 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-11-07 20:25:08 +00:00
const nodesLength = Selected.Nodes.length
for (let i = nodesLength - 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')
2016-04-15 01:11:50 +00:00
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
},
2016-11-07 20:25:08 +00:00
updateSelectedMetacodes: function(metacodeId) {
2016-04-15 01:11:50 +00:00
var node, topic
2016-09-22 10:31:56 +00:00
GlobalUI.notifyUser('Working...')
2016-04-15 01:11:50 +00:00
2016-11-07 20:25:08 +00:00
var metacode = DataModel.Metacodes.get(metacodeId)
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
// 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({
2016-11-07 20:25:08 +00:00
'metacode_id': metacodeId
2016-04-15 01:11:50 +00:00
})
nCount++
}
}
var nString = nCount === 1 ? (nCount.toString() + ' topic') : (nCount.toString() + ' topics')
2016-04-15 01:11:50 +00:00
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()
}
}
export default Control