<commit_before>/*global PDFJS, jQuery, _*/
/*jslint nomen: true, vars: true*/
function PowerPointScript($scope, $q, $timeout) {
    'use strict';

    /*temporary testing*/
    var content = $scope.referenceSlide.content,
        url = '/pdfProxy/' + encodeURIComponent(content.url),
        pageRendering = false,
        pageNr = 1,
        pageNrPending = null,
        canvas,
        ctx,
        pdfDoc;

    if (content.pdfDoc) {
        pdfDoc = content.pdfDoc;
    }

    /**
    * Get page info from document, resize canvas accordingly, and render page.
    * @param num Page number.
    */
    function renderPage(num, callback) {
        pageRendering = true;
        // Using promise to fetch the page
        pdfDoc.getPage(num).then(function (page) {
            var viewport = page.getViewport(1);
            canvas.height = viewport.height;
            canvas.width = viewport.width;

            // Render PDF page into canvas context
            var renderContext = {
                canvasContext: ctx,
                viewport: viewport
            };
            var renderTask = page.render(renderContext);

            // Wait for rendering to finish
            renderTask.promise.then(function () {
                //alert('rendering finished for page: ' + num);
                pageRendering = false;
                if (pageNrPending !== null) {
                    // New page rendering is pending
                    renderPage(pageNrPending, callback);
                    pageNrPending = null;
                } else if (callback) {
                    callback();
                }
            });
        });
    }

    /**
    * If another page rendering in progress, waits until the rendering is
    * finised. Otherwise, executes rendering immediately.
    */
    function queueRenderPage(num) {
        if (pageRendering) {
            pageNrPending = num;
        } else {
            renderPage(num);
        }
    }

    /**
    * Displays previous page.
    */
    $scope.onPrevPage = function () {
        if (pageNr <= 1) {
            return;
        }
        pageNr = pageNr - 1;
        queueRenderPage(pageNr);
    };

    /**
    * Displays next page.
    */
    $scope.onNextPage = function () {
        if (pageNr >= pdfDoc.numPages) {
            return;
        }
        pageNr = pageNr + 1;
        queueRenderPage(pageNr);
    };

    function applyScopeIfNotPhase() {
        if (!$scope.$$phase) {
            $scope.$apply();
        }
    }

    function initPage(rootElement, callback) {
        canvas = jQuery(rootElement).find('#the-canvas')[0];
        ctx = canvas.getContext('2d');
        renderPage(pageNr, function () {
            applyScopeIfNotPhase();
            if (callback) {
                callback();
            }
        });
    }

    var ensurePdfIsLoaded = function () {
        if (pdfDoc) {
            return;
        }

        PDFJS.workerSrc = '/modules/slideshows/slideTemplates/PowerPointOnline/pdf.worker.js';
        PDFJS.getDocument(url).then(function (pdfDoc_) {
            pdfDoc = pdfDoc_;
            $scope.pdfDocLoaded = true;
            applyScopeIfNotPhase();
        });
    };

    function pdfScriptLoaded() {
        return _.includes($scope.loadedScripts, 'modules/slideshows/slideTemplates/PowerPointOnline/pdf.js');
    }

    if (pdfScriptLoaded()) {
        ensurePdfIsLoaded();
    } else {
        $scope.$watch("loadedScripts.length", function (event, loadedScripts) {
            if (pdfScriptLoaded()) {
                ensurePdfIsLoaded();
            }
        });
    }

    function executeExpansion (callback) {
        var idx;
        var expandedSlidesSpecificContents = [];
        for (idx = 1; idx <= pdfDoc.numPages; idx = idx + 1) {
            expandedSlidesSpecificContents.push({pageNr: idx, isExpanded: true, pdfDoc: pdfDoc});
        }

        callback(expandedSlidesSpecificContents);
    }
<commit_msg>fixed bug when no content for PDF template<commit_after>/*global PDFJS, jQuery, _*/
/*jslint nomen: true, vars: true*/
function PowerPointScript($scope, $q, $timeout) {
    'use strict';

    /*temporary testing*/

    var content = $scope.referenceSlide.content || {},
        url = '/pdfProxy/' + encodeURIComponent(content.url),
        pageRendering = false,
        pageNr = 1,
        pageNrPending = null,
        canvas,
        ctx,
        pdfDoc;

    if (content.pdfDoc) {
        pdfDoc = content.pdfDoc;
    }

    /**
    * Get page info from document, resize canvas accordingly, and render page.
    * @param num Page number.
    */
    function renderPage(num, callback) {
        pageRendering = true;
        // Using promise to fetch the page
        pdfDoc.getPage(num).then(function (page) {
            var viewport = page.getViewport(1);
            canvas.height = viewport.height;
            canvas.width = viewport.width;

            // Render PDF page into canvas context
            var renderContext = {
                canvasContext: ctx,
                viewport: viewport
            };
            var renderTask = page.render(renderContext);

            // Wait for rendering to finish
            renderTask.promise.then(function () {
                //alert('rendering finished for page: ' + num);
                pageRendering = false;
                if (pageNrPending !== null) {
                    // New page rendering is pending
                    renderPage(pageNrPending, callback);
                    pageNrPending = null;
                } else if (callback) {
                    callback();
                }
            });
        });
    }

    /**
    * If another page rendering in progress, waits until the rendering is
    * finised. Otherwise, executes rendering immediately.
    */
    function queueRenderPage(num) {
        if (pageRendering) {
            pageNrPending = num;
        } else {
            renderPage(num);
        }
    }

    /**
    * Displays previous page.
    */
    $scope.onPrevPage = function () {
        if (pageNr <= 1) {
            return;
        }
        pageNr = pageNr - 1;
        queueRenderPage(pageNr);
    };

    /**
    * Displays next page.
    */
    $scope.onNextPage = function () {
        if (pageNr >= pdfDoc.numPages) {
            return;
        }
        pageNr = pageNr + 1;
        queueRenderPage(pageNr);
    };

    function applyScopeIfNotPhase() {
        if (!$scope.$$phase) {
            $scope.$apply();
        }
    }

    function initPage(rootElement, callback) {
        canvas = jQuery(rootElement).find('#the-canvas')[0];
        ctx = canvas.getContext('2d');
        renderPage(pageNr, function () {
            applyScopeIfNotPhase();
            if (callback) {
                callback();
            }
        });
    }

    var ensurePdfIsLoaded = function () {
        if (pdfDoc) {
            return;
        }

        PDFJS.workerSrc = '/modules/slideshows/slideTemplates/PowerPointOnline/pdf.worker.js';
        if (!content.url) {
            return;
        }
        PDFJS.getDocument(url).then(function (pdfDoc_) {
            pdfDoc = pdfDoc_;
            $scope.pdfDocLoaded = true;
            applyScopeIfNotPhase();
        });
    };

    function pdfScriptLoaded() {
        return _.includes($scope.loadedScripts, 'modules/slideshows/slideTemplates/PowerPointOnline/pdf.js');
    }

    if (pdfScriptLoaded()) {
        ensurePdfIsLoaded();
    } else {
        $scope.$watch("loadedScripts.length", function (event, loadedScripts) {
            if (pdfScriptLoaded()) {
                ensurePdfIsLoaded();
            }
        });
    }

    function executeExpansion (callback) {
        var idx;
        var expandedSlidesSpecificContents = [];
        for (idx = 1; idx <= pdfDoc.numPages; idx = idx + 1) {
            expandedSlidesSpecificContents.push({pageNr: idx, isExpanded: true, pdfDoc: pdfDoc});
        }

        callback(expandedSlidesSpecificContents);
    }

{'fraction_non_alphanumeric': 0.11204306459689534, 'fraction_numerical': 0.002253380070105158, 'mean_word_length': 1.6603396603396603, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1115863', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2182, 'n_words': 480}