clean up backbone file imports

This commit is contained in:
Devin Howard 2016-09-23 00:20:05 +08:00
parent a996734c79
commit 30fc943833

View file

@ -4,29 +4,30 @@ import _ from 'lodash'
import Backbone from 'backbone' import Backbone from 'backbone'
Backbone.$ = window.$ Backbone.$ = window.$
import Active from '../Active'
import Filter from '../Filter'
import JIT from '../JIT'
import Map, { InfoBox } from '../Map'
import Mapper from '../Mapper'
import Realtime from '../Realtime'
import Synapse from '../Synapse'
import SynapseCard from '../SynapseCard'
import Topic from '../Topic'
import TopicCard from '../TopicCard'
import Visualize from '../Visualize'
/* /*
* Metamaps.Backbone.js.erb * Metamaps.Backbone.js.erb
* *
* Dependencies: * Dependencies:
* - Metamaps.Active
* - Metamaps.Collaborators * - Metamaps.Collaborators
* - Metamaps.Creators * - Metamaps.Creators
* - Metamaps.Filter
* - Metamaps.JIT
* - Metamaps.Loading * - Metamaps.Loading
* - Metamaps.Map
* - Metamaps.Mapper
* - Metamaps.Mappers * - Metamaps.Mappers
* - Metamaps.Mappings * - Metamaps.Mappings
* - Metamaps.Metacodes * - Metamaps.Metacodes
* - Metamaps.Realtime
* - Metamaps.Synapse
* - Metamaps.SynapseCard
* - Metamaps.Synapses * - Metamaps.Synapses
* - Metamaps.Topic
* - Metamaps.TopicCard
* - Metamaps.Topics * - Metamaps.Topics
* - Metamaps.Visualize
*/ */
const _Backbone = {} const _Backbone = {}
@ -62,7 +63,7 @@ _Backbone.Map = Backbone.Model.extend({
this.on('saved', this.savedEvent) this.on('saved', this.savedEvent)
}, },
savedEvent: function () { savedEvent: function () {
Metamaps.Realtime.sendMapChange(this) Realtime.sendMapChange(this)
}, },
authorizeToEdit: function (mapper) { authorizeToEdit: function (mapper) {
if (mapper && ( if (mapper && (
@ -82,7 +83,7 @@ _Backbone.Map = Backbone.Model.extend({
} }
}, },
getUser: function () { getUser: function () {
return Metamaps.Mapper.get(this.get('user_id')) return Mapper.get(this.get('user_id'))
}, },
fetchContained: function () { fetchContained: function () {
var bb = _Backbone var bb = _Backbone
@ -126,10 +127,10 @@ _Backbone.Map = Backbone.Model.extend({
return this.get('mappers') return this.get('mappers')
}, },
updateView: function () { updateView: function () {
var map = Metamaps.Active.Map var map = Active.Map
var isActiveMap = this.id === map.id var isActiveMap = this.id === map.id
if (isActiveMap) { if (isActiveMap) {
Metamaps.Map.InfoBox.updateNameDescPerm(this.get('name'), this.get('desc'), this.get('permission')) InfoBox.updateNameDescPerm(this.get('name'), this.get('desc'), this.get('permission'))
this.updateMapWrapper() this.updateMapWrapper()
// mobile menu // mobile menu
$('#header_content').html(this.get('name')) $('#header_content').html(this.get('name'))
@ -137,9 +138,9 @@ _Backbone.Map = Backbone.Model.extend({
} }
}, },
updateMapWrapper: function () { updateMapWrapper: function () {
var map = Metamaps.Active.Map var map = Active.Map
var isActiveMap = this.id === map.id var isActiveMap = this.id === map.id
var authorized = map && map.authorizeToEdit(Metamaps.Active.Mapper) ? 'canEditMap' : '' var authorized = map && map.authorizeToEdit(Active.Mapper) ? 'canEditMap' : ''
var commonsMap = map && map.get('permission') === 'commons' ? 'commonsMap' : '' var commonsMap = map && map.get('permission') === 'commons' ? 'commonsMap' : ''
if (isActiveMap) { if (isActiveMap) {
$('.wrapper').removeClass('canEditMap commonsMap').addClass(authorized + ' ' + commonsMap) $('.wrapper').removeClass('canEditMap commonsMap').addClass(authorized + ' ' + commonsMap)
@ -324,10 +325,10 @@ _Backbone.init = function () {
initialize: function () { initialize: function () {
if (this.isNew()) { if (this.isNew()) {
this.set({ this.set({
'user_id': Metamaps.Active.Mapper.id, 'user_id': Active.Mapper.id,
'desc': this.get('desc') || '', 'desc': this.get('desc') || '',
'link': this.get('link') || '', 'link': this.get('link') || '',
'permission': Metamaps.Active.Map ? Metamaps.Active.Map.get('permission') : 'commons' 'permission': Active.Map ? Active.Map.get('permission') : 'commons'
}) })
} }
@ -339,7 +340,7 @@ _Backbone.init = function () {
mappableid: this.id mappableid: this.id
} }
$(document).trigger(Metamaps.JIT.events.removeTopic, [removeTopicData]) $(document).trigger(JIT.events.removeTopic, [removeTopicData])
}) })
this.on('noLongerPrivate', function () { this.on('noLongerPrivate', function () {
var newTopicData = { var newTopicData = {
@ -347,10 +348,10 @@ _Backbone.init = function () {
mappableid: this.id mappableid: this.id
} }
$(document).trigger(Metamaps.JIT.events.newTopic, [newTopicData]) $(document).trigger(JIT.events.newTopic, [newTopicData])
}) })
this.on('change:metacode_id', Metamaps.Filter.checkMetacodes, this) this.on('change:metacode_id', Filter.checkMetacodes, this)
}, },
authorizeToEdit: function (mapper) { authorizeToEdit: function (mapper) {
if (mapper && if (mapper &&
@ -371,10 +372,10 @@ _Backbone.init = function () {
return Metamaps.Metacodes.get(this.get('metacode_id')) return Metamaps.Metacodes.get(this.get('metacode_id'))
}, },
getMapping: function () { getMapping: function () {
if (!Metamaps.Active.Map) return false if (!Active.Map) return false
return Metamaps.Mappings.findWhere({ return Metamaps.Mappings.findWhere({
map_id: Metamaps.Active.Map.id, map_id: Active.Map.id,
mappable_type: 'Topic', mappable_type: 'Topic',
mappable_id: this.isNew() ? this.cid : this.id mappable_id: this.isNew() ? this.cid : this.id
}) })
@ -387,7 +388,7 @@ _Backbone.init = function () {
name: this.get('name') name: this.get('name')
} }
if (Metamaps.Active.Map) { if (Active.Map) {
mapping = this.getMapping() mapping = this.getMapping()
node.data = { node.data = {
$mapping: null, $mapping: null,
@ -402,7 +403,7 @@ _Backbone.init = function () {
var node = this.get('node') var node = this.get('node')
node.setData('topic', this) node.setData('topic', this)
if (Metamaps.Active.Map) { if (Active.Map) {
mapping = this.getMapping() mapping = this.getMapping()
node.setData('mapping', mapping) node.setData('mapping', mapping)
} }
@ -410,38 +411,38 @@ _Backbone.init = function () {
return node return node
}, },
savedEvent: function () { savedEvent: function () {
Metamaps.Realtime.sendTopicChange(this) Realtime.sendTopicChange(this)
}, },
updateViews: function () { updateViews: function () {
var onPageWithTopicCard = Metamaps.Active.Map || Metamaps.Active.Topic var onPageWithTopicCard = Active.Map || Active.Topic
var node = this.get('node') var node = this.get('node')
// update topic card, if this topic is the one open there // update topic card, if this topic is the one open there
if (onPageWithTopicCard && this == Metamaps.TopicCard.openTopicCard) { if (onPageWithTopicCard && this == TopicCard.openTopicCard) {
Metamaps.TopicCard.showCard(node) TopicCard.showCard(node)
} }
// update the node on the map // update the node on the map
if (onPageWithTopicCard && node) { if (onPageWithTopicCard && node) {
node.name = this.get('name') node.name = this.get('name')
Metamaps.Visualize.mGraph.plot() Visualize.mGraph.plot()
} }
}, },
updateCardView: function () { updateCardView: function () {
var onPageWithTopicCard = Metamaps.Active.Map || Metamaps.Active.Topic var onPageWithTopicCard = Active.Map || Active.Topic
var node = this.get('node') var node = this.get('node')
// update topic card, if this topic is the one open there // update topic card, if this topic is the one open there
if (onPageWithTopicCard && this == Metamaps.TopicCard.openTopicCard) { if (onPageWithTopicCard && this == TopicCard.openTopicCard) {
Metamaps.TopicCard.showCard(node) TopicCard.showCard(node)
} }
}, },
updateNodeView: function () { updateNodeView: function () {
var onPageWithTopicCard = Metamaps.Active.Map || Metamaps.Active.Topic var onPageWithTopicCard = Active.Map || Active.Topic
var node = this.get('node') var node = this.get('node')
// update the node on the map // update the node on the map
if (onPageWithTopicCard && node) { if (onPageWithTopicCard && node) {
node.name = this.get('name') node.name = this.get('name')
Metamaps.Visualize.mGraph.plot() Visualize.mGraph.plot()
} }
} }
}) })
@ -489,8 +490,8 @@ _Backbone.init = function () {
initialize: function () { initialize: function () {
if (this.isNew()) { if (this.isNew()) {
this.set({ this.set({
'user_id': Metamaps.Active.Mapper.id, 'user_id': Active.Mapper.id,
'permission': Metamaps.Active.Map ? Metamaps.Active.Map.get('permission') : 'commons', 'permission': Active.Map ? Active.Map.get('permission') : 'commons',
'category': 'from-to' 'category': 'from-to'
}) })
} }
@ -504,15 +505,15 @@ _Backbone.init = function () {
mappableid: this.id mappableid: this.id
} }
$(document).trigger(Metamaps.JIT.events.newSynapse, [newSynapseData]) $(document).trigger(JIT.events.newSynapse, [newSynapseData])
}) })
this.on('nowPrivate', function () { this.on('nowPrivate', function () {
$(document).trigger(Metamaps.JIT.events.removeSynapse, [{ $(document).trigger(JIT.events.removeSynapse, [{
mappableid: this.id mappableid: this.id
}]) }])
}) })
this.on('change:desc', Metamaps.Filter.checkSynapses, this) this.on('change:desc', Filter.checkSynapses, this)
}, },
prepareLiForFilter: function () { prepareLiForFilter: function () {
var li = '' var li = ''
@ -546,10 +547,10 @@ _Backbone.init = function () {
] : false ] : false
}, },
getMapping: function () { getMapping: function () {
if (!Metamaps.Active.Map) return false if (!Active.Map) return false
return Metamaps.Mappings.findWhere({ return Metamaps.Mappings.findWhere({
map_id: Metamaps.Active.Map.id, map_id: Active.Map.id,
mappable_type: 'Synapse', mappable_type: 'Synapse',
mappable_id: this.isNew() ? this.cid : this.id mappable_id: this.isNew() ? this.cid : this.id
}) })
@ -567,7 +568,7 @@ _Backbone.init = function () {
} }
} }
if (Metamaps.Active.Map) { if (Active.Map) {
mapping = providedMapping || this.getMapping() mapping = providedMapping || this.getMapping()
mappingID = mapping.isNew() ? mapping.cid : mapping.id mappingID = mapping.isNew() ? mapping.cid : mapping.id
edge.data.$mappings = [] edge.data.$mappings = []
@ -581,7 +582,7 @@ _Backbone.init = function () {
var edge = this.get('edge') var edge = this.get('edge')
edge.getData('synapses').push(this) edge.getData('synapses').push(this)
if (Metamaps.Active.Map) { if (Active.Map) {
mapping = this.getMapping() mapping = this.getMapping()
edge.getData('mappings').push(mapping) edge.getData('mappings').push(mapping)
} }
@ -589,28 +590,28 @@ _Backbone.init = function () {
return edge return edge
}, },
savedEvent: function () { savedEvent: function () {
Metamaps.Realtime.sendSynapseChange(this) Realtime.sendSynapseChange(this)
}, },
updateViews: function () { updateViews: function () {
this.updateCardView() this.updateCardView()
this.updateEdgeView() this.updateEdgeView()
}, },
updateCardView: function () { updateCardView: function () {
var onPageWithSynapseCard = Metamaps.Active.Map || Metamaps.Active.Topic var onPageWithSynapseCard = Active.Map || Active.Topic
var edge = this.get('edge') var edge = this.get('edge')
// update synapse card, if this synapse is the one open there // update synapse card, if this synapse is the one open there
if (onPageWithSynapseCard && edge == Metamaps.SynapseCard.openSynapseCard) { if (onPageWithSynapseCard && edge == SynapseCard.openSynapseCard) {
Metamaps.SynapseCard.showCard(edge) SynapseCard.showCard(edge)
} }
}, },
updateEdgeView: function () { updateEdgeView: function () {
var onPageWithSynapseCard = Metamaps.Active.Map || Metamaps.Active.Topic var onPageWithSynapseCard = Active.Map || Active.Topic
var edge = this.get('edge') var edge = this.get('edge')
// update the edge on the map // update the edge on the map
if (onPageWithSynapseCard && edge) { if (onPageWithSynapseCard && edge) {
Metamaps.Visualize.mGraph.plot() Visualize.mGraph.plot()
} }
} }
}) })
@ -629,20 +630,20 @@ _Backbone.init = function () {
initialize: function () { initialize: function () {
if (this.isNew()) { if (this.isNew()) {
this.set({ this.set({
'user_id': Metamaps.Active.Mapper.id, 'user_id': Active.Mapper.id,
'map_id': Metamaps.Active.Map ? Metamaps.Active.Map.id : null 'map_id': Active.Map ? Active.Map.id : null
}) })
} }
}, },
getMap: function () { getMap: function () {
return Metamaps.Map.get(this.get('map_id')) return Map.get(this.get('map_id'))
}, },
getTopic: function () { getTopic: function () {
if (this.get('mappable_type') === 'Topic') return Metamaps.Topic.get(this.get('mappable_id')) if (this.get('mappable_type') === 'Topic') return Topic.get(this.get('mappable_id'))
else return false else return false
}, },
getSynapse: function () { getSynapse: function () {
if (this.get('mappable_type') === 'Synapse') return Metamaps.Synapse.get(this.get('mappable_id')) if (this.get('mappable_type') === 'Synapse') return Synapse.get(this.get('mappable_id'))
else return false else return false
} }
}) })
@ -665,34 +666,34 @@ _Backbone.init = function () {
// this is for topic view // this is for topic view
Metamaps.Creators = Metamaps.Creators ? new self.MapperCollection(Metamaps.Creators) : new self.MapperCollection() Metamaps.Creators = Metamaps.Creators ? new self.MapperCollection(Metamaps.Creators) : new self.MapperCollection()
if (Metamaps.Active.Map) { if (Active.Map) {
Metamaps.Mappings = Metamaps.Mappings ? new self.MappingCollection(Metamaps.Mappings) : new self.MappingCollection() Metamaps.Mappings = Metamaps.Mappings ? new self.MappingCollection(Metamaps.Mappings) : new self.MappingCollection()
Metamaps.Active.Map = new self.Map(Metamaps.Active.Map) Active.Map = new self.Map(Active.Map)
} }
if (Metamaps.Active.Topic) Metamaps.Active.Topic = new self.Topic(Metamaps.Active.Topic) if (Active.Topic) Active.Topic = new self.Topic(Active.Topic)
// attach collection event listeners // attach collection event listeners
self.attachCollectionEvents = function () { self.attachCollectionEvents = function () {
Metamaps.Topics.on('add remove', function (topic) { Metamaps.Topics.on('add remove', function (topic) {
Metamaps.Map.InfoBox.updateNumbers() InfoBox.updateNumbers()
Metamaps.Filter.checkMetacodes() Filter.checkMetacodes()
Metamaps.Filter.checkMappers() Filter.checkMappers()
}) })
Metamaps.Synapses.on('add remove', function (synapse) { Metamaps.Synapses.on('add remove', function (synapse) {
Metamaps.Map.InfoBox.updateNumbers() InfoBox.updateNumbers()
Metamaps.Filter.checkSynapses() Filter.checkSynapses()
Metamaps.Filter.checkMappers() Filter.checkMappers()
}) })
if (Metamaps.Active.Map) { if (Active.Map) {
Metamaps.Mappings.on('add remove', function (mapping) { Metamaps.Mappings.on('add remove', function (mapping) {
Metamaps.Map.InfoBox.updateNumbers() InfoBox.updateNumbers()
Metamaps.Filter.checkSynapses() Filter.checkSynapses()
Metamaps.Filter.checkMetacodes() Filter.checkMetacodes()
Metamaps.Filter.checkMappers() Filter.checkMappers()
}) })
} }
} }