diff --git a/app/assets/javascripts/backbone/models/habit.js.coffee b/app/assets/javascripts/backbone/models/habit.js.coffee
index 363444f285..26923570e5 100644
--- a/app/assets/javascripts/backbone/models/habit.js.coffee
+++ b/app/assets/javascripts/backbone/models/habit.js.coffee
@@ -3,14 +3,67 @@ class HabitTracker.Models.Habit extends Backbone.Model
defaults:
name: null
- habit_type: null
- score: null
+ habit_type: 1
+ score: 0.0
notes: null
- up: null
- down: null
- done: null
- position: null
+ up: true
+ down: true
+ done: false
+ position: 0
+
+ isHabit: ->
+ @get("habit_type")==1
+ isDaily: ->
+ @get("habit_type")==2
+
+ isDoneTodo: ->
+ @get("habit_type")==3 and @get("done")
+
+ isRemainingTodo: ->
+ @get("habit_type")==3 and !@get("done")
+
+ # TODO before_save on user, calculate delta on score & experience there, not in vote
+ # TODO retrieve this formula from server so don't have to have it in two locations (Habit.clear requires it server-side)
+ vote: (direction) ->
+ # For negative values, use a line: something like y=-.1x+1
+ # For positibe values, taper off with inverse log: y=.9^x
+ # Would love to use inverse log for the whole thing, but after 13 fails it hits infinity
+ sign = if (direction == "up") then 1 else -1
+ score = this.get("score")
+ delta = 0
+ if score < 0
+ delta = (( -0.1 * score + 1 ) * sign)
+ else
+ delta = (( Math.pow(0.9, score) ) * sign)
+
+ score += delta
+
+ # up/down -voting as checkbox & assigning as done, 2 birds one stone
+ done = this.get("done")
+ if this.get("habit_type")!=1
+ done = true if direction=="up"
+ done = false if direction=="down"
+
+ this.save({ score: score, done: done })
+
class HabitTracker.Collections.HabitsCollection extends Backbone.Collection
model: HabitTracker.Models.Habit
+
url: '/habits'
+
+ #TODO
+ # standard: () ->
+ # @filter (habit)->
+ # habit.get('habit_type')==1
+#
+ # daily: () ->
+ # @filter (habit) ->
+ # habit.get('habit_type')==2
+#
+ # todosDone: () ->
+ # @filter (habit) ->
+ # (habit.get "done") and (habit.get('habit_type')==3)
+#
+ # todosRemaining: () ->
+ # @without.apply(this, this.done()) and (habit.get('habit_type')==3)
\ No newline at end of file
diff --git a/app/assets/javascripts/backbone/templates/habits/edit.jst.ejs b/app/assets/javascripts/backbone/templates/habits/edit.jst.ejs
index 46967283ce..5285bf5f01 100644
--- a/app/assets/javascripts/backbone/templates/habits/edit.jst.ejs
+++ b/app/assets/javascripts/backbone/templates/habits/edit.jst.ejs
@@ -11,11 +11,6 @@
-
-
-
-
-
@@ -31,16 +26,6 @@
-
-
-
-
-
-
-
-
-
-
diff --git a/app/assets/javascripts/backbone/templates/habits/habit.jst.ejs b/app/assets/javascripts/backbone/templates/habits/habit.jst.ejs
index 4df9ebd184..108782bce5 100644
--- a/app/assets/javascripts/backbone/templates/habits/habit.jst.ejs
+++ b/app/assets/javascripts/backbone/templates/habits/habit.jst.ejs
@@ -1,12 +1,27 @@
-<%= name %> |
-<%= habit_type %> |
-<%= score %> |
-<%= notes %> |
-<%= up %> |
-<%= down %> |
-<%= done %> |
-<%= position %> |
-
-Show |
-Edit |
-Destroy |
\ No newline at end of file
+
+
+
+ <% if (habit_type==1) { %>
+ +
+ -
+ <%= name %>
+ <% } else { %>
+
+
+ <% if (done) { %>
+ [X]
+ <%= name %>
+ <% } else { %>
+ [ ]
+ <%= name %>
+ <% } %>
+ <% } %>
+
+ <% if (notes) { %>
+
+
+
<%= notes %>
+ <% } %>
+
Edit
+
+
diff --git a/app/assets/javascripts/backbone/templates/habits/index.jst.ejs b/app/assets/javascripts/backbone/templates/habits/index.jst.ejs
index 6489889827..067f2d9888 100644
--- a/app/assets/javascripts/backbone/templates/habits/index.jst.ejs
+++ b/app/assets/javascripts/backbone/templates/habits/index.jst.ejs
@@ -1,21 +1,75 @@
-Listing habits
+New Habit
-
+
+
+
- | Name |
- Habit type |
- Score |
- Notes |
- Up |
- Down |
- Done |
- Position |
- |
- |
- |
+
+
+ |
+
+
+
+ |
+
+
+
+
+ |
+
+
+
+
+ |
-
-New Habit
\ No newline at end of file
+
+
+
+
diff --git a/app/assets/javascripts/backbone/templates/habits/new.jst.ejs b/app/assets/javascripts/backbone/templates/habits/new.jst.ejs
index 9e0a66e0d3..61c96142e7 100644
--- a/app/assets/javascripts/backbone/templates/habits/new.jst.ejs
+++ b/app/assets/javascripts/backbone/templates/habits/new.jst.ejs
@@ -11,11 +11,6 @@
-
-
-
-
-
@@ -31,16 +26,6 @@
-
-
-
-
-
-
-
-
-
-
diff --git a/app/assets/javascripts/backbone/views/habits/edit_view.js.coffee b/app/assets/javascripts/backbone/views/habits/edit_view.js.coffee
index 8bae18d02d..3008d1a2c7 100644
--- a/app/assets/javascripts/backbone/views/habits/edit_view.js.coffee
+++ b/app/assets/javascripts/backbone/views/habits/edit_view.js.coffee
@@ -13,7 +13,7 @@ class HabitTracker.Views.Habits.EditView extends Backbone.View
@model.save(null,
success : (habit) =>
@model = habit
- window.location.hash = "/#{@model.id}"
+ window.location.hash = "#/index"
)
render : ->
diff --git a/app/assets/javascripts/backbone/views/habits/habit_view.js.coffee b/app/assets/javascripts/backbone/views/habits/habit_view.js.coffee
index 89733694c4..c79f7ffe6d 100644
--- a/app/assets/javascripts/backbone/views/habits/habit_view.js.coffee
+++ b/app/assets/javascripts/backbone/views/habits/habit_view.js.coffee
@@ -5,15 +5,31 @@ class HabitTracker.Views.Habits.HabitView extends Backbone.View
events:
"click .destroy" : "destroy"
+ "click .vote-up" : "voteUp"
+ "click .vote-down" : "voteDown"
- tagName: "tr"
-
+ tagName: "li"
+
destroy: () ->
@model.destroy()
this.remove()
-
return false
+
+ voteUp: ->
+ @model.vote("up")
+ @trigger("reset")
+ # console.log($(@el).parent().sibling('#habits-todos-done'))
+ # if @model.isRemainingTodo and $(@el).parent().id=="habits-remaining-todos"
+ # $(@el).parent().sibling('#habits-done-todos').append(this)
+
+ voteDown: ->
+ @model.vote("down")
render: ->
$(@el).html(@template(@model.toJSON() ))
+
+ @$(".comment").qtip content:
+ text: (api) ->
+ $(this).next().html()
+
return this
diff --git a/app/assets/javascripts/backbone/views/habits/index_view.js.coffee b/app/assets/javascripts/backbone/views/habits/index_view.js.coffee
index 4712ca9b0b..a744cc6c16 100644
--- a/app/assets/javascripts/backbone/views/habits/index_view.js.coffee
+++ b/app/assets/javascripts/backbone/views/habits/index_view.js.coffee
@@ -5,16 +5,21 @@ class HabitTracker.Views.Habits.IndexView extends Backbone.View
initialize: () ->
@options.habits.bind('reset', @addAll)
+ #TODO this is causing "Remaining" tab to be auto-selected
+ @options.habits.bind('change', @render, this)
addAll: () =>
@options.habits.each(@addOne)
addOne: (habit) =>
view = new HabitTracker.Views.Habits.HabitView({model : habit})
- @$("tbody").append(view.render().el)
-
+ 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)
+
render: =>
$(@el).html(@template(habits: @options.habits.toJSON() ))
@addAll()
-
- return this
+ @$("#habits-todos").tabs()
+ return this
\ No newline at end of file
diff --git a/app/assets/javascripts/backbone/views/habits/new_view.js.coffee b/app/assets/javascripts/backbone/views/habits/new_view.js.coffee
index ef89617fb0..36063acf17 100644
--- a/app/assets/javascripts/backbone/views/habits/new_view.js.coffee
+++ b/app/assets/javascripts/backbone/views/habits/new_view.js.coffee
@@ -23,7 +23,7 @@ class HabitTracker.Views.Habits.NewView extends Backbone.View
@collection.create(@model.toJSON(),
success: (habit) =>
@model = habit
- window.location.hash = "/#{@model.id}"
+ window.location.hash = "#/index"
error: (habit, jqXHR) =>
@model.set({errors: $.parseJSON(jqXHR.responseText)})
diff --git a/app/assets/javascripts/habits.js.coffee b/app/assets/javascripts/habits.js.coffee
index 133148c0d1..cfcbcc04f6 100644
--- a/app/assets/javascripts/habits.js.coffee
+++ b/app/assets/javascripts/habits.js.coffee
@@ -7,10 +7,6 @@ $(document).ready ->
$(".one-time a.vote-link").click ->
$(this).parent().fadeOut()
- $(".comment").qtip content:
- text: (api) ->
- $(this).next().html()
-
$.each ['#habits', '#daily', '#one-offs'], (index, list_id) ->
$(list_id).sortable
axis: "y"
diff --git a/app/assets/stylesheets/habits.css.scss b/app/assets/stylesheets/habits.css.scss
index 5380ce55c5..9896659b8c 100644
--- a/app/assets/stylesheets/habits.css.scss
+++ b/app/assets/stylesheets/habits.css.scss
@@ -36,12 +36,10 @@
cursor: move;
}
-.up, .down, .check {
+.vote-up, .vote-down {
text-decoration:none;
}
-.up, .down { font-weight: bold; }
-
.edit { float:right; }
#progressbar { margin-top: 20px; }
diff --git a/app/controllers/habits_controller.rb b/app/controllers/habits_controller.rb
index 1c23a8ba2e..6be44c489d 100644
--- a/app/controllers/habits_controller.rb
+++ b/app/controllers/habits_controller.rb
@@ -2,14 +2,10 @@ class HabitsController < ApplicationController
before_filter :authenticate_user!
- # GET /habits
# GET /habits.json
def index
- @habits = current_user.habits.where(:habit_type => Habit::ALWAYS)
- @daily = current_user.habits.where(:habit_type => Habit::DAILY)
- @one_time = current_user.habits.where(:habit_type => Habit::ONE_TIME).where(:done=>false)
- @rewards = current_user.rewards
-
+ # TODO @rewards = current_user.rewards
+ @habits = current_user.habits
respond_to do |format|
format.html # index.html.erb
format.json { render json: @habits }
diff --git a/app/views/habits/index.html.erb b/app/views/habits/index.html.erb
index b46a760edd..de48a2725d 100644
--- a/app/views/habits/index.html.erb
+++ b/app/views/habits/index.html.erb
@@ -6,4 +6,4 @@
window.router = new HabitTracker.Routers.HabitsRouter({habits: <%= @habits.to_json.html_safe -%>});
Backbone.history.start();
});
-
+
\ No newline at end of file