2016-07-30 11:25:22 +02:00
|
|
|
const Backbone = require('backbone');
|
2015-10-17 23:49:24 +02:00
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const DropdownView = Backbone.View.extend({
|
2015-12-16 22:50:45 +01:00
|
|
|
template: require('templates/dropdown.hbs'),
|
2015-10-17 23:49:24 +02:00
|
|
|
|
|
|
|
events: {
|
|
|
|
'click .dropdown__item': 'itemClick'
|
|
|
|
},
|
|
|
|
|
2019-08-16 23:05:39 +02:00
|
|
|
initialize: function() {
|
2015-10-17 23:49:24 +02:00
|
|
|
this.bodyClick = this.bodyClick.bind(this);
|
2016-07-30 11:25:22 +02:00
|
|
|
this.listenTo(Backbone, 'show-context-menu', this.bodyClick);
|
|
|
|
$('body').on('click contextmenu keyup', this.bodyClick);
|
2015-10-17 23:49:24 +02:00
|
|
|
},
|
|
|
|
|
2019-08-16 23:05:39 +02:00
|
|
|
render: function(config) {
|
2015-10-17 23:49:24 +02:00
|
|
|
this.options = config.options;
|
|
|
|
this.renderTemplate(config);
|
|
|
|
this.$el.appendTo(document.body);
|
2017-01-31 07:50:28 +01:00
|
|
|
const ownRect = this.$el[0].getBoundingClientRect();
|
|
|
|
const bodyRect = document.body.getBoundingClientRect();
|
2019-08-16 23:05:39 +02:00
|
|
|
let left = config.position.left || config.position.right - ownRect.right + ownRect.left;
|
2016-07-30 11:25:22 +02:00
|
|
|
let top = config.position.top;
|
|
|
|
if (left + ownRect.width > bodyRect.right) {
|
|
|
|
left = Math.max(0, bodyRect.right - ownRect.width);
|
|
|
|
}
|
|
|
|
if (top + ownRect.height > bodyRect.bottom) {
|
|
|
|
top = Math.max(0, bodyRect.bottom - ownRect.height);
|
|
|
|
}
|
|
|
|
this.$el.css({ top: top, left: left });
|
2015-10-17 23:49:24 +02:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function() {
|
2016-07-30 11:25:22 +02:00
|
|
|
this.viewRemoved = true;
|
|
|
|
$('body').off('click contextmenu keyup', this.bodyClick);
|
2015-10-17 23:49:24 +02:00
|
|
|
Backbone.View.prototype.remove.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
bodyClick: function() {
|
2016-07-30 11:25:22 +02:00
|
|
|
if (!this.viewRemoved) {
|
|
|
|
this.trigger('cancel');
|
|
|
|
}
|
2015-10-17 23:49:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
itemClick: function(e) {
|
|
|
|
e.stopPropagation();
|
2017-01-31 07:50:28 +01:00
|
|
|
const el = $(e.target).closest('.dropdown__item');
|
|
|
|
const selected = el.data('value');
|
2016-06-11 16:18:11 +02:00
|
|
|
this.trigger('select', { item: selected, el: el });
|
2015-10-17 23:49:24 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = DropdownView;
|