2013-05-12 15:47:18 +00:00
|
|
|
_ = require 'underscore'
|
2013-05-17 22:33:24 +00:00
|
|
|
lodash = require 'lodash'
|
2013-05-12 15:47:18 +00:00
|
|
|
|
2013-05-11 23:05:13 +00:00
|
|
|
module.exports.app = (appExports, model) ->
|
2013-05-12 15:47:18 +00:00
|
|
|
browser = require './browser'
|
|
|
|
|
helpers = require './helpers'
|
|
|
|
|
|
2013-05-11 23:05:13 +00:00
|
|
|
user = model.at '_user'
|
|
|
|
|
|
|
|
|
|
appExports.challengeCreate = ->
|
|
|
|
|
model.set '_challenge.new',
|
|
|
|
|
name: ''
|
|
|
|
|
habits: []
|
2013-05-12 16:22:07 +00:00
|
|
|
dailys: []
|
2013-05-11 23:05:13 +00:00
|
|
|
todos: []
|
|
|
|
|
rewards: []
|
2013-05-12 12:57:17 +00:00
|
|
|
assignTo: 'Party'
|
2013-05-12 17:54:45 +00:00
|
|
|
id: model.id()
|
2013-05-12 15:47:18 +00:00
|
|
|
uuid: user.get('id')
|
|
|
|
|
user: helpers.username(model.get('_user.auth'), model.get('_user.profile.name'))
|
|
|
|
|
timestamp: +new Date
|
2013-05-12 16:22:07 +00:00
|
|
|
|
|
|
|
|
model.set '_challenge.creating', true
|
|
|
|
|
|
|
|
|
|
appExports.challengeSave = ->
|
|
|
|
|
model.unshift '_party.challenges', model.get('_challenge.new'), -> challengeDiscard()
|
2013-05-12 15:47:18 +00:00
|
|
|
browser.growlNotification('Challenge Created','success')
|
2013-05-11 23:05:13 +00:00
|
|
|
|
2013-05-12 15:47:18 +00:00
|
|
|
appExports.challengeDiscard = challengeDiscard = ->
|
2013-05-11 23:05:13 +00:00
|
|
|
model.set '_challenge.new', {}
|
2013-05-12 16:22:07 +00:00
|
|
|
model.set '_challenge.creating', false
|
|
|
|
|
|
|
|
|
|
appExports.challengeSubscribe = (e) ->
|
|
|
|
|
chal = e.get()
|
2013-05-17 22:33:24 +00:00
|
|
|
|
|
|
|
|
# Add challenge name as a tag for user
|
|
|
|
|
tags = user.get('tags')
|
|
|
|
|
unless tags and _.findWhere(tags,{id: chal.id})
|
|
|
|
|
model.push('_user.tags', {id: chal.id, name: chal.name})
|
|
|
|
|
|
|
|
|
|
tags = {}; tags[chal.id] = true
|
|
|
|
|
# Add all challenge's tasks to user's tasks
|
|
|
|
|
userChallenges = user.get('challenges')
|
2013-05-12 16:22:07 +00:00
|
|
|
user.unshift('challenges', chal.id) unless userChallenges and (userChallenges.indexOf(chal.id) != -1)
|
|
|
|
|
_.each ['habit', 'daily', 'todo', 'reward'], (type) ->
|
2013-05-17 22:33:24 +00:00
|
|
|
_.each chal["#{type}s"], (task) ->
|
|
|
|
|
task.tags = tags
|
|
|
|
|
task.challenge = chal.id
|
|
|
|
|
model.push("_#{type}List", task)
|
2013-05-12 16:22:07 +00:00
|
|
|
|
|
|
|
|
appExports.challengeUnsubscribe = (e) ->
|
2013-05-17 22:33:24 +00:00
|
|
|
chal = e.get()
|
|
|
|
|
i = user.get('challenges')?.indexOf chal.id
|
2013-05-12 16:22:07 +00:00
|
|
|
user.remove("challenges.#{i}") if i? and i != -1
|
2013-05-17 22:33:24 +00:00
|
|
|
_.each ['habit', 'daily', 'todo', 'reward'], (type) ->
|
|
|
|
|
_.each chal["#{type}s"], (task) ->
|
|
|
|
|
model.remove "_#{type}List", lodash.findIndex(model.get("_#{type}List",{id:task.id}))
|
|
|
|
|
model.del "_user.tasks.#{task.id}"
|
2013-05-12 17:54:45 +00:00
|
|
|
|
|
|
|
|
appExports.challengeCollapse = (e, el) ->
|
|
|
|
|
$(el).next().toggle()
|
|
|
|
|
i = $(el).find('i').toggleClass 'icon-chevron-down'
|