<commit_before>import ListenerGenerator from '../listeners';
import { getScope, isObject, addClass, removeClass } from '../utils/helpers';

const listenersInstances = [];

const defaultClassNames = {
    touched: 'touched', // the control has been blurred
    untouched: 'untouched', // the control hasn't been blurred
    valid: 'valid', // model is valid
    invalid: 'invalid', // model is invalid
    pristine: 'pristine', // control has not been interacted with
    dirty: 'dirty' // control has been interacted with
};

function addClasses(el, fields, classNames = null) {
    const field = el.getAttribute('name');

    if (!field) {
        return;
    }

    classNames = Object.assign({}, defaultClassNames, classNames);

    const isDirty = fields.dirty(name);
    const isValid = fields.valid(name);

    if (isDirty) {
        addClass(el, classNames.touched);
        removeClass(el, classNames.untouched);
    } else {
        addClass(el, classNames.untouched);
        removeClass(el, classNames.touched);
    }

    if (isValid) {
        addClass(el, classNames.valid);
        removeClass(el, classNames.invalid);
    } else {
        addClass(el, classNames.invalid);
        removeClass(el, classNames.valid);
    }
}

function setDirty(el, classNames) {
    classNames = Object.assign({}, defaultClassNames, classNames);

    addClass(el, classNames.dirty);
    removeClass(el, classNames.pristine);
}

function setPristine(el, classNames) {
    classNames = Object.assign({}, defaultClassNames, classNames);

    addClass(el, classNames.pristine);
    removeClass(el, classNames.dirty);
}

export default (options) => ({
    bind(el, binding, vnode) {
        const listener = new ListenerGenerator(el, binding, vnode, options);
        listener.attach();
        listenersInstances.push({ vm: vnode.context, el, instance: listener });

        if (options.enableAutoClasses) {
            const classNames = options.classNames;

            setPristine(el, classNames);

            el.onfocus = () => {
                setDirty(el, classNames);
            };

            addClasses(el, vnode.context.fields, classNames);
        }
    },
    update(el, { expression, value, oldValue }, { context }) {
        if (options.enableAutoClasses) {
            addClasses(el, context.fields, options.classNames);
        }

        if (! expression || JSON.stringify(value) === JSON.stringify(oldValue)) return;

        const holder = listenersInstances.filter(l => l.vm === context && l.el === el)[0];
        const scope = isObject(value) ? (value.scope || getScope(el)) : getScope(el);

        context.$validator.updateField(
            holder.instance.fieldName,
            isObject(value) ? value.rules : value,
            { scope }
        );
    },
    unbind(el, { value }, { context }) {
        const holder = listenersInstances.filter(l => l.vm === context && l.el === el)[0];
        if (typeof holder === 'undefined') {
            return;
        }

        const scope = isObject(value) ? value.scope : getScope(el);
        context.$validator.detach(holder.instance.fieldName, scope);
        listenersInstances.splice(listenersInstances.indexOf(holder), 1);
    }
});<commit_msg>get proper field name<commit_after>import ListenerGenerator from '../listeners';
import { getScope, isObject, addClass, removeClass } from '../utils/helpers';

const listenersInstances = [];

const defaultClassNames = {
    touched: 'touched', // the control has been blurred
    untouched: 'untouched', // the control hasn't been blurred
    valid: 'valid', // model is valid
    invalid: 'invalid', // model is invalid
    pristine: 'pristine', // control has not been interacted with
    dirty: 'dirty' // control has been interacted with
};

function addClasses(el, fieldName, fields, classNames = null) {
    if (!fieldName) {
        return;
    }

    classNames = Object.assign({}, defaultClassNames, classNames);

    const isDirty = fields.dirty(fieldName);
    const isValid = fields.valid(fieldName);

    if (isDirty) {
        addClass(el, classNames.touched);
        removeClass(el, classNames.untouched);
    } else {
        addClass(el, classNames.untouched);
        removeClass(el, classNames.touched);
    }

    if (isValid) {
        addClass(el, classNames.valid);
        removeClass(el, classNames.invalid);
    } else {
        addClass(el, classNames.invalid);
        removeClass(el, classNames.valid);
    }
}

function setDirty(el, classNames) {
    classNames = Object.assign({}, defaultClassNames, classNames);

    addClass(el, classNames.dirty);
    removeClass(el, classNames.pristine);
}

function setPristine(el, classNames) {
    classNames = Object.assign({}, defaultClassNames, classNames);

    addClass(el, classNames.pristine);
    removeClass(el, classNames.dirty);
}

export default (options) => ({
    bind(el, binding, vnode) {
        const listener = new ListenerGenerator(el, binding, vnode, options);

        listener.attach();
        listenersInstances.push({ vm: vnode.context, el, instance: listener });

        if (options.enableAutoClasses) {
            const classNames = options.classNames;

            setPristine(el, classNames);

            el.onfocus = () => {
                setDirty(el, classNames);
            };

            addClasses(el, listener.fieldName, vnode.context.fields, classNames);
        }
    },
    update(el, { expression, value, oldValue }, { context }) {
        const holder = listenersInstances.filter(l => l.vm === context && l.el === el)[0];
 
        if (options.enableAutoClasses) {
            addClasses(el, holder.instance.fieldName, context.fields, options.classNames);
        }

        if (! expression || JSON.stringify(value) === JSON.stringify(oldValue)) return;

        const scope = isObject(value) ? (value.scope || getScope(el)) : getScope(el);

        context.$validator.updateField(
            holder.instance.fieldName,
            isObject(value) ? value.rules : value,
            { scope }
        );
    },
    unbind(el, { value }, { context }) {
        const holder = listenersInstances.filter(l => l.vm === context && l.el === el)[0];
        if (typeof holder === 'undefined') {
            return;
        }

        const scope = isObject(value) ? value.scope : getScope(el);
        context.$validator.detach(holder.instance.fieldName, scope);
        listenersInstances.splice(listenersInstances.indexOf(holder), 1);
    }
});
{'fraction_non_alphanumeric': 0.13250038562393954, 'fraction_numerical': 0.0009254974548819991, 'mean_word_length': 2.7458116695551706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '7527513', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1829, 'n_words': 423}