From d06a9ed3f06d2cb282b7e7724d7b67812c410b61 Mon Sep 17 00:00:00 2001 From: wvengen Date: Fri, 12 Aug 2016 15:54:28 +0200 Subject: [PATCH] Fix dynamic select2 usage (oops!) --- app/assets/javascripts/application.js | 8 ++-- app/assets/stylesheets/application.css | 1 + app/views/deliveries/_form.html.haml | 44 ++++++++----------- .../form_on_supplier_id_change.js.erb | 4 +- app/views/orders/_edit_amounts.html.haml | 21 ++++----- .../current_orders/articles/_form.html.haml | 5 ++- .../ordergroups/_form.html.haml | 2 +- 7 files changed, 41 insertions(+), 44 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index f617a16d..dd3febae 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -1,6 +1,6 @@ //= require jquery //= require jquery_ujs -//= require select2 +//= require select2-full //= require twitter/bootstrap //= require jquery.tokeninput //= require bootstrap-datepicker/core @@ -20,6 +20,8 @@ //= require delta_input //= require recurring_select +$.fn.select2.defaults.set('theme', 'bootstrap'); + // Load following statements, when DOM is ready $(function() { @@ -151,7 +153,7 @@ $(function() { $(document).tooltip({ selector: '[data-toggle~="tooltip"]', }); - + // See stupidtable.js for initialization of local table sorting }); @@ -177,5 +179,3 @@ function highlightRow(checkbox) { function setHiddenId(text, li) { $('hidden_id').value = li.id; } - - diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 10c5985f..6bdfecd2 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -1,6 +1,7 @@ /* *= require bootstrap_and_overrides *= require select2 +*= require select2-bootstrap *= require token-input-bootstrappy *= require bootstrap-datepicker *= require list.unlist diff --git a/app/views/deliveries/_form.html.haml b/app/views/deliveries/_form.html.haml index d9433acc..5103e4c7 100644 --- a/app/views/deliveries/_form.html.haml +++ b/app/views/deliveries/_form.html.haml @@ -3,7 +3,7 @@ $(function() { $('#stock_changes').on('touchclick', '.destroy_stock_change', function() { $(this).prev('input').val('1'); // check for destruction - + var stock_change = $(this).closest('tr'); stock_change.hide(); // do not remove (to ensure destruction) stock_change.removeAttr('id'); // remove id to allow re-adding @@ -17,37 +17,31 @@ mark_article_for_delivery( stock_change.data('id') ); return false; }) - + $('#new_stock_article').removeAttr('disabled').select2({ + tags: true, placeholder: '#{t '.create_stock_article'}', data: #{articles_for_select2(@supplier.articles).to_json}, - createSearchChoice: function(term) { + createTag: function(data) { return { id: 'new', - text: term + name: data.term, // store for ajax request + text: data.term + ' (#{t '.create_from_blank'})' }; - }, - formatResult: function(result, container, query, escapeMarkup) { - if(result.id == 'new') { - return result.text + ' (#{t '.create_from_blank'})'; - } - var markup=[]; - Select2.util.markMatch(result.text, query.term, markup, escapeMarkup); - return markup.join(""); } - }).on('change', function(e) { - var selectedArticle = $(e.currentTarget).select2('data'); - if(!selectedArticle) { + }).on('select2:select', function(e) { + var selectedArticle = e.params.data; + if (!selectedArticle) { return false; } if('new' == selectedArticle.id) { $.ajax({ url: '#{new_stock_article_path}', type: 'get', - data: {stock_article: {name: selectedArticle.text, supplier_id: #{@supplier.id}}}, + data: {stock_article: {name: selectedArticle.name, supplier_id: #{@supplier.id}}}, contentType: 'application/json; charset=UTF-8' }); - $('#new_stock_article').select2('data', null); + $('#new_stock_article').val('').trigger('change'); return true; } if('' != selectedArticle.id) { @@ -57,11 +51,11 @@ data: {old_article_id: selectedArticle.id}, contentType: 'application/json; charset=UTF-8' }); - $('#new_stock_article').select2('data', null); + $('#new_stock_article').val('').trigger('change'); return true; } }); - + // Subscribe to database changes. // See publish/subscribe design pattern in /doc. $(document).on('StockArticle#create', function(e) { @@ -72,7 +66,7 @@ contentType: 'application/json; charset=UTF-8' }); }); - + $(document).on('StockArticle#update', function(e) { $.ajax({ url: '#{form_on_stock_article_update_supplier_deliveries_path(@supplier)}', @@ -82,7 +76,7 @@ }); }); }); - + function mark_article_for_delivery(stock_article_id) { var articleTr = $('#stock_article_' + stock_article_id); if( is_article_available_for_delivery(stock_article_id) ) { @@ -102,7 +96,7 @@ = f.error_notification = base_errors f.object = f.association :supplier, :as => :hidden - + %h2= t '.title_select_stock_articles' %table#stock_articles_for_adding.table.table-hover.stupidtable %thead @@ -118,11 +112,11 @@ - if @supplier.articles.empty? = link_to t('.create_stock_article'), new_stock_article_path, :remote => true, :class => 'btn' - else - %input#new_stock_article{:style => 'width: 500px;'} + %select#new_stock_article{:style => 'width: 500px;'} %tbody - for article in articles_for_table(@supplier.stock_articles) = render :partial => 'stock_article_for_adding', :locals => {:article => article} - + %h2= t '.title_fill_quantities' %table.table#stock_changes.stupidtable %thead @@ -135,7 +129,7 @@ %tbody = f.simple_fields_for :stock_changes do |stock_change_form| = render :partial => 'stock_change_fields', :locals => {:f => stock_change_form} - + %h2= t '.title_finish_delivery' = f.input :delivered_on, as: :date_picker = f.input :note, input_html: {size: '35x4'} diff --git a/app/views/finance/invoices/form_on_supplier_id_change.js.erb b/app/views/finance/invoices/form_on_supplier_id_change.js.erb index 894859ec..f024ec06 100644 --- a/app/views/finance/invoices/form_on_supplier_id_change.js.erb +++ b/app/views/finance/invoices/form_on_supplier_id_change.js.erb @@ -3,8 +3,8 @@ deliveries_data = @deliveries_collection.map {|d| { id: d.id, text: format_deliv orders_data = @orders_collection.map {|o| { id: o.id, text: format_order_item(o) } } %> (function() { - function update_select2(id, data) { - var ele = $(id); + function update_select2(sel, data) { + var ele = $(sel); if (!ele.length) return; ele.select2("destroy"); diff --git a/app/views/orders/_edit_amounts.html.haml b/app/views/orders/_edit_amounts.html.haml index 63935ab2..b9c5290c 100644 --- a/app/views/orders/_edit_amounts.html.haml +++ b/app/views/orders/_edit_amounts.html.haml @@ -45,32 +45,34 @@ $('input[data-units-expected]').each(function() { update_delta(this); }); - + init_add_article('#add_article'); }); - + function init_add_article(sel) { $(sel).removeAttr('disabled').select2({ placeholder: '#{j t('orders.receive.add_article')}', formatNoMatches: function(term) { return '#{j t('.no_articles_available')}';} // TODO implement adding a new article, like in deliveries }).on('change', function(e) { - var selectedArticle = $(e.currentTarget).select2('data'); - if(!selectedArticle) { + var $input = $(e.target); + var selectedArticleId = $input.val(); + if(!selectedArticleId) { return false; } $.ajax({ url: '#{order_order_articles_path(@order)}', type: 'post', - data: JSON.stringify({order_article: {article_id: selectedArticle.id}}), + data: JSON.stringify({order_article: {article_id: selectedArticleId}}), contentType: 'application/json; charset=UTF-8' }); - - $('#add_article').select2('data', null); - }).select2('data', null); + + $input.val('').trigger('change'); + }); + $(sel).val('').trigger('change'); } - + function unlock_receive_input_field() { $('.units_received', $(this).closest('tr')).prop('disabled', false).focus(); $(this).closest('.input-prepend').prop('title', I18n.t('orders.edit_amount.field_unlocked_title')); @@ -99,4 +101,3 @@ %select#add_article{:style => 'width: 500px;'} - new_article_data.each do |option| %option{id: "add_article_#{option[:id]}", value: option[:id]}= option[:text] - diff --git a/plugins/current_orders/app/views/current_orders/articles/_form.html.haml b/plugins/current_orders/app/views/current_orders/articles/_form.html.haml index 92e29714..b5e10b7a 100644 --- a/plugins/current_orders/app/views/current_orders/articles/_form.html.haml +++ b/plugins/current_orders/app/views/current_orders/articles/_form.html.haml @@ -53,7 +53,8 @@ $(document).on('#update_articles_summary', function() { // initialize add ordergroup control - $('#group_order_article_ordergroup_id').select2().select2('data', null); + $('#group_order_article_ordergroup_id').select2(); + $('#group_order_article_ordergroup_id').val('').trigger('change'); }); $(function() { @@ -64,7 +65,7 @@ $(document).on('GroupOrderArticle#create', function(e) { var base_unit = $('#articles_by_articles').data('base-unit'); // reset selection - $('#group_order_article_ordergroup_id').select2('data', null); + $('#group_order_article_ordergroup_id').val('').trigger('change'); // update table $.ajax({ url: '#{show_on_group_order_article_create_current_orders_articles_path}', diff --git a/plugins/current_orders/app/views/current_orders/ordergroups/_form.html.haml b/plugins/current_orders/app/views/current_orders/ordergroups/_form.html.haml index f074be45..6b62f92d 100644 --- a/plugins/current_orders/app/views/current_orders/ordergroups/_form.html.haml +++ b/plugins/current_orders/app/views/current_orders/ordergroups/_form.html.haml @@ -36,7 +36,7 @@ $(document).on('GroupOrderArticle#create', function(e) { var base_unit = $('#articles_by_groups_table').data('base-unit'); // reset selection - $('#group_order_article_order_article_id').select2('data', null); + $('#group_order_article_order_article_id').val('').trigger('change'); // update table $.ajax({ url: '#{show_on_group_order_article_create_current_orders_ordergroups_path}',