2016-10-02 22:37:38 +08:00
|
|
|
/* global $ */
|
2016-10-02 17:58:37 +08:00
|
|
|
|
|
|
|
import _ from 'lodash'
|
|
|
|
import Backbone from 'backbone'
|
|
|
|
Backbone.$ = window.$
|
|
|
|
|
|
|
|
import Active from '../Active'
|
|
|
|
import Filter from '../Filter'
|
|
|
|
import JIT from '../JIT'
|
|
|
|
import Realtime from '../Realtime'
|
|
|
|
import SynapseCard from '../SynapseCard'
|
|
|
|
import Visualize from '../Visualize'
|
|
|
|
|
2016-10-02 22:28:00 +08:00
|
|
|
import DataModel from './index'
|
|
|
|
|
2016-10-02 17:58:37 +08:00
|
|
|
const Synapse = Backbone.Model.extend({
|
|
|
|
urlRoot: '/synapses',
|
|
|
|
blacklist: ['edge', 'created_at', 'updated_at'],
|
|
|
|
toJSON: function (options) {
|
|
|
|
return _.omit(this.attributes, this.blacklist)
|
|
|
|
},
|
|
|
|
save: function (key, val, options) {
|
|
|
|
var attrs
|
|
|
|
|
|
|
|
// Handle both `"key", value` and `{key: value}` -style arguments.
|
|
|
|
if (key == null || typeof key === 'object') {
|
|
|
|
attrs = key
|
|
|
|
options = val
|
|
|
|
} else {
|
|
|
|
(attrs = {})[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
var newOptions = options || {}
|
|
|
|
var s = newOptions.success
|
|
|
|
|
|
|
|
var permBefore = this.get('permission')
|
|
|
|
|
|
|
|
newOptions.success = function (model, response, opt) {
|
|
|
|
if (s) s(model, response, opt)
|
|
|
|
model.trigger('saved')
|
|
|
|
|
|
|
|
if (permBefore === 'private' && model.get('permission') !== 'private') {
|
|
|
|
model.trigger('noLongerPrivate')
|
2016-10-02 18:21:56 +08:00
|
|
|
} else if (permBefore !== 'private' && model.get('permission') === 'private') {
|
2016-10-02 17:58:37 +08:00
|
|
|
model.trigger('nowPrivate')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Backbone.Model.prototype.save.call(this, attrs, newOptions)
|
|
|
|
},
|
|
|
|
initialize: function () {
|
|
|
|
if (this.isNew()) {
|
|
|
|
this.set({
|
|
|
|
'user_id': Active.Mapper.id,
|
|
|
|
'permission': Active.Map ? Active.Map.get('permission') : 'commons',
|
|
|
|
'category': 'from-to'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on('changeByOther', this.updateCardView)
|
|
|
|
this.on('change', this.updateEdgeView)
|
|
|
|
this.on('saved', this.savedEvent)
|
|
|
|
this.on('noLongerPrivate', function () {
|
|
|
|
var newSynapseData = {
|
|
|
|
mappingid: this.getMapping().id,
|
|
|
|
mappableid: this.id
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document).trigger(JIT.events.newSynapse, [newSynapseData])
|
|
|
|
})
|
|
|
|
this.on('nowPrivate', function () {
|
|
|
|
$(document).trigger(JIT.events.removeSynapse, [{
|
|
|
|
mappableid: this.id
|
|
|
|
}])
|
|
|
|
})
|
|
|
|
|
|
|
|
this.on('change:desc', Filter.checkSynapses, this)
|
|
|
|
},
|
|
|
|
prepareLiForFilter: function () {
|
|
|
|
var li = ''
|
|
|
|
li += '<li data-id="' + this.get('desc') + '">'
|
2016-10-02 22:37:38 +08:00
|
|
|
li += '<img src="' + Metamaps.ServerData['synapse16.png'] + '"'
|
2016-10-02 17:58:37 +08:00
|
|
|
li += ' alt="synapse icon" />'
|
|
|
|
li += '<p>' + this.get('desc') + '</p></li>'
|
|
|
|
return li
|
|
|
|
},
|
|
|
|
authorizeToEdit: function (mapper) {
|
|
|
|
if (mapper && (this.get('calculated_permission') === 'commons' || this.get('collaborator_ids').includes(mapper.get('id')) || this.get('user_id') === mapper.get('id'))) return true
|
|
|
|
else return false
|
|
|
|
},
|
|
|
|
authorizePermissionChange: function (mapper) {
|
|
|
|
if (mapper && this.get('user_id') === mapper.get('id')) return true
|
|
|
|
else return false
|
|
|
|
},
|
|
|
|
getTopic1: function () {
|
2016-10-02 22:28:00 +08:00
|
|
|
return DataModel.Topics.get(this.get('topic1_id'))
|
2016-10-02 17:58:37 +08:00
|
|
|
},
|
|
|
|
getTopic2: function () {
|
2016-10-02 22:28:00 +08:00
|
|
|
return DataModel.Topics.get(this.get('topic2_id'))
|
2016-10-02 17:58:37 +08:00
|
|
|
},
|
|
|
|
getDirection: function () {
|
2016-10-02 18:21:56 +08:00
|
|
|
var t1 = this.getTopic1()
|
|
|
|
var t2 = this.getTopic2()
|
2016-10-02 17:58:37 +08:00
|
|
|
|
|
|
|
return t1 && t2 ? [
|
|
|
|
t1.get('node').id,
|
|
|
|
t2.get('node').id
|
|
|
|
] : false
|
|
|
|
},
|
|
|
|
getMapping: function () {
|
|
|
|
if (!Active.Map) return false
|
|
|
|
|
2016-10-02 22:28:00 +08:00
|
|
|
return DataModel.Mappings.findWhere({
|
2016-10-02 17:58:37 +08:00
|
|
|
map_id: Active.Map.id,
|
|
|
|
mappable_type: 'Synapse',
|
|
|
|
mappable_id: this.isNew() ? this.cid : this.id
|
|
|
|
})
|
|
|
|
},
|
|
|
|
createEdge: function (providedMapping) {
|
|
|
|
var mapping, mappingID
|
|
|
|
var synapseID = this.isNew() ? this.cid : this.id
|
|
|
|
|
|
|
|
var edge = {
|
|
|
|
nodeFrom: this.get('topic1_id'),
|
|
|
|
nodeTo: this.get('topic2_id'),
|
|
|
|
data: {
|
|
|
|
$synapses: [],
|
2016-10-02 18:21:56 +08:00
|
|
|
$synapseIDs: [synapseID]
|
2016-10-02 17:58:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Active.Map) {
|
|
|
|
mapping = providedMapping || this.getMapping()
|
|
|
|
mappingID = mapping.isNew() ? mapping.cid : mapping.id
|
|
|
|
edge.data.$mappings = []
|
|
|
|
edge.data.$mappingIDs = [mappingID]
|
|
|
|
}
|
|
|
|
|
|
|
|
return edge
|
|
|
|
},
|
|
|
|
updateEdge: function () {
|
|
|
|
var mapping
|
|
|
|
var edge = this.get('edge')
|
|
|
|
edge.getData('synapses').push(this)
|
|
|
|
|
|
|
|
if (Active.Map) {
|
|
|
|
mapping = this.getMapping()
|
|
|
|
edge.getData('mappings').push(mapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
return edge
|
|
|
|
},
|
|
|
|
savedEvent: function () {
|
|
|
|
Realtime.updateSynapse(this)
|
|
|
|
},
|
|
|
|
updateViews: function () {
|
|
|
|
this.updateCardView()
|
|
|
|
this.updateEdgeView()
|
|
|
|
},
|
|
|
|
updateCardView: function () {
|
|
|
|
var onPageWithSynapseCard = Active.Map || Active.Topic
|
|
|
|
var edge = this.get('edge')
|
|
|
|
|
|
|
|
// update synapse card, if this synapse is the one open there
|
2016-10-02 18:21:56 +08:00
|
|
|
if (onPageWithSynapseCard && edge === SynapseCard.openSynapseCard) {
|
2016-10-02 17:58:37 +08:00
|
|
|
SynapseCard.showCard(edge)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateEdgeView: function () {
|
|
|
|
var onPageWithSynapseCard = Active.Map || Active.Topic
|
|
|
|
var edge = this.get('edge')
|
|
|
|
|
|
|
|
// update the edge on the map
|
|
|
|
if (onPageWithSynapseCard && edge) {
|
|
|
|
Visualize.mGraph.plot()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
export default Synapse
|