Ignore:
Timestamp:
2017-03-21T15:22:50+13:00 (7 years ago)
Author:
ak19
Message:

Bringing the GS3 version of gsajaxapi.js file up to speed with GS2 version with the addition of 6 functions otherwise unused by GS3. A variation of a couple of these functions will appear in GS3's javascript-global-functions.js and those will be used by GS3 for the upcoming GS3 UserComments feature.

File:
1 edited

Legend:

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

    r24771 r31527  
    11
    2 function GSAjaxAPI(gwcgi,collect)
     2function GSAjaxAPI(gwcgi,collect,un,ky)
    33{
    44    var gwcgi_   = gwcgi;
    55    var collect_ = collect;
     6    var un_ = un;
     7    var ky_ = ky;
    68
    79
     
    101103    }
    102104
     105    if(un_ != null) {
     106        url += "&un=" + un_;
     107    }
     108    if(ky_ != null) {
     109        url += "&ky=" + ky_;
     110    }
     111
    103112    xmlHttp.open("GET",url,true);
    104113    xmlHttp.send(null);
     
    130139         }
    131140       }
    132    
     141
     142       if(un_ != null) {
     143       url += "&un=" + un_;
     144       }
     145       if(ky_ != null) {
     146       url += "&ky=" + ky_;
     147       }
     148
    133149       xmlHttp.open("GET",url,false);
    134150       xmlHttp.send(null);
     
    138154       return xmlHttp.responseText;
    139155    }
    140    
    141 
    142    
    143    
     156
     157//*********ADDITIONS TO BRING GS3 VERSION OF THIS FILE UP TO SPEED WITH GS2 VERSION********************//
     158//*********BUT NOT USED BY GS3. SEE GS3's javascript-global-functions.js INSTEAD (UPCOMING CHANGES)
     159//*********FOR THE PORTED VERSIONS OF THOSE FUNCTIONS AMONG THESE ADDITIONS THAT ARE NECESSARY FOR GS3.
     160
     161// New, an Ajax Synchronous Post method.
     162// http://www.degraeve.com/reference/simple-ajax-example.php
     163// Async vs Sync: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
     164// Also:
     165// http://stackoverflow.com/questions/6312447/in-an-ajax-post-do-i-need-to-urlencode-parameters-before-sending
     166// http://api.jquery.com/jQuery.post/
     167// http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
     168    this.urlPostSync = function(scriptURL, params) {
     169    var xmlHttp=false;
     170       try {
     171         // Firefox, Opera 8.0+, Safari
     172         xmlHttp=new XMLHttpRequest();
     173       }
     174       catch (e) {
     175         // Internet Explorer
     176         try {
     177           xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     178         }
     179         catch (e) {
     180           try {
     181             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     182           }
     183           catch (e) {
     184             alert("Your browser does not support AJAX!");
     185             return false;
     186           }
     187         }
     188       }
     189
     190    // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
     191    xmlHttp.open('POST', scriptURL, false); // false means synchronous
     192    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     193   
     194    if(un_ != null) {
     195    params += "&un=" + un_;
     196    }
     197    if(ky_ != null) {
     198    params += "&ky=" + ky_;
     199    }
     200
     201    xmlHttp.send(params); // needs to be escaped/encoded
     202
     203    //alert(scriptURL + "?" + params);
     204    //alert(xmlHttp.responseText); // if synchronous, process xmlHttp.responseText AFTER send() call
     205    return xmlHttp.responseText;
     206}
     207
     208    // New, an Ajax Asynchronous Post method.
     209    // For helpful links, see the urlPostSync() method above
     210    this.urlPostAsync = function(scriptURL, params, callback) {
     211    var xmlHttp=false;
     212       try {
     213         // Firefox, Opera 8.0+, Safari
     214         xmlHttp=new XMLHttpRequest();
     215       }
     216       catch (e) {
     217         // Internet Explorer
     218         try {
     219           xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     220         }
     221         catch (e) {
     222           try {
     223             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     224           }
     225           catch (e) {
     226             alert("Your browser does not support AJAX!");
     227             return false;
     228           }
     229         }
     230       }
     231
     232
     233
     234    // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
     235    xmlHttp.open('POST', scriptURL, true); // true means asynchronous
     236    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     237
     238
     239    // If asynchronous:
     240    // If the callback param is a function, we will set it up to get called when
     241    // the async post has finished (is ready)
     242    // if the callback parameter isn't a function, the param represents a field
     243    // that we want to dynamically update when the async post process has finished
     244
     245    var typeof_callback = typeof(callback);
     246    if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) {
     247        var locid = callback;
     248
     249        xmlHttp.onreadystatechange=function() {
     250        if(xmlHttp.readyState==4) {
     251            if (locelem != null) {
     252            var locelem = document.getElementById(locid);
     253           
     254            locelem.innerHTML = xmlHttp.responseText;
     255            }
     256        }
     257        }
     258    }
     259    else if (typeof_callback == "function") {
     260        xmlHttp.onreadystatechange=function() {
     261        if(xmlHttp.readyState==4) {
     262            callback(xmlHttp); // e.g. this might do: updatepage(xmlHttp.responseText);
     263        }
     264        }
     265    }
     266    else {
     267        alert("Unrecognized type of callback value: " + typeof_callback);
     268    }
     269   
     270    if(un_ != null) {
     271    params += "&un=" + un_;
     272    }
     273    if(ky_ != null) {
     274    params += "&ky=" + ky_;
     275    }
     276    //alert("Posting Async: " + scriptURL + "?" + params);
     277
     278    xmlHttp.send(params); // needs to be escaped/encoded
     279    // if synchronous, would process xmlHttp AFTER send() call, such as by
     280    // accessing xmlHttp.responseText to return that to the caller at this point.
     281}
     282
     283    // New
     284    // The where parameter can be specified as one or more of: import, archives, index, live
     285    // separated by |. If null, it is assumed to be index which is the original default
     286    // behaviour of calling set-metadata. E.g. where=import|archives|index
     287    this.setMetadata = function(docid,metaname,metapos,metavalue,metamode,where)
     288    {
     289        var mdserver = this.metadataserverURL();
     290   
     291        var params = "a=set-metadata";
     292    if(where != null) {
     293        params += "&where=" + where; // if where not specified, meta-server will default to setting index meta
     294        //} else {
     295        //    params += "&where=import|archives|index";
     296    }
     297        params += "&c="+collect_;
     298        params += "&d="+docid;
     299        params += "&metaname=" + metaname;
     300        if (metapos!=null) {
     301            params += "&metapos=" + metapos;
     302        }
     303        params += "&metavalue=" + metavalue;
     304    if (metamode!=null) {
     305            params += "&metamode=" + metamode;
     306    }
     307   
     308        //this.urlGetSync(mdserver + "?" + params);
     309        this.urlPostSync(mdserver,params);
     310    }
     311
     312    // New
     313    // The where parameter can be specified as one or more of: import, archives, index, live
     314    // separated by |. If null, it is assumed to be index which is the original default
     315    // behaviour of calling set-metadata-array). E.g. where=import|archives|index
     316    this.setMetadataArray = function(docArray,metamode,where)
     317    {
     318    docArrayJSON = JSON.stringify(docArray);
     319   
     320    var mdserver = this.metadataserverURL();
     321   
     322    var params = "a=" + escape("set-metadata-array"); //"a=set-metadata-array";
     323    if(where != null) {
     324        params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
     325        //} else {
     326        //    params += "&where=import|archives|index";
     327    }
     328    params += "&c="+escape(collect_);
     329    params += "&json="+escape(docArrayJSON);
     330   
     331    if (metamode!=null) {
     332        params += "&metamode=" + escape(metamode);
     333    }
     334   
     335    //this.urlGetSync(mdserver + "?" + params);
     336    return this.urlPostSync(mdserver,params);   
     337    }
     338
     339    // New
     340    this.getArchivesMetadata = function(docoid,metaname,metapos)
     341    {
     342        var mdserver = this.metadataserverURL();
     343   
     344        var url = mdserver + "?a=get-archives-metadata";
     345        url += "&c="+collect_;
     346        url += "&d="+docoid;
     347        url += "&metaname=" + metaname;
     348        if (metapos!=null) {
     349            url += "&metapos=" + metapos;
     350        }
     351
     352    //alert("In getArchivesMeta. URL: " + url)
     353        return this.urlGetSync(url); //Once this works, make it POST
     354    }
     355
     356    this.getMetadataArray = function(docArray,where)
     357    {
     358    docArrayJSON = JSON.stringify(docArray);
     359
     360    var mdserver = this.metadataserverURL();
     361   
     362    var params = "a=" + escape("get-metadata-array"); //"a=set-metadata-array";
     363    if(where != null) {
     364        params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
     365        //} else {
     366        //    params += "&where=import|archives|index";
     367    }
     368    params += "&c="+escape(collect_);
     369    params += "&json="+escape(docArrayJSON);
     370   
     371    //this.urlGetSync(mdserver + "?" + params);
     372    return this.urlPostSync(mdserver,params);   
     373    }
     374//*******END OF ADDITIONS TO BRING GS3 VERSION OF THIS FILE UP TO SPEED WITH GS2 VERSION********//
    144375   
    145376    this.setLiveMetadata = function(id,metaname,metavalue)
     
    155386        this.urlGetSync(url);
    156387    }
    157    
     388
    158389    this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode)
    159390    {
     
    168399        }
    169400        params += "&metavalue=" + metavalue;
    170         if (metamode!=null) {
     401    if (metamode!=null) {
    171402            params += "&metamode=" + metamode;
    172         }
     403    }
    173404   
    174405        this.urlGetSync(mdserver + "?" + params);
     
    177408   
    178409   
    179     this._setDocumentArrayMetadata = function(mode,docArray,metamode)
    180     {
    181         docArrayJSON = JSON.stringify(docArray);
    182  
    183         var mdserver = this.metadataserverURL();
    184    
    185         var params = "a=set" + mode + "-metadata-array";
    186         params += "&c="+collect_;
    187         params += "&json="+docArrayJSON;
    188    
    189         if (metamode!=null) {
    190             params += "&metamode=" + metamode;
    191         }
    192        
    193         this.urlGetSync(mdserver + "?" + params);
    194    
    195     }
     410    this._setDocumentArrayMetadata = function(mode,docArray,metamode)
     411    {
     412    docArrayJSON = JSON.stringify(docArray);
     413   
     414    var mdserver = this.metadataserverURL();
     415
     416    var params = "a=set" + mode + "-metadata-array";
     417    params += "&c="+collect_;
     418    params += "&json="+docArrayJSON;
     419   
     420    if (metamode!=null) {
     421        params += "&metamode=" + metamode;
     422    }
     423   
     424    this.urlGetSync(mdserver + "?" + params);
     425   
     426    }
    196427   
    197428   
     
    210441    }
    211442
    212     this.setDocumentArrayMetadata = function(docArray,metamode)
    213     {
    214         //showDialog('Greenstone Javascript API','This sequence of changes has been commited into the system.','success', 2);
    215    
    216         this._setDocumentArrayMetadata("",docArray,metamode);
    217         this._setDocumentArrayMetadata("-archives",docArray,metamode);
    218     }
     443    this.setDocumentArrayMetadata = function(docArray,metamode)
     444    {
     445    //showDialog('Greenstone Javascript API','This sequence of changes has been commited into the system.','success', 2);
     446   
     447    this._setDocumentArrayMetadata("",docArray,metamode);
     448    this._setDocumentArrayMetadata("-archives",docArray,metamode);
     449    }
    219450   
    220451    this.setNewDocumentMetadata = function(docid,metaname,metavalue)
Note: See TracChangeset for help on using the changeset viewer.