<commit_before>define('views/homepage',
    ['apps', 'core/capabilities', 'core/l10n', 'core/models', 'core/z',
     'templates', 'image_helper', 'smart_button', 'spatial_navigation'],
    function(apps, caps, l10n, models, z,
             nunjucks, imageHelper, smartButton, SpatialNavigation) {
    var gettext = l10n.gettext;
    var appsModel = models('apps');

    var $appPreview;
    var $appList;
    var $appContextMenuItem;

    var appListHeight;

    function findLargestIcon(icons) {
        var iconSizes = Object.keys(icons);
        var maxIconSize = iconSizes.reduce(function(prev, current) {
            return parseInt(prev, 10) < parseInt(current, 10) ? current : prev;
        });

        return icons[maxIconSize];
    }

    // Prevent back button so the website will not go back to the tutorial page.
    window.addEventListener('keydown', function(e) {
        if (e.keyCode === window.KeyEvent.DOM_VK_BACK_SPACE ||
            e.key === 'Backspace') {
            if (z.page.find('.app-preview').length) {
                e.preventDefault();
            }
        }
    });

    z.page.on('loaded reloaded_chrome', function() {
        if (z.page.find('.app-preview').length) {
            $appPreview = z.page.find('.app-preview');
            $appList = z.page.find('.app-list');
            $appContextMenuItem = z.page.find('.contextmenu-item');

            appListHeight = $appList.innerHeight();

            SpatialNavigation.startFocus();
        }
    });

    z.page.on('sn:willfocus', '.app-button', function() {
        SpatialNavigation.pause();

        var callback = function() {
            SpatialNavigation.focus(this);
            SpatialNavigation.resume();
        };

        var scrollTop = $appList.scrollTop();
        var top = this.offsetTop;
        var bottom = top + this.offsetHeight;
        var margin = 5.8 * 10;
        var newPosition;

        if (scrollTop > top - margin) {
            newPosition = top - margin;
        } else if (scrollTop < bottom + margin - appListHeight) {
            newPosition = bottom + margin - appListHeight;
        }
<commit_msg>Bug 1244000 - Fix scrolling when screen size is not ideal size<commit_after>define('views/homepage',
    ['apps', 'core/capabilities', 'core/l10n', 'core/models', 'core/z',
     'templates', 'image_helper', 'smart_button', 'spatial_navigation'],
    function(apps, caps, l10n, models, z,
             nunjucks, imageHelper, smartButton, SpatialNavigation) {
    var gettext = l10n.gettext;
    var appsModel = models('apps');

    var $appPreview;
    var $appList;
    var $appContextMenuItem;

    function findLargestIcon(icons) {
        var iconSizes = Object.keys(icons);
        var maxIconSize = iconSizes.reduce(function(prev, current) {
            return parseInt(prev, 10) < parseInt(current, 10) ? current : prev;
        });

        return icons[maxIconSize];
    }

    // Prevent back button so the website will not go back to the tutorial page.
    window.addEventListener('keydown', function(e) {
        if (e.keyCode === window.KeyEvent.DOM_VK_BACK_SPACE ||
            e.key === 'Backspace') {
            if (z.page.find('.app-preview').length) {
                e.preventDefault();
            }
        }
    });

    z.page.on('loaded reloaded_chrome', function() {
        if (z.page.find('.app-preview').length) {
            $appPreview = z.page.find('.app-preview');
            $appList = z.page.find('.app-list');
            $appContextMenuItem = z.page.find('.contextmenu-item');

            SpatialNavigation.startFocus();
        }
    });

    z.page.on('sn:willfocus', '.app-button', function() {
        SpatialNavigation.pause();

        var callback = function() {
            SpatialNavigation.focus(this);
            SpatialNavigation.resume();
        };

        var appListScrollTop = $appList.scrollTop();
        var appListHeight = $appList.innerHeight();

        var top = this.offsetTop;
        var height = this.offsetHeight;
        var margin = 5.8 * 10;

        var appTop = top - margin;
        var appBottom = top + height + margin - appListHeight;
        var appHeight = height + margin * 2;

        var newPosition;

        if (appListHeight < appHeight) {
            // Current scope height is too narrow.
            // Scroll to the center of the app.
            newPosition = appTop + (appHeight - appListHeight) / 2;
        } else if (appListScrollTop >= appTop) {
            // App is above current scope.
            newPosition = appTop;
        } else if (appListScrollTop < appBottom) {
            // App is below current scope.
            newPosition = appBottom;
        }

{'fraction_non_alphanumeric': 0.12923864363403711, 'fraction_numerical': 0.007890808274685433, 'mean_word_length': 2.160377358490566, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, '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': '3373045', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1343, 'n_words': 311}