habitica/src/app/challenges.coffee

45 lines
1.4 KiB
CoffeeScript
Raw Normal View History

_ = require 'underscore'
2013-05-11 23:05:13 +00:00
module.exports.app = (appExports, model) ->
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: []
dailys: []
2013-05-11 23:05:13 +00:00
todos: []
rewards: []
assignTo: 'Party'
id: model.id()
uuid: user.get('id')
user: helpers.username(model.get('_user.auth'), model.get('_user.profile.name'))
timestamp: +new Date
model.set '_challenge.creating', true
appExports.challengeSave = ->
model.unshift '_party.challenges', model.get('_challenge.new'), -> challengeDiscard()
browser.growlNotification('Challenge Created','success')
2013-05-11 23:05:13 +00:00
appExports.challengeDiscard = challengeDiscard = ->
2013-05-11 23:05:13 +00:00
model.set '_challenge.new', {}
model.set '_challenge.creating', false
appExports.challengeSubscribe = (e) ->
userChallenges = user.get('challenges')
chal = e.get()
user.unshift('challenges', chal.id) unless userChallenges and (userChallenges.indexOf(chal.id) != -1)
_.each ['habit', 'daily', 'todo', 'reward'], (type) ->
_.each chal["#{type}s"], (task) -> model.push("_#{type}List", task)
appExports.challengeUnsubscribe = (e) ->
i = user.get('challenges')?.indexOf e.get('id')
user.remove("challenges.#{i}") if i? and i != -1
appExports.challengeCollapse = (e, el) ->
$(el).next().toggle()
i = $(el).find('i').toggleClass 'icon-chevron-down'