<commit_before>/**
 * Трэкер данных
 * @module components/StateTracker
 */

var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var inherits = require('inherits');
var env = require('slot/env');

module.exports = StateTracker;
function StateTracker(app) {
    if (this instanceof StateTracker) {
        EventEmitter.call(this);
    } else {
        return new StateTracker(app);
    }
}
inherits(StateTracker, EventEmitter);

StateTracker.prototype.resolveURL = function(url) {
    if (this.disabled) return;

    url = url || document.location.pathname;

    if (!history.emulate) { // если у нас не hash url, добавляем query string чтобы не потерялась
        url += location.search;
    }

    return url;
};

/**
 * Добавить состояние в историю браузера и поменять урл
 *
 * @param {String} method (push|replace)
 * @param {Object} newState
 * @param {String} [url]
 * @returns {boolean}
 */
StateTracker.prototype.add = function(method, newState, url) {
    if (this.disabled) return;

    var stop = false;
    var event = {
        method: method,
        state: newState,
        url: url,
        title: typeof document != 'undefined' ? document.title : null,
        cancel: function() {
            stop = true;
        }
    };
    this.emit('beforeAdd', event);
    if (stop || typeof history == 'undefined') return false;

    url = this.resolveURL(event.url);

    history[method + 'State'](_.cloneDeep(event.state), event.title, url);

    this.emit('afterAdd', event);

    this.applyState();
};

/**
 * Прилетел новый стэйт который нужно применить
 */
StateTracker.prototype.applyState = function() {
    var newState = history.state || {};
    this.emit('statechange', newState);
};

StateTracker.prototype.push = function(state, url) {
    this.add('push', state, url);
};

StateTracker.prototype.replace = function(state, url) {
    this.add('replace', state, url);
};

StateTracker.prototype.bind = function() {
    if (typeof window == 'undefined') return false;

    var self = this;

    $(window).on('popstate', function() {
        if (!this.disabled) {
            self.applyState();
        }
    });
    return true;
};

StateTracker.prototype.disable = function() {
    this.disabled = true;
};<commit_msg>Fix. Use correct context in StateTracker.bind method<commit_after>/**
 * Трэкер данных
 * @module components/StateTracker
 */

var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var inherits = require('inherits');
var env = require('slot/env');

module.exports = StateTracker;
function StateTracker(app) {
    if (this instanceof StateTracker) {
        EventEmitter.call(this);
    } else {
        return new StateTracker(app);
    }
}
inherits(StateTracker, EventEmitter);

StateTracker.prototype.resolveURL = function(url) {
    if (this.disabled) return;

    url = url || document.location.pathname;

    if (!history.emulate) { // если у нас не hash url, добавляем query string чтобы не потерялась
        url += location.search;
    }

    return url;
};

/**
 * Добавить состояние в историю браузера и поменять урл
 *
 * @param {String} method (push|replace)
 * @param {Object} newState
 * @param {String} [url]
 * @returns {boolean}
 */
StateTracker.prototype.add = function(method, newState, url) {
    if (this.disabled) return;

    var stop = false;
    var event = {
        method: method,
        state: newState,
        url: url,
        title: typeof document != 'undefined' ? document.title : null,
        cancel: function() {
            stop = true;
        }
    };
    this.emit('beforeAdd', event);
    if (stop || typeof history == 'undefined') return false;

    url = this.resolveURL(event.url);

    history[method + 'State'](_.cloneDeep(event.state), event.title, url);

    this.emit('afterAdd', event);

    this.applyState();
};

/**
 * Прилетел новый стэйт который нужно применить
 */
StateTracker.prototype.applyState = function() {
    var newState = history.state || {};
    this.emit('statechange', newState);
};

StateTracker.prototype.push = function(state, url) {
    this.add('push', state, url);
};

StateTracker.prototype.replace = function(state, url) {
    this.add('replace', state, url);
};

StateTracker.prototype.bind = function() {
    if (typeof window == 'undefined') return false;

    var self = this;
    $(window).on('popstate', function() {
        if (!self.disabled) {
            self.applyState();
        }
    });
    return true;
};

StateTracker.prototype.disable = function() {
    this.disabled = true;
};
{'fraction_non_alphanumeric': 0.143670608848007, 'fraction_numerical': 0.0, 'mean_word_length': 3.1144144144144144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3680750', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1434, 'n_words': 346}