<commit_before>'use strict';

var defineProperties = require('functions/define-properties'),
    onValueUpdate = require('functions/on-value-update'),
    renderTemplate = require('functions/render-template'),
    Tracker = require('classes/tracker');

var storage = window.localStorage;

/**
 * TrackerCollection class
 * @param {object} $        dom elements
 * @param {string} template tracker template string
 * @return {void}
 */
function TrackerCollection($, template) {

    var nextTrackerID = 0,
        trackers = {};

    if (!($.wrap instanceof HTMLDivElement)) throw new Error('invalid container');
    if (!($.list instanceof HTMLUListElement)) throw new Error('invalid list');
    if (typeof template !== 'string') throw new Error('invalid template');

    if (!storage) {
        throw new Error('localStorage not available');
    }

    defineProperties(this, {

        trackers: {
            get: function() {
                return trackers;
            }
        },

        minified: {
            get: function() {
                return Object.keys(trackers).reduce(function(trackerArr, trackerID) {
                    return trackerArr.concat([trackers[trackerID].minified]);
                }, []);
            }
        },

        mergeMode: {
            get: function() {
                return (/(^|\ )trackers-merge-mode(\ |$)/i).test($.wrap.className);
            }
        },

        deleteMode: {
            get: function() {
                return (/(^|\ )trackers-delete-mode(\ |$)/i).test($.wrap.className);
            }
        }

    });

    /**
     * add a tracker to tracker list
     * @param {tracker} tracker tracker instance
     * @param {boolean} store   whether to store the tracker collection after adding or not
     * @return {trackerCollection} this
     */
    this.add = function(tracker, store) {
        var container,
            trackerID = nextTrackerID++;
        store = typeof store === 'boolean' ? store : true;
        tracker = trackers[trackerID] = tracker instanceof Tracker ? tracker : new Tracker();
        container = renderTemplate(template, {
            tracked: tracker.format(),
            description: tracker.description,
            tracking: tracker.tracking,
            id: trackerID
        });
        $.list.insertBefore(container, $.list.firstChild);
        this.addTrackerEvents(tracker, trackerID, {
            container: container,
            tracked: container.querySelector('.tracker-tracked'),
            description: container.querySelector('.tracker-description'),
            toggle: container.querySelector('.tracker-toggle'),
            merge: container.querySelector('.tracker-merge'),
            remove: container.querySelector('.tracker-remove')
        });
        if (store) this.store();
        return this;
    };

    this.remove = function(trackerID, store) {
        store = typeof store === 'boolean' ? store : true;
        if (trackers.hasOwnProperty(trackerID)) {
            delete trackers[trackerID];
            $.list.removeChild(document.getElementById('tracker-' + trackerID));
            if (store) this.store();
        }
        return this;
    };

    this.enableMergeMode = function() {
        this.disableDeleteMode();
        $.wrap.className += ' trackers-merge-mode';
    };

    this.disableMergeMode = function() {
        $.wrap.className = $.wrap.className.replace(/(^|\ )trackers-merge-mode(\ |$)/ig, '$2');
    };

    this.enableDeleteMode = function() {
        this.disableMergeMode();
        $.wrap.className += ' trackers-delete-mode';
    };

    this.disableDeleteMode = function() {
        $.wrap.className = $.wrap.className.replace(/(^|\ )trackers-delete-mode(\ |$)/ig, '$2');
    };

    this.new = function() {
        var trackerID = nextTrackerID;
        this.disableDeleteMode();
        this.disableDeleteMode();
        this.add();
        trackers[trackerID].start();
        document.getElementById('tracker-' + trackerID).querySelector('.tracker-description').focus();
    }.bind(this);

}

/**
 * store tracker in local storage
 * @return {void}
 */
TrackerCollection.prototype.store = function() {
    try {
        storage.setItem('trackers', JSON.stringify(this.minified));
    } catch (err) {
        throw new Error('could not store trackers');
    }
    return this;
};<commit_msg>fix disabling merge mode when adding new tracker<commit_after>'use strict';

