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

327 lines
8.9 KiB
JavaScript
Raw Normal View History

/* global $ */
2016-04-15 09:11:50 +08:00
2016-09-22 23:51:33 +08:00
import _ from 'lodash'
import outdent from 'outdent'
2016-09-22 23:51:33 +08:00
2016-09-22 17:36:47 +08:00
import Active from './Active'
import DataModel from './DataModel'
2016-09-22 17:36:47 +08:00
import Filter from './Filter'
2016-09-22 18:31:56 +08:00
import GlobalUI from './GlobalUI'
2016-09-22 17:36:47 +08: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 15:25:08 -05:00
init: function() {},
selectNode: function(node, e) {
2016-04-15 09:11:50 +08:00
var filtered = node.getData('alpha') === 0
2016-11-07 15:25:08 -05:00
if (filtered || Selected.Nodes.indexOf(node) !== -1) return
2016-04-15 09:11:50 +08:00
node.selected = true
node.setData('dim', 30, 'current')
2016-09-22 17:36:47 +08:00
Selected.Nodes.push(node)
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
deselectAllNodes: function() {
2016-09-22 17:36:47 +08:00
var l = Selected.Nodes.length
2016-04-15 09:11:50 +08:00
for (var i = l - 1; i >= 0; i -= 1) {
2016-09-22 17:36:47 +08:00
var node = Selected.Nodes[i]
Control.deselectNode(node)
2016-04-15 09:11:50 +08:00
}
2016-09-22 17:36:47 +08:00
Visualize.mGraph.plot()
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
deselectNode: function(node) {
2016-04-15 09:11:50 +08:00
delete node.selected
node.setData('dim', 25, 'current')
// remove the node
2016-09-22 17:36:47 +08:00
Selected.Nodes.splice(
Selected.Nodes.indexOf(node), 1)
2016-04-15 09:11:50 +08:00
},
removeSelected: function() {
Control.removeSelectedEdges()
Control.removeSelectedNodes()
2016-04-15 09:11:50 +08:00
},
hideSelected: function() {
Control.hideSelectedEdges()
Control.hideSelectedNodes()
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
removeSelectedNodes: function() { // refers to removing topics permanently from a map
2016-09-22 17:36:47 +08:00
if (Active.Topic) {
// hideNode will handle synapses as well
2016-09-22 17:36:47 +08:00
var nodeids = _.map(Selected.Nodes, function(node) {
return node.id
})
_.each(nodeids, function(nodeid) {
2016-09-22 17:36:47 +08:00
if (Active.Topic.id !== nodeid) {
DataModel.Topics.remove(nodeid)
Control.hideNode(nodeid)
}
})
return
}
2016-09-22 17:36:47 +08:00
if (!Active.Map) return
2016-04-15 09:11:50 +08:00
2016-11-07 15:25:08 -05:00
const l = Selected.Nodes.length
const authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 09:11:50 +08:00
if (!authorized) {
2016-09-22 18:31:56 +08:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 09:11:50 +08:00
return
}
2016-11-07 15:25:08 -05:00
for (let i = l - 1; i >= 0; i -= 1) {
const node = Selected.Nodes[i]
Control.removeNode(node.id)
2016-04-15 09:11:50 +08:00
}
},
2016-11-07 15:25:08 -05:00
removeNode: function(nodeid) { // refers to removing topics permanently from a map
2016-09-22 17:36:47 +08:00
if (!Active.Map) return
2016-04-15 09:11:50 +08:00
2016-09-22 17:36:47 +08:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
var node = Visualize.mGraph.graph.getNode(nodeid)
2016-04-15 09:11:50 +08:00
if (!authorized) {
2016-09-22 18:31:56 +08:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 09:11:50 +08: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 17:36:47 +08:00
$(document).trigger(JIT.events.removeTopic, [{
2016-04-15 09:11:50 +08:00
mappableid: mappableid
}])
Control.hideNode(nodeid)
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05: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 09:11:50 +08:00
}
},
2016-11-07 15:25:08 -05:00
hideNode: function(nodeid) {
2016-09-22 17:36:47 +08:00
var node = Visualize.mGraph.graph.getNode(nodeid)
var graph = Visualize.mGraph
2016-04-15 09:11:50 +08:00
Control.deselectNode(node)
2016-04-15 09:11:50 +08:00
node.setData('alpha', 0, 'end')
2016-11-07 15:25:08 -05:00
node.eachAdjacency(function(adj) {
2016-04-15 09:11:50 +08:00
adj.setData('alpha', 0, 'end')
})
2016-09-22 17:36:47 +08:00
Visualize.mGraph.fx.animate({
2016-04-15 09:11:50 +08:00
modes: ['node-property:alpha',
'edge-property:alpha'
],
duration: 500
})
2016-11-07 15:25:08 -05:00
setTimeout(function() {
if (nodeid === Visualize.mGraph.root) { // && Visualize.type === "RGraph"
2016-11-07 15:25:08 -05:00
var newroot = _.find(graph.graph.nodes, function(n) { return n.id !== nodeid })
2016-04-15 09:11:50 +08:00
graph.root = newroot ? newroot.id : null
}
2016-09-22 17:36:47 +08:00
Visualize.mGraph.graph.removeNode(nodeid)
2016-04-15 09:11:50 +08:00
}, 500)
2016-09-22 17:36:47 +08:00
Filter.checkMetacodes()
Filter.checkMappers()
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
selectEdge: function(edge) {
var filtered = edge.getData('alpha') === 0 // don't select if the edge is filtered
2016-04-15 09:11:50 +08:00
2016-11-07 15:25:08 -05:00
if (filtered || Selected.Edges.indexOf(edge) !== -1) return
2016-04-15 09:11:50 +08:00
2016-09-22 17:36:47 +08:00
var width = Mouse.edgeHoveringOver === edge ? 4 : 2
2016-04-15 09:11:50 +08:00
edge.setDataset('current', {
showDesc: true,
lineWidth: width,
2016-09-22 17:36:47 +08:00
color: Settings.colors.synapses.selected
2016-04-15 09:11:50 +08:00
})
2016-09-22 17:36:47 +08:00
Visualize.mGraph.plot()
2016-04-15 09:11:50 +08:00
2016-09-22 17:36:47 +08:00
Selected.Edges.push(edge)
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
deselectAllEdges: function() {
2016-09-22 17:36:47 +08:00
var l = Selected.Edges.length
2016-04-15 09:11:50 +08:00
for (var i = l - 1; i >= 0; i -= 1) {
2016-09-22 17:36:47 +08:00
var edge = Selected.Edges[i]
Control.deselectEdge(edge)
2016-04-15 09:11:50 +08:00
}
2016-09-22 17:36:47 +08:00
Visualize.mGraph.plot()
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
deselectEdge: function(edge) {
2016-04-15 09:11:50 +08:00
edge.setData('showDesc', false, 'current')
edge.setDataset('current', {
lineWidth: 2,
2016-09-22 17:36:47 +08:00
color: Settings.colors.synapses.normal
2016-04-15 09:11:50 +08:00
})
if (Mouse.edgeHoveringOver === edge) {
2016-04-15 09:11:50 +08:00
edge.setDataset('current', {
showDesc: true,
lineWidth: 4
})
}
2016-09-22 17:36:47 +08:00
Visualize.mGraph.plot()
2016-04-15 09:11:50 +08:00
// remove the edge
2016-09-22 17:36:47 +08:00
Selected.Edges.splice(
Selected.Edges.indexOf(edge), 1)
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
removeSelectedEdges: function() {
// Topic view is handled by removeSelectedNodes
2016-09-22 17:36:47 +08:00
if (!Active.Map) return
2016-11-07 15:25:08 -05:00
const l = Selected.Edges.length
2016-04-15 09:11:50 +08:00
2016-09-22 17:36:47 +08:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 09:11:50 +08:00
if (!authorized) {
GlobalUI.notifyUser('Cannot edit this map.')
2016-04-15 09:11:50 +08:00
return
}
2016-11-07 15:25:08 -05:00
for (let i = l - 1; i >= 0; i -= 1) {
const edge = Selected.Edges[i]
Control.removeEdge(edge)
2016-04-15 09:11:50 +08:00
}
2016-09-22 17:36:47 +08:00
Selected.Edges = [ ]
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
removeEdge: function(edge) {
2016-09-22 17:36:47 +08:00
if (!Active.Map) return
2016-04-15 09:11:50 +08:00
2016-09-22 17:36:47 +08:00
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
2016-04-15 09:11:50 +08:00
if (!authorized) {
2016-09-22 18:31:56 +08:00
GlobalUI.notifyUser('Cannot edit Public map.')
2016-04-15 09:11:50 +08:00
return
}
if (edge.getData('mappings').length - 1 === 0) {
Control.hideEdge(edge)
2016-04-15 09:11:50 +08: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 09:11:50 +08:00
edge.getData('mappings').splice(index, 1)
edge.getData('synapses').splice(index, 1)
if (edge.getData('displayIndex')) {
delete edge.data.$displayIndex
}
2016-09-22 17:36:47 +08:00
$(document).trigger(JIT.events.removeSynapse, [{
2016-04-15 09:11:50 +08:00
mappableid: mappableid
}])
},
2016-11-07 15:25:08 -05: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 09:11:50 +08:00
}
2016-09-22 17:36:47 +08:00
Selected.Edges = [ ]
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
hideEdge: function(edge) {
2016-04-15 09:11:50 +08:00
var from = edge.nodeFrom.id
var to = edge.nodeTo.id
edge.setData('alpha', 0, 'end')
Control.deselectEdge(edge)
2016-09-22 17:36:47 +08:00
Visualize.mGraph.fx.animate({
2016-04-15 09:11:50 +08:00
modes: ['edge-property:alpha'],
duration: 500
})
2016-11-07 15:25:08 -05:00
setTimeout(function() {
2016-09-22 17:36:47 +08:00
Visualize.mGraph.graph.removeAdjacence(from, to)
2016-04-15 09:11:50 +08:00
}, 500)
2016-09-22 17:36:47 +08:00
Filter.checkSynapses()
Filter.checkMappers()
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
updateSelectedPermissions: function(permission) {
2016-04-15 09:11:50 +08:00
var edge, synapse, node, topic
2016-09-22 18:31:56 +08:00
GlobalUI.notifyUser('Working...')
2016-04-15 09:11:50 +08:00
// variables to keep track of how many nodes and synapses you had the ability to change the permission of
2016-11-07 15:25:08 -05:00
var nCount = 0
var sCount = 0
2016-04-15 09:11:50 +08:00
// change the permission of the selected synapses, if logged in user is the original creator
2016-11-07 15:25:08 -05:00
const edgesLength = Selected.Edges.length
for (let i = edgesLength - 1; i >= 0; i -= 1) {
2016-09-22 17:36:47 +08:00
edge = Selected.Edges[i]
2016-04-15 09:11:50 +08:00
synapse = edge.getData('synapses')[0]
2016-09-22 17:36:47 +08:00
if (synapse.authorizePermissionChange(Active.Mapper)) {
2016-04-15 09:11:50 +08:00
synapse.save({
permission: permission
})
sCount++
}
}
// change the permission of the selected topics, if logged in user is the original creator
2016-11-07 15:25:08 -05:00
const nodesLength = Selected.Nodes.length
for (let i = nodesLength - 1; i >= 0; i -= 1) {
2016-09-22 17:36:47 +08:00
node = Selected.Nodes[i]
2016-04-15 09:11:50 +08:00
topic = node.getData('topic')
2016-09-22 17:36:47 +08:00
if (topic.authorizePermissionChange(Active.Mapper)) {
2016-04-15 09:11:50 +08: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 09:11:50 +08:00
var message = nString + sString + ' you created updated to ' + permission
2016-09-22 18:31:56 +08:00
GlobalUI.notifyUser(message)
2016-04-15 09:11:50 +08:00
},
2016-11-07 15:25:08 -05:00
updateSelectedMetacodes: function(metacodeId) {
2016-04-15 09:11:50 +08:00
var node, topic
2016-09-22 18:31:56 +08:00
GlobalUI.notifyUser('Working...')
2016-04-15 09:11:50 +08:00
2016-11-07 15:25:08 -05:00
var metacode = DataModel.Metacodes.get(metacodeId)
2016-04-15 09:11:50 +08: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 17:36:47 +08:00
var l = Selected.Nodes.length
2016-04-15 09:11:50 +08:00
for (var i = l - 1; i >= 0; i -= 1) {
2016-09-22 17:36:47 +08:00
node = Selected.Nodes[i]
2016-04-15 09:11:50 +08:00
topic = node.getData('topic')
2016-09-22 17:36:47 +08:00
if (topic.authorizeToEdit(Active.Mapper)) {
2016-04-15 09:11:50 +08:00
topic.save({
2016-11-07 15:25:08 -05:00
'metacode_id': metacodeId
2016-04-15 09:11:50 +08:00
})
nCount++
}
}
var nString = nCount === 1 ? (nCount.toString() + ' topic') : (nCount.toString() + ' topics')
2016-04-15 09:11:50 +08:00
var message = nString + ' you can edit updated to ' + metacode.get('name')
2016-09-22 18:31:56 +08:00
GlobalUI.notifyUser(message)
2016-09-22 17:36:47 +08:00
Visualize.mGraph.plot()
}
}
export default Control