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

89 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
var Backbone = require('backbone'),
Keys = require('../const/keys'),
KeyHandler = require('../comp/key-handler');
2015-10-17 23:49:24 +02:00
var ModalView = Backbone.View.extend({
el: 'body',
2015-12-16 22:50:45 +01:00
template: require('templates/modal.hbs'),
2015-10-17 23:49:24 +02:00
events: {
'click .modal__buttons button': 'buttonClick',
'click': 'bodyClick'
},
initialize: function () {
if (typeof this.model.esc === 'string') {
KeyHandler.onKey(Keys.DOM_VK_ESCAPE, this.escPressed, this, false, true);
}
if (typeof this.model.enter === 'string') {
KeyHandler.onKey(Keys.DOM_VK_RETURN, this.enterPressed, this, false, true);
}
KeyHandler.setModal(true);
},
2016-07-17 13:30:38 +02:00
remove: function() {
2015-10-17 23:49:24 +02:00
KeyHandler.offKey(Keys.DOM_VK_ESCAPE, this.escPressed, this);
KeyHandler.offKey(Keys.DOM_VK_RETURN, this.enterPressed, this);
KeyHandler.setModal(false);
Backbone.View.prototype.remove.apply(this, arguments);
},
render: function () {
var parent = this.$el;
this.setElement($(this.template(this.model)));
parent.append(this.$el);
var el = this.$el;
el.addClass('modal--hidden');
2016-07-17 13:30:38 +02:00
setTimeout(() => {
2015-10-17 23:49:24 +02:00
el.removeClass('modal--hidden');
}, 20);
return this;
},
2016-03-31 22:52:04 +02:00
change: function(config) {
if (config.header) {
this.$el.find('.modal__header').html(config.header);
}
},
2015-10-17 23:49:24 +02:00
buttonClick: function(e) {
var result = $(e.target).data('result');
this.closeWithResult(result);
},
bodyClick: function() {
if (typeof this.model.click === 'string') {
this.closeWithResult(this.model.click);
}
},
escPressed: function() {
this.closeWithResult(this.model.esc);
},
enterPressed: function(e) {
e.stopImmediatePropagation();
e.preventDefault();
this.closeWithResult(this.model.enter);
},
closeWithResult: function(result) {
2015-11-17 21:57:32 +01:00
var checked = this.model.checkbox ? this.$el.find('#modal__check').is(':checked') : undefined;
this.trigger('result', result, checked);
2015-10-17 23:49:24 +02:00
this.$el.addClass('modal--hidden');
this.undelegateEvents();
setTimeout(this.remove.bind(this), 100);
2016-04-03 21:16:05 +02:00
},
closeImmediate: function() {
this.trigger('result', undefined);
this.undelegateEvents();
this.remove();
2015-10-17 23:49:24 +02:00
}
});
module.exports = ModalView;