Skip to content

Instantly share code, notes, and snippets.

@hrumhrumble
Created March 10, 2016 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrumhrumble/cff50efda07b5de32ba8 to your computer and use it in GitHub Desktop.
Save hrumhrumble/cff50efda07b5de32ba8 to your computer and use it in GitHub Desktop.
Mealtime = (id, name) ->
@id = id
@name = name
return
Positions = (id, name, protein, fat, carbohydrates, kilocalorie, quantity, mealtime, mealtimeList, mealtimeSelected) ->
@id = id
@name = name
@quantity = ko.observable(quantity)
@mealtime = ko.observable(mealtime)
@mealtimeList = ko.observableArray(mealtimeList)
@mealtimeSelected = ko.observable(mealtimeSelected)
@protein = ko.computed =>
(protein / 100 * @quantity()).toFixed()
@fat = ko.computed =>
(fat / 100 * @quantity()).toFixed()
@carbohydrates = ko.computed =>
(carbohydrates / 100 * @quantity()).toFixed()
@kilocalorie = ko.computed =>
(kilocalorie / 100 * @quantity()).toFixed()
@shouldShowEditActions = ko.observable(true)
@shouldHideEditActions = ko.observable(false)
return
Menu = ->
that = @
submit_btn = $('.menus').find('.btn_ajax')
planDate = $('.plans_show').data('plan-date')
@addPositionSearchQuery = ko.observable()
@addPositionQuantity = ko.observable()
@addPositionMealtime = ko.observable()
@positionsTable = ko.observableArray()
@positionSelected = ko.observable()
@mealtime = ko.observable()
@mealtimeSelected = ko.observable()
@mealtimeList = ko.observableArray()
$.ajax
url: '/profiles/mealtimes'
dataType: "json"
success: (data) ->
$.each data, (index, value)->
that.mealtimeList.push(
new Mealtime(
value.id,
value.name
)
)
$.ajax
url: '/profiles/positions'
data: { date: planDate }
dataType: "json"
success: (data) ->
$.each data, (index, value)->
that.positionsTable.push(
new Positions(
value.id,
value.name,
value.protein,
value.fat,
value.carbohydrates,
value.kilocalorie,
value.quantity,
value.mealtime,
that.mealtimeList(),
value.mealtime.id
)
)
@foodSearch = ko.computed =>
$.ajax
url: "/foods/"
data: { search: @addPositionSearchQuery() }
dataType: "json"
success: (data) ->
that.buildAutoCompleteList(data)
@totalInfo = ko.computed =>
protein = 0
fat = 0
carbohydrates = 0
kilocalorie = 0
quantity = 0
$.each @positionsTable(), (index, value)->
kilocalorie += parseInt(value.kilocalorie())
fat += parseInt(value.fat())
protein += parseInt(value.protein())
carbohydrates += parseInt(value.carbohydrates())
quantity += parseInt(value.quantity())
return {
protein: protein,
fat: fat,
carbohydrates: carbohydrates,
kilocalorie: kilocalorie
quantity: quantity
}
@buildAutoCompleteList = (data) ->
$("#query").autocomplete
source: (request, response) ->
response $.map data, (value, key) ->
{ label: value.name, value: value.id }
return
focus: (event, ui) ->
$('#query').val(ui.item.label)
false
select: (event, ui) ->
that.positionSelected(ui.item.value)
that.addPositionSearchQuery(ui.item.label)
false
@addPosition =->
$.ajax
url: '/profiles/positions'
method: 'POST'
data: {
date: planDate,
food_id: that.positionSelected(),
mealtime_id: that.mealtimeSelected(),
quantity: that.addPositionQuantity()
}
beforeSend: ->
submit_btn.val('Сохраняем...').addClass('before_send')
success: (data) ->
that.addPositionQuantity('')
that.addPositionSearchQuery('')
$('#query').focus()
that.positionsTable.push(
new Positions(
data.position.id,
data.position.name,
data.position.protein,
data.position.fat,
data.position.carbohydrates,
data.position.kilocalorie,
data.position.quantity,
data.position.mealtime,
that.mealtimeList(),
that.mealtimeSelected()
)
)
submit_btn.val('Сохранено!').removeClass('before_send').addClass('success')
setTimeout(->
submit_btn.val('Сохранить').removeClass('success')
, 2000)
error: ->
submit_btn.val('Не сохранено').removeClass('before_send').addClass('error')
setTimeout(->
submit_btn.val('Сохранить').removeClass('error')
, 2000)
@selectPosition = (position) ->
that.positionSelected(position.id)
that.addPositionSearchQuery(position.name)
@showEditPosition = ->
@shouldShowEditActions(false)
@shouldHideEditActions(true)
@closeEditPosition = ->
@shouldShowEditActions(true)
@shouldHideEditActions(false)
@updatePosition = (position) ->
$.ajax
url: '/profiles/positions/' + position.id
method: 'PUT'
data: {
date: planDate,
quantity: position.quantity(),
mealtime_id: position.mealtimeSelected()
}
success: (data) ->
position.shouldShowEditActions(true)
position.shouldHideEditActions(false)
position.mealtime(data.position.mealtime)
@destroyPosition = (position) ->
$.ajax
url: '/profiles/positions/' + position.id
method: 'POST'
data: { date: planDate, _method: 'delete' }
success: (data) ->
that.positionsTable.remove(position)
return
init = ->
if $('.plans_show').length > 0
ko.applyBindings(new Menu)
$(document).on('ready page:load', init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment