<commit_before>"use strict";
/*jslint browser:true, vars:true, white:true, plusplus:true, nomen:true */
/*global  $, Templates*/

function makeAPIRequest(textToTranslate) {
    var params = {
        "lang": "en-ru",
        "text": textToTranslate.trim(),
        "flags": "5"
    };
    return $.get("http://translate.yandex.net/dicservice.json/lookup", params);
}

function getSelectionAttributes(selectionObj) {
    var range = selectionObj.getRangeAt(0);
    range.collapse(false);

    var dummy = document.createElement("span");
    range.insertNode(dummy);
    
    var dummyHeight = $(dummy).outerHeight();
    var coordinates = $(dummy).offset();
    dummy.parentNode.removeChild(dummy);

    return {
        left: coordinates.left,
        top: coordinates.top,
        height: dummyHeight
    };
}

function onWantTranslate(callback) {
    var ctrlCCount = 0;
    var fired = false;
    $(document).keydown(function (event) {
        if (event.which === 67 && event.ctrlKey) {
            ctrlCCount++;
            if(ctrlCCount === 2) {
                fired = true;
            }
        }
        else {
            ctrlCCount = 0;
        }
    });

    $(document).keyup(function (event) {
        if(fired) {
            fired = false;
            ctrlCCount = 0;
            callback();
        }
    });
}

function showPopover(selectionAttributes, outputHTML) {
    $("#ru_nloginov_slovari").remove();
    $("body").append(outputHTML);

    var popover = $("#ru_nloginov_slovari");
    $(document).click(function () {
        popover.hide();
    });

    $(document).keyup(function(event) {
        if(event.keyCode === 27)  { //Esc
            popover.hide();
        }
    });

    var width = popover.outerWidth(true);
    var height = popover.outerHeight(true);

    popover.css('left', selectionAttributes.left - width/2);
    popover.css('top', selectionAttributes.top - height - selectionAttributes.height);
}

function buildViewModel(word, rawData) {
    var result = {"translations": [], "title": word };
    var translations = result.translations;
    if (rawData.def.length > 0) {
        $.each(rawData.def, function(_, definition) {
            $.each(definition.tr, function (_, translation) {
                translations.push({"translation" : translation.text});
            });
       });
    }
    else {
        translations.push({"translation" : "<В словаре не найдено>"});
    }

    return result;
}

onWantTranslate(function () {<commit_msg>FIX: popover disappeared if selection was on top of the screen.<commit_after>"use strict";
/*jslint browser:true, vars:true, white:true, plusplus:true, nomen:true */
/*global  $, Templates*/

function makeAPIRequest(textToTranslate) {
    var params = {
        "lang": "en-ru",
        "text": textToTranslate.trim(),
        "flags": "5"
    };
    return $.get("http://translate.yandex.net/dicservice.json/lookup", params);
}

function getSelectionAttributes(selectionObj) {
    var range = selectionObj.getRangeAt(0);
    range.collapse(false);

    var dummy = document.createElement("span");
    range.insertNode(dummy);
    
    var dummyHeight = $(dummy).outerHeight();
    var coordinates = $(dummy).offset();
    dummy.parentNode.removeChild(dummy);

    return {
        left: coordinates.left,
        top: coordinates.top,
        height: dummyHeight
    };
}

function onWantTranslate(callback) {
    var ctrlCCount = 0;
    var fired = false;
    $(document).keydown(function (event) {
        if (event.which === 67 && event.ctrlKey) {
            ctrlCCount++;
            if(ctrlCCount === 2) {
                fired = true;
            }
        }
        else {
            ctrlCCount = 0;
        }
    });

    $(document).keyup(function (event) {
        if(fired) {
            fired = false;
            ctrlCCount = 0;
            callback();
        }
    });
}

function buildPopoverPosition(selectionAttributes, width, height) {
    var top = selectionAttributes.top - height - selectionAttributes.height;
    var arrowOrientation = "top";
    if(top < 0) {
        top = selectionAttributes.top + selectionAttributes.height;
        arrowOrientation = "bottom";
    }

    return { 
        left: selectionAttributes.left - width/2,
        top: top,
        arrow: arrowOrientation
    };
}

function showPopover(selectionAttributes, outputHTML) {
    $("#ru_nloginov_slovari").remove();
    $("body").append(outputHTML);

    var popover = $("#ru_nloginov_slovari");
    $(document).click(function () {
        popover.hide();
    });

    $(document).keyup(function(event) {
        if(event.keyCode === 27)  { //Esc
            popover.hide();
        }
    });

    var width = popover.outerWidth(true);
    var height = popover.outerHeight(true);
    var popoverPosition = buildPopoverPosition(selectionAttributes, width, height);

    popover.css('left', popoverPosition.left);
    popover.css('top', popoverPosition.top);
    popover.removeClass("top left");
    popover.addClass(popoverPosition.arrow);
}

function buildViewModel(word, rawData) {
    var result = {"translations": [], "title": word };
    var translations = result.translations;
    if (rawData.def.length > 0) {
        $.each(rawData.def, function(_, definition) {
            $.each(definition.tr, function (_, translation) {
                translations.push({"translation" : translation.text});
            });
       });
    }
    else {
        translations.push({"translation" : "<В словаре не найдено>"});
    }

    return result;
}

onWantTranslate(function () {
{'fraction_non_alphanumeric': 0.1450134770889488, 'fraction_numerical': 0.004492362982929021, 'mean_word_length': 2.485284909204759, 'pattern_counts': {'":': 10, '<': 6, '<?xml version=': 0, '>': 7, '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': '2591194', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1628, 'n_words': 314}