1
0
mirror of https://github.com/keeweb/keeweb.git synced 2024-06-26 07:39:04 +02:00
keeweb/app/scripts/views/dropdown-view.js
2016-07-30 12:25:22 +03:00

57 lines
1.7 KiB
JavaScript

'use strict';
const Backbone = require('backbone');
let DropdownView = Backbone.View.extend({
template: require('templates/dropdown.hbs'),
events: {
'click .dropdown__item': 'itemClick'
},
initialize: function () {
this.bodyClick = this.bodyClick.bind(this);
this.listenTo(Backbone, 'show-context-menu', this.bodyClick);
$('body').on('click contextmenu keyup', this.bodyClick);
},
render: function (config) {
this.options = config.options;
this.renderTemplate(config);
this.$el.appendTo(document.body);
let ownRect = this.$el[0].getBoundingClientRect();
let bodyRect = document.body.getBoundingClientRect();
let left = config.position.left || (config.position.right - ownRect.right + ownRect.left);
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 });
return this;
},
remove: function() {
this.viewRemoved = true;
$('body').off('click contextmenu keyup', this.bodyClick);
Backbone.View.prototype.remove.apply(this, arguments);
},
bodyClick: function() {
if (!this.viewRemoved) {
this.trigger('cancel');
}
},
itemClick: function(e) {
e.stopPropagation();
var el = $(e.target).closest('.dropdown__item');
var selected = el.data('value');
this.trigger('select', { item: selected, el: el });
}
});
module.exports = DropdownView;