2013-05-27 14:14:21 +00:00
|
|
|
_ = require 'lodash'
|
2013-05-26 14:29:54 +00:00
|
|
|
helpers = require 'habitrpg-shared/script/helpers'
|
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'
|
2013-05-11 23:05:13 +00:00
|
|
|
user = model.at '_user'
|
|
|
|
|
|
2013-05-28 11:32:57 +00:00
|
|
|
appExports.challengeCreate = (e,el) ->
|
|
|
|
|
[type, gid] = [$(el).attr('data-type'), $(el).attr('data-gid')]
|
2013-05-11 23:05:13 +00:00
|
|
|
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 17:54:45 +00:00
|
|
|
id: model.id()
|
2013-05-28 10:59:19 +00:00
|
|
|
uid: user.get('id')
|
2013-05-12 15:47:18 +00:00
|
|
|
user: helpers.username(model.get('_user.auth'), model.get('_user.profile.name'))
|
2013-05-28 11:32:57 +00:00
|
|
|
group: {type, id:gid}
|
2013-05-12 15:47:18 +00:00
|
|
|
timestamp: +new Date
|
2013-05-12 16:22:07 +00:00
|
|
|
|
|
|
|
|
appExports.challengeSave = ->
|
2013-05-28 11:32:57 +00:00
|
|
|
gid = model.get('_challenge.new.group.id')
|
|
|
|
|
debugger
|
|
|
|
|
model.unshift "groups.#{gid}.challenges", model.get('_challenge.new'), ->
|
|
|
|
|
browser.growlNotification('Challenge Created','success')
|
|
|
|
|
challengeDiscard()
|
2013-05-11 23:05:13 +00:00
|
|
|
|
2013-05-29 22:33:47 +00:00
|
|
|
appExports.toggleChallengeEdit = (e, el) ->
|
|
|
|
|
path = "_editing.challenges.#{$(el).attr('data-id')}"
|
|
|
|
|
model.set path, !model.get(path)
|
|
|
|
|
|
2013-05-28 11:32:57 +00:00
|
|
|
appExports.challengeDiscard = challengeDiscard = -> model.del '_challenge.new'
|
2013-05-12 16:22:07 +00:00
|
|
|
|
|
|
|
|
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')
|
2013-05-27 14:14:21 +00:00
|
|
|
unless tags and _.find(tags,{id: chal.id})
|
2013-05-28 10:52:32 +00:00
|
|
|
model.push '_user.tags', {id: chal.id, name: chal.name, challenge: true}
|
2013-05-17 22:33:24 +00:00
|
|
|
|
|
|
|
|
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
|
2013-05-28 10:52:32 +00:00
|
|
|
task.group = {id: chal.group.id, type: chal.group.type}
|
2013-05-17 22:33:24 +00:00
|
|
|
model.push("_#{type}List", task)
|
2013-05-27 14:14:21 +00:00
|
|
|
true
|
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) ->
|
2013-05-27 14:14:21 +00:00
|
|
|
model.remove "_#{type}List", _.findIndex(model.get("_#{type}List",{id:task.id}))
|
2013-05-17 22:33:24 +00:00
|
|
|
model.del "_user.tasks.#{task.id}"
|
2013-05-27 14:14:21 +00:00
|
|
|
true
|