Ignore:
Timestamp:
2012-04-12T15:06:50+12:00 (12 years ago)
Author:
sjm84
Message:

Some big changes to how the document view is handled

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/oran/js/document_scripts.js

    r25295 r25366  
    77********************/
    88
    9 function toggleSection(sectionID)
     9function getTextForSection(sectionID, callback)
     10{
     11    if(!callback)
     12    {
     13        console.log("Cannot get text as the callback function is not defined");
     14    }
     15
     16    var template = "";
     17    template += '<xsl:template match="/">';
     18    template +=   '<text>';
     19    template +=     '<xsl:apply-templates select="/page/pageResponse/document//documentNode[@nodeID = \'' + sectionID + '\']" mode="document"/>';
     20    template +=   '</text>';
     21    template += '</xsl:template>';
     22
     23    var ajax = gs.functions.ajaxRequest();
     24    ajax.open("GET", gs.xsltParams.library_name + "/collection/" + gs.cgiParams.c + "/document/" + sectionID + "?p.s=TextQuery&ilt=" + template.replace(" ", "%20"), true);
     25    ajax.onreadystatechange = function()
     26    {
     27        if(ajax.readyState == 4 && ajax.status == 200)
     28        {
     29            var response = ajax.responseText;
     30           
     31            if(response)
     32            {
     33                var textStart = response.indexOf(">", response.indexOf(">") + 1) + 1;
     34                var textEnd = response.lastIndexOf("<");
     35               
     36                if(textStart == 0 || textEnd == -1 || textEnd <= textStart)
     37                {
     38                    callback("");
     39                }
     40               
     41                var text = response.substring(textStart, textEnd);
     42                callback(text);
     43            }
     44            else
     45            {
     46                callback(null);
     47            }
     48        }
     49        else if(ajax.readyState == 4)
     50        {
     51            callback(null);
     52        }
     53    }
     54    ajax.send();
     55}
     56
     57function getSubSectionsForSection(sectionID, callback)
     58{
     59    if(!callback)
     60    {
     61        console.log("Cannot get sub sections as the callback function is not defined");
     62    }
     63
     64    var template = "";
     65    template += '<xsl:template match="/">';
     66    template +=   '<sections>';
     67    template +=     '<xsl:for-each select="/page/pageResponse/document//documentNode[@nodeID = \'' + sectionID + '\']">';
     68    template +=       '<xsl:call-template name="wrapDocumentNodes"/>';
     69    template +=     '</xsl:for-each>';
     70    template +=   '</sections>';
     71    template += '</xsl:template>';
     72
     73    var ajax = gs.functions.ajaxRequest();
     74    ajax.open("GET", gs.xsltParams.library_name + "/collection/" + gs.cgiParams.c + "/document/" + sectionID + "?ilt=" + template.replace(" ", "%20"), true);
     75    ajax.onreadystatechange = function()
     76    {
     77        if(ajax.readyState == 4 && ajax.status == 200)
     78        {
     79            var response = ajax.responseText;
     80           
     81            if(response)
     82            {
     83                var sectionsStart = response.indexOf(">") + 1;
     84                var sectionsEnd = response.lastIndexOf("<");
     85               
     86                if(sectionsStart == 0 || sectionsEnd == -1 || sectionsEnd <= sectionsStart)
     87                {
     88                    callback("");
     89                }
     90               
     91                var sections = response.substring(sectionsStart, sectionsEnd);
     92                sections = sections.replace(/href=".*?#top"/g, "href=\"" + gs.requestInformation.fullURL + "#top\"");
     93               
     94                callback(sections);
     95            }
     96            else
     97            {
     98                callback(null);
     99            }
     100        }
     101        else if(ajax.readyState == 4)
     102        {
     103            callback(null);
     104        }
     105    }
     106    ajax.send();
     107}
     108
     109function toggleSection(sectionID, callback)
    10110{
    11111    var docElem = document.getElementById("doc" + sectionID);
     
    17117    if(docElem.style.display == "none")
    18118    {
    19         docElem.style.display = "block";
    20         docToggleElem.setAttribute("src", gs.imageURLs.collapse);
    21        
    22119        if(tocToggleElem)
    23120        {
     
    28125        {
    29126            tocElem.style.display = "block";
     127        }
     128       
     129        if(gs.functions.hasClass(docElem, "noText"))
     130        {
     131            getTextForSection(sectionID, function(text)
     132            {
     133                if(text)
     134                {   
     135                    getSubSectionsForSection(sectionID, function(sections)
     136                    {
     137                        if(sections)
     138                        {
     139                            var textElem = document.getElementById("doc" + sectionID);
     140                            textElem.innerHTML = text + sections;
     141                           
     142                            docElem.setAttribute("class", docElem.getAttribute("class").replace(/\bnoText\b/g, ""));
     143                            docElem.style.display = "block";
     144                            docToggleElem.setAttribute("src", gs.imageURLs.collapse);
     145                           
     146                            if(callback)
     147                            {
     148                                callback(true);
     149                            }
     150                        }
     151                        else
     152                        {
     153                            docToggleElem.setAttribute("src", gs.imageURLs.expand);
     154                            if(callback)
     155                            {
     156                                callback(false);
     157                            }
     158                        }
     159                    });
     160                }
     161                else
     162                {
     163                    docToggleElem.setAttribute("src", gs.imageURLs.expand);
     164                    if(callback)
     165                    {
     166                        callback(false);
     167                    }
     168                }
     169            });
     170       
     171            docToggleElem.setAttribute("src", gs.imageURLs.loading);
     172        }
     173        else
     174        {
     175            docToggleElem.setAttribute("src", gs.imageURLs.collapse);
     176            docElem.style.display = "block";
     177           
     178            if(callback)
     179            {
     180                callback(true);
     181            }
    30182        }
    31183    }
     
    46198            tocElem.style.display = "none";
    47199        }
    48     }
     200       
     201        if(callback)
     202        {
     203            callback(true);
     204        }
     205    }
     206}
     207
     208function scrollToTop()
     209{
     210    $('html, body').stop().animate({scrollTop: 0}, 1000);
     211}
     212
     213function focusSection(sectionID, level)
     214{
     215    if(!level)
     216    {
     217        level = 0;
     218    }
     219
     220    var parts = sectionID.split(".");
     221    if(level >= parts.length)
     222    {
     223        var topVal = $(document.getElementById("doc" + sectionID)).offset().top - 50;
     224        $('html, body').stop().animate({scrollTop: topVal}, 1000);
     225        return;
     226    }
     227   
     228    var idToExpand = "";
     229    for(var i = 0; i < level + 1; i++)
     230    {
     231        if(i > 0)
     232        {
     233            idToExpand += ".";
     234        }
     235   
     236        idToExpand += parts[i];
     237    }
     238   
     239    if(!isExpanded(idToExpand))
     240    {
     241        toggleSection(idToExpand, function(success)
     242        {
     243            if(success)
     244            {
     245                focusSection(sectionID, level + 1);
     246            }
     247        });
     248    }
     249    else
     250    {
     251        focusSection(sectionID, level + 1);
     252    }
     253}
     254
     255function expandOrCollapseAll(expand)
     256{
     257    var divs = document.getElementsByTagName("DIV");
     258    var startCounter = 0;
     259    var endCounter = 0;
     260   
     261    for(var i = 0; i < divs.length; i++)
     262    {
     263        if(divs[i].getAttribute("id") && divs[i].getAttribute("id").search(/^doc/) != -1)
     264        {
     265            var id = divs[i].getAttribute("id").replace(/^doc(.*)/, "$1");
     266            if(isExpanded(id) != expand)
     267            {
     268                //Don't collapse the top level
     269                if(!expand && id.indexOf(".") == -1)
     270                {
     271                    continue;
     272                }
     273                startCounter++;
     274               
     275                var toggleFunction = function(tid)
     276                {
     277                    toggleSection(tid, function(success)
     278                    {
     279                        if(success)
     280                        {
     281                            endCounter++;
     282                        }
     283                        else
     284                        {
     285                            setTimeout(function(){toggleFunction(tid)}, 500);
     286                        }
     287                    });
     288                }
     289                toggleFunction(id);
     290            }
     291        }
     292    }
     293   
     294    if(startCounter != 0)
     295    {
     296        var checkFunction = function()
     297        {
     298            if(startCounter == endCounter)
     299            {
     300                expandOrCollapseAll(expand);
     301            }
     302            else
     303            {
     304                setTimeout(checkFunction, 500);
     305            }
     306        }
     307        setTimeout(checkFunction, 500);
     308    }
     309}
     310
     311function loadTopLevelPage(callbackFunction)
     312{
     313    var ajax = gs.functions.ajaxRequest();
     314   
     315    ajax.open("GET", gs.xsltParams.library_name + "?a=d&dt=hierarchy&c=" + gs.cgiParams.c + "&d=" + gs.cgiParams.d.replace(/([\.]*)\..*/, "$1") + "&excerptid=gs-document", true);
     316    ajax.onreadystatechange = function()
     317    {
     318        if(ajax.readyState == 4 && ajax.status == 200)
     319        {
     320            var response = ajax.responseText;
     321           
     322            if(response)
     323            {
     324                var targetElem = document.getElementById("gs-document");
     325                var docStart = response.indexOf(">") + 1;
     326                var docEnd = response.lastIndexOf("<");
     327                var doc = response.substring(docStart, docEnd);
     328                targetElem.innerHTML = doc;
     329               
     330                if(callbackFunction)
     331                {
     332                    callbackFunction();
     333                }
     334            }
     335        }
     336        else if(ajax.readyState == 4)
     337        {
     338            var targetElem = document.getElementById("gs-document");
     339            targetElem.innerHTML = targetElem.innerHTML + "<br/> <br/> FAILED TO LOAD PAGE";
     340        }
     341    };
     342    ajax.send();
     343}
     344
     345function retrieveFullTableOfContents()
     346{
     347    var ajax = gs.functions.ajaxRequest();
     348   
     349    ajax.open("GET", gs.xsltParams.library_name + "?a=d&ed=1&c=" + gs.cgiParams.c + "&d=" + gs.cgiParams.d + "&excerptid=tableOfContents", true);
     350    ajax.onreadystatechange = function()
     351    {
     352        if(ajax.readyState == 4 && ajax.status == 200)
     353        {
     354            var newTOCElem = ajax.responseText;
     355            var tocStart = newTOCElem.indexOf(">") + 1;
     356            var tocEnd = newTOCElem.lastIndexOf("<");
     357           
     358            var newTOC = newTOCElem.substring(tocStart, tocEnd);
     359           
     360            //Add an "expand all" link
     361            newTOC = "<table style=\"width:100%; text-align:center;\"><tr><td><a href=\"javascript:expandOrCollapseAll(true);\">Expand document</a></td><td><a href=\"javascript:expandOrCollapseAll(false);\">Collapse document</a></td></tr></table>" + newTOC;
     362           
     363            //Collapse the TOC
     364            newTOC = newTOC.replace(/display:block/g, "display:none");
     365            newTOC = newTOC.replace(/display:none/, "display:block");
     366            newTOC = newTOC.replace(/images\/collapse/g, "images/expand");
     367           
     368            var tocElem = document.getElementById("tableOfContents");
     369            tocElem.innerHTML = newTOC;
     370        }
     371        else if(ajax.readyState == 4)
     372        {
     373            var loading = document.getElementById("tocLoadingImage");
     374            loading.parentNode.removeChild(loading);
     375            console.log("Error Loading TOC");
     376        }
     377    }
     378    ajax.send();
    49379}
    50380
Note: See TracChangeset for help on using the changeset viewer.