implement joining table

This commit is contained in:
2012-08-30 18:32:30 +02:00
parent 6696992320
commit aa0821d0ae
15 changed files with 228 additions and 35 deletions
+229
View File
@@ -0,0 +1,229 @@
window.Quser=
handle_active_list: (callback) ->
$.get('/user/list_info.json', (res) ->
if !res.list_active
window.location = '/user?list_closed=true'
return
window.active_list = res
callback.call() if callback
Quser.handle_active_list_default_actions(res)
)
handle_active_list_default_actions: (response)->
response ||= {}
if response.table_number
$('.table-number').text(response.table_number)
Quser.list_needs_payment_default_action()
Quser.list_needs_help_default_action()
# join_request_active is to ensure that there are no more modals loaded when one is still active
console.log('join_request_active=' + window.join_request_active)
if !window.join_request_active && response.join_requests && response.join_requests.length
window.join_request_active = true
for join_request in response.join_requests
wrapper = $('<div class="modal"></div>')
join_callback = ( (request)->
->
$.post('/user/approve_join_request', {user_id: request.user_id}, -> window.join_request_active = false; wrapper.modal('hide') )
)(join_request)
reject_callback = ( (request)->
->
$.post('/user/reject_join_request', {user_id: request.user_id}, -> window.join_request_active = false; wrapper.modal('hide' ))
)(join_request)
header = $('<div class="modal-header"></div>')
.append('<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>')
.append('<h3>Join request</h3>').appendTo(wrapper)
body = $('<div class="modal-body"></div>')
body.append(join_request.user_email + ' wants to join the table')
body.appendTo(wrapper)
footer = $('<div class="modal-footer"></div>')
.append($('<a href="#" class="btn">Reject</a>').click(reject_callback))
.append($('<a href="#" class="btn btn-primary">Approve</a>').click(join_callback))
.appendTo(wrapper)
wrapper.modal()
list_needs_help_default_action: ->
needs_help_container = $('#list-needs-help-button')
if needs_help_container.length
if window.active_list.needs_help
needs_help_container.html('<i class="icon-hand-up"></i>')
else
needs_help_container.html($('<button class="btn btn-info">I have a question</button>').click(Quser.list_needs_help)) #TODO TEXT
list_needs_help: ->
return unless window.active_list && !window.active_list.needs_help
$.post('/user/needs_help.json', (res) -> window.active_list = res; Quser.list_needs_help_default_action())
list_needs_payment_default_action: ->
needs_payment_container = $('#list-needs-payment-button')
if needs_payment_container.length
if window.active_list.needs_payment
needs_payment_container.html('<i class="icon-check"></i>')
else
needs_payment_container.html($('<button class="btn btn-warning">Check please</button>').click(Quser.list_needs_payment)) #TODO TEXT
list_needs_payment: ->
return unless window.active_list && !window.active_list.needs_payment
$.post('/user/list_needs_payment.json', (res) -> window.active_list = res; Quser.list_needs_payment_default_action())
load_active_list: () ->
$.get('/user/active_list.json', (res) ->
window.active_list = res
unless res.list_active
window.location = '/user/list_history/'+res._id + '?list_closed=true'
return
Quser.handle_active_list_default_actions(res)
body = $('#active-list-table tbody')
foot = $('#active-list-table tfoot')
body.find('tr').remove()
foot.find('tr').remove()
if !res.orders && !res.orders.length
alert('No orders in list')
return
for order in res.orders
order_txts = []
row = $('<tr></tr>').appendTo(body)
row.addClass(order.state)
#if(order.state == 'placed') row.addClass('info');
#if(order.state == 'delivered') row.addClass('success');
row.addClass('error') if order.state == 'cancelled'
for product in order.products
order_txts.push(product.name + ' (' + product['number'] + ')')
row.append($('<td></td>').text(order_txts.join(', ')))
row.append($('<td class="currency"></td>').html(currency(order.total_amount)))
foot.append('<tr><td></td><td class="currency"><strong>'+currency(res.total_amount)+'</strong></td></tr>')
)
order_selected_products: (h)->
return if $.isEmptyObject(window.active_products_list)
h ||= {}
for product_id, info of window.active_products_list
h['products['+product_id+']'] = info.number
$.post('/user/order_selected_products', h, ((res) -> Quser.handle_response(res)), 'json')
handle_response: (res) ->
if(typeof(res) == 'string')
return unless res.length
if res[0] == '{'
res = JSON.parse(res)
else
eval(res)
return
window.location = '/user' if res['message'] && !res['ok']
window.location = res.location || '/user/list_products' if res['ok']
build_product_list: ->
table = $('#active-order-table')
tbody = table.find('tbody')
tbody = $('<tbody></tbody>').appendTo(table) unless tbody.length
tbody.find('tr').remove()
total = 0.0
for product_id, info of window.active_products_list
total += info.product.price * info.number
row = $('<tr></tr>').attr('id', 'active-order-row-'+product_id).appendTo(tbody)
row.append('<td>'+info.product.name+'</td>')
row.append('<td>'+info.number+'</td>')
row.append('<td class="currency">'+currency(info.product.price * info.number)+'</td>')
x_btn = $('<button class="btn btn-warning btn-mini">x</button>').click(-> delete(window.active_products_list[product_id]) && Quser.build_product_list() )
row.append($('<td></td>').append(x_btn))
$('#active-order-total').html(currency(total))
table.show()
load_active_list_products: ->
Quser.populate_products_table('/user/list_products.json', true)
load_table_products: (table_id, occupied)->
Quser.populate_products_table('/user/list_products_for_table.json?table_id='+table_id, !occupied)
populate_products_table: (src, include_order_buttons)->
$.get(src, (res) ->
body = $('#products-table tbody')
if res.table_number
$('.table-number').text(res.table_number)
delete(res['table_number'])
for category, products of res
body.append('<tr><td colspan="4"><h4>'+category+'<h4></td></tr>')
for product in products
row = $('<tr></tr>')
row.append('<td>'+product.name+'</td>')
if include_order_buttons
button = $('<button class="btn btn-mini btn-primary">Add</button>')
callback = ((prod) ->
->
product_count_holder = $('#order-product-count-'+prod._id)
count = parseInt(product_count_holder.text())
product_count_holder.text(1)
Quser.add_product(prod, count)
)(product)
button.click(callback)
order_product_count = $('<span id="order-product-count-'+product._id+'" class="order-product-count">1</span>')
order_count_minus = $('<span class="btn btn-info btn-mini">-</span>')
order_count_minus.click(->
val_holder = $(this).siblings('.order-product-count')
val = parseInt(val_holder.text())
val_holder.text(val - 1) if val > 1
)
order_count_plus = $('<span class="btn btn-info btn-mini">+</span>')
order_count_plus.click(->
val_holder = $(this).siblings('.order-product-count')
val = parseInt(val_holder.text())
val_holder.text(val + 1)
)
row.append($('<td class="order-count-cell"></td>').append(order_count_minus).append('&nbsp;').append(order_product_count).append('&nbsp;').append(order_count_plus))
row.append('<td>'+currency(product.price)+'</td>')
row.append($('<td></td>').append(button)) if include_order_buttons
body.append(row)
)
actions_for_table: (table)->
table = JSON.parse(table) if typeof(table) == 'string'
$.get('/user/table_info.json?table_id='+table.table_id, (res)->
if res.current_table_id
if res.other_supplier
#TODO cannot do something with other supplier when list is active
else if res.current_table_id == table.table_id
#nothing has changed, show product list
window.location = '/user/list_products'
else if res.current_table_id != table.table_id
if res.occupied
redirect_to 'user_root', {message: 'table_is_occupied'}
else if res.reserved
redirect_to 'user_root', {message: 'table_is_reserved'}
else if table.closed
redirect_to 'user_root', {message: 'table_is_closed'}
else if table.supplier_closed
redirect_to 'user_root', {message: 'supplier_is_closed'}
else
#TODO Offer to move table
$.post('/user/move_table', {table_id: table.table_id}, (res2)->
if res2.occupied
alert('Cannot move to occupied table')
else
window.location = '/user/list_products'
)
else
if res.occupied
redirect_to 'join_occupied_table', {table_id: table.table_id}
else
#$.post('/user/create_list.json', {table_id: table.table_id}, (res)-> Quser.handle_response(res))
redirect_to 'list_products_for_table', {table_id: table.table_id}
, 'json')
join_occupied_table: (table_id) ->
$('.form-actions').remove()
cont = $('#join-occupied-table-progress-container')
cont.html('')
cont.append $('<img src="/assets/spinner.gif" />')
cont.append $('<p>Waiting for approval of the person on this table</p>')
$.post('/user/join_occupied_table', {table_id: table_id})
setInterval('Quser.check_if_can_join_occupied_table("'+table_id+'")', 7500)
check_if_can_join_occupied_table: (table_id)->
$.post('/user/check_table_join_status', {table_id: table_id}, (res) ->
res ||= {}
if res.approved
redirect_to 'list_products'
else if res.waiting
# do nothing, keep waiting....
else
redirect_to 'user_root', {message: 'join_request_rejected'}
)
add_product: (product, count) ->
count ||= 1
window.active_products_list = {} unless window.active_products_list
window.active_products_list[product._id] = {product: product, number: 0} unless window.active_products_list[product._id]
window.active_products_list[product._id].number += count
Quser.build_product_list()
clear_selected_products: ->
window.active_products_list = {}
$('#active-order-table').hide()