metamaps--metamaps/frontend/src/Metamaps/DataModel/Map.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

/* global $ */
import _ from 'lodash'
import Backbone from 'backbone'
2016-10-03 08:32:37 +08:00
try { Backbone.$ = window.$ } catch (err) {}
import Active from '../Active'
2016-10-03 08:32:37 +08:00
import InfoBox from '../Map/InfoBox'
import Mapper from '../Mapper'
import Realtime from '../Realtime'
const Map = Backbone.Model.extend({
urlRoot: '/maps',
blacklist: ['created_at', 'updated_at', 'created_at_clean', 'updated_at_clean', 'user_name', 'contributor_count', 'topic_count', 'synapse_count', 'topics', 'synapses', 'mappings', 'mappers'],
2016-11-07 15:25:08 -05:00
toJSON: function(options) {
return _.omit(this.attributes, this.blacklist)
},
2016-11-07 15:25:08 -05:00
initialize: function() {
this.on('changeByOther', this.updateView)
},
2016-11-07 15:25:08 -05:00
authorizeToEdit: function(mapper) {
if (mapper && (
this.get('permission') === 'commons' ||
(this.get('collaborator_ids') || []).includes(mapper.get('id')) ||
this.get('user_id') === mapper.get('id'))) {
return true
} else {
return false
}
},
2016-11-07 15:25:08 -05:00
authorizePermissionChange: function(mapper) {
if (mapper && this.get('user_id') === mapper.get('id')) {
return true
} else {
return false
}
},
2016-11-07 15:25:08 -05:00
getUser: function() {
return Mapper.get(this.get('user_id'))
},
2016-11-07 15:25:08 -05:00
updateView: function() {
var map = Active.Map
var isActiveMap = this.id === map.id
if (isActiveMap) {
InfoBox.updateNameDescPerm(this.get('name'), this.get('desc'), this.get('permission'))
this.updateMapWrapper()
// mobile menu
$('#header_content').html(this.get('name'))
document.title = this.get('name') + ' | Metamaps'
}
},
2016-11-07 15:25:08 -05:00
updateMapWrapper: function() {
var map = Active.Map
var isActiveMap = this.id === map.id
var authorized = map && map.authorizeToEdit(Active.Mapper) ? 'canEditMap' : ''
var commonsMap = map && map.get('permission') === 'commons' ? 'commonsMap' : ''
if (isActiveMap) {
$('.wrapper').removeClass('canEditMap commonsMap').addClass(authorized + ' ' + commonsMap)
}
}
})
export default Map