From 9fb2dfbfc5eb1c45300cd69b246645ec0b9dd751 Mon Sep 17 00:00:00 2001 From: Daniele Palombo Date: Fri, 10 Jul 2020 16:32:52 +0200 Subject: [PATCH] Add SelectableTable jQuery event --- backend/app/assets/javascripts/spree/backend.js | 1 + .../spree/backend/components/selectable_table.js | 13 +------------ .../spree/backend/returns/return_item_selection.js | 5 +++++ .../assets/javascripts/spree/backend/views/index.js | 1 + .../spree/backend/views/tables/return_items.js | 9 +++++++++ .../spree/backend/views/tables/selectable_table.js | 10 ++++++++++ 6 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/returns/return_item_selection.js create mode 100644 backend/app/assets/javascripts/spree/backend/views/tables/return_items.js diff --git a/backend/app/assets/javascripts/spree/backend.js b/backend/app/assets/javascripts/spree/backend.js index 5da1258695e..a653a86ae18 100644 --- a/backend/app/assets/javascripts/spree/backend.js +++ b/backend/app/assets/javascripts/spree/backend.js @@ -49,6 +49,7 @@ //= require spree/backend/progress //= require spree/backend/promotions //= require spree/backend/promotions/activation +//= require spree/backend/returns/return_item_selection //= require spree/backend/routes //= require spree/backend/shipments //= require spree/backend/spree-select2 diff --git a/backend/app/assets/javascripts/spree/backend/components/selectable_table.js b/backend/app/assets/javascripts/spree/backend/components/selectable_table.js index 789925b0479..df7ba519b2c 100644 --- a/backend/app/assets/javascripts/spree/backend/components/selectable_table.js +++ b/backend/app/assets/javascripts/spree/backend/components/selectable_table.js @@ -1,16 +1,5 @@ Spree.ready(function() { $('table.selectable-table').each(function() { - var selectableTableModel = new Backbone.Model({ - allSelected: false, - selectedItems: [] - }) - - new Spree.Views.Tables.SelectableTable({el: this, model: selectableTableModel}); - - if($(this).hasClass('return-items-table')) { - var tfoot = document.createElement('tfoot') - new Spree.Views.Tables.SelectableTable.SumReturnItemAmount({ el: tfoot, model: selectableTableModel}); - $(this).append(tfoot); - } + $(document).trigger("selectableTable:init", [new Spree.Views.Tables.SelectableTable({el: this})]); }) }); diff --git a/backend/app/assets/javascripts/spree/backend/returns/return_item_selection.js b/backend/app/assets/javascripts/spree/backend/returns/return_item_selection.js new file mode 100644 index 00000000000..b4159a34687 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/returns/return_item_selection.js @@ -0,0 +1,5 @@ +$(document).on("selectableTable:init", function(_event, selectableTable) { + if(selectableTable.$el.hasClass('return-items-table')) { + new Spree.Views.Tables.ReturnItems({el: selectableTable.el, model: selectableTable.model}); + } +}) diff --git a/backend/app/assets/javascripts/spree/backend/views/index.js b/backend/app/assets/javascripts/spree/backend/views/index.js index dec213af48c..3c969dbc9ec 100644 --- a/backend/app/assets/javascripts/spree/backend/views/index.js +++ b/backend/app/assets/javascripts/spree/backend/views/index.js @@ -24,6 +24,7 @@ //= require 'spree/backend/views/promotions/option_values_rule' //= require 'spree/backend/views/tables/editable_table' //= require 'spree/backend/views/tables/editable_table_row' +//= require 'spree/backend/views/tables/return_items' //= require 'spree/backend/views/tables/selectable_table' //= require 'spree/backend/views/tables/selectable_table_summary' //= require 'spree/backend/views/tables/selectable_table_sum_return_item_amount' diff --git a/backend/app/assets/javascripts/spree/backend/views/tables/return_items.js b/backend/app/assets/javascripts/spree/backend/views/tables/return_items.js new file mode 100644 index 00000000000..e31fbb29e49 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/views/tables/return_items.js @@ -0,0 +1,9 @@ +Spree.Views.Tables.ReturnItems = Backbone.View.extend({ + initialize: function() { + if(this.$el.hasClass('return-items-table')) { + var tfoot = document.createElement('tfoot') + new Spree.Views.Tables.SelectableTable.SumReturnItemAmount({ el: tfoot, model: this.model}); + this.$el.append(tfoot); + } + }, +}) diff --git a/backend/app/assets/javascripts/spree/backend/views/tables/selectable_table.js b/backend/app/assets/javascripts/spree/backend/views/tables/selectable_table.js index 1742079a7f4..422548ecfcc 100644 --- a/backend/app/assets/javascripts/spree/backend/views/tables/selectable_table.js +++ b/backend/app/assets/javascripts/spree/backend/views/tables/selectable_table.js @@ -3,7 +3,16 @@ Spree.Views.Tables.SelectableTable = Backbone.View.extend({ 'change .selectable': 'onSelectedItem', }, + preinitialize({autoRender}) { + this.autoRender = autoRender; + }, + initialize: function() { + this.model = new Backbone.Model({ + allSelected: false, + selectedItems: [] + }); + this.listenTo(this.model, 'change', this.detectChanges) if(this.$el.find('.selectable').length > 0) { var tr = document.createElement('tr') @@ -12,6 +21,7 @@ Spree.Views.Tables.SelectableTable = Backbone.View.extend({ } this.render(); + this.trigger("selectableTable:init", this) }, onSelectedItem: function(event) {