2016-09-22 18:31:56 +08:00
|
|
|
/* global $ */
|
|
|
|
|
|
|
|
import Active from './Active'
|
2017-03-06 22:49:46 -05:00
|
|
|
import Create from './Create'
|
2016-09-22 18:31:56 +08:00
|
|
|
import Control from './Control'
|
2016-12-11 16:21:36 -05:00
|
|
|
import DataModel from './DataModel'
|
2016-09-22 18:31:56 +08:00
|
|
|
import JIT from './JIT'
|
|
|
|
import Realtime from './Realtime'
|
|
|
|
import Selected from './Selected'
|
|
|
|
import Topic from './Topic'
|
2016-10-07 00:33:16 +00:00
|
|
|
import Util from './Util'
|
2016-09-22 18:31:56 +08:00
|
|
|
import Visualize from './Visualize'
|
2016-09-30 11:33:39 +08:00
|
|
|
import { Search } from './GlobalUI'
|
2016-04-15 08:31:22 +08:00
|
|
|
|
2016-09-22 15:21:59 +08:00
|
|
|
const Listeners = {
|
2016-11-07 15:25:08 -05:00
|
|
|
init: function() {
|
2016-08-08 21:46:05 +08:00
|
|
|
var self = this
|
2016-11-07 15:25:08 -05:00
|
|
|
$(document).on('keydown', function(e) {
|
2016-09-22 18:31:56 +08:00
|
|
|
if (!(Active.Map || Active.Topic)) return
|
2016-04-15 08:31:22 +08:00
|
|
|
|
2016-10-23 18:02:59 +08:00
|
|
|
const onCanvas = e.target.tagName === 'BODY'
|
2017-03-10 15:22:30 -08:00
|
|
|
|
2016-04-15 08:31:22 +08:00
|
|
|
switch (e.which) {
|
|
|
|
case 13: // if enter key is pressed
|
2016-10-05 22:17:04 -04:00
|
|
|
// prevent topic creation if sending a message
|
|
|
|
if (e.target.className !== 'chat-input') {
|
|
|
|
JIT.enterKeyHandler()
|
|
|
|
}
|
2016-04-15 08:31:22 +08:00
|
|
|
break
|
|
|
|
case 27: // if esc key is pressed
|
2016-09-22 18:31:56 +08:00
|
|
|
JIT.escKeyHandler()
|
2016-04-15 08:31:22 +08:00
|
|
|
break
|
2017-03-10 15:22:30 -08:00
|
|
|
case 38: // if UP key is pressed
|
2017-03-17 00:11:57 -04:00
|
|
|
if ((e.ctrlKey || e.metaKey) && e.shiftKey) {
|
2017-03-10 15:22:30 -08:00
|
|
|
Control.selectNeighbors()
|
|
|
|
}
|
|
|
|
break
|
2017-02-03 14:23:04 -05:00
|
|
|
case 46: // if DEL is pressed
|
2017-03-10 15:22:30 -08:00
|
|
|
if (e.target.tagName !== 'INPUT' && e.target.tagName !== 'TEXTAREA' && (Selected.Nodes.length + Selected.Edges.length) > 0) {
|
2017-03-06 22:49:46 -05:00
|
|
|
e.preventDefault()
|
|
|
|
Control.removeSelectedNodes()
|
|
|
|
Control.removeSelectedEdges()
|
|
|
|
}
|
2017-02-03 14:23:04 -05:00
|
|
|
break
|
2016-04-15 08:31:22 +08:00
|
|
|
case 65: // if a or A is pressed
|
2017-03-06 22:49:46 -05:00
|
|
|
if (Create.isSwitchingSet && e.ctrlKey || e.metaKey) {
|
|
|
|
Create.metacodeSelectorToggleSelectAll()
|
|
|
|
e.preventDefault()
|
|
|
|
break
|
|
|
|
} else if ((e.ctrlKey || e.metaKey) && onCanvas) {
|
2016-12-11 16:21:36 -05:00
|
|
|
const nodesCount = Object.keys(Visualize.mGraph.graph.nodes).length
|
|
|
|
const selectedNodesCount = Selected.Nodes.length
|
2016-04-15 08:31:22 +08:00
|
|
|
e.preventDefault()
|
2016-12-11 16:21:36 -05:00
|
|
|
|
|
|
|
// Hit Ctrl+A once to select all nodes
|
|
|
|
Control.deselectAllNodes()
|
|
|
|
Visualize.mGraph.graph.eachNode(node => {
|
|
|
|
Control.selectNode(node, e)
|
2016-04-15 08:31:22 +08:00
|
|
|
})
|
|
|
|
|
2016-12-11 16:21:36 -05:00
|
|
|
// Hitting Ctrl+A a second time will select all edges too
|
|
|
|
Control.deselectAllEdges()
|
|
|
|
if (nodesCount === selectedNodesCount) {
|
|
|
|
DataModel.Synapses.models.forEach(synapse => {
|
|
|
|
const topic1id = synapse.get('topic1_id')
|
|
|
|
const topic2id = synapse.get('topic2_id')
|
|
|
|
const edge = Visualize.mGraph.graph.edges[topic1id][topic2id]
|
|
|
|
Control.selectEdge(edge, e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-22 18:31:56 +08:00
|
|
|
Visualize.mGraph.plot()
|
2016-04-15 08:31:22 +08:00
|
|
|
}
|
|
|
|
|
2016-08-08 21:46:05 +08:00
|
|
|
break
|
|
|
|
case 68: // if d or D is pressed
|
2016-10-07 14:03:48 +08:00
|
|
|
if (e.ctrlKey || e.metaKey) {
|
2016-08-08 21:46:05 +08:00
|
|
|
e.preventDefault()
|
2016-09-22 18:31:56 +08:00
|
|
|
Control.deleteSelected()
|
2016-08-08 21:46:05 +08:00
|
|
|
}
|
2016-04-15 08:31:22 +08:00
|
|
|
break
|
|
|
|
case 69: // if e or E is pressed
|
2016-10-07 14:03:48 +08:00
|
|
|
if ((e.ctrlKey || e.metaKey) && Active.Map) {
|
2016-08-09 23:53:50 +08:00
|
|
|
e.preventDefault()
|
2016-09-22 18:31:56 +08:00
|
|
|
JIT.zoomExtents(null, Visualize.mGraph.canvas)
|
2016-08-09 23:53:50 +08:00
|
|
|
break
|
|
|
|
}
|
2016-09-22 18:31:56 +08:00
|
|
|
if (e.altKey && Active.Topic) {
|
2016-04-15 08:31:22 +08:00
|
|
|
e.preventDefault()
|
2016-08-09 23:53:50 +08:00
|
|
|
|
2016-09-22 18:31:56 +08:00
|
|
|
if (Active.Topic) {
|
|
|
|
self.centerAndReveal(Selected.Nodes, {
|
2016-08-08 21:46:05 +08:00
|
|
|
center: true,
|
|
|
|
reveal: false
|
|
|
|
})
|
2016-04-15 08:31:22 +08:00
|
|
|
}
|
2016-08-09 23:53:50 +08:00
|
|
|
break
|
2016-04-15 08:31:22 +08:00
|
|
|
}
|
|
|
|
break
|
2016-08-08 21:46:05 +08:00
|
|
|
case 72: // if h or H is pressed
|
2016-10-07 14:03:48 +08:00
|
|
|
if (e.ctrlKey || e.metaKey) {
|
2016-08-08 21:46:05 +08:00
|
|
|
e.preventDefault()
|
2016-09-22 18:31:56 +08:00
|
|
|
Control.hideSelectedNodes()
|
|
|
|
Control.hideSelectedEdges()
|
2016-08-08 21:46:05 +08:00
|
|
|
}
|
|
|
|
break
|
2016-04-15 08:31:22 +08:00
|
|
|
case 77: // if m or M is pressed
|
2016-10-07 14:03:48 +08:00
|
|
|
if (e.ctrlKey || e.metaKey) {
|
2016-04-15 08:31:22 +08:00
|
|
|
e.preventDefault()
|
2016-09-22 18:31:56 +08:00
|
|
|
Control.removeSelectedNodes()
|
|
|
|
Control.removeSelectedEdges()
|
2016-04-15 08:31:22 +08:00
|
|
|
}
|
|
|
|
break
|
2016-08-08 21:46:05 +08:00
|
|
|
case 82: // if r or R is pressed
|
2016-09-22 18:31:56 +08:00
|
|
|
if (e.altKey && Active.Topic) {
|
2016-04-15 08:31:22 +08:00
|
|
|
e.preventDefault()
|
2016-09-22 18:31:56 +08:00
|
|
|
self.centerAndReveal(Selected.Nodes, {
|
2016-08-08 21:46:05 +08:00
|
|
|
center: false,
|
|
|
|
reveal: true
|
|
|
|
})
|
2016-04-15 08:31:22 +08:00
|
|
|
}
|
|
|
|
break
|
2016-08-08 21:46:05 +08:00
|
|
|
case 84: // if t or T is pressed
|
2016-09-22 18:31:56 +08:00
|
|
|
if (e.altKey && Active.Topic) {
|
2016-04-15 08:31:22 +08:00
|
|
|
e.preventDefault()
|
2016-09-22 18:31:56 +08:00
|
|
|
self.centerAndReveal(Selected.Nodes, {
|
2016-08-08 21:46:05 +08:00
|
|
|
center: true,
|
|
|
|
reveal: true
|
|
|
|
})
|
2016-04-15 08:31:22 +08:00
|
|
|
}
|
|
|
|
break
|
2016-09-30 11:33:39 +08:00
|
|
|
case 191: // if / is pressed
|
2016-10-07 14:03:48 +08:00
|
|
|
if (e.ctrlKey || e.metaKey) {
|
2016-09-30 11:33:39 +08:00
|
|
|
Search.focus()
|
|
|
|
}
|
|
|
|
break
|
2016-04-15 08:31:22 +08:00
|
|
|
default:
|
2016-08-08 21:46:05 +08:00
|
|
|
// console.log(e.which)
|
|
|
|
break
|
2016-04-15 08:31:22 +08:00
|
|
|
}
|
|
|
|
})
|
2016-11-07 15:25:08 -05:00
|
|
|
$(window).resize(function() {
|
2016-10-10 17:20:43 +08:00
|
|
|
if (Visualize && Visualize.mGraph) {
|
2016-10-22 05:50:31 +00:00
|
|
|
Util.resizeCanvas(Visualize.mGraph.canvas)
|
2016-10-07 00:51:52 +00:00
|
|
|
}
|
2016-10-10 17:20:43 +08:00
|
|
|
|
2016-09-22 18:31:56 +08:00
|
|
|
if (Active.Map && Realtime.inConversation) Realtime.positionVideos()
|
2016-04-15 08:31:22 +08:00
|
|
|
})
|
2016-08-08 21:46:05 +08:00
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
centerAndReveal: function(nodes, opts) {
|
2016-08-08 21:46:05 +08:00
|
|
|
if (nodes.length < 1) return
|
|
|
|
var node = nodes[nodes.length - 1]
|
2016-08-09 23:53:50 +08:00
|
|
|
if (opts.center && opts.reveal) {
|
2016-11-07 15:25:08 -05:00
|
|
|
Topic.centerOn(node.id, function() {
|
2017-09-18 23:30:33 -04:00
|
|
|
Topic.fetchSiblings(nodes)
|
2016-08-09 23:53:50 +08:00
|
|
|
})
|
|
|
|
} else if (opts.center) {
|
2016-09-22 18:31:56 +08:00
|
|
|
Topic.centerOn(node.id)
|
2016-08-09 23:53:50 +08:00
|
|
|
} else if (opts.reveal) {
|
2017-09-18 23:30:33 -04:00
|
|
|
Topic.fetchSiblings(nodes)
|
2016-08-08 21:46:05 +08:00
|
|
|
}
|
2016-04-15 08:31:22 +08:00
|
|
|
}
|
2016-09-22 15:21:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Listeners
|