Files
mozo-backend/app/assets/javascripts/user/quser.js.coffee
T

364 lines
16 KiB
CoffeeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
data_host = window.data_host
root_url = QMobile.root_url()
authentication_string = QMobile.authentication_string()
authentication_object = $.parseJSON(QMobile.authentication_object())
window.Quser=
format_date: (utc) ->
formatted = ''
formatted += utc.substr(0,10)
formatted += ' '
formatted += utc.substr(11, 5)
formatted
home_loader: ->
$.getJSON(data_host + '/user/list_info.json?' + authentication_string, (res) -> Quser.handle_active_list_default_actions(res))
handle_active_list: (callback) ->
$.getJSON(data_host + '/user/list_info.json?' + authentication_string, (res) ->
if !res.list_active
redirect_to 'user_root', {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)
if response.supplier_name
$('.supplier-name').text(response.supplier_name)
if response.not_present
$('.home-link').hide()
else
$('.home-link').show()
Quser.list_needs_payment_default_action(response)
Quser.list_needs_help_default_action(response)
# 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(data_host + '/user/approve_join_request', $.extend({user_id: request.user_id}, authentication_object), -> window.join_request_active = false; wrapper.modal('hide') )
)(join_request)
reject_callback = ( (request)->
->
$.post(data_host + '/user/reject_join_request', $.extend({user_id: request.user_id}, authentication_object), -> 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></h3>').text(t('join_request.title'))).appendTo(wrapper)
body = $('<div class="modal-body"></div>')
#body.append(join_request.user_email + ' wants to join the table')
body.text(t('join_request.body', {email: join_request.user_email}))
body.appendTo(wrapper)
footer = $('<div class="modal-footer"></div>')
.append($('<a href="#" class="btn"></a>').text(t('join_request.reject')).click(reject_callback))
.append($('<a href="#" class="btn btn-primary"></a>').text(t('join_request.approve')).click(join_callback))
.appendTo(wrapper)
wrapper.modal()
list_needs_help_default_action: (response)->
response ||= window.active_list
needs_help_container = $('#list-needs-help-button')
if needs_help_container.length
if response.needs_help
needs_help_container.data('needs-help', true)
needs_help_container.addClass('active')
else
needs_help_container.data('needs-help', false)
needs_help_container.removeClass('active')
unless needs_help_container.data('click-initialized')
needs_help_container.click( ->
if $(this).data('needs-help')
Qwaiter.alert(t('list_needs_help.help_is_on_its_way'))
else
Qwaiter.confirm(
title: t('list_needs_help.title')
content: t('list_needs_help.content')
ok: Quser.list_needs_help
)
)
needs_help_container.data('click-initialized', true)
list_needs_help: ->
return unless window.active_list && !window.active_list.needs_help
$.post(data_host + '/user/needs_help.json', authentication_object, (res) -> window.active_list = res; Quser.list_needs_help_default_action(res))
list_needs_payment_default_action: (response)->
response ||= window.active_list
needs_payment_container = $('#list-needs-payment-button')
if needs_payment_container.length
if response.needs_payment
needs_payment_container.data('needs-payment', true)
needs_payment_container.addClass('active')
else
needs_payment_container.data('needs-payment', false)
needs_payment_container.removeClass('active')
unless needs_payment_container.data('click-initialized')
needs_payment_container.click( ->
if $(this).data('needs-payment')
Qwaiter.alert(t('list_needs_payment.payment_already_requested'))
else
Qwaiter.confirm(
title: t('list_needs_payment.title')
content: t('list_needs_payment.content')
ok: Quser.list_needs_payment
)
)
needs_payment_container.data('click-initialized', true)
list_needs_payment: ->
return unless window.active_list && !window.active_list.needs_payment
$.post(data_host + '/user/list_needs_payment.json', authentication_object, (res) -> window.active_list = res; Quser.list_needs_payment_default_action(res))
load_active_list: () ->
$.getJSON(data_host + '/user/active_list.json?'+authentication_string, (res) ->
window.active_list = res
unless res.list_active
redirect_to 'history_list', {list_id: res._id, list_closed: true}
return
Quser.handle_active_list_default_actions(res)
body = $('#active-list-table tbody')
foot = $('#active-list-table tfoot')
Quser.build_list_table(body, foot, res)
)
load_history_list: () ->
match = window.document.URL.toString().match('list_id=([0-9a-zA-Z]+)')
if match
list_id = match[1]
else
return
$.getJSON(data_host + '/user/history_list.json?list_id='+list_id+'&'+ authentication_string, (res) ->
body = $('#history-list-table tbody')
foot = $('#history-list-table tfoot')
Quser.build_list_table(body, foot, res)
$('.list-created-at').text(Quser.format_date(res.created_at))
$('.supplier-name').text(res.supplier_name)
)
load_list_history: ->
match = window.document.URL.toString().match('page=([0-9]+)')
if match
page = match[1]
else
page = 1
$.getJSON(data_host + '/user/list_history.json?'+authentication_string+'&page='+page, (res) ->
Quser.paginate(res, '/user/list_history.html')
container = $('#list-history-container')
for list in res.lists
li = $('<li></li>').appendTo(container)
link = $('<a></a>').appendTo(li)
link.attr('href', root_url + '/user/history_list.html?list_id='+list._id)
txt = list.supplier_name
txt += ' - '
txt += Quser.format_date(list.created_at)
link.text(txt)
)
paginate: (wrapper, src) ->
container = $('nav.pagination')
container.html('')
list = $('<ul></ul>').appendTo(container)
if wrapper.num_pages && wrapper.num_pages > 1
for i in [1..wrapper.num_pages]
li = $('<li class="page"></li>')
link = $('<a></a>')
link.text(i)
if wrapper.current_page && wrapper.current_page == i
li.addClass('active')
link.attr('href', 'javascript:void(0)')
else
link.attr('href', root_url + src + '?'+authentication_string+'&page='+i)
li.append(link)
list.append(li)
build_list_table: (body, foot, res) ->
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: ()->
return if $.isEmptyObject(window.active_products_list)
h = {}
match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)')
h['table_id'] = match[1] if match
for product_id, info of window.active_products_list
h['products['+product_id+']'] = info.number
$.post(data_host + '/user/order_selected_products', $.extend(h, authentication_object), ((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
if res['message'] && !res['ok']
redirect_to 'user_root', {message: res['message']}
else
redirect_to res.location || '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?'+authentication_string)
load_table_products: ->
match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)')
if match
table_id = match[1]
else
redirect_to 'user_root', {message: 'cannot_identify_table'}
return
Quser.populate_products_table('/user/list_products_for_table.json?'+authentication_string+'&table_id='+table_id)
populate_products_table: (src)->
$.getJSON(data_host + src, (res) ->
if res.has_occupied_info
include_order_buttons = !res.is_occupied
delete(res['has_occupied_info'])
delete(res['is_occupied'])
else
include_order_buttons = true
body = $('#products-table tbody')
if res.table_number
$('.table-number').text(res.table_number)
delete(res['table_number'])
if res.supplier_name
$('.supplier-name').text(res.supplier_name)
delete(res['supplier_name'])
body.find('tr').remove()
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'
$.getJSON(data_host + '/user/table_info.json?'+authentication_string+'&table_id='+table.table_id, (res)->
if res.current_table_id
if res.other_supplier
redirect_to 'user_root', {message: 'table_is_from_other_supplier'}
else if res.current_table_id == table.table_id
#nothing has changed, show product list
redirect_to '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 res.supplier_closed
redirect_to 'user_root', {message: 'supplier_is_closed'}
else
## Offer to move table
Qwaiter.confirm(
ok: ->
$.post(data_host + '/user/move_table', $.extend({table_id: table.table_id}, authentication_object), (res2)->
if res2.occupied
redirect_to 'user_root', {message: 'move_table.cannot_move_to_occupied_tabe'}
else
redirect_to 'list_products', {message: 'move_table.moved_to_another_table'}
)
cancel: ->
redirect_to 'list_products'
title: t('move_table.confirmation_title')
content: t('move_table.confirmation_body')
)
else
if res.occupied
redirect_to 'join_occupied_table', {table_id: table.table_id}
else if res.supplier_closed
redirect_to 'user_root', {message: 'supplier_is_closed'}
else
#$.post(data_host + '/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: () ->
match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)')
if match
table_id = match[1]
else
redirect_to 'user_root', {message: 'cannot_identify_table'}
return
$('.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(data_host + '/user/join_occupied_table', $.extend({table_id: table_id}, authentication_object))
setInterval('Quser.check_if_can_join_occupied_table("'+table_id+'")', 7500)
check_if_can_join_occupied_table: (table_id)->
$.post(data_host + '/user/check_table_join_status', $.extend({table_id: table_id}, authentication_object), (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()