2012-02-08 00:09:58 +00:00
|
|
|
HabitTracker.Views.Habits ||= {}
|
|
|
|
|
|
|
|
|
|
class HabitTracker.Views.Habits.IndexView extends Backbone.View
|
|
|
|
|
template: JST["backbone/templates/habits/index"]
|
2012-02-11 19:16:23 +00:00
|
|
|
|
|
|
|
|
events:
|
|
|
|
|
"keypress .new-habit": "createOnEnter",
|
|
|
|
|
"keyup .new-habit": "showTooltip",
|
|
|
|
|
"click .todo-clear a": "clearCompleted"
|
2012-02-08 00:09:58 +00:00
|
|
|
|
|
|
|
|
initialize: () ->
|
|
|
|
|
@options.habits.bind('reset', @addAll)
|
2012-02-10 02:14:46 +00:00
|
|
|
@options.habits.bind('change', @render, this) #TODO this ruins tabs, revisit
|
2012-02-11 19:16:23 +00:00
|
|
|
@options.habits.bind('add', @render, this)
|
2012-02-11 04:04:30 +00:00
|
|
|
window.userStats.bind('updatedStats', @updateStats, this)
|
2012-02-11 19:16:23 +00:00
|
|
|
|
|
|
|
|
createOnEnter: (e) ->
|
|
|
|
|
input = $(e.target)
|
|
|
|
|
if (!input.val() or e.keyCode != 13) then return
|
|
|
|
|
@options.habits.create {name: input.val(), habit_type: input.attr('data-type')} #,
|
|
|
|
|
#TODO
|
|
|
|
|
# success: (habit) =>
|
|
|
|
|
# @model = habit
|
|
|
|
|
# error: (habit, jqXHR) =>
|
|
|
|
|
# @model.set({errors: $.parseJSON(jqXHR.responseText)}
|
|
|
|
|
input.val('')
|
|
|
|
|
|
|
|
|
|
#TODO
|
|
|
|
|
clearCompleted: ->
|
|
|
|
|
# _.each(Todos.done(), function(todo){ todo.destroy(); });
|
|
|
|
|
# return false;
|
|
|
|
|
|
|
|
|
|
#TODO
|
|
|
|
|
showTooltip: (e) ->
|
|
|
|
|
# var tooltip = this.$(".ui-tooltip-top");
|
|
|
|
|
# var val = this.input.val();
|
|
|
|
|
# tooltip.fadeOut();
|
|
|
|
|
# if (this.tooltipTimeout) clearTimeout(this.tooltipTimeout);
|
|
|
|
|
# if (val == '' || val == this.input.attr('placeholder')) return;
|
|
|
|
|
# var show = function(){ tooltip.show().fadeIn(); };
|
|
|
|
|
# this.tooltipTimeout = _.delay(show, 1000);
|
2012-02-11 04:04:30 +00:00
|
|
|
|
2012-02-11 05:35:12 +00:00
|
|
|
# TODO create a view & template, bind to existing element
|
2012-02-11 04:04:30 +00:00
|
|
|
updateStats: () =>
|
|
|
|
|
stats = window.userStats
|
2012-02-11 07:26:09 +00:00
|
|
|
$('#tnl').html( "(Level #{stats.get('lvl')}) #{Math.round(stats.get('exp'))} / #{stats.tnl()}" )
|
|
|
|
|
$( "#progressbar" ).progressbar value: stats.get('exp')/stats.tnl() * 100
|
2012-02-11 05:35:12 +00:00
|
|
|
|
2012-02-11 07:26:09 +00:00
|
|
|
# TODO for some reason this has to be @$, but above has to be $
|
2012-02-11 05:35:12 +00:00
|
|
|
money = stats.get('money').toFixed(1).split('.')
|
|
|
|
|
@$('#money').html("#{money[0]} <img src='assets/coin_single_gold.png'/> #{money[1]} <img src='assets/coin_single_silver.png'/>")
|
2012-02-08 00:09:58 +00:00
|
|
|
|
|
|
|
|
addAll: () =>
|
|
|
|
|
@options.habits.each(@addOne)
|
|
|
|
|
|
|
|
|
|
addOne: (habit) =>
|
|
|
|
|
view = new HabitTracker.Views.Habits.HabitView({model : habit})
|
2012-02-09 01:40:43 +00:00
|
|
|
if habit.isHabit() then @$("#habits-habits").append(view.render().el)
|
|
|
|
|
if habit.isDaily() then @$("#habits-daily").append(view.render().el)
|
|
|
|
|
if habit.isDoneTodo() then @$("#habits-todos-done").append(view.render().el)
|
|
|
|
|
if habit.isRemainingTodo() then @$("#habits-todos-remaining").append(view.render().el)
|
2012-02-11 07:26:09 +00:00
|
|
|
if habit.isReward()
|
|
|
|
|
view = new HabitTracker.Views.Habits.RewardView({model : habit})
|
|
|
|
|
@$("#rewards").append(view.render().el)
|
2012-02-10 02:14:46 +00:00
|
|
|
|
2012-02-08 00:09:58 +00:00
|
|
|
render: =>
|
|
|
|
|
$(@el).html(@template(habits: @options.habits.toJSON() ))
|
|
|
|
|
@addAll()
|
2012-02-09 01:40:43 +00:00
|
|
|
@$("#habits-todos").tabs()
|
2012-02-11 05:35:12 +00:00
|
|
|
@updateStats()
|
2012-02-09 01:40:43 +00:00
|
|
|
return this
|