2014-01-26 17:46:25 -05:00
|
|
|
storage = require 'lib/storage'
|
2014-04-09 22:07:44 -04:00
|
|
|
deltasLib = require 'lib/deltas'
|
2014-01-26 17:46:25 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
class CocoModel extends Backbone.Model
|
|
|
|
idAttribute: "_id"
|
|
|
|
loaded: false
|
|
|
|
loading: false
|
2014-01-26 17:46:25 -05:00
|
|
|
saveBackups: false
|
2014-05-22 14:22:52 -04:00
|
|
|
notyErrors: true
|
2014-01-03 13:32:13 -05:00
|
|
|
@schema: null
|
|
|
|
|
2014-05-05 20:37:14 -04:00
|
|
|
getMe: -> @me or @me = require('lib/auth').me
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
initialize: ->
|
|
|
|
super()
|
|
|
|
if not @constructor.className
|
|
|
|
console.error("#{@} needs a className set.")
|
|
|
|
@markToRevert()
|
2014-04-12 13:51:02 -04:00
|
|
|
@addSchemaDefaults()
|
2014-05-20 00:14:27 -04:00
|
|
|
@on 'sync', @onLoaded, @
|
2014-04-25 18:31:38 -04:00
|
|
|
@on 'error', @onError, @
|
2014-01-26 17:46:25 -05:00
|
|
|
@saveBackup = _.debounce(@saveBackup, 500)
|
2014-02-12 15:41:41 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
type: ->
|
|
|
|
@constructor.className
|
2014-04-13 17:48:36 -04:00
|
|
|
|
2014-04-12 00:11:52 -04:00
|
|
|
clone: (withChanges=true) ->
|
|
|
|
# Backbone does not support nested documents
|
|
|
|
clone = super()
|
|
|
|
clone.set($.extend(true, {}, if withChanges then @attributes else @_revertAttributes))
|
|
|
|
clone
|
2014-05-06 19:58:08 -04:00
|
|
|
|
2014-04-25 18:31:38 -04:00
|
|
|
onError: ->
|
|
|
|
@loading = false
|
2014-05-27 19:33:57 -04:00
|
|
|
@jqxhr = null
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-01-26 17:46:25 -05:00
|
|
|
onLoaded: ->
|
2014-01-03 13:32:13 -05:00
|
|
|
@loaded = true
|
|
|
|
@loading = false
|
2014-05-27 19:33:57 -04:00
|
|
|
@jqxhr = null
|
2014-04-12 13:51:02 -04:00
|
|
|
@markToRevert()
|
|
|
|
@loadFromBackup()
|
2014-05-21 13:27:38 -04:00
|
|
|
|
2014-05-02 13:31:20 -04:00
|
|
|
getNormalizedURL: -> "#{@urlRoot}/#{@id}"
|
2014-02-12 15:41:41 -05:00
|
|
|
|
2014-01-26 17:46:25 -05:00
|
|
|
set: ->
|
|
|
|
res = super(arguments...)
|
2014-02-05 14:03:32 -05:00
|
|
|
@saveBackup() if @saveBackups and @loaded and @hasLocalChanges()
|
2014-01-26 17:46:25 -05:00
|
|
|
res
|
2014-02-12 15:41:41 -05:00
|
|
|
|
2014-03-23 13:29:08 -04:00
|
|
|
loadFromBackup: ->
|
|
|
|
return unless @saveBackups
|
|
|
|
existing = storage.load @id
|
|
|
|
if existing
|
|
|
|
@set(existing, {silent:true})
|
|
|
|
CocoModel.backedUp[@id] = @
|
|
|
|
|
2014-01-26 17:46:25 -05:00
|
|
|
saveBackup: ->
|
|
|
|
storage.save(@id, @attributes)
|
|
|
|
CocoModel.backedUp[@id] = @
|
2014-02-12 15:41:41 -05:00
|
|
|
|
2014-01-26 17:46:25 -05:00
|
|
|
@backedUp = {}
|
2014-01-03 13:32:13 -05:00
|
|
|
schema: -> return @constructor.schema
|
|
|
|
|
|
|
|
validate: ->
|
2014-05-30 16:57:21 -04:00
|
|
|
result = tv4.validateMultiple(@attributes, @constructor.schema or {})
|
2014-01-03 13:32:13 -05:00
|
|
|
if result.errors?.length
|
|
|
|
console.log @, "got validate result with errors:", result
|
|
|
|
return result.errors unless result.valid
|
|
|
|
|
|
|
|
save: (attrs, options) ->
|
2014-04-17 20:30:55 -04:00
|
|
|
@set 'editPath', document.location.pathname
|
2014-01-03 13:32:13 -05:00
|
|
|
options ?= {}
|
|
|
|
success = options.success
|
2014-05-21 13:27:38 -04:00
|
|
|
error = options.error
|
|
|
|
options.success = (model, res) =>
|
2014-01-03 13:32:13 -05:00
|
|
|
@trigger "save:success", @
|
2014-05-21 13:27:38 -04:00
|
|
|
success(@, res) if success
|
2014-01-03 13:32:13 -05:00
|
|
|
@markToRevert()
|
2014-01-26 17:46:25 -05:00
|
|
|
@clearBackup()
|
2014-05-21 13:27:38 -04:00
|
|
|
options.error = (model, res) =>
|
|
|
|
error(@, res) if error
|
2014-05-22 14:22:52 -04:00
|
|
|
return unless @notyErrors
|
2014-05-21 13:27:38 -04:00
|
|
|
errorMessage = "Error saving #{@get('name') ? @type()}"
|
|
|
|
console.error errorMessage, res.responseJSON
|
|
|
|
noty text: "#{errorMessage}: #{res.status} #{res.statusText}", layout: 'topCenter', type: 'error', killer: false, timeout: 10000
|
2014-01-03 13:32:13 -05:00
|
|
|
@trigger "save", @
|
|
|
|
return super attrs, options
|
|
|
|
|
|
|
|
fetch: ->
|
2014-05-02 13:31:20 -04:00
|
|
|
@jqxhr = super(arguments...)
|
2014-01-03 13:32:13 -05:00
|
|
|
@loading = true
|
2014-05-02 13:31:20 -04:00
|
|
|
@jqxhr
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-05-30 16:40:38 -04:00
|
|
|
markToRevert: (force=false) ->
|
|
|
|
return unless @saveBackups or force
|
2014-03-23 13:00:16 -04:00
|
|
|
if @type() is 'ThangType'
|
|
|
|
@_revertAttributes = _.clone @attributes # No deep clones for these!
|
|
|
|
else
|
2014-03-21 22:21:23 -04:00
|
|
|
@_revertAttributes = $.extend(true, {}, @attributes)
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
revert: ->
|
|
|
|
@set(@_revertAttributes, {silent: true}) if @_revertAttributes
|
2014-01-26 17:46:25 -05:00
|
|
|
@clearBackup()
|
2014-02-12 15:41:41 -05:00
|
|
|
|
2014-01-26 17:46:25 -05:00
|
|
|
clearBackup: ->
|
|
|
|
storage.remove @id
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
hasLocalChanges: ->
|
2014-05-29 13:48:48 -04:00
|
|
|
@_revertAttributes and not _.isEqual @attributes, @_revertAttributes
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
cloneNewMinorVersion: ->
|
2014-05-05 23:07:34 -04:00
|
|
|
newData = _.clone @attributes
|
2014-05-05 20:37:14 -04:00
|
|
|
|
2014-04-12 01:33:09 -04:00
|
|
|
clone = new @constructor(newData)
|
|
|
|
clone
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
cloneNewMajorVersion: ->
|
|
|
|
clone = @cloneNewMinorVersion()
|
|
|
|
clone.unset('version')
|
|
|
|
clone
|
|
|
|
|
|
|
|
isPublished: ->
|
|
|
|
for permission in @get('permissions') or []
|
|
|
|
return true if permission.target is 'public' and permission.access is 'read'
|
|
|
|
false
|
|
|
|
|
|
|
|
publish: ->
|
|
|
|
if @isPublished() then throw new Error("Can't publish what's already-published. Can't kill what's already dead.")
|
|
|
|
@set "permissions", (@get("permissions") or []).concat({access: 'read', target: 'public'})
|
|
|
|
|
|
|
|
addSchemaDefaults: ->
|
2014-04-12 13:51:02 -04:00
|
|
|
return if @addedSchemaDefaults
|
2014-01-03 13:32:13 -05:00
|
|
|
@addedSchemaDefaults = true
|
2014-04-12 04:35:56 -04:00
|
|
|
for prop, defaultValue of @constructor.schema.default or {}
|
2014-01-03 13:32:13 -05:00
|
|
|
continue if @get(prop)?
|
|
|
|
#console.log "setting", prop, "to", defaultValue, "from attributes.default"
|
|
|
|
@set prop, defaultValue
|
2014-04-12 04:35:56 -04:00
|
|
|
for prop, sch of @constructor.schema.properties or {}
|
2014-01-03 13:32:13 -05:00
|
|
|
continue if @get(prop)?
|
2014-04-21 19:15:23 -04:00
|
|
|
continue if prop is 'emails' # hack, defaults are handled through User.coffee's email-specific methods.
|
2014-01-03 13:32:13 -05:00
|
|
|
#console.log "setting", prop, "to", sch.default, "from sch.default" if sch.default?
|
|
|
|
@set prop, sch.default if sch.default?
|
2014-03-23 13:29:08 -04:00
|
|
|
if @loaded
|
|
|
|
@markToRevert()
|
|
|
|
@loadFromBackup()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
@isObjectID: (s) ->
|
2014-04-11 23:38:34 -04:00
|
|
|
s.length is 24 and s.match(/[a-f0-9]/gi)?.length is 24
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-03-03 14:41:35 -05:00
|
|
|
hasReadAccess: (actor) ->
|
|
|
|
# actor is a User object
|
|
|
|
|
2014-05-05 20:37:14 -04:00
|
|
|
actor ?= @getMe()
|
2014-04-10 16:09:44 -04:00
|
|
|
return true if actor.isAdmin()
|
2014-03-03 14:41:35 -05:00
|
|
|
if @get('permissions')?
|
|
|
|
for permission in @get('permissions')
|
|
|
|
if permission.target is 'public' or actor.get('_id') is permission.target
|
|
|
|
return true if permission.access in ['owner', 'read']
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
2014-03-03 15:13:02 -05:00
|
|
|
hasWriteAccess: (actor) ->
|
|
|
|
# actor is a User object
|
2014-03-03 14:41:35 -05:00
|
|
|
|
2014-05-05 20:37:14 -04:00
|
|
|
actor ?= @getMe()
|
2014-04-10 16:09:44 -04:00
|
|
|
return true if actor.isAdmin()
|
2014-03-03 14:41:35 -05:00
|
|
|
if @get('permissions')?
|
|
|
|
for permission in @get('permissions')
|
|
|
|
if permission.target is 'public' or actor.get('_id') is permission.target
|
|
|
|
return true if permission.access in ['owner', 'write']
|
|
|
|
|
|
|
|
return false
|
2014-04-13 17:48:36 -04:00
|
|
|
|
2014-04-09 19:09:35 -04:00
|
|
|
getDelta: ->
|
2014-04-12 00:11:52 -04:00
|
|
|
differ = deltasLib.makeJSONDiffer()
|
|
|
|
differ.diff @_revertAttributes, @attributes
|
2014-05-21 13:27:38 -04:00
|
|
|
|
2014-05-08 13:54:39 -04:00
|
|
|
getDeltaWith: (otherModel) ->
|
|
|
|
differ = deltasLib.makeJSONDiffer()
|
|
|
|
differ.diff @attributes, otherModel.attributes
|
2014-04-13 17:48:36 -04:00
|
|
|
|
2014-04-12 00:11:52 -04:00
|
|
|
applyDelta: (delta) ->
|
|
|
|
newAttributes = $.extend(true, {}, @attributes)
|
|
|
|
jsondiffpatch.patch newAttributes, delta
|
|
|
|
@set newAttributes
|
2014-04-13 17:48:36 -04:00
|
|
|
|
2014-04-09 22:07:44 -04:00
|
|
|
getExpandedDelta: ->
|
|
|
|
delta = @getDelta()
|
2014-04-12 05:46:42 -04:00
|
|
|
deltasLib.expandDelta(delta, @_revertAttributes, @schema())
|
2014-04-13 17:48:36 -04:00
|
|
|
|
2014-05-08 13:54:39 -04:00
|
|
|
getExpandedDeltaWith: (otherModel) ->
|
|
|
|
delta = @getDeltaWith(otherModel)
|
|
|
|
deltasLib.expandDelta(delta, @attributes, @schema())
|
|
|
|
|
2014-04-15 18:09:36 -04:00
|
|
|
watch: (doWatch=true) ->
|
|
|
|
$.ajax("#{@urlRoot}/#{@id}/watch", {type:'PUT', data:{on:doWatch}})
|
2014-04-16 14:02:40 -04:00
|
|
|
@watching = -> doWatch
|
2014-04-22 14:11:08 -04:00
|
|
|
|
2014-04-15 18:09:36 -04:00
|
|
|
watching: ->
|
2014-04-16 14:02:40 -04:00
|
|
|
return me.id in (@get('watchers') or [])
|
2014-05-06 19:58:08 -04:00
|
|
|
|
2014-04-22 20:56:41 -04:00
|
|
|
populateI18N: (data, schema, path='') ->
|
|
|
|
# TODO: Better schema/json walking
|
|
|
|
sum = 0
|
|
|
|
data ?= $.extend true, {}, @attributes
|
|
|
|
schema ?= @schema() or {}
|
|
|
|
if schema.properties?.i18n and _.isPlainObject(data) and not data.i18n?
|
|
|
|
data.i18n = {}
|
|
|
|
sum += 1
|
2014-05-06 19:58:08 -04:00
|
|
|
|
2014-04-22 20:56:41 -04:00
|
|
|
if _.isPlainObject data
|
|
|
|
for key, value of data
|
|
|
|
numChanged = 0
|
|
|
|
numChanged = @populateI18N(value, childSchema, path+'/'+key) if childSchema = schema.properties?[key]
|
|
|
|
if numChanged and not path # should only do this for the root object
|
|
|
|
@set key, value
|
|
|
|
sum += numChanged
|
2014-05-06 19:58:08 -04:00
|
|
|
|
2014-04-22 20:56:41 -04:00
|
|
|
if schema.items and _.isArray data
|
|
|
|
sum += @populateI18N(value, schema.items, path+'/'+index) for value, index in data
|
2014-05-06 19:58:08 -04:00
|
|
|
|
2014-04-22 20:56:41 -04:00
|
|
|
sum
|
2014-03-03 14:41:35 -05:00
|
|
|
|
2014-04-25 22:11:32 -04:00
|
|
|
@getReferencedModel: (data, schema) ->
|
|
|
|
return null unless schema.links?
|
|
|
|
linkObject = _.find schema.links, rel: "db"
|
|
|
|
return null unless linkObject
|
|
|
|
return null if linkObject.href.match("thang.type") and not @isObjectID(data) # Skip loading hardcoded Thang Types for now (TODO)
|
|
|
|
|
|
|
|
# not fully extensible, but we can worry about that later
|
|
|
|
link = linkObject.href
|
|
|
|
link = link.replace('{(original)}', data.original)
|
|
|
|
link = link.replace('{(majorVersion)}', '' + (data.majorVersion ? 0))
|
|
|
|
link = link.replace('{($)}', data)
|
|
|
|
@getOrMakeModelFromLink(link)
|
|
|
|
|
|
|
|
@getOrMakeModelFromLink: (link) ->
|
|
|
|
makeUrlFunc = (url) -> -> url
|
|
|
|
modelUrl = link.split('/')[2]
|
|
|
|
modelModule = _.string.classify(modelUrl)
|
|
|
|
modulePath = "models/#{modelModule}"
|
|
|
|
|
|
|
|
try
|
|
|
|
Model = require modulePath
|
|
|
|
catch e
|
|
|
|
console.error 'could not load model from link path', link, 'using path', modulePath
|
|
|
|
return
|
|
|
|
|
|
|
|
model = new Model()
|
|
|
|
model.url = makeUrlFunc(link)
|
|
|
|
return model
|
2014-05-06 19:58:08 -04:00
|
|
|
|
2014-04-26 15:54:03 -04:00
|
|
|
setURL: (url) ->
|
|
|
|
makeURLFunc = (u) -> -> u
|
|
|
|
@url = makeURLFunc(url)
|
|
|
|
@
|
2014-05-06 19:58:08 -04:00
|
|
|
|
2014-04-26 15:54:03 -04:00
|
|
|
getURL: ->
|
|
|
|
return if _.isString @url then @url else @url()
|
2014-05-06 19:58:08 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
module.exports = CocoModel
|