overleaf/services/web/public/coffee/models/Project.coffee

112 lines
3.5 KiB
CoffeeScript
Raw Normal View History

2014-02-12 10:23:40 +00:00
define [
"models/User"
"models/ProjectMemberList"
"models/Folder"
"models/Doc"
2014-02-12 10:23:40 +00:00
"libs/backbone"
], (User, ProjectMemberList, Folder, Doc) ->
2014-02-12 10:23:40 +00:00
Project = Backbone.Model.extend
initialize: ->
@on "change:ide", (project, ide) =>
@bindToRootDocId()
@bindToProjectName()
@bindToPublicAccessLevel()
@bindToCompiler()
@bindToSpellingPreference()
@bindToProjectDescription()
parse: (rawAttributes) ->
attributes =
id : rawAttributes._id
name : rawAttributes.name
rootFolder : new Folder(rawAttributes.rootFolder[0], parse: true)
rootDoc_id : rawAttributes.rootDoc_id
publicAccesLevel : rawAttributes.publicAccesLevel
features : rawAttributes.features
compiler : rawAttributes.compiler
spellCheckLanguage: rawAttributes.spellCheckLanguage
dropboxEnabled : rawAttributes.dropboxEnabled
description : rawAttributes.description
deletedByExternalDataSource: rawAttributes.deletedByExternalDataSource
useOt : rawAttributes.useOt
attributes.members = members = new ProjectMemberList()
attributes.owner = owner = User.findOrBuild rawAttributes.owner._id, rawAttributes.owner
owner.set("privileges", "owner")
members.add owner
2014-06-11 10:26:45 +00:00
for rawMember in rawAttributes.members or []
2014-02-12 10:23:40 +00:00
member = User.findOrBuild rawMember._id, rawMember
members.add member
2014-06-11 10:26:45 +00:00
for doc in rawAttributes.deletedDocs or []
doc.deleted = true
attributes.deletedDocs = new Folder({
_id: "deleted-docs-folder"
name: "Deleted documents"
docs: rawAttributes.deletedDocs
}, parse: true)
2014-02-12 10:23:40 +00:00
return attributes
bindToRootDocId: ->
remoteChange = false
2014-02-12 10:23:40 +00:00
@on "change:rootDoc_id", (project, docId) =>
if !remoteChange
@get("ide").socket.emit "setRootDoc", docId
2014-02-12 10:23:40 +00:00
@get("ide").socket.on "rootDocUpdated", (docId) =>
remoteChange = true
2014-02-12 10:23:40 +00:00
@set("rootDoc_id", docId)
remoteChange = false
2014-02-12 10:23:40 +00:00
bindToProjectName: ->
remoteChange = false
2014-02-12 10:23:40 +00:00
@on "change:name", (project, name) =>
if !remoteChange
@get("ide").socket.emit "setProjectName", name
@get("ide").socket.on "projectNameUpdated", (name) =>
remoteChange = true
2014-02-12 10:23:40 +00:00
@set("name", name)
remoteChange = false
2014-02-12 10:23:40 +00:00
bindToCompiler: ->
remoteChange = false
2014-02-12 10:23:40 +00:00
@on "change:compiler", (project, name) =>
if !remoteChange
@get("ide").socket.emit "setCompiler", name
2014-02-12 10:23:40 +00:00
@get("ide").socket.on "compilerUpdated", (compiler) =>
remoteChange = true
@set("compiler", compiler)
remoteChange = false
2014-02-12 10:23:40 +00:00
bindToSpellingPreference: ->
@on "change:spellCheckLanguage", (project, languageCode) =>
@get("ide").socket.emit "setSpellCheckLanguage", languageCode
bindToPublicAccessLevel: ->
remoteChange = false
2014-02-12 10:23:40 +00:00
@on "change:publicAccesLevel", (project, level) =>
if !remoteChange
@get("ide").socket.emit "setPublicAccessLevel", level
2014-02-12 10:23:40 +00:00
@get("ide").socket.on 'publicAccessLevelUpdated', (level) =>
remoteChange = true
2014-02-12 10:23:40 +00:00
@set("publicAccesLevel", level)
remoteChange = false
2014-02-12 10:23:40 +00:00
bindToProjectDescription: ->
remoteChange = false
2014-02-12 10:23:40 +00:00
@on "change:description", (project, description) =>
if !remoteChange
@get("ide").socket.emit "updateProjectDescription", description
2014-02-12 10:23:40 +00:00
@get("ide").socket.on 'projectDescriptionUpdated', (description) =>
remoteChange = true
2014-02-12 10:23:40 +00:00
@set("description", description)
remoteChange = false
2014-02-12 10:23:40 +00:00
getRootDocumentsList: (callback) ->
@get("ide").socket.emit "getRootDocumentsList", (err, listOfDocs) ->
callback(listOfDocs)