<commit_before>import './example-runner.scss';

import * as angular from 'angular';
import * as jQuery from 'jquery';

import {whenInViewPort, trackIfInViewPort} from './lib/viewport';
import {highlight} from './lib/highlight';

const docs: angular.IModule = angular.module('documentation');

docs.service('HighlightService', function() {
    this.highlight = function(code: string, language: string) {
        return highlight(code, language);
    };
});

docs.directive('snippet', function() {
    return {
        restrict: 'E',
        scope: {
            language: '='
        },
        link: function(scope, element, attrs) {
            whenInViewPort(jQuery(element), function() {
                const language = attrs.language || 'js';
                const highlightedSource = highlight(element.text(), language);
                element.empty().html('<pre><code>' + highlightedSource + '</code></pre>');
            });
        }
    };
});

// taken from https://github.com/angular/angular.js/blob/489835dd0b36a108bedd5ded439a186aca4fa739/docs/app/src/examples.js#L53
docs.factory('formPostData', [
    '$document',
    function($document) {
        return function(url, newWindow, fields) {
            /*
             * If the form posts to target="_blank", pop-up blockers can cause it not to work.
             * If a user choses to bypass pop-up blocker one time and click the link, they will arrive at
             * a new default plnkr, not a plnkr with the desired template.  Given this undesired behavior,
             * some may still want to open the plnk in a new window by opting-in via ctrl+click.  The
             * newWindow param allows for this possibility.
             */
            var target = newWindow ? '_blank' : '_self';
            var form: any = angular.element('<form style="display: none;" method="post" action="' + url + '" target="' + target + '"></form>');
            angular.forEach(fields, function(value, name) {
                var input = angular.element('<input type="hidden" name="' + name + '">');
                input.attr('value', value);
                form.append(input);
            });
            $document.find('body').append(form);
            form[0].submit();
            form.remove();
        };
    }
]);

const ACTIVE_EXAMPLE_RUNNERS = [];

class ExampleRunner {
    private ready: boolean = false;
    private source: any;
    private loadingSource: boolean;
    private showFrameworksDropdown: boolean;
    private selectedTab: string;

    private selectedFile: string;
    private resultUrl: string;

    private files: string[];
    private title: string;
    private section: string;
    private name: string;
    private type: string;
    private currentType: string;
    private boilerplateFiles: string[];
    private boilerplatePath: string;
    sourcePrefix: string;

    private options: {
        showResult?: boolean;
        initialFile?: string;
        exampleHeight?: number;
    };

    private config: any;

    private iframeStyle: any;

    constructor(
        private $http: angular.IHttpService,
        private $timeout: angular.ITimeoutService,
        private $sce: angular.ISCEService,
        private $q: angular.IQService,
        private formPostData,
        private $element: Element,
        private $cookies: angular.cookies.ICookiesService
    ) {
        $http.defaults.cache = true;
    }

    private availableTypes: string[];

    private openFwDropdown: boolean = false;
    private visible: boolean = false;

    toggleFwDropdown() {
        this.openFwDropdown = !this.openFwDropdown;
    }

    hideFwDropdown() {
        this.$timeout(() => (this.openFwDropdown = false), 200);
    }

