<commit_before>define('feed_previews',
    ['collection_colors', 'feed', 'l10n', 'templates', 'utils_local', 'z'],
    function(colors, feed, l10n, nunjucks, utils, z) {
    var colors = colors.COLLECTION_COLORS;
    var gettext = l10n.gettext;

    // Constants are constant.
    var THUMB = 'https://marketplace.cdn.mozilla.net/img/uploads/addon_icons/461/461685-64.png';
    var SAMPLE_BG = '/media/img/sample_bg.jpg';

    String.prototype.escape = function() {
        var tagsToReplace = {
            '<': '&lt;',
            '>': '&gt;'
        };
        return this.replace(/[&<>]/g, function(tag) {
            return tagsToReplace[tag] || tag;
        });
    };

    function app_factory() {
        return $.extend({
            name: 'A Sample App',
            author: '<NAME>',
            icons: {
                64: THUMB
            },
            ratings: {
                average: 3
            },
            price: '$0.81',
            price_locale: '$0.81',
            slug: 'test-slug',
            url: '#',
        }, $('[name="app"]').data('app') || {});
    }

    function multi_app_factory() {
        var apps = [];
        var $results = $('.apps-widget .result:not(.app-group)');

        if (!$results.length) {
            return apps;
        }

        var grouped = $('.app-group').length;

        $results.each(function(i) {
            var $this = $(this);

            var app = {
                author: $this.find('.author').text(),
                icons: {64: $this.find('.icon').attr('src')},
                name: $this.find('a.name').text(),
                ratings: {
                    average: $this.data('rating')
                },
                price: $this.data('price'),
                price_locale: $this.data('price'),
                slug: 'test-slug',
                url: '#',
            };

            if (grouped) {
                app.group = $($this.prevAll('.app-group')[0]).find(
                    'input.localized:not(.hidden)').val();
            }

            apps.push(app);
        });

        return apps;
    }

    function collection_apps_factory(n) {
        var apps = multi_app_factory();
        if (!apps.length) {
            apps = [];
            for (var i = 0; i < n; i++ ) {
                apps.push(app_factory());
            }
        }
        return apps;
    }

    function preview_factory() {
        return {
            thumbnail_size: $('.screenshots li.selected .screenshot').data('image-size'),
            thumbnail_url: $('.screenshots li.selected a').attr('href'),
        };
    }

    function feed_app_factory() {
        return {
            app: app_factory(),
            background_color: $('.bg-color input:checked').data('color'),
            background_image: $('.background-image-input .preview').attr('src'),
            description: $('.description .localized:not(.hidden').val().escape() || '',
            preview: preview_factory(),
            pullquote_attribution: $('[name="pq-attribution"]').val().escape() || '',
            pullquote_rating: $('.pq-rating input:checked').val() || 0,
            pullquote_text: $('.pq-text .localized:not(.hidden').val().escape() || '',
            type: $('.featured-type-choices').val() || 'icon',
        };
    }

    function brand_factory() {
        var apps = collection_apps_factory(feed.MAX_BRAND_APPS + 1);
        return {
            apps: apps,
            app_count: apps.length,
            layout: $('#brand-layout').val() || 'grid',
            type: $('#brand-type').val() || 'apps-for-albania',
        };
    }

    function collection_factory() {
        var apps = collection_apps_factory(feed.MAX_BRAND_APPS + 1);
        return {
            apps: apps,
            app_count: apps.length,
            background_color: $('.bg-color input:checked').data('color'),
            background_image: $('.background-image-input .preview').attr('src'),
            description: $('.description .localized:not(.hidden').val().escape() || '',
            name: $('.name .localized:not(.hidden').val().escape() || 'A Sample Collection',
            type: $('.collection-type-choices').val()
        };
    }

    function shelf_factory() {
        var apps = collection_apps_factory(feed.MAX_BRAND_APPS + 1);
        return {
            apps: apps,
            app_count: apps.length,
            background_color: $('.bg-color input:checked').val(),
            background_image: $('.bg-banner .preview').attr('src') || SAMPLE_BG,
            background_image_landing: $('.bg-banner-landing .preview').attr('src') || SAMPLE_BG,
            carrier: $('.carrier select').val(),
            description: $('.description .localized:not(.hidden').val().escape() || '',
            name: $('.name .localized:not(.hidden').val().escape() || 'A Sample Shelf',
            region: $('.region select').val(),
            type: $('.collection-type-choices input:checked').val() || feed.COLL_PROMO,
        };
    }

    // Listeners.
    z.page.on('change keyup input', 'input, textarea, select', _.throttle(refresh, 250));
    z.page.on('refresh_preview aviary_saved', _.throttle(refresh, 250));

    function refresh() {
        empty();

        var type = $('.transonic-form').data('type');
        if (type == 'apps') {
            refresh_preview(feed_app_factory(), 'app');
        } else if (type == 'brands') {
            refresh_preview(brand_factory(), 'brand');
        } else if (type == 'collections') {
            refresh_preview(collection_factory(), 'collection');
        } else if (type == 'shelves') {
            refresh_preview(shelf_factory(), 'shelf');
        } else if ($('.feed-builder').length) {
            refresh_feed_preview();
        }
    }

    function refresh_feed_preview() {
        stub_globals();

        $('.region-feed:not(.hidden) .feed-elements li').each(function(i, feed_element) {
            var $feed_element = $(feed_element);
            var type = $feed_element.data('type');
            var slug = $feed_element.data('slug');

            var feed_element = require('models')('feed-' + type).lookup(slug);

            // Polyfill collection colors when we were hardcoding hexes.
            if (!feed_element.background_color && feed_element.color) {
                feed_element.background_color = colors[feed_element.color] ||
                                                colors.sapphire;
            }

            $('.feed').append(
                nunjucks.env.render('feed_item_preview.html', {
                    cast_app: function() {},
                    obj: feed_element,
                    item_type: type,
                    url: function() {return '#';}
                })
            );
        });
        load_images();
    }

    function refresh_preview(obj, item_type) {
        stub_globals();<commit_msg>fix shelf color in previews (bug 1133767)<commit_after>define('feed_previews',
    ['collection_colors', 'feed', 'l10n', 'models', 'templates', 'utils_local',
     'z'],
    function(colors, feed, l10n, models, nunjucks, utils,
             z) {
    var colors = colors.COLLECTION_COLORS;
    var gettext = l10n.gettext;

    // Constants are constant.
    var THUMB = 'https://marketplace.cdn.mozilla.net/img/uploads/addon_icons/461/461685-64.png';
    var SAMPLE_BG = '/media/img/sample_bg.jpg';

    String.prototype.escape = function() {
        var tagsToReplace = {
            '<': '&lt;',
            '>': '&gt;'
        };
        return this.replace(/[&<>]/g, function(tag) {
            return tagsToReplace[tag] || tag;
        });
    };

    function app_factory() {
        return $.extend({
            name: 'A Sample App',
            author: '<NAME>',
            icons: {
                64: THUMB
            },
            ratings: {
                average: 3
            },
            price: '$0.81',
            price_locale: '$0.81',
            slug: 'test-slug',
            url: '#',
        }, $('[name="app"]').data('app') || {});
    }

    function multi_app_factory() {
        var apps = [];
        var $results = $('.apps-widget .result:not(.app-group)');

        if (!$results.length) {
            return apps;
        }

        var grouped = $('.app-group').length;

        $results.each(function(i) {
            var $this = $(this);

            var app = {
                author: $this.find('.author').text(),
                icons: {64: $this.find('.icon').attr('src')},
                name: $this.find('a.name').text(),
                ratings: {
                    average: $this.data('rating')
                },
                price: $this.data('price'),
                price_locale: $this.data('price'),
                slug: 'test-slug',
                url: '#',
            };

            if (grouped) {
                app.group = $($this.prevAll('.app-group')[0]).find(
                    'input.localized:not(.hidden)').val();
            }

            apps.push(app);
        });

        return apps;
    }

    function collection_apps_factory(n) {
        var apps = multi_app_factory();
        if (!apps.length) {
            apps = [];
            for (var i = 0; i < n; i++ ) {
                apps.push(app_factory());
            }
        }
        return apps;
    }

    function preview_factory() {
        return {
            thumbnail_size: $('.screenshots li.selected .screenshot').data('image-size'),
            thumbnail_url: $('.screenshots li.selected a').attr('href'),
        };
    }

    function feed_app_factory() {
        return {
            app: app_factory(),
            background_color: $('.bg-color input:checked').data('color'),
            background_image: $('.background-image-input .preview').attr('src'),
            description: $('.description .localized:not(.hidden').val().escape() || '',
            preview: preview_factory(),
            pullquote_attribution: $('[name="pq-attribution"]').val().escape() || '',
            pullquote_rating: $('.pq-rating input:checked').val() || 0,
            pullquote_text: $('.pq-text .localized:not(.hidden').val().escape() || '',
            type: $('.featured-type-choices').val() || 'icon',
        };
    }

    function brand_factory() {
        var apps = collection_apps_factory(feed.MAX_BRAND_APPS + 1);
        return {
            apps: apps,
            app_count: apps.length,
            layout: $('#brand-layout').val() || 'grid',
            type: $('#brand-type').val() || 'apps-for-albania',
        };
    }

    function collection_factory() {
        var apps = collection_apps_factory(feed.MAX_BRAND_APPS + 1);
        return {
            apps: apps,
            app_count: apps.length,
            background_color: $('.bg-color input:checked').data('color'),
            background_image: $('.background-image-input .preview').attr('src'),
            description: $('.description .localized:not(.hidden').val().escape() || '',
            name: $('.name .localized:not(.hidden').val().escape() || 'A Sample Collection',
            type: $('.collection-type-choices').val()
        };
    }

    function shelf_factory() {
        var apps = collection_apps_factory(feed.MAX_BRAND_APPS + 1);
        return {
            apps: apps,
            app_count: apps.length,
            background_color: $('.bg-color input:checked').val(),
            background_image: $('.bg-banner .preview').attr('src') || SAMPLE_BG,
            background_image_landing: $('.bg-banner-landing .preview').attr('src') || SAMPLE_BG,
            carrier: $('.carrier select').val(),
            description: $('.description .localized:not(.hidden').val().escape() || '',
            name: $('.name .localized:not(.hidden').val().escape() || 'A Sample Shelf',
            region: $('.region select').val(),
            type: $('.collection-type-choices input:checked').val() || feed.COLL_PROMO,
        };
    }

    // Listeners.
    z.page.on('change keyup input', 'input, textarea, select', _.throttle(refresh, 250));
    z.page.on('refresh_preview aviary_saved', _.throttle(refresh, 250));

    function refresh() {
        empty();

        var type = $('.transonic-form').data('type');
        if (type == 'apps') {
            refresh_preview(feed_app_factory(), 'app');
        } else if (type == 'brands') {
            refresh_preview(brand_factory(), 'brand');
        } else if (type == 'collections') {
            refresh_preview(collection_factory(), 'collection');
        } else if (type == 'shelves') {
            refresh_preview(shelf_factory(), 'shelf');
        } else if ($('.feed-builder').length) {
            refresh_feed_preview();
        }
    }

    function refresh_feed_preview() {
        stub_globals();

        $('.region-feed:not(.hidden) .feed-elements li').each(function(i, feed_element) {
            var $feed_element = $(feed_element);
            var type = $feed_element.data('type');
            var slug = $feed_element.data('slug');

            feed_element = models('feed-' + type).lookup(slug);

            // Polyfill collection colors when we were hardcoding hexes.
            if (type != 'shelf' && !feed_element.background_color &&
                feed_element.color) {
                feed_element.background_color = colors[feed_element.color] ||
                                                colors.sapphire;
            }

            $('.feed').append(
                nunjucks.env.render('feed_item_preview.html', {
                    cast_app: function() {},
                    obj: feed_element,
                    item_type: type,
                    url: function() {return '#';}
                })
            );
        });
        load_images();
    }

    function refresh_preview(obj, item_type) {
        stub_globals();
{'fraction_non_alphanumeric': 0.16844997108155002, 'fraction_numerical': 0.006289762868710237, 'mean_word_length': 1.7877871825876663, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1529321', 'n_tokens_mistral': 4076, 'n_tokens_neox': 3831, 'n_words': 706}