Changeset 33146

Show
Ignore:
Timestamp:
11.06.2019 19:46:35 (9 days ago)
Author:
wy59
Message:

Major refactoring of map-scripts.js and classifier_scripts.js and document_scripts.js. Prior to this commit, there was some behaviour in map-scripts.js that was not straightforward: it was mimicking 'overriding' (function overrides don't exist in Javascript) and actually redefining the 2 functions httpRequest() already implemented in classifier_scripts, and toggleSection() already implemented in classifier_scripts and document_scripts. ToggleSection?() was implemented differently in the 2 original js files, and the one that got called depended on whether we were in doc view or classifier view. The original solution kept to this function overriding way of doing things and was messy. But we've now gone with Dr Bainbridge's suggestion of having a global variable mapEnabled that is declared/defined at the top of map-scripts.js. We then test for its existence in classifier_scripts::toggleSection now, to determine if we can call the extra map-specific functions that map_scripts did when doing the 'overriding'. This changeover allowed us to get rid of the function overrides in map-scripts.js and cleaned up unnecessary behaviour and variables introduced into document_scripts in a recent commit merely to work with the function override way of doing things.

Location:
main/trunk/greenstone3/web/interfaces/default/js
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/classifier_scripts.js

    r33145 r33146  
    1111    } 
    1212    return false; 
    13 } 
    14  
    15 // TODO: Is there a better solution 
    16 function _basicToggleSection(sectionID) { 
    17     // Leave this function here, even though it's empty. 
    18     // This function is here because: 
    19     // - it mirrors the identically named version in document_scripts 
    20     // - and this allows map_scripts to override it without knowing whether it's 
    21     //   overriding classifier_scripts::toggleSection() or document_scripts::toggleSection(). 
    22      
    23     // This method is not meant to call toggleSection. If anything it would be the other way around 
    24     // (as occurs in doc_scripts.js). But this method has to remain empty for classifier_scripts.js 
    25     // since map_scripts largely repeats classifier_scripts.js::toggleSection() code. 
    2613} 
    2714 
     
    5946        } 
    6047        updateOpenClassifiers(); 
     48        if(typeof mapEnabled !== 'undefined'){ 
     49            updateMap(); 
     50        } 
    6151    } 
    6252    else 
     
    163153                dmcheckout(); 
    164154            } 
    165             updateOpenClassifiers(); 
     155            updateOpenClassifiers();             
     156            if(typeof mapEnabled !== 'undefined'){ 
     157                getSubClassifier(sectionID); 
     158            } 
    166159        }) 
    167160        .error(function() 
  • main/trunk/greenstone3/web/interfaces/default/js/document_scripts.js

    r33128 r33146  
    11/** Javascript file for viewing documents */ 
    2  
    3 //-------TODO--------// 
    4 // map-scripts.js needs these, but non-map viewing does not. Yet if we add these to map_scripts, 
    5 // then in classifier mode, map_scripts will use classifier_scripts.js which already has these 
    6 // So for now, we add them into this file for when map_scripts is in doc view mode and includes document_scripts.js. 
    7 // It seems suboptimal, as these variables and function will be wasted when in regular non-map mode. 
    8 // But unable to think of any other solution at present. 
    9 var inProgress = new Array(); 
    10 var openClassifiers = new Array(); 
    11 var busy = false; 
    12  
    13  
    14  
    15 function updateOpenClassifiers() 
    16 { 
    17     var oc = ""; 
    18     var first = true; 
    19     for(var key in openClassifiers) 
    20     { 
    21         if(first) 
    22         { 
    23             first = false; 
    24         } 
    25         else 
    26         { 
    27             oc += ","; 
    28         } 
    29          
    30         oc += key; 
    31     } 
    32      
    33     if(oc != undefined) 
    34     { 
    35         window.location.hash = oc; 
    36     } 
    37 } 
    38  
    39 //---------------// 
    402 
    413/** NOTE, this file uses inline templates which look for httpPath, assocfilepath, Source, Thumb, Title metadata. These need to be added into any xsl file that uses this javascript (using <gsf:metadata name="xx" hidden="true"/> if they are not already being collected. */ 
     
    187149} 
    188150 
    189 // TODO: 
    190 // DO NOT "MERGE" _basicToggleSection into toggleSection! 
    191 // map_scripts.js calls _basicToggleSection as it redefines toggleSection() to do (more) stuff. 
     151 
    192152function toggleSection(sectionID, callback, tocDisabled) 
    193 { 
    194     _basicToggleSection(sectionID, callback, tocDisabled);   
    195 } 
    196 function _basicToggleSection(sectionID, callback, tocDisabled) 
    197153{ 
    198154    var docElem = gs.jqGet("doc" + sectionID); 
  • main/trunk/greenstone3/web/interfaces/default/js/map-scripts.js

    r33145 r33146  
     1var mapEnabled = true; // variable to detect when map-scripts have been included into document_scripts.js and classifier_scripts.js 
     2 
    13//var newLat, newLng = 0; 
    24var _docList = new Array(); 
     
    1820function initializeMapScripts()  
    1921{ 
    20     modifyFunctions(); 
    2122    setUpMap(); 
    2223     
     
    876877    return indexName + ":" + beforeDec + "+AND+" + indexName + ":" + afterDec; 
    877878} 
    878  
    879 // This function "overrides" (or rather, redefines) toggleSection in both classifier_scripts.js and document_scripts.js 
    880 // However, classifier_scripts.js::toggleSection() only took one parameter, sectionID, whereas 
    881 // document_scripts.js::toggleSection() took 3 params (sectionID, callback, tocDisabled). 
    882 // So to be compatible with both, need map-scripts::toggleSection() here to support all 3 in that order. 
    883 function modifyFunctions() 
    884 {    
    885     toggleSection = function(sectionID, callback, tocDisabled) 
    886     { 
    887         _basicToggleSection(sectionID, callback, tocDisabled); 
    888          
    889         var section = gs.jqGet("div" + sectionID); 
    890         var sectionToggle = gs.jqGet("toggle" + sectionID);      
    891                  
    892         if(sectionToggle == undefined) 
    893         {            
    894             return; 
    895         }        
    896          
    897         //If the div exists 
    898         // Test if 'section' exists.   
    899         // ==> Because we're using jQuery to do this we need to test the length of the object returned 
    900         // http://stackoverflow.com/questions/920236/how-can-i-detect-if-a-selector-returns-null 
    901         if(section.length !== 0) 
    902         {                
    903             if(isExpanded(sectionID)) 
    904             { 
    905                 section.css("display", "none"); 
    906                 sectionToggle.attr("src", gs.imageURLs.expand); 
    907                  
    908                 if(openClassifiers[sectionID].length !== 0) //if(openClassifiers[sectionID] != undefined) 
    909                 { 
    910                     delete openClassifiers[sectionID]; 
    911                 } 
    912             } 
    913             else 
    914             { 
    915                 section.css("display", "block"); 
    916                 sectionToggle.attr("src", gs.imageURLs.collapse); 
    917                 openClassifiers[sectionID] = true;   
    918             } 
    919             updateOpenClassifiers(); 
    920             updateMap(); 
    921         } 
    922         else 
    923         { 
    924             httpRequest(sectionID); 
    925         }        
    926          
    927     } 
    928      
    929     httpRequest = function(sectionID) 
    930     { 
    931         if(!inProgress[sectionID]) 
    932         { 
    933             inProgress[sectionID] = true; 
    934              
    935             var sectionToggle = gs.jqGet("toggle" + sectionID); 
    936             sectionToggle.attr("src", gs.imageURLs.loading); 
    937  
    938             var url = gs.xsltParams.library_name + "/collection/" + gs.cgiParams.c + "/browse/" + sectionID.replace(/\./g, "/") + "?excerptid=div" + sectionID; 
    939  
    940             if(gs.cgiParams.berrybasket == "on") 
    941             { 
    942                 url = url + "&berrybasket=on"; 
    943             } 
    944  
    945             if(url.indexOf("#") != -1) 
    946             { 
    947                 url = url.substring(0, url.indexOf("#")); 
    948             } 
    949              
    950             $.ajax(url) 
    951             .success(function(responseText) 
    952             { 
    953                 var newDiv = $("<div>");                                         
    954                 var sibling = gs.jqGet("title" + sectionID); 
    955                 sibling.after(newDiv); 
    956                  
    957                 newDiv.html(responseText); 
    958                 sectionToggle.attr("src", gs.imageURLs.collapse); 
    959                 openClassifiers[sectionID] = true;   
    960                  
    961                 if(gs.cgiParams.berrybasket == "on") 
    962                 { 
    963                     checkout(); 
    964                 } 
    965                 else if(gs.cgiParams.documentbasket == "on") 
    966                 { 
    967                     dmcheckout(); 
    968                 } 
    969                 updateOpenClassifiers(); 
    970                 getSubClassifier(sectionID); 
    971             }) 
    972             .error(function() 
    973             { 
    974                 sectionToggle.attr("src", gs.imageURLs.expand); 
    975             }) 
    976             .complete(function() 
    977             { 
    978                 inProgress[sectionID] = false; 
    979                 busy = false; 
    980             }); 
    981         } 
    982     } 
    983 }