Skip to content
This repository was archived by the owner on Mar 6, 2020. It is now read-only.

Commit 7c8d387

Browse files
author
René Kooi
committed
settings: dropdown code cleanup
1 parent a52f73d commit 7c8d387

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

src/views/users/settings/DropdownView.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ define(function (require, exports, module) {
22

33
const Backbone = require('backbone');
44
const $ = require('jquery');
5-
const _ = require('underscore');
5+
const { each, defer } = require('underscore');
66

77
const DropdownView = Backbone.View.extend({
88
className: 'extplug-dropdown',
9-
tagName: 'div',
9+
1010
initialize() {
1111
if (!this.options.selected) {
1212
this.options.selected = Object.keys(this.options.options)[0];
1313
}
1414

1515
this.onDocumentClick = this.onDocumentClick.bind(this);
16-
this.onBaseClick = this.onBaseClick.bind(this);
17-
this.onRowClick = this.onRowClick.bind(this);
1816
},
17+
1918
render() {
2019
this.$label = $('<label />').addClass('title').text(this.options.label);
2120
this.$dl = $('<dl />').addClass('dropdown');
@@ -27,14 +26,14 @@ define(function (require, exports, module) {
2726

2827
this.$rows = $('<dd />');
2928
let selected;
30-
_.each(this.options.options, function (text, value) {
29+
each(this.options.options, (text, value) => {
3130
let row = $('<div />').addClass('row').data('value', value);
3231
let el = $('<span />').text(text);
3332
if (this.options.selected === value) {
3433
selected = row;
3534
}
3635
row.append(el).appendTo(this.$rows);
37-
}, this);
36+
});
3837

3938
this.$dl
4039
.append(this.$selected)
@@ -44,23 +43,26 @@ define(function (require, exports, module) {
4443
.append(this.$label)
4544
.append(this.$dl);
4645

47-
this.$selected.on('click', this.onBaseClick);
48-
this.$rows.on('click', this.onRowClick);
46+
this.$selected.on('click', this.onBaseClick.bind(this));
47+
this.$rows.on('click', this.onRowClick.bind(this));
4948
// trigger the above as a default
5049
if (selected) {
5150
selected.click();
5251
}
5352
return this;
5453
},
55-
close() {
56-
this.$dl.removeClass('open extplug-dropdown-up');
57-
$(document).off('click', this.onDocumentClick);
58-
},
54+
5955
remove() {
6056
this.$('dt, dd').off();
6157
$(document).off('click', this.onDocumentClick);
6258
this._super();
6359
},
60+
61+
close() {
62+
this.$dl.removeClass('open extplug-dropdown-up');
63+
$(document).off('click', this.onDocumentClick);
64+
},
65+
6466
onBaseClick(e) {
6567
if (this.$dl.hasClass('open')) {
6668
this.close();
@@ -72,21 +74,27 @@ define(function (require, exports, module) {
7274
else {
7375
this.$dl.addClass('open extplug-dropdown-up');
7476
}
75-
_.defer(() => {
77+
defer(() => {
7678
$(document).on('click', this.onDocumentClick);
7779
});
7880
}
7981
},
82+
8083
onRowClick(e) {
8184
let row = $(e.target).closest('.row');
82-
this.$dl.find('.row').removeClass('selected');
85+
86+
this.$rows.children().removeClass('selected');
8387
row.addClass('selected');
84-
this.$dl.removeClass('open');
8588
this.$selectedValue.text(row.text());
8689
this.trigger('change', row.data('value'));
90+
91+
// will be closed by onDocumentClick()
8792
},
93+
8894
onDocumentClick(e) {
89-
_.defer(this.close.bind(this));
95+
defer(() => {
96+
this.close();
97+
});
9098
},
9199

92100
canExpandDownward() {

0 commit comments

Comments
 (0)