diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index 82c17702..07a7844d 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -136,9 +136,11 @@ window.Quser= row.append($('').text(order_txts.join(', '))) row.append($('').html(currency(order.total_amount))) foot.append(''+currency(res.total_amount)+'') - order_selected_products: (h)-> + order_selected_products: ()-> return if $.isEmptyObject(window.active_products_list) - h ||= {} + 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', h, ((res) -> Quser.handle_response(res)), 'json') @@ -170,11 +172,23 @@ window.Quser= 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)-> + Quser.populate_products_table('/user/list_products.json') + 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?table_id='+table_id) + populate_products_table: (src)-> $.get(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) diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 4dce251d..72bd8a91 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -127,7 +127,12 @@ class UserController < ApplicationController products = @table.supplier.products products.include_relation(:product_categories) products.sort_by!{|p| p.product_category.try(:position) || 90000} - h = products.inject({table_number: @table.number, supplier_name: @table.supplier.name}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h} + h = products.inject({ + table_number: @table.number, + supplier_name: @table.supplier.name, + has_occupied_info: true, + is_occupied: @table.occupied? + }){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h} render json: h end end diff --git a/app/views/user/list_products_for_table.html.slim b/app/views/user/list_products_for_table.html.slim index 8203ee58..045f1ecc 100644 --- a/app/views/user/list_products_for_table.html.slim +++ b/app/views/user/list_products_for_table.html.slim @@ -18,7 +18,7 @@ tfoot tr td colspan=2 - button class="btn btn-primary" onClick="Quser.order_selected_products({table_id: '#{@table.id}'})" data-t="selected_products.order"= t('selected_products.order') + button class="btn btn-primary" onClick="Quser.order_selected_products()" data-t="selected_products.order"= t('selected_products.order') |  button class="btn btn btn-warning" onClick="Quser.clear_selected_products()" data-t="selected_products.clear"= t('selected_products.clear') td.currency @@ -27,7 +27,7 @@ - content_for :footer do javascript: jQuery(function(){ - Quser.load_table_products('#{@table.id}', #{@table.occupied? ? 'true' : 'false'}); + Quser.load_table_products(); QMobile.activateRotation(); })