root = exports ? this data_host = '' root.Qsupplier= 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($('').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($('').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='+($('#current_section_selector').val() || ''), (res) -> body = $('#active-lists-table tbody') body.find('tr').remove() foot = $('#active-lists-table tfoot') for list in res.lists order_txts = [] row = $('').appendTo(body) close_btn = $('') close_callback = ( (lst, r) -> -> my_btn = $(this) $.post('/supplier/close_list', {list_id: list._id}, (res)-> r.slideUp('slow')) )(list, row) close_btn.click(close_callback) needs_help_btn = $('') needs_help_callback = ( (lst, r) -> -> my_btn = $(this) $.post('/supplier/mark_list_as_helped', {list_id: list._id}, (res)-> my_btn.remove() ) )(list, row) needs_help_btn.click(needs_help_callback) icons_td = $('').appendTo(row) icons_td.append('').append(' ') if list.needs_help # or icon-bell icons_td.append('') if list.needs_payment row.append($('').append($('').text(list.table_number))) row.append($('').text(list.section_title)) row.append($('').html(currency(list.total_amount))) td_buttons = $('') td_buttons.append(needs_help_btn).append(' ') if list.needs_help td_buttons.append(close_btn) row.append(td_buttons) #foot.append(''+currency(res.total_amount)+''); ) load_active_orders: () -> $.get('/supplier/active_orders.json?section_id='+($('#current_section_selector').val() || ''), (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 = $('').appendTo(body) process_btn = $('') process_callback = ( (ord) -> -> my_btn = $(this) $.post('/supplier/mark_order_in_process', {order_id: ord.id}, (res)-> my_btn.remove()) )(order) process_btn.click(process_callback) delivered_btn = $('') delivered_callback = ( (ord, r) -> -> my_btn = $(this) $.post('/supplier/order_is_delivered', {order_id: ord.id}, (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($('').text(order_txts.join(', '))) row.append($('').text(order.table_number)) row.append($('').text(order.section_title)) row.append($('').html(currency(order.total_amount))) td_buttons = $('') td_buttons.append(process_btn).append(' ') if order.state == 'placed' td_buttons.append(delivered_btn) row.append(td_buttons) #foot.append(''+currency(res.total_amount)+''); ) 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) -> 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(currency(order.total_amount))) foot.append(''+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') number_end = $('#add-tables-number-end')