Files
mozo-backend/app/assets/javascripts/qrammer.js.coffee
T
2012-08-23 18:50:06 +02:00

152 lines
6.5 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.
root = exports ? this
root.Qrammer =
alert: (msg) ->
alert(msg)
confirm: (callback, content) ->
content ||= 'Are you sure?'
wrapper = $('<div class="modal"></div>')
callback_wrapper = ->
wrapper.modal('hide')
callback()
header = $('<div class="modal-header"></div>')
.append('<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>')
.append('<h3>Confirm</h3>').appendTo(wrapper)
body = $('<div class="modal-body"></div>').append('<p>'+content+'</p>').appendTo(wrapper)
footer = $('<div class="modal-footer"></div>')
.append($('<a href="#" class="btn">Close</a>').click(-> wrapper.modal('hide')))
.append($('<a href="#" class="btn btn-primary">Yes</a>').click(callback_wrapper))
.appendTo(wrapper)
wrapper.modal()
currency: (num) ->
num = 0.0 if isNaN(num) || num == '' || num == null
'&euro; ' + parseFloat(num).toFixed(2)
add_product: (product) ->
window.active_list = {} unless window.active_list
window.active_list[product._id] = {product: product, number: 0} unless window.active_list[product._id]
window.active_list[product._id].number += 1
Qrammer.build_product_list()
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_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">'+Qrammer.currency(info.product.price * info.number)+'</td>')
x_btn = $('<button class="btn btn-warning btn-mini">x</button>').click(-> delete(window.active_list[product_id]) && Qrammer.build_product_list() )
row.append($('<td></td>').append(x_btn))
$('#active-order-total').html(Qrammer.currency(total))
table.show()
clear_active_list: ->
window.active_list = {}
$('#active-order-table').hide()
order_active_list: (post_uri)->
h = {list_id: active_list_id}
for product_id, info of window.active_list
h['products['+product_id+']'] = info.number
$.post(post_uri, h, ((res) -> Qrammer.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
alert(res['message']) if res['message'] && !res['ok']
alert(res['message']) if res['message'] && res['ok']
load_active_order_list: (supplier_id) ->
$.get('/suppliers/'+supplier_id+'/active_order_list.json', (res) ->
body = $('#active-orders-table tbody')
body.find('tr').remove()
foot = $('#active-orders-table tfoot')
if(!res.orders && !res.orders.length)
alert('No orders in list');
return;
for order in res.orders
order_txts = []
row = $('<tr></tr>').appendTo(body)
process_btn = $('<button class="btn btn-success">In process!</button>')
process_callback = ( (ord) ->
->
my_btn = $(this)
$.post('/orders/'+ord.id+'/is_being_processed', {}, (res)-> my_btn.remove())
)(order)
process_btn.click(process_callback)
delivered_btn = $('<button class="btn btn-inverse">Is delivered!</button>')
delivered_callback = ( (ord, r) ->
->
my_btn = $(this)
$.post('/orders/'+ord.id+'/is_delivered', {}, (res)-> r.slideUp('slow'))
)(order, row)
delivered_btn.click(delivered_callback)
for product in order.products
order_txts.push(product.name + ' (' + product['number'] + ')')
row.append($('<td></td>').text(order_txts.join(', ')))
row.append($('<td class="numeric"></td>').text(order.table_number))
row.append($('<td class="currency"></td>').html(Qrammer.currency(order.total_amount)))
td_buttons = $('<td class="actions"></td>')
td_buttons.append(process_btn).append('&nbsp;') if order.state == 'placed'
td_buttons.append(delivered_btn)
row.append(td_buttons)
#foot.append('<tr><td></td><td class="currency"><strong>'+Qrammer.currency(res.total_amount)+'</strong></td></tr>');
)
load_active_lists: (supplier_id) ->
$.get('/suppliers/'+supplier_id+'/active_lists.json', (res) ->
body = $('#active-lists-table tbody')
body.find('tr').remove()
foot = $('#active-lists-table tfoot')
for list in res.lists
order_txts = []
row = $('<tr></tr>').appendTo(body)
close_btn = $('<button class="btn btn-success">Close!</button>')
close_callback = ( (lst, r) ->
->
my_btn = $(this)
$.post('/lists/'+lst._id+'/is_closed', {}, (res)-> r.slideUp('slow'))
)(list, row)
close_btn.click(close_callback)
row.append($('<td></td>').text(list._id))
row.append($('<td class="currency"></td>').html(Qrammer.currency(list.total_amount)))
td_buttons = $('<td class="actions"></td>')
td_buttons.append(close_btn)
row.append(td_buttons)
#foot.append('<tr><td></td><td class="currency"><strong>'+Qrammer.currency(res.total_amount)+'</strong></td></tr>');
)
build_product_list_as_modal: ->
wrapper = $('<div class="modal"></div>')
callback_wrapper = ->
wrapper.modal('hide')
callback()
header = $('<div class="modal-header"></div>')
.append('<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>')
.append('<h3>Product list</h3>').appendTo(wrapper)
body = $('<div class="modal-body"></div>')
table = $('<table class="table"></table>').appendTo(body)
tbody = $('<tbody></tbody>').appendTo(table)
for product_id, info of window.active_list
row = $('<tr></tr>').appendTo(tbody)
row.append('<td>'+info.product.name+'</td>')
row.append('<td>'+info.number+'</td>')
row.append('<td>'+Qrammer.currency(info.product.price * info.number)+'</td>')
body.appendTo(wrapper)
footer = $('<div class="modal-footer"></div>')
.append($('<a href="#" class="btn">Close</a>').click(-> wrapper.modal('hide')))
.append($('<a href="#" class="btn btn-primary">Yes</a>').click(callback_wrapper))
.appendTo(wrapper)
wrapper.modal()
jQuery.ajaxSetup
'beforeSend': (xhr) ->
xhr.setRequestHeader("Accept", "text/javascript")