keeweb/app/scripts/views/dropdown-view.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
var Backbone = require('backbone');
var 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'
},
initialize: function () {
this.bodyClick = this.bodyClick.bind(this);
$('body').on('click', this.bodyClick);
},
render: function (config) {
this.options = config.options;
this.renderTemplate(config);
this.$el.appendTo(document.body);
var ownRect = this.$el[0].getBoundingClientRect();
2016-03-05 09:35:22 +01:00
var left = config.position.left || (config.position.right - ownRect.right + ownRect.left);
this.$el.css({ top: config.position.top, left: left });
2015-10-17 23:49:24 +02:00
return this;
},
remove: function() {
$('body').off('click', this.bodyClick);
Backbone.View.prototype.remove.apply(this, arguments);
},
bodyClick: function() {
this.trigger('cancel');
},
itemClick: function(e) {
e.stopPropagation();
2016-06-11 16:18:11 +02:00
var el = $(e.target).closest('.dropdown__item');
var selected = el.data('value');
this.trigger('select', { item: selected, el: el });
2015-10-17 23:49:24 +02:00
}
});
module.exports = DropdownView;