UNPKG

comindware.core.ui

Version:

Comindware Core UI provides the basic components like editors, lists, dropdowns, popups that we so desperately need while creating Marionette-based single-page applications.

65 lines (59 loc) 1.55 kB
import _ from 'underscore'; import Backbone from 'backbone'; const windowEventList = [ { name: 'click', capture: true }, { name: 'keydown', capture: true }, { name: 'keydown', capture: false }, { name: 'mousedown', capture: true }, { name: 'mouseup', capture: true }, { name: 'resize', capture: false }, { name: 'load', capture: false } ]; const globalEventService = /** @lends module:core.services.GlobalEventService */ { initialize() { this.__windowEvents = windowEventList.map(x => { const captureSuffix = x.capture ? ':captured' : ''; const eventName = `window:${x.name}${captureSuffix}`; return { name: x.name, capture: x.capture, handler: e => { // what for pass 2 argument, if first is nested in the second ? this.trigger(eventName, e.target, e); } }; }); this.__windowEvents.forEach(x => { window.addEventListener(x.name, x.handler, x.capture); }); this.listenTo(this, 'window:load', () => (this.pageLoaded = true)); }, onDestroy() { this.__windowEvents.forEach(x => { window.removeEventListener(x.name, x.handler, x.capture); }); } }; _.extend(globalEventService, Backbone.Events); export default globalEventService;