All files / ima/debug DevTool.js

68.42% Statements 13/19
37.5% Branches 3/8
42.86% Functions 3/7
68.42% Lines 13/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                  2x             4x                                   2x             2x             2x             2x             2x             2x 2x 1x     2x 2x                                                                       2x  
// @client-side
 
import ns from '../namespace';
import Dispatcher from '../event/Dispatcher';
import EventBus from '../event/EventBus';
import PageManager from '../page/manager/PageManager';
import PageStateManager from '../page/state/PageStateManager';
import Window from '../window/Window';
 
ns.namespace('ima.debug');
 
/**
 * Developer tools, used mostly for navigating the page state history.
 */
export default class DevTool {
  static get $dependencies() {
    return [PageManager, PageStateManager, Window, Dispatcher, EventBus];
  }
 
  /**
	 * Initializes the developer tools.
	 *
	 * @param {PageManager} pageManager Application page manager.
	 * @param {PageStateManager} stateManager Application state manager.
	 * @param {Window} window IMA window wrapper.
	 * @param {Dispatcher} dispatcher IMA event dispatcher.
	 * @param {EventBus} eventBus IMA DOM event bus.
	 */
  constructor(pageManager, stateManager, window, dispatcher, eventBus) {
    /**
		 * Application page manager.
		 *
		 * @type {PageManager}
		 */
    this._pageManager = pageManager;
 
    /**
		 * Application state manager.
		 *
		 * @type {PageStateManager}
		 */
    this._stateManager = stateManager;
 
    /**
		 * IMA window wrapper.
		 *
		 * @type {Window}
		 */
    this._window = window;
 
    /**
		 * IMA event dispatcher.
		 *
		 * @type {Dispatcher}
		 */
    this._dispatcher = dispatcher;
 
    /**
		 * IMA DOM event bus.
		 *
		 * @type {EventBus}
		 */
    this._eventBus = eventBus;
  }
 
  /**
	 * Initializes the developer tools.
	 */
  init() {
    Eif ($Debug) {
      if (this._window.isClient()) {
        this._window.getWindow().$IMA.$DevTool = this;
      }
 
      let window = this._window.getWindow();
      this._window.bindEventListener(window, 'keydown', e => {
        if (e.altKey && e.keyCode === 83) {
          // Alt + S
          console.log(this._stateManager.getState()); //eslint-disable-line no-console
        }
      });
    }
  }
 
  /**
	 * Sets the provided state to the state manager.
	 *
	 * @param {Object<string, *>} statePatch A patch of the current page state.
	 */
  setState(statePatch) {
    this._stateManager.setState(statePatch);
  }
 
  /**
	 * Returns the current page state.
	 *
	 * @return {Object<string, *>} The current page state.
	 */
  getState() {
    return this._stateManager.getState();
  }
 
  /**
	 * Clears the current application state.
	 */
  clearAppSource() {
    this._pageManager.destroy();
    this._dispatcher.clear();
  }
}
 
ns.ima.debug.DevTool = DevTool;