2015-02-13 05:22:32 +01:00
|
|
|
import Ember from 'ember';
|
2014-10-24 23:09:50 +02:00
|
|
|
|
|
|
|
// Code modified from Addepar/ember-widgets
|
|
|
|
// https://github.com/Addepar/ember-widgets/blob/master/src/mixins.coffee#L39
|
|
|
|
|
2014-05-31 02:07:15 +02:00
|
|
|
var BodyEventListener = Ember.Mixin.create({
|
|
|
|
bodyElementSelector: 'html',
|
|
|
|
bodyClick: Ember.K,
|
2014-10-24 23:09:50 +02:00
|
|
|
|
2014-05-31 02:07:15 +02:00
|
|
|
init: function () {
|
|
|
|
this._super();
|
2014-10-24 23:09:50 +02:00
|
|
|
|
2014-05-31 02:07:15 +02:00
|
|
|
return Ember.run.next(this, this._setupDocumentHandlers);
|
|
|
|
},
|
2014-10-24 23:09:50 +02:00
|
|
|
|
2014-05-31 02:07:15 +02:00
|
|
|
willDestroy: function () {
|
|
|
|
this._super();
|
2014-10-24 23:09:50 +02:00
|
|
|
|
2014-05-31 02:07:15 +02:00
|
|
|
return this._removeDocumentHandlers();
|
|
|
|
},
|
2014-10-24 23:09:50 +02:00
|
|
|
|
2014-05-31 02:07:15 +02:00
|
|
|
_setupDocumentHandlers: function () {
|
|
|
|
if (this._clickHandler) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-24 23:09:50 +02:00
|
|
|
|
2014-05-31 02:07:15 +02:00
|
|
|
var self = this;
|
2014-10-24 23:09:50 +02:00
|
|
|
|
2014-05-31 02:07:15 +02:00
|
|
|
this._clickHandler = function () {
|
|
|
|
return self.bodyClick();
|
|
|
|
};
|
2014-10-24 23:09:50 +02:00
|
|
|
|
2014-05-31 02:07:15 +02:00
|
|
|
return $(this.get('bodyElementSelector')).on('click', this._clickHandler);
|
|
|
|
},
|
2014-10-24 23:09:50 +02:00
|
|
|
|
2014-05-31 02:07:15 +02:00
|
|
|
_removeDocumentHandlers: function () {
|
|
|
|
$(this.get('bodyElementSelector')).off('click', this._clickHandler);
|
|
|
|
this._clickHandler = null;
|
|
|
|
},
|
2014-10-24 23:09:50 +02:00
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/152975/how-to-detect-a-click-outside-an-element
|
2014-05-31 02:07:15 +02:00
|
|
|
click: function (event) {
|
|
|
|
return event.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-10-24 23:09:50 +02:00
|
|
|
export default BodyEventListener;
|