var defineProperties = require('functions/define-properties'),
    onValueUpdate = require('functions/on-value-update'),
    renderTemplate = require('functions/render-template'),
    Tracker = require('classes/tracker');

var storage = window.localStorage;

/**
 * TrackerCollection class
 * @param {object} $        dom elements
 * @param {string} template tracker template string
 * @return {void}
 */
function TrackerCollection($, template) {

    var nextTrackerID = 0,
        trackers = {};

    if (!($.wrap instanceof HTMLDivElement)) throw new Error('invalid container');
    if (!($.list instanceof HTMLUListElement)) throw new Error('invalid list');
    if (typeof template !== 'string') throw new Error('invalid template');

    if (!storage) {
        throw new Error('localStorage not available');
    }

    defineProperties(this, {

        trackers: {
            get: function() {
                return trackers;
            }
        },

        minified: {
            get: function() {
                return Object.keys(trackers).reduce(function(trackerArr, trackerID) {
                    return trackerArr.concat([trackers[trackerID].minified]);
                }, []);
            }
        },

        mergeMode: {
            get: function() {
                return (/(^|\ )trackers-merge-mode(\ |$)/i).test($.wrap.className);
            }
        },

        deleteMode: {
            get: function() {
                return (/(^|\ )trackers-delete-mode(\ |$)/i).test($.wrap.className);
            }
        }

    });

    /**
     * add a tracker to tracker list
     * @param {tracker} tracker tracker instance
     * @param {boolean} store   whether to store the tracker collection after adding or not
     * @return {trackerCollection} this
     */
    this.add = function(tracker, store) {
        var container,
            trackerID = nextTrackerID++;
        store = typeof store === 'boolean' ? store : true;
        tracker = trackers[trackerID] = tracker instanceof Tracker ? tracker : new Tracker();
        container = renderTemplate(template, {
            tracked: tracker.format(),
            description: tracker.description,
            tracking: tracker.tracking,
            id: trackerID
        });
        $.list.insertBefore(container, $.list.firstChild);
        this.addTrackerEvents(tracker, trackerID, {
            container: container,
            tracked: container.querySelector('.tracker-tracked'),
            description: container.querySelector('.tracker-description'),
            toggle: container.querySelector('.tracker-toggle'),
            merge: container.querySelector('.tracker-merge'),
            remove: container.querySelector('.tracker-remove')
        });
        if (store) this.store();
        return this;
    };

    this.remove = function(trackerID, store) {
        store = typeof store === 'boolean' ? store : true;
        if (trackers.hasOwnProperty(trackerID)) {
            delete trackers[trackerID];
            $.list.removeChild(document.getElementById('tracker-' + trackerID));
            if (store) this.store();
        }
        return this;
    };

    this.enableMergeMode = function() {
        this.disableDeleteMode();
        $.wrap.className += ' trackers-merge-mode';
    };

    this.disableMergeMode = function() {
        $.wrap.className = $.wrap.className.replace(/(^|\ )trackers-merge-mode(\ |$)/ig, '$2');
    };

    this.enableDeleteMode = function() {
        this.disableMergeMode();
        $.wrap.className += ' trackers-delete-mode';
    };

    this.disableDeleteMode = function() {
        $.wrap.className = $.wrap.className.replace(/(^|\ )trackers-delete-mode(\ |$)/ig, '$2');
    };

    this.new = function() {
        var trackerID = nextTrackerID;
        this.disableDeleteMode();
        this.disableMergeMode();
        this.add();
        trackers[trackerID].start();
        document.getElementById('tracker-' + trackerID).querySelector('.tracker-description').focus();
    }.bind(this);

}

/**
 * store tracker in local storage
 * @return {void}
 */
TrackerCollection.prototype.store = function() {
    try {
        storage.setItem('trackers', JSON.stringify(this.minified));
    } catch (err) {
        throw new Error('could not store trackers');
    }
    return this;
};
{'fraction_non_alphanumeric': 0.1412919424788861, 'fraction_numerical': 0.0006847751654873316, 'mean_word_length': 2.279565868263473, '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': '2547989', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2285, 'n_words': 505}