    $onInit() {
        this.iframeStyle = {};

        const options = this.config.options;

        if (options.exampleHeight) {
            this.iframeStyle.height = options.exampleHeight + 'px';
        }

        this.selectedTab = options.showResult === false ? 'code' : 'result';

        this.title = this.config.title;
        this.name = this.config.name;
        this.section = this.config.section;
        this.showFrameworksDropdown = !options.onlyShow && (this.config.type === 'multi' || this.config.type === 'generated');

        this.availableTypes = options.onlyShow ? [options.onlyShow.toLowerCase()] : Object.keys(this.config.types);

        const divWrapper = jQuery(this.$element).find('div.example-wrapper');

        this.$timeout(() => {
            let visibleToggle: angular.IPromise<void>;
            let nextVisible: boolean = false;

            trackIfInViewPort(divWrapper, visible => {
                this.$timeout(() => {
                    if (visible && !this.visible) {
                        this.visible = true;
                        ACTIVE_EXAMPLE_RUNNERS.push(this);
                        // max active examples is set in the webpack define plugin ./ webpack-config/site.js
                        if (ACTIVE_EXAMPLE_RUNNERS.length > MAX_ACTIVE_EXAMPLES) { 
                            ACTIVE_EXAMPLE_RUNNERS.shift().visible = false;
                        }
                    }
                });
            });
        });

        whenInViewPort(divWrapper, () => {
            this.$timeout(() => {<commit_msg>AG-1162 - added 'showOnly' option to the example runner<commit_after>import './example-runner.scss';

import * as angular from 'angular';
import * as jQuery from 'jquery';

import {whenInViewPort, trackIfInViewPort} from './lib/viewport';
import {highlight} from './lib/highlight';

const docs: angular.IModule = angular.module('documentation');

docs.service('HighlightService', function() {
    this.highlight = function(code: string, language: string) {
        return highlight(code, language);
    };
});

docs.directive('snippet', function() {
    return {
        restrict: 'E',
        scope: {
            language: '='
        },
        link: function(scope, element, attrs) {
            whenInViewPort(jQuery(element), function() {
                const language = attrs.language || 'js';
                const highlightedSource = highlight(element.text(), language);
                element.empty().html('<pre><code>' + highlightedSource + '</code></pre>');
            });
        }
    };
});

// taken from https://github.com/angular/angular.js/blob/489835dd0b36a108bedd5ded439a186aca4fa739/docs/app/src/examples.js#L53
docs.factory('formPostData', [
    '$document',
    function($document) {
        return function(url, newWindow, fields) {
            /*
             * If the form posts to target="_blank", pop-up blockers can cause it not to work.
             * If a user choses to bypass pop-up blocker one time and click the link, they will arrive at
             * a new default plnkr, not a plnkr with the desired template.  Given this undesired behavior,
             * some may still want to open the plnk in a new window by opting-in via ctrl+click.  The
             * newWindow param allows for this possibility.
             */
            var target = newWindow ? '_blank' : '_self';
            var form: any = angular.element('<form style="display: none;" method="post" action="' + url + '" target="' + target + '"></form>');
            angular.forEach(fields, function(value, name) {
                var input = angular.element('<input type="hidden" name="' + name + '">');
                input.attr('value', value);
                form.append(input);
            });
            $document.find('body').append(form);
            form[0].submit();
            form.remove();
        };
    }
]);

const ACTIVE_EXAMPLE_RUNNERS = [];

class ExampleRunner {
    private ready: boolean = false;
    private source: any;
    private loadingSource: boolean;
    private showFrameworksDropdown: boolean;
    private selectedTab: string;

    private selectedFile: string;
    private resultUrl: string;

    private files: string[];
    private title: string;
    private section: string;
    private name: string;
    private type: string;
    private currentType: string;
    private boilerplateFiles: string[];
    private boilerplatePath: string;
    sourcePrefix: string;

    private options: {
        showResult?: boolean;
        initialFile?: string;
        exampleHeight?: number;
    };

    private config: any;

    private iframeStyle: any;

    constructor(
        private $http: angular.IHttpService,
        private $timeout: angular.ITimeoutService,
        private $sce: angular.ISCEService,
        private $q: angular.IQService,
        private formPostData,
        private $element: Element,
        private $cookies: angular.cookies.ICookiesService
    ) {
        $http.defaults.cache = true;
    }

    private availableTypes: string[];

    private openFwDropdown: boolean = false;
    private visible: boolean = false;

    toggleFwDropdown() {
        this.openFwDropdown = !this.openFwDropdown;
    }

    hideFwDropdown() {
        this.$timeout(() => (this.openFwDropdown = false), 200);
    }

    $onInit() {
        this.iframeStyle = {};

        const options = this.config.options;

        if (options.exampleHeight) {
            this.iframeStyle.height = options.exampleHeight + 'px';
        }

        this.selectedTab = options.showResult === false ? 'code' : 'result';

        this.title = this.config.title;
        this.name = this.config.name;
        this.section = this.config.section;
        this.showFrameworksDropdown = !options.showOnly && (this.config.type === 'multi' || this.config.type === 'generated');

        this.availableTypes = options.showOnly ? [options.showOnly.toLowerCase()] : Object.keys(this.config.types);

        const divWrapper = jQuery(this.$element).find('div.example-wrapper');

        this.$timeout(() => {
            let visibleToggle: angular.IPromise<void>;
            let nextVisible: boolean = false;

            trackIfInViewPort(divWrapper, visible => {
                this.$timeout(() => {
                    if (visible && !this.visible) {
                        this.visible = true;
                        ACTIVE_EXAMPLE_RUNNERS.push(this);
                        // max active examples is set in the webpack define plugin ./ webpack-config/site.js
                        if (ACTIVE_EXAMPLE_RUNNERS.length > MAX_ACTIVE_EXAMPLES) { 
                            ACTIVE_EXAMPLE_RUNNERS.shift().visible = false;
                        }
                    }
                });
            });
        });

        whenInViewPort(divWrapper, () => {
            this.$timeout(() => {
{'fraction_non_alphanumeric': 0.12190979430081147, 'fraction_numerical': 0.00585016040762408, 'mean_word_length': 2.203082502266546, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '6523342', 'n_tokens_mistral': 2945, 'n_tokens_neox': 2812, 'n_words': 703}