root = exports ? this root.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() ) handle_active_list_default_actions: -> Quser.list_needs_payment_default_action() Quser.list_needs_help_default_action() 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('') else needs_help_container.html($('').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('') else needs_payment_container.html($('').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() 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 = $('').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($('').text(order_txts.join(', '))) row.append($('').html(Qrammer.currency(order.total_amount))) foot.append(''+Qrammer.currency(res.total_amount)+'') ) order_selected_products: (h)-> 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 = $('').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 = $('').attr('id', 'active-order-row-'+product_id).appendTo(tbody) row.append(''+info.product.name+'') row.append(''+info.number+'') row.append(''+Qrammer.currency(info.product.price * info.number)+'') x_btn = $('').click(-> delete(window.active_products_list[product_id]) && Quser.build_product_list() ) row.append($('').append(x_btn)) $('#active-order-total').html(Qrammer.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') for category, products of res body.append('

'+category+'

') for product in products row = $('') row.append(''+product.name+'') if include_order_buttons 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 = $('1') order_count_minus = $('-') 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 = $('+') order_count_plus.click(-> val_holder = $(this).siblings('.order-product-count') val = parseInt(val_holder.text()) val_holder.text(val + 1) ) row.append($('').append(order_count_minus).append(' ').append(order_product_count).append(' ').append(order_count_plus)) row.append(''+Qrammer.currency(product.price)+'') row.append($('').append(button)) if include_order_buttons body.append(row) ) actions_for_table: (table)-> table = JSON.parse(table) if typeof(table) == 'string' alert(table) $.get('/user/table_info.json?table_id='+table.table_id, (res)-> if res.occupied alert('Table is occupied') else if res.other_supplier #TODO cannot do something with other supplier when list is active else if res.current_table_id && res.current_table_id == table.table_id #nothing has changed, show product list window.location = '/user/list_products' else if res.current_table_id && res.current_table_id != table.table_id #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 #$.post('/user/create_list.json', {table_id: table.table_id}, (res)-> Quser.handle_response(res)) window.location = '/user/list_products_for_table?table_id='+table.table_id , 'json') 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()