<commit_before>
// TODO we may want to remove this global variable as some point; not sure if it is still used in ref app somewhere
var Navigator = {isReady: false}


// TODO figure out out to set up mocking to test this
function KeyboardController(formElement) {

    var initFormModels = function(formEl) {
        var formElement = formEl;
        if (!formElement) {
            formElement = $('div#content > form').first();
        }
        formElement.prepend('<ul id="formBreadcrumb" class="options"></ul>');
        var breadcrumb = formElement.find('#formBreadcrumb').first();

        var sections = _.map(formElement.find('section'), function(s) {
            return new SectionModel(s, breadcrumb);
        });

        var confirmationSection = new ConfirmationSectionModel($('#confirmation'), breadcrumb, _.clone(sections));
        sections.push(confirmationSection);

        var questions = _.flatten( _.map(sections, function(s) { return s.questions; }), true);
        var fields = _.flatten(_.map(questions, function(q) { return q.fields; }), true);
        return [sections, questions, fields];

    }

    var initKeyboardHandlersChain = function(questions, fields) {
        var questionsHandler = QuestionsKeyboardHandler(questions);
        var fieldsHandler = FieldsKeyboardHandler(fields, questionsHandler);
        return fieldsHandler;
    }

    var initMouseHandlers = function(sections, questions, fields) {
        sectionsMouseHandlerInitializer(sections);
        questionsMouseHandlerInitializer(questions);
        fieldsMouseHandlerInitializer(fields);
    }

    var modelsList = initFormModels(formElement);
    var sections=modelsList[0], questions=modelsList[1], fields=modelsList[2];
    initMouseHandlers(sections, questions, fields);
    var handlerChainRoot = initKeyboardHandlersChain(questions, fields);

    handlerChainRoot.handleTabKey();

    $('body').keydown(function(key) {
        switch(key.keyCode ? key.keyCode : key.which) {
            case 38:
                handlerChainRoot.handleUpKey() && key.preventDefault();
                break;
            case 40:
                handlerChainRoot.handleDownKey() && key.preventDefault();
                break;
            case 27:
                handlerChainRoot.handleEscKey() && key.preventDefault();
                break;
            case 9:
                if(event.shiftKey) {
                    handlerChainRoot.handleShiftTabKey();
                } else {
                    handlerChainRoot.handleTabKey();<commit_msg>Tab between two options will get stuck on one of the options instead of switching back to the other when pressed again - RA-237<commit_after>
// TODO we may want to remove this global variable as some point; not sure if it is still used in ref app somewhere
var Navigator = {isReady: false}


// TODO figure out out to set up mocking to test this
function KeyboardController(formElement) {

    var initFormModels = function(formEl) {
        var formElement = formEl;
        if (!formElement) {
            formElement = $('div#content > form').first();
        }
        formElement.prepend('<ul id="formBreadcrumb" class="options"></ul>');
        var breadcrumb = formElement.find('#formBreadcrumb').first();

        var sections = _.map(formElement.find('section'), function(s) {
            return new SectionModel(s, breadcrumb);
        });

        var confirmationSection = new ConfirmationSectionModel($('#confirmation'), breadcrumb, _.clone(sections));
        sections.push(confirmationSection);

        var questions = _.flatten( _.map(sections, function(s) { return s.questions; }), true);
        var fields = _.flatten(_.map(questions, function(q) { return q.fields; }), true);
        return [sections, questions, fields];

    }

    var initKeyboardHandlersChain = function(questions, fields) {
        var questionsHandler = QuestionsKeyboardHandler(questions);
        var fieldsHandler = FieldsKeyboardHandler(fields, questionsHandler);
        return fieldsHandler;
    }

    var initMouseHandlers = function(sections, questions, fields) {
        sectionsMouseHandlerInitializer(sections);
        questionsMouseHandlerInitializer(questions);
        fieldsMouseHandlerInitializer(fields);
    }

    var modelsList = initFormModels(formElement);
    var sections=modelsList[0], questions=modelsList[1], fields=modelsList[2];
    initMouseHandlers(sections, questions, fields);
    var handlerChainRoot = initKeyboardHandlersChain(questions, fields);

    handlerChainRoot.handleTabKey();

    $('body').keydown(function(key) {
        switch(key.keyCode ? key.keyCode : key.which) {
            case 38:
                handlerChainRoot.handleUpKey() && key.preventDefault();
                break;
            case 40:
                handlerChainRoot.handleDownKey() && key.preventDefault();
                break;
            case 27:
                handlerChainRoot.handleEscKey() && key.preventDefault();
                break;
            case 9:
                if(key.shiftKey) {
                    handlerChainRoot.handleShiftTabKey();
                } else {
                    handlerChainRoot.handleTabKey();
{'fraction_non_alphanumeric': 0.10431100846805234, 'fraction_numerical': 0.004426481909160893, 'mean_word_length': 2.7496392496392494, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '3226197', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1291, 'n_words': 327}