Skip to content

Commit 3c10b09

Browse files
committed
Fix duplication of filtering-multiselect on browser back
Fixes #3211
1 parent bf353cc commit 3c10b09

2 files changed

Lines changed: 53 additions & 29 deletions

File tree

app/assets/javascripts/rails_admin/ra.filtering-multiselect.js

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@
3232
xhr: false
3333
},
3434

35+
wrapper: null,
36+
filter: null,
37+
collection: null,
38+
addAll: null,
39+
add: null,
40+
remove: null,
41+
up: null,
42+
down: null,
43+
selection: null,
44+
removeAll: null,
45+
3546
_create: function() {
3647
this._cache = {};
3748
this._build();
@@ -42,73 +53,77 @@
4253
_build: function() {
4354
var i;
4455

45-
this.wrapper = $('<div class="ra-multiselect">');
56+
this.wrapper = this.element.siblings(
57+
'.ra-multiselect[data-input-for="' + this.element.attr('id') + '"]'
58+
);
59+
60+
// Prevent duplication on browser back
61+
if (this.wrapper.length > 0) {
62+
this.filter = this.wrapper.find('input.ra-multiselect-search');
63+
this.collection = this.wrapper.find('select.ra-multiselect-collection');
64+
this.addAll = this.wrapper.find('a.ra-multiselect-item-add-all');
65+
this.add = this.wrapper.find('a.ra-multiselect-item-add');
66+
this.remove = this.wrapper.find('a.ra-multiselect-item-remove');
67+
this.up = this.wrapper.find('a.ra-multiselect-item-up');
68+
this.down = this.wrapper.find('a.ra-multiselect-item-down');
69+
this.selection = this.wrapper.find('select.ra-multiselect-selection');
70+
this.removeAll = this.wrapper.find('a.ra-multiselect-item-remove-all');
71+
return;
72+
}
4673

74+
this.wrapper = $('<div class="ra-multiselect">').attr('data-input-for', this.element.attr('id'))
4775
this.wrapper.insertAfter(this.element);
48-
49-
this.header = $('<div class="ra-multiselect-header ui-helper-clearfix">');
50-
76+
var header = $('<div class="ra-multiselect-header ui-helper-clearfix">');
5177
this.filter = $('<input type="search" placeholder="' + this.options.regional.search + '" class="form-control ra-multiselect-search"/>');
78+
header.append(this.filter);
79+
this.wrapper.append(header);
5280

53-
this.header.append(this.filter);
54-
55-
this.wrapper.append(this.header);
56-
57-
this.columns = {
81+
var columns = {
5882
left: $('<div class="ra-multiselect-column ra-multiselect-left">'),
5983
center: $('<div class="ra-multiselect-column ra-multiselect-center">'),
6084
right: $('<div class="ra-multiselect-column ra-multiselect-right">')
6185
};
6286

63-
for (i in this.columns) {
64-
if (this.columns.hasOwnProperty(i)) {
65-
this.wrapper.append(this.columns[i]);
87+
for (i in columns) {
88+
if (columns.hasOwnProperty(i)) {
89+
this.wrapper.append(columns[i]);
6690
}
6791
}
6892

6993
this.collection = $('<select multiple="multiple"></select>');
70-
7194
this.collection.addClass("form-control ra-multiselect-collection");
72-
7395
this.addAll = $('<a href="#" class="ra-multiselect-item-add-all"><span class="ui-icon ui-icon-circle-triangle-e"></span>' + this.options.regional.chooseAll + '</a>');
74-
75-
this.columns.left.html(this.collection)
76-
.append(this.addAll);
77-
96+
columns.left.html(this.collection)
97+
.append(this.addAll);
7898
this.collection.wrap('<div class="wrapper"/>');
7999

80100

81101
this.add = $('<a href="#" class="ui-icon ui-icon-circle-triangle-e ra-multiselect-item-add">' + this.options.regional.add + '</a>');
82-
this.columns.center.append(this.add);
83-
102+
columns.center.append(this.add);
84103
if (this.options.removable) {
85104
this.remove = $('<a href="#" class="ui-icon ui-icon-circle-triangle-w ra-multiselect-item-remove">' + this.options.regional.remove + '</a>');
86-
this.columns.center.append(this.remove);
105+
columns.center.append(this.remove);
87106
}
88-
89107
if (this.options.sortable) {
90108
this.up = $('<a href="#" class="ui-icon ui-icon-circle-triangle-n ra-multiselect-item-up">' + this.options.regional.up + '</a>');
91109
this.down = $('<a href="#" class="ui-icon ui-icon-circle-triangle-s ra-multiselect-item-down">' + this.options.regional.down + '</a>');
92-
this.columns.center.append(this.up).append(this.down);
110+
columns.center.append(this.up).append(this.down);
93111
}
94112

95113
this.selection = $('<select class="form-control ra-multiselect-selection" multiple="multiple"></select>');
96-
this.columns.right.append(this.selection);
97-
98-
114+
columns.right.append(this.selection);
99115
if (this.options.removable) {
100116
this.removeAll = $('<a href="#" class="ra-multiselect-item-remove-all"><span class="ui-icon ui-icon-circle-triangle-w"></span>' + this.options.regional.clearAll + '</a>');
101-
this.columns.right.append(this.removeAll);
117+
columns.right.append(this.removeAll);
102118
}
103-
104119
this.selection.wrap('<div class="wrapper"/>');
105120

106121
this.element.css({display: "none"});
107122

108123
this.tooManyObjectsPlaceholder = $('<option disabled="disabled" />').text(RailsAdmin.I18n.t("too_many_objects"));
109124
this.noObjectsPlaceholder = $('<option disabled="disabled" />').text(RailsAdmin.I18n.t("no_objects"))
110125

111-
if(this.options.xhr){
126+
if (this.options.xhr) {
112127
this.collection.append(this.tooManyObjectsPlaceholder);
113128
}
114129
},

spec/integration/widgets/filtering_multi_select_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,13 @@
114114
end
115115
end
116116
end
117+
118+
it 'does not cause duplication when using browser back' do
119+
visit new_path(model_name: 'team')
120+
find(%{[href$="/admin/team/export"]}).click
121+
is_expected.to have_content 'Export Teams'
122+
page.go_back
123+
is_expected.to have_content 'New Team'
124+
expect(all(:css, 'input.ra-multiselect-search').count).to eq 1
125+
end
117126
end

0 commit comments

Comments
 (0)