<commit_before>/**
 * @copyright Prometheus Research, LLC 2014
 */
'use strict';

var Immutable = require('immutable');

var state = Immutable.Map();

function getComponentFingerprint(component) {
  return `${component._rootNodeID}__${component._mountDepth}`;
}

var ScopeMixin = {

  componentWillMount() {
    FocusStore.registerScope(this);
  },

  componentWillUnmount() {
    FocusStore.unregisterScope(this);
  },

  focusElement(keyPath) {
    FocusStore.focusElement(this, keyPath);
  }
  
};

var FocusableMixin = {

  componentDidMount() {
    var scope = this.getFocusScope();
    var keyPath = this.getKeyPath();
    FocusStore.registerFocusable(scope, keyPath, this);
  },

  componentWillUnmount() {
    var scope = this.getFocusScope();
    var keyPath = this.getKeyPath();
    FocusStore.unregisterFocusable(scope, keyPath, this);
  }
};

var FocusStore = {

  ScopeMixin,
  FocusableMixin,

  registerScope(scopeComponent) {
    var scopeID = getComponentFingerprint(scopeComponent);
    state = state.set(scopeID, Immutable.Map());
  },

  unregisterScope(scopeComponent) {
    var scopeID = getComponentFingerprint(scopeComponent);
    state = state.remove(scopeID);
  },

  registerFocusable(scopeComponent, keyPath, focusableComponent) {
    var key = keyPath.join('.');
    var scopeID = getComponentFingerprint(scopeComponent);
    state = state.update(scopeID, scope => scope.set(key, focusableComponent));
  },

  unregisterFocusable(scopeComponent, keyPath, focusableComponent) {
    var key = keyPath.join('.');
    var scopeID = getComponentFingerprint(scopeComponent);
    state = state.updateIn(scopeID, scope => scope && scope.remove(key));
  },

  focusElement(scopeComponent, keyPath) {
    var key = keyPath.join('.');
    var scopeID = getComponentFingerprint(scopeComponent);
    console.log(scopeID, key);
    console.log(state.toJS())
    var component = state.getIn([scopeID, key]);
    if (typeof component.focus === 'function') {
      component.focus();
    } else {
      var node = component.getDOMNode();
      if (node) {
        node.focus();
      }
    }
  },

  _getState() {
    return state;
  }

};

window.FocusStore = module.exports = FocusStore;<commit_msg>FocusStore: gardening<commit_after>/**
 * @copyright Prometheus Research, LLC 2014
 */
'use strict';

var Immutable = require('immutable');
var invariant = require('./invariant');

var state = Immutable.Map();

function getComponentFingerprint(component) {
  return `${component._rootNodeID}__${component._mountDepth}`;
}

var ScopeMixin = {

  componentWillMount() {
    FocusStore.registerScope(this);
  },

  componentWillUnmount() {
    FocusStore.unregisterScope(this);
  },

  /**
   * Put focus on an element.
   *
   * @param {Array<String>} keyPath
   */
  focusElement(keyPath) {
    FocusStore.focusElement(this, keyPath);
  }
  
};

var FocusableMixin = {

  componentDidMount() {
    invariant(
      typeof this.getFocusScope === 'function',
      'ReactForms.FocusStore.FocusableMixin: component should implement getFocusScope()'
    );
    invariant(
      typeof this.getKeyPath === 'function',
      'ReactForms.FocusStore.FocusableMixin: component should implement getKeyPath()'
    );
    var scope = this.getFocusScope();
    var keyPath = this.getKeyPath();
    FocusStore.registerFocusable(scope, keyPath, this);
  },

  componentWillUnmount() {
    var scope = this.getFocusScope();
    var keyPath = this.getKeyPath();
    FocusStore.unregisterFocusable(scope, keyPath, this);
  }
};

var FocusStore = {

  ScopeMixin,
  FocusableMixin,

  registerScope(scopeComponent) {
    var scopeID = getComponentFingerprint(scopeComponent);
    state = state.set(scopeID, Immutable.Map());
  },

  unregisterScope(scopeComponent) {
    var scopeID = getComponentFingerprint(scopeComponent);
    state = state.remove(scopeID);
  },

  registerFocusable(scopeComponent, keyPath, focusableComponent) {
    var key = keyPath.join('.');
    var scopeID = getComponentFingerprint(scopeComponent);
    state = state.update(scopeID, scope => scope.set(key, focusableComponent));
  },

  unregisterFocusable(scopeComponent, keyPath, focusableComponent) {
    var key = keyPath.join('.');
    var scopeID = getComponentFingerprint(scopeComponent);
    state = state.updateIn(scopeID, scope => scope && scope.remove(key));
  },

  focusElement(scopeComponent, keyPath) {
    var key = keyPath.join('.');
    var scopeID = getComponentFingerprint(scopeComponent);
    var component = state.getIn([scopeID, key]);
    if (typeof component.focus === 'function') {
      component.focus();
    } else {
      var node = component.getDOMNode();
      if (node) {
        node.focus();
      }
    }
  },

  _getState() {
    return state;
  }

};

window.FocusStore = module.exports = FocusStore;
{'fraction_non_alphanumeric': 0.1314475873544093, 'fraction_numerical': 0.0016638935108153079, 'mean_word_length': 3.8575757575757574, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2800980', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1463, 'n_words': 280}