github.com/outbrain/consul@v1.4.5/ui-v2/app/mixins/click-outside.js (about) 1 import Mixin from '@ember/object/mixin'; 2 3 import { next } from '@ember/runloop'; 4 import { get } from '@ember/object'; 5 const isOutside = function(element, e) { 6 if (element) { 7 const isRemoved = !e.target || !document.contains(e.target); 8 const isInside = element === e.target || element.contains(e.target); 9 return !isRemoved && !isInside; 10 } else { 11 return false; 12 } 13 }; 14 const handler = function(e) { 15 const el = get(this, 'element'); 16 if (isOutside(el, e)) { 17 this.onblur(e); 18 } 19 }; 20 export default Mixin.create({ 21 init: function() { 22 this._super(...arguments); 23 this.handler = handler.bind(this); 24 }, 25 onchange: function() {}, 26 onblur: function() {}, 27 didInsertElement: function() { 28 this._super(...arguments); 29 next(this, () => { 30 document.addEventListener('click', this.handler); 31 }); 32 }, 33 willDestroyElement: function() { 34 this._super(...arguments); 35 document.removeEventListener('click', this.handler); 36 }, 37 });