diff --git a/app/assets/javascripts/supplier/qsupplier.js.coffee b/app/assets/javascripts/supplier/qsupplier.js.coffee index b8fbadec..3806e8b2 100644 --- a/app/assets/javascripts/supplier/qsupplier.js.coffee +++ b/app/assets/javascripts/supplier/qsupplier.js.coffee @@ -166,5 +166,18 @@ root.Qsupplier= 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') + number_start = $('#add-tables-number-start').val() + number_end = $('#add-tables-number-end').val() + $.post('/supplier/sections/'+current_section_id+'/add_tables', {number_start: number_start, number_end: number_end}, -> window.location.reload()) + #$('#add-tables-modal').modal('hide') + false + arrange_tables_of_active_section: -> + option = $('input[name=arrange-table-option]:checked').val() + by_row_count = parseInt($('#arrange-tables-by-row-count').val()) + by_column_count = parseInt($('#arrange-tables-by-column-count').val()) + if(option == "by_row") + return alert('Please fill in a positive number representing the number of tables per row') unless by_row_count && by_row_count > 0 + if(option == "by_column") + return alert('Please fill in a positive number representing the number of tables per column') unless by_column_count && by_column_count > 0 + $.post('/supplier/sections/'+current_section_id+'/arrange_tables', {option: option, row_count: by_row_count, column_count: by_column_count}, -> window.location.reload()) + false diff --git a/app/assets/stylesheets/twitter-bootstrap/bootstrap_overrides.css.sass b/app/assets/stylesheets/twitter-bootstrap/bootstrap_overrides.css.sass index 8f71a676..1bc76df2 100644 --- a/app/assets/stylesheets/twitter-bootstrap/bootstrap_overrides.css.sass +++ b/app/assets/stylesheets/twitter-bootstrap/bootstrap_overrides.css.sass @@ -32,3 +32,9 @@ body dd &:after content: '\a0' + form + .controls + label + display: inline-block + padding-left: 1em + padding-right: 1em diff --git a/app/controllers/suppliers/sections_controller.rb b/app/controllers/suppliers/sections_controller.rb index 77060587..88cf961d 100644 --- a/app/controllers/suppliers/sections_controller.rb +++ b/app/controllers/suppliers/sections_controller.rb @@ -111,5 +111,32 @@ module Suppliers end end end + + # POST /sections/1/add_tables {number_start: 1423, number_end: 234234} + def add_tables + @section = Section.find_by_supplier_and_id(current_supplier, params[:id]) + number_start = params[:number_start].to_i + number_end = params[:number_end].to_i + for table_number in number_start..number_end + next if table_number.zero? + table = Table.new(number: table_number) + table.supplier = current_supplier + table.section = @section + table.save + end + @section.arrange_tables_in_grid + render json: {ok: true} + end + + # POST /sections/1/arrange_tables {number_start: 1423, number_end: 234234} + def arrange_tables + @section = Section.find_by_supplier_and_id(current_supplier, params[:id]) + case params[:option] + when 'distribute' then @section.arrange_tables_in_grid + when 'by_row' then @section.arrange_tables_in_rows_of(params[:row_count].to_i) + when 'by_column' then @section.arrange_tables_in_columns_of(params[:column_count].to_i) + end + render json: {ok: true} + end end end diff --git a/app/models/section.rb b/app/models/section.rb index 60ed7dc7..512cd4b0 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -78,4 +78,82 @@ class Section end @for_tables_as_json = h end + + def arrange_tables_in_grid + w = width + h = height + n = tables.size + l = Math.sqrt((w*h).to_f/n) + epsilon = (10 ** -(Float::DIG - 1)).to_f + while (w/l).floor * (h/l).floor < n # find a fitting combination + l = if w.remainder(l) < epsilon then (h/l).ceil + elsif h.remainder(l) < epsilon then (w/l).ceil + elsif w.remainder(l) < epsilon && h.remainder(l) < epsilon then [w / (w/l).ceil.succ, h / (h/l).ceil.succ].max # Failsafe for when both are valid and it still does not fit + else [w / (w/l).ceil, h / (h/l).ceil].max end + end + x0 = w.remainder(l - epsilon)/2 # Start with half a remainder space, will end with the other halve + x = x0 + y = h.remainder(l - epsilon)/2 + saves = [] + for table in tables.sort_by(&:number) + if x + l > w + (1e4*epsilon) # New row, error = epsilon times possible tables + x = x0 + y += l + end + table.position_x = x + (l/2) - (table_width/2) # Starting point of square + half the square (center) minus half the table size + table.position_y = y + (l/2) - (table_height/2) + saves << table.save + x += l + end + [l, saves.all?] + end + + def arrange_tables_in_rows_of(n) + return unless n.present? + n = n.to_i + return unless n > 0 + dx = width / n + dy = height / (tables.size.to_f/n).ceil + x = 0.0 + y = 0.0 + tables.sort_by(&:number).each.with_index do |table, i| + table.position_x = x + (dx/2) - (table_width/2) + table.position_y = y + (dy/2) - (table_height/2) + x += dx + if (i + 1).multiple_of?( n ) + x = 0.0 + y += dy + end + table.save + end + end + def arrange_tables_in_columns_of(n) + return unless n.present? + n = n.to_i + return unless n > 0 + dx = width / (tables.size.to_f/n).ceil + dy = height / n + x = 0.0 + y = 0.0 + tables.sort_by(&:number).each.with_index do |table, i| + table.position_x = x + (dx/2) - (table_width/2) + table.position_y = y + (dy/2) - (table_height/2) + y += dy + if (i + 1).multiple_of?( n ) + y = 0.0 + x += dx + end + table.save + end + end + + # Method returning the sections table width + def table_width + 2.0 + end + + # Method returning the sections table height + def table_height + 2.0 + end end diff --git a/app/views/suppliers/sections/manage_tables.html.slim b/app/views/suppliers/sections/manage_tables.html.slim index 17ee73b3..621f3406 100644 --- a/app/views/suppliers/sections/manage_tables.html.slim +++ b/app/views/suppliers/sections/manage_tables.html.slim @@ -62,6 +62,30 @@ .controls input.input-mini#add-tables-number-end type=:number value=120 .modal-footer - a.btn href='#' Close - a.btn.btn-primary href='#' onclick=%|Qsupplier.add_tables_to_active_section()| Add - + button.btn data-dismiss="modal" aria-hidden=true Close + button.btn.btn-primary onclick="Qsupplier.add_tables_to_active_section()" Add +#arrange-tables-modal.modal.hide.fade tabindex=-1 role=:dialog aria-labeledby='add-tables-modal-label' aria-hidden=true + button.close type=:button data-dismiss=:modal aria-hidden=true x + h3#arrange-tables-modal-label= t('supplier.section.arrange_tables.modal.title') + .modal-body + p= t('supplier.section.arrange_tables.modal.body_header') + form.form-horizontal + .control-group + label.control-label for='arrange-tables-distributed' = t('supplier.section.arrange_tables.modal.distributed') + .controls + input#arrange-tables-distributed type="radio" name="arrange-table-option" checked=true value="distributed" + .control-group + label.control-label for='arrange-tables-by_row' = t('supplier.section.arrange_tables.modal.by_row') + .controls + input#arrange-tables-by_row type="radio" name="arrange-table-option" value="by_row" + label for="arrange-tables-by-row-count"= t('supplier.section.arrange_tables.modal.by_row_count') + input.input-mini#arrange-tables-by-row-count type="text" value=0 + .control-group + label.control-label for='arrange-tables-by_column' = t('supplier.section.arrange_tables.modal.by_column') + .controls + input#arrange-tables-by_column type="radio" name="arrange-table-option" value="by_column" + label for="arrange-tables-by-column-count"= t('supplier.section.arrange_tables.modal.by_column_count') + input.input-mini#arrange-tables-by-column-count type="text" value=0 + .modal-footer + button.btn data-dismiss="modal" aria-hidden=true Close + button.btn.btn-primary onclick="Qsupplier.arrange_tables_of_active_section()" Add diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 41069a55..42653434 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -113,6 +113,11 @@ nl: modal: title: Positioneer tafels body_header: "" + distributed: Verspreid + by_row: Per rij + by_row_count: van + by_column: Per kolom + by_column_count: van user: active_list: diff --git a/config/routes.rb b/config/routes.rb index e8f815d1..736d93b8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -79,6 +79,8 @@ Qrammer::Application.routes.draw do member do get :manage_tables get :tables_view + post :add_tables + post :arrange_tables end end resources :tables do