Vertafore/docular

Possible RTE in docsPathWatchAction

bvaughn opened this issue · 2 comments

While attempting to integrate docular into my project I encountered an unexpected RTE. Looking at the source to docs_module_begin.js I noticed the following:

                $scope.currentSource = {
                    source: $scope.currentPage.source, // Bad
                    codeBlocks: $scope.currentPage.codeBlocks // Also bad
                };

                if (!$scope.currentPage) {
                    $scope.partialTitle = 'Error: Page Not Found!';
                } else {

                    //lets expose additional data into the $scope so extensions can take advanatage of it
                    $scope.sectionInfo = sections.groupMap[groupId].sections[sectionId];
                    var pageOrder = $scope.sectionInfo.rank || {};
                    $scope.sectionPages = sortPages(sections.pages[groupId][sectionId], pageOrder);

                }

Since a null page is clearly possible (given that you check for it in the following lines) the lines that retrieve "source" and "codeBlocks" should be moved within the else block:

                $scope.currentPage = page;

                $scope.currentSource = {};

                if (!$scope.currentPage) {
                    $scope.partialTitle = 'Error: Page Not Found!';
                } else {
                    $scope.source = $scope.currentPage.source;
                    $scope.codeBlocks = $scope.currentPage.codeBlocks;

                    //lets expose additional data into the $scope so extensions can take advanatage of it
                    $scope.sectionInfo = sections.groupMap[groupId].sections[sectionId];
                    var pageOrder = $scope.sectionInfo.rank || {};
                    $scope.sectionPages = sortPages(sections.pages[groupId][sectionId], pageOrder);

                }

I ran into this bug and a few similar ones, all of which were found in docs_module_begin.js. The RTEs seemed to be triggered when we did not have "index.ngdoc" files in our sections.

FYI: The fix in the comment above should include the "currentSource" on the LHS, as in:

$scope.currentSource.source = $scope.currentPage.source;
$scope.currentSource.codeBlocks = $scope.currentPage.codeBlocks;

Also, to fix other RTEs I was encountering, I also ended up wrapping most of the "getPage" function with a guard, as in:

getPage: function(groupId, sectionId, partialId) {
    var pages = sections.pages[groupId][sectionId];
    if (pages) {
        if (sectionId && partialId) {
            // for loop - omitted for brevity
        } else {
            // for loop - omitted for brevity
        }
    }
    return null;
},

I also made a similar change in the $watch function at:

//determine if we have already loaded this page
var pageAlreadyLoaded = $scope.currentPage
  && $scope.currentPage.partialUrl
  && page                                                           // added this check
 ? (page.partialUrl == $scope.currentPage.partialUrl ? true : false) : false;

And I wrapped the body of sortPages with a guard, as in:

function sortPages (pages, pageOrder) {
    if (pages) {
        ...
    }
}

This should be all fixed up as of the beta.