Files
mozo-backend/app/assets/javascripts/supplier/qsupplier.js.coffee
T
2013-01-10 01:00:59 +01:00

175 lines
8.1 KiB
CoffeeScript

root = exports ? this
root.Qsupplier=
watch_events: ->
faye = new Faye.Client(event_host)
faye.subscribe "/supplier/"+supplier_id, (e)=>
if(e.event == 'new_order')
body = $('#active-orders-table tbody')
order = new Order(e.data)
body.append @mustache('#active-order-template', order)
else if(e.event == 'list_needs_help')
$('#list-needs-help-indicator-'+e.data.id).removeClass('hide')
$('#list-is-helped-button-'+e.data.id).removeClass('hide')
else if(e.event == 'list_needs_payment')
$('#list-needs-payment-indicator-'+e.data.id).removeClass('hide')
else if(e.event == 'list_added')
$('#active-lists-table tbody').append @mustache('#active-list-template', new List(e.data))
else if e.event == 'list_update'
list = new List(e.data)
row = $('#list-row-'+list.id())
content = @mustache('#active-list-template', list)
if row.length then row.replaceWith(content) else $('#active-lists-table tbody').append(content)
else if e.event == 'list_closed'
$('#list-row-'+e.data.id).remove()
$('.of-list-'+e.data.id).remove()
else if e.event == 'list_helped'
list_id = e.data.id
$('#list-needs-help-indicator-'+list_id).addClass('hide')
$('#list-is-helped-button-'+list_id).addClass('hide')
else if e.event == 'order_being_processed'
$('#order-in-process-button-'+e.data.id).hide()
$('#order-row-'+e.data.id).removeClass('placed').addClass('active')
else if e.event == 'order_being_delivered'
$('#order-row-'+e.data.id).remove()
else if e.event == 'list_changed_table'
list_row = $('#list-row-'+e.data.list_id)
list_row.find('.table_number').text(e.data.table.number).addClass('changed')
list_row.find('.section_title').text(e.data.section_title)
order_rows = $('.of-list-'+e.data.list_id)
order_rows.find('.table_number').text(e.data.table.number).addClass('changed')
order_rows.find('.section_title').text(e.data.section_title)
console.log(e)
false
move_table_to_active_section: (table_id)->
table_container = $('#section-table-'+table_id)
section_container = $('.section-tables-active')
section_container.append(table_container)
Qsupplier.position_table_in_active_section(section_container, table_container, true)
# Set the section id of the table in the database
$.ajax(
type: 'PUT',
url: '/supplier/tables/'+table_container.data('table-id'),
data: {table: {section_id: current_section_id}},
dataType: 'json'
)
position_table_in_active_section: (section_container, table_container, make_draggable)->
make_draggable ||= false
button_container = table_container.find('.action-button-container')
button_container.html('')
button_container.append($('<button class="btn btn-warning btn-mini">x</button>').click( -> Qsupplier.move_table_to_inactive_section(table_container.data('table-id')) ))
position_x = parseFloat(table_container.data('position-x'))
position_y = parseFloat(table_container.data('position-y'))
table_container.css('left', section_container.width()*position_x/current_section_width)
table_container.css('top', section_container.height()*position_y/current_section_height)
#TODO place element at 0.0 if it happens to be outside the region
table_container.show()
table_container.draggable(
containment: section_container,
stop: ->
position_x = current_section_width * $(this).position().left / section_container.width()
position_y = current_section_height * $(this).position().top / section_container.height()
table_container.data('position-x', position_x)
table_container.data('position-y', position_y)
$.ajax(
type: 'PUT',
url: '/supplier/tables/'+table_container.data('table-id'),
data: {table: {position_x: position_x, position_y: position_y}},
dataType: 'json'
)
) if make_draggable
move_table_to_inactive_section: (table_id)->
table_container = $('#section-table-'+table_id)
table_container.css('left', 'auto')
table_container.css('top', 'auto')
section_container = $('.section-tables-inactive')
section_container.prepend(table_container)
button_container = table_container.find('.action-button-container')
button_container.html('')
button_container.append($('<button class="btn btn-primary btn-mini">+</button>').click( -> Qsupplier.move_table_to_active_section(table_container.data('table-id')) ))
#TODO make ajax call safe
$.ajax(
type: 'PUT',
url: '/supplier/tables/'+table_container.data('table-id'),
data: {table: {section_id: ''}},
dataType: 'json'
)
load_active_lists: () ->
$.get('/supplier/active_lists.json?section_id='+($('#section_selector').val() || ''), (res) =>
body = $('#active-lists-table tbody')
body.find('tr').remove()
foot = $('#active-lists-table tfoot')
for list in res.lists
body.append @mustache('#active-list-template', new List(list))
)
mark_list_as_helped: (list_id)->
$.post('/supplier/mark_list_as_helped', {list_id: list_id}, (res)->
)
close_list: (list_id)->
$.post('/supplier/close_list', {list_id: list_id}, (res)->
)
load_active_orders: () ->
$.get('/supplier/active_orders.json?section_id='+($('#section_selector').val() || ''), (res) =>
body = $('#active-orders-table tbody')
body.html('')
foot = $('#active-orders-table tfoot')
return unless res.orders
for order in res.orders
ord = new Order(order)
body.append @mustache('#active-order-template', ord)
)
mark_order_in_process: (order_id)->
$.post('/supplier/mark_order_in_process', {order_id: order_id})
mark_order_delivered: (order_id)->
$.post('/supplier/order_is_delivered', {order_id: order_id})
load_list: (list_id) ->
$.get(data_host + '/supplier/lists/'+list_id+'.json', (res) ->
body = $('#list-table tbody')
foot = $('#list-table tfoot')
Qsupplier.build_list_table(body, foot, res)
)
build_list_table: (body, foot, res) ->
if !res.orders && !res.orders.length
alert('No orders in list')
return
body.html('')
for order in res.orders
body.append @mustache('#list-order-template', new Order(order))
foot.find('.list-total').html(currency(res.total_amount))
update_section_tables_view: (section_id)->
$.get(data_host + '/supplier/sections/'+section_id+'/tables_view.json', (res)->
for table in res.tables
to = $('#section-table-'+table._id)
if table.occupied then to.addClass('occupied') else to.removeClass('occupied')
if table.needs_help then to.addClass('needs_help') else to.removeClass('needs_help')
if table.needs_payment then to.addClass('needs_payment') else to.removeClass('needs_payment')
)
add_tables_to_active_section: ->
number_start = $('#add-tables-number-start').val()
number_end = $('#add-tables-number-end').val()
$.post('/supplier/sections/'+current_section_id+'/add_tables', {number_start: number_start, number_end: number_end}, -> window.location.reload())
#$('#add-tables-modal').modal('hide')
false
arrange_tables_of_active_section: ->
option = $('input[name=arrange-table-option]:checked').val()
by_row_count = parseInt($('#arrange-tables-by-row-count').val())
by_column_count = parseInt($('#arrange-tables-by-column-count').val())
if(option == "by_row")
return alert('Please fill in a positive number representing the number of tables per row') unless by_row_count && by_row_count > 0
if(option == "by_column")
return alert('Please fill in a positive number representing the number of tables per column') unless by_column_count && by_column_count > 0
$.post('/supplier/sections/'+current_section_id+'/arrange_tables', {option: option, row_count: by_row_count, column_count: by_column_count}, -> window.location.reload())
false
mustache: (selector, locals)->
locs = $.extend(locals,
currency: ->
(val)->
currency(Mustache.render(val, this))
)
Mustache.to_html($(selector).html(), locs)