2013-05-19 22:48:51 +00:00
|
|
|
algos = require 'habitrpg-shared/script/algos'
|
2013-05-19 17:53:13 +00:00
|
|
|
helpers = require 'habitrpg-shared/script/helpers'
|
2013-05-19 22:48:51 +00:00
|
|
|
_ = require 'lodash'
|
2013-02-08 22:44:06 +00:00
|
|
|
moment = require 'moment'
|
2013-05-21 23:40:46 +00:00
|
|
|
misc = require './misc'
|
2013-02-08 22:44:06 +00:00
|
|
|
|
2013-05-19 22:48:51 +00:00
|
|
|
|
|
|
|
|
###
|
|
|
|
|
Make scoring functionality available to the app
|
|
|
|
|
###
|
2013-02-08 22:44:06 +00:00
|
|
|
module.exports.app = (appExports, model) ->
|
2013-05-19 22:48:51 +00:00
|
|
|
character = require './character'
|
2013-02-08 22:44:06 +00:00
|
|
|
user = model.at('_user')
|
|
|
|
|
|
2013-05-10 17:35:14 +00:00
|
|
|
appExports.addTask = (e, el) ->
|
2013-02-08 22:44:06 +00:00
|
|
|
type = $(el).attr('data-task-type')
|
|
|
|
|
newModel = model.at('_new' + type.charAt(0).toUpperCase() + type.slice(1))
|
|
|
|
|
text = newModel.get()
|
2013-02-20 21:29:06 +00:00
|
|
|
# Don't add a blank task; 20/02/13 Added a check for undefined value, more at issue #463 -lancemanfv
|
2013-05-05 21:03:26 +00:00
|
|
|
return if /^(\s)*$/.test(text) || text == undefined
|
2013-02-08 22:44:06 +00:00
|
|
|
|
2013-05-17 22:32:24 +00:00
|
|
|
newTask = {id: model.id(), type: type, text: text, notes: '', value: 0, tags:{}}
|
2013-05-12 18:23:16 +00:00
|
|
|
isChallenge = e.at().path().indexOf('_challenge.new') != -1
|
2013-05-17 22:32:24 +00:00
|
|
|
newTask.tags = if isChallenge then {} else _.reduce user.get('filters'), ((memo,v,k) -> memo[k]=v if v;memo), {}
|
2013-05-12 18:23:16 +00:00
|
|
|
|
2013-02-08 22:44:06 +00:00
|
|
|
switch type
|
|
|
|
|
when 'habit'
|
2013-05-05 21:03:26 +00:00
|
|
|
newTask = _.defaults {up: true, down: true}, newTask
|
2013-02-08 22:44:06 +00:00
|
|
|
when 'reward'
|
2013-05-05 21:03:26 +00:00
|
|
|
newTask = _.defaults {value: 20}, newTask
|
2013-02-08 22:44:06 +00:00
|
|
|
when 'daily'
|
2013-05-05 21:03:26 +00:00
|
|
|
newTask = _.defaults {repeat:{su:true,m:true,t:true,w:true,th:true,f:true,s:true}, completed: false }, newTask
|
2013-02-08 22:44:06 +00:00
|
|
|
when 'todo'
|
2013-05-05 21:03:26 +00:00
|
|
|
newTask = _.defaults {completed: false }, newTask
|
2013-05-12 12:36:30 +00:00
|
|
|
e.at().unshift newTask # e.at() in this case is the list, which was scoped here using {#with @list}...{/}
|
2013-05-05 21:03:26 +00:00
|
|
|
newModel.set ''
|
2013-02-08 22:44:06 +00:00
|
|
|
|
2013-05-19 22:48:51 +00:00
|
|
|
appExports.del = (e) ->
|
2013-02-08 22:44:06 +00:00
|
|
|
# Derby extends model.at to support creation from DOM nodes
|
2013-03-18 02:09:33 +00:00
|
|
|
task = e.at()
|
2013-02-09 15:45:00 +00:00
|
|
|
id = task.get('id')
|
2013-02-08 22:44:06 +00:00
|
|
|
|
|
|
|
|
history = task.get('history')
|
2013-05-19 22:48:51 +00:00
|
|
|
if history and history.length > 2
|
2013-02-08 22:44:06 +00:00
|
|
|
# prevent delete-and-recreate hack on red tasks
|
|
|
|
|
if task.get('value') < 0
|
2013-05-19 22:48:51 +00:00
|
|
|
if confirm("Are you sure? Deleting this task will hurt you (to prevent deleting, then re-creating red tasks).") is true
|
2013-02-08 22:44:06 +00:00
|
|
|
task.set('type','habit') # hack to make sure it hits HP, instead of performing "undo checkbox"
|
2013-05-23 16:35:24 +00:00
|
|
|
misc.score(model, id, 'down', true)
|
2013-05-19 22:48:51 +00:00
|
|
|
else
|
|
|
|
|
return # Cancel. Don't delete, don't hurt user
|
2013-02-08 22:44:06 +00:00
|
|
|
|
|
|
|
|
# prevent accidently deleting long-standing tasks
|
|
|
|
|
else
|
2013-05-19 22:48:51 +00:00
|
|
|
return unless confirm("Are you sure you want to delete this task?") is true
|
2013-02-08 22:44:06 +00:00
|
|
|
|
|
|
|
|
#TODO bug where I have to delete from _users.tasks AND _{type}List,
|
|
|
|
|
# fix when query subscriptions implemented properly
|
|
|
|
|
$('[rel=tooltip]').tooltip('hide')
|
|
|
|
|
|
|
|
|
|
user.del('tasks.'+id)
|
2013-02-09 15:45:00 +00:00
|
|
|
task.remove()
|
2013-02-08 22:44:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
appExports.clearCompleted = (e, el) ->
|
2013-04-10 23:24:33 +00:00
|
|
|
completedIds = _.pluck( _.where(model.get('_todoList'), {completed:true}), 'id')
|
2013-02-09 01:48:31 +00:00
|
|
|
todoIds = user.get('todoIds')
|
2013-04-10 23:24:33 +00:00
|
|
|
|
2013-05-22 10:29:24 +00:00
|
|
|
_.each completedIds, (id) -> user.del "tasks.#{id}"; true
|
2013-04-10 23:24:33 +00:00
|
|
|
user.set 'todoIds', _.difference(todoIds, completedIds)
|
2013-02-08 22:44:06 +00:00
|
|
|
|
|
|
|
|
appExports.toggleDay = (e, el) ->
|
|
|
|
|
task = model.at(e.target)
|
|
|
|
|
if /active/.test($(el).attr('class')) # previous state, not current
|
|
|
|
|
task.set('repeat.' + $(el).attr('data-day'), false)
|
|
|
|
|
else
|
|
|
|
|
task.set('repeat.' + $(el).attr('data-day'), true)
|
|
|
|
|
|
|
|
|
|
appExports.toggleTaskEdit = (e, el) ->
|
|
|
|
|
hideId = $(el).attr('data-hide-id')
|
|
|
|
|
toggleId = $(el).attr('data-toggle-id')
|
2013-03-06 03:19:20 +00:00
|
|
|
$(document.getElementById(hideId)).addClass('visuallyhidden')
|
|
|
|
|
$(document.getElementById(toggleId)).toggleClass('visuallyhidden')
|
2013-02-08 22:44:06 +00:00
|
|
|
|
|
|
|
|
appExports.toggleChart = (e, el) ->
|
|
|
|
|
hideSelector = $(el).attr('data-hide-id')
|
|
|
|
|
chartSelector = $(el).attr('data-toggle-id')
|
|
|
|
|
historyPath = $(el).attr('data-history-path')
|
|
|
|
|
$(document.getElementById(hideSelector)).hide()
|
|
|
|
|
$(document.getElementById(chartSelector)).toggle()
|
|
|
|
|
|
|
|
|
|
matrix = [['Date', 'Score']]
|
|
|
|
|
for obj in model.get(historyPath)
|
|
|
|
|
date = +new Date(obj.date)
|
|
|
|
|
readableDate = moment(date).format('MM/DD')
|
|
|
|
|
matrix.push [ readableDate, obj.value ]
|
|
|
|
|
data = google.visualization.arrayToDataTable matrix
|
|
|
|
|
|
|
|
|
|
options = {
|
2013-03-09 19:06:53 +00:00
|
|
|
title: 'History'
|
2013-03-21 22:31:23 +00:00
|
|
|
backgroundColor: { fill:'transparent' }
|
2013-02-08 22:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chart = new google.visualization.LineChart(document.getElementById( chartSelector ))
|
|
|
|
|
chart.draw(data, options)
|
|
|
|
|
|
2013-05-12 09:52:32 +00:00
|
|
|
appExports.todosShowRemaining = -> model.set '_showCompleted', false
|
|
|
|
|
appExports.todosShowCompleted = -> model.set '_showCompleted', true
|
2013-02-09 15:45:00 +00:00
|
|
|
|
2013-03-18 00:30:54 +00:00
|
|
|
###
|
|
|
|
|
Call scoring functions for habits & rewards (todos & dailies handled below)
|
|
|
|
|
###
|
|
|
|
|
appExports.score = (e, el) ->
|
2013-05-21 23:40:46 +00:00
|
|
|
id = $(el).parents('li').attr('data-id')
|
2013-02-08 22:44:06 +00:00
|
|
|
direction = $(el).attr('data-direction')
|
2013-05-23 16:35:24 +00:00
|
|
|
misc.score(model, id, direction, true)
|
2013-03-18 00:30:54 +00:00
|
|
|
|
|
|
|
|
###
|
|
|
|
|
This is how we handle appExports.score for todos & dailies. Due to Derby's special handling of `checked={:task.completd}`,
|
|
|
|
|
the above function doesn't work so we need a listener here
|
|
|
|
|
###
|
|
|
|
|
user.on 'set', 'tasks.*.completed', (i, completed, previous, isLocal, passed) ->
|
2013-05-23 16:35:24 +00:00
|
|
|
return if passed?.cron # Don't do this stuff on cron
|
2013-03-18 00:30:54 +00:00
|
|
|
direction = if completed then 'up' else 'down'
|
2013-05-23 16:35:24 +00:00
|
|
|
misc.score(model, i, direction, true)
|
2013-03-18 00:30:54 +00:00
|
|
|
|
|
|
|
|
###
|
|
|
|
|
Undo
|
|
|
|
|
###
|
|
|
|
|
appExports.undo = () ->
|
|
|
|
|
undo = model.get '_undo'
|
2013-03-18 03:15:19 +00:00
|
|
|
clearTimeout(undo.timeoutId) if undo?.timeoutId
|
2013-03-18 00:30:54 +00:00
|
|
|
batch = character.BatchUpdate(model)
|
|
|
|
|
batch.startTransaction()
|
2013-03-18 03:15:19 +00:00
|
|
|
model.del '_undo'
|
2013-05-22 10:29:24 +00:00
|
|
|
_.each undo.stats, (val, key) -> batch.set "stats.#{key}", val; true
|
2013-03-18 00:30:54 +00:00
|
|
|
taskPath = "tasks.#{undo.task.id}"
|
2013-03-18 01:43:03 +00:00
|
|
|
_.each undo.task, (val, key) ->
|
2013-05-22 10:29:24 +00:00
|
|
|
return true if key in ['id', 'type'] # strange bugs in this world: https://workflowy.com/shared/a53582ea-43d6-bcce-c719-e134f9bf71fd/
|
2013-03-18 01:43:03 +00:00
|
|
|
if key is 'completed'
|
|
|
|
|
user.pass({cron:true}).set("#{taskPath}.completed",val)
|
|
|
|
|
else
|
|
|
|
|
batch.set "#{taskPath}.#{key}", val
|
2013-05-22 10:29:24 +00:00
|
|
|
true
|
2013-03-18 00:30:54 +00:00
|
|
|
batch.commit()
|
2013-03-03 04:52:47 +00:00
|
|
|
|
2013-03-05 23:07:22 +00:00
|
|
|
appExports.tasksToggleAdvanced = (e, el) ->
|
2013-03-06 05:49:02 +00:00
|
|
|
$(el).next('.advanced-option').toggleClass('visuallyhidden')
|
2013-03-05 23:07:22 +00:00
|
|
|
|
2013-03-03 04:52:47 +00:00
|
|
|
appExports.tasksSaveAndClose = ->
|
|
|
|
|
# When they update their notes, re-establish tooltip & popover
|
|
|
|
|
$('[rel=tooltip]').tooltip()
|
|
|
|
|
$('[rel=popover]').popover()
|
2013-03-03 22:54:14 +00:00
|
|
|
|
2013-03-04 16:33:36 +00:00
|
|
|
appExports.tasksSetPriority = (e, el) ->
|
2013-03-03 22:54:14 +00:00
|
|
|
dataId = $(el).parent('[data-id]').attr('data-id')
|
|
|
|
|
#"_user.tasks.#{dataId}"
|
2013-03-18 21:09:42 +00:00
|
|
|
model.at(e.target).set 'priority', $(el).attr('data-priority')
|