source: main/trunk/greenstone3/web/interfaces/default/js/gsajaxapi.js@ 32873

Last change on this file since 32873 was 32873, checked in by ak19, 5 years ago

javascript-global-functions' local function callMetadataServer() now renamed to _callMetadataServer() and takes and extra parameter BEFORE existing last parameter opts. The extra parameter is errorResponseFunction, which if not null is called on an ajax error if using jQuery mode to do ajax (errorResponseFunction is not used by gsajaxapi yet). All functions that call _callMetadataServer() now also take an extra last param, errorResponseFunction, and force it to null if undefined. It is always the last param for these public functions, so that the existing method calls from outside the file will continue to work. Tested getting and setting regular meta with the doceditor and mapeditor: saving and reloading saved data still works. Not tested set/getMetaArrays used by usercomments, but the changes haven't been drastic or complicated, so shouldn't have broken anything in those 2 functions either.

File size: 17.3 KB
Line 
1
2function GSAjaxAPI(gwcgi,collect,un,ky)
3{
4 var gwcgi_ = gwcgi;
5 var collect_ = collect;
6 var un_ = un;
7 var ky_ = ky;
8
9
10 this.fullDomainURL = function(localURL)
11 {
12 return window.location.protocol+'//'+window.location.host+localURL;
13 }
14
15 this.apiURL = function(apiProg)
16 {
17 //get the location of the cgi program
18 splitpos = gwcgi_.lastIndexOf("/");
19
20 var mdserver;
21 if (splitpos >= 0) {
22 mdserver = gwcgi.substring(0,(splitpos+1)) + apiProg;
23 }
24 else {
25 mdserver = apiProg;
26 }
27
28 return mdserver;
29 }
30
31 this.metadataserverURL = function()
32 {
33 return this.apiURL("metadata-server.pl");
34 }
35
36 this.indexserverURL = function()
37 {
38 return this.apiURL("index-server.pl");
39 }
40
41 this.buildserverURL = function()
42 {
43 return this.apiURL("build-server.pl");
44 }
45
46 this.explodeserverURL = function()
47 {
48 return this.apiURL("explode-server.pl");
49 }
50
51 this.myspaceserverURL = function()
52 {
53 return this.apiURL("myspace-server.pl");
54 }
55
56
57 this.urlGetAsync = function(url,callback)
58 {
59 var xmlHttp;
60 try {
61 // Firefox, Opera 8.0+, Safari
62 xmlHttp=new XMLHttpRequest();
63 }
64 catch (e) {
65 // Internet Explorer
66 try {
67 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
68 }
69 catch (e) {
70 try {
71 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
72 }
73 catch (e) {
74 alert("Your browser does not support AJAX!");
75 return false;
76 }
77 }
78 }
79
80 var typeof_callback = typeof(callback);
81 if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) {
82 var locid = callback;
83
84 xmlHttp.onreadystatechange=function() {
85 if(xmlHttp.readyState==4) {
86 if (locelem != null) {
87 var locelem = document.getElementById(locid);
88
89 locelem.innerHTML = xmlHttp.responseText;
90 }
91 }
92 }
93 }
94 else if (typeof_callback == "function") {
95 xmlHttp.onreadystatechange=function() {
96 if(xmlHttp.readyState==4) {
97 callback(xmlHttp);
98 }
99 }
100 }
101 else {
102 alert("Unrecognized type of callback value: " + typeof_callback);
103 }
104
105 if(un_ != null) {
106 url += "&un=" + un_;
107 }
108 if(ky_ != null) {
109 url += "&ky=" + ky_;
110 }
111
112 xmlHttp.open("GET",url,true);
113 xmlHttp.send(null);
114 }
115
116
117 this.urlGetSync = function(url)
118 {
119 // alert("url = " + url);
120
121 var xmlHttp;
122 try {
123 // Firefox, Opera 8.0+, Safari
124 xmlHttp=new XMLHttpRequest();
125 }
126 catch (e) {
127 // Internet Explorer
128 try {
129 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
130 }
131 catch (e) {
132 try {
133 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
134 }
135 catch (e) {
136 alert("Your browser does not support AJAX!");
137 return false;
138 }
139 }
140 }
141
142 if(un_ != null) {
143 url += "&un=" + un_;
144 }
145 if(ky_ != null) {
146 url += "&ky=" + ky_;
147 }
148
149 xmlHttp.open("GET",url,false);
150 xmlHttp.send(null);
151
152 // alert("response = '" + xmlHttp.responseText + "'");
153
154 return xmlHttp.responseText;
155 }
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 // NOTE: if passing in an error callback function *separate* from success callback (i.e. if current callback param won't check xmlHttp.status == 200)
246 // then see https://stackoverflow.com/questions/1442425/detect-xhr-error-is-really-due-to-browser-stop-or-click-to-new-page
247 // and https://stackoverflow.com/questions/8866761/xmlhttprequest-ajax-error
248
249 var typeof_callback = typeof(callback);
250 if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) {
251 var locid = callback;
252
253 xmlHttp.onreadystatechange=function() {
254 if(xmlHttp.readyState==4) {
255 if (locelem != null) {
256 var locelem = document.getElementById(locid);
257
258 locelem.innerHTML = xmlHttp.responseText;
259 }
260 }
261 }
262 }
263 else if (typeof_callback == "function") {
264 xmlHttp.onreadystatechange=function() {
265 if(xmlHttp.readyState==4) {
266 callback(xmlHttp); // e.g. this might do: updatepage(xmlHttp.responseText);
267 }
268 }
269 }
270 else {
271 alert("Unrecognized type of callback value: " + typeof_callback);
272 }
273
274 if(un_ != null) {
275 params += "&un=" + un_;
276 }
277 if(ky_ != null) {
278 params += "&ky=" + ky_;
279 }
280 //alert("Posting Async: " + scriptURL + "?" + params);
281
282 xmlHttp.send(params); // needs to be escaped/encoded
283 // if synchronous, would process xmlHttp AFTER send() call, such as by
284 // accessing xmlHttp.responseText to return that to the caller at this point.
285 }
286
287 // New
288 // The where parameter can be specified as one or more of: import, archives, index, live
289 // separated by |. If null, it is assumed to be index which is the original default
290 // behaviour of calling set-metadata. E.g. where=import|archives|index
291 this.setMetadata = function(docid,metaname,metapos,metavalue,metamode,where)
292 {
293 var mdserver = this.metadataserverURL();
294
295 var params = "a=set-metadata";
296 if(where != null) {
297 params += "&where=" + where; // if where not specified, meta-server will default to setting index meta
298 //} else {
299 // params += "&where=import|archives|index";
300 }
301 params += "&c="+collect_;
302 params += "&d="+docid;
303 params += "&metaname=" + metaname;
304 if (metapos!=null) {
305 params += "&metapos=" + metapos;
306 }
307 params += "&metavalue=" + metavalue;
308 if (metamode!=null) {
309 params += "&metamode=" + metamode;
310 }
311
312 //this.urlGetSync(mdserver + "?" + params);
313 this.urlPostSync(mdserver,params);
314 }
315
316 // New
317 // The where parameter can be specified as one or more of: import, archives, index, live
318 // separated by |. If null, it is assumed to be index which is the original default
319 // behaviour of calling set-metadata-array). E.g. where=import|archives|index
320 this.setMetadataArray = function(docArray,metamode,where)
321 {
322 docArrayJSON = JSON.stringify(docArray);
323
324 var mdserver = this.metadataserverURL();
325
326 var params = "a=" + escape("set-metadata-array"); //"a=set-metadata-array";
327 if(where != null) {
328 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
329 //} else {
330 // params += "&where=import|archives|index";
331 }
332 params += "&c="+escape(collect_);
333 params += "&json="+escape(docArrayJSON);
334
335 if (metamode!=null) {
336 params += "&metamode=" + escape(metamode);
337 }
338
339 //this.urlGetSync(mdserver + "?" + params);
340 return this.urlPostSync(mdserver,params);
341 }
342
343 // New
344 this.getArchivesMetadata = function(docoid,metaname,metapos)
345 {
346 var mdserver = this.metadataserverURL();
347
348 var url = mdserver + "?a=get-archives-metadata";
349 url += "&c="+collect_;
350 url += "&d="+docoid;
351 url += "&metaname=" + metaname;
352 if (metapos!=null) {
353 url += "&metapos=" + metapos;
354 }
355
356 //alert("In getArchivesMeta. URL: " + url)
357 return this.urlGetSync(url); //Once this works, make it POST
358 }
359
360 this.getMetadataArray = function(docArray,where)
361 {
362 docArrayJSON = JSON.stringify(docArray);
363
364 var mdserver = this.metadataserverURL();
365
366 var params = "a=" + escape("get-metadata-array"); //"a=set-metadata-array";
367 if(where != null) {
368 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
369 //} else {
370 // params += "&where=import|archives|index";
371 }
372 params += "&c="+escape(collect_);
373 params += "&json="+escape(docArrayJSON);
374
375 //this.urlGetSync(mdserver + "?" + params);
376 return this.urlPostSync(mdserver,params);
377 }
378//*******END OF ADDITIONS TO BRING GS3 VERSION OF THIS FILE UP TO SPEED WITH GS2 VERSION********//
379
380 this.setLiveMetadata = function(id,metaname,metavalue)
381 {
382 var mdserver = this.metadataserverURL();
383
384 var url = mdserver + "?a=set-live-metadata";
385 url += "&c="+collect_;
386 url += "&d="+id;
387 url += "&metaname=" + metaname;
388 url += "&metavalue=" + metavalue;
389
390 this.urlGetSync(url);
391 }
392
393 this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode)
394 {
395 var mdserver = this.metadataserverURL();
396
397 var params = "a=set" + mode + "-metadata";
398 params += "&c="+collect_;
399 params += "&d="+docid;
400 params += "&metaname=" + metaname;
401 if (metapos!=null) {
402 params += "&metapos=" + metapos;
403 }
404 params += "&metavalue=" + metavalue;
405 if (metamode!=null) {
406 params += "&metamode=" + metamode;
407 }
408
409 this.urlGetSync(mdserver + "?" + params);
410 //this.urlPostSync(mdserver,params);
411 }
412
413
414 this._setDocumentArrayMetadata = function(mode,docArray,metamode)
415 {
416 docArrayJSON = JSON.stringify(docArray);
417
418 var mdserver = this.metadataserverURL();
419
420 var params = "a=set" + mode + "-metadata-array";
421 params += "&c="+collect_;
422 params += "&json="+docArrayJSON;
423
424 if (metamode!=null) {
425 params += "&metamode=" + metamode;
426 }
427
428 this.urlGetSync(mdserver + "?" + params);
429
430 }
431
432
433 this.setDocumentMetadata = function(docid,metaname,metapos,metavalue)
434 {
435 // Allow for three param call to function, where metapos is missed out
436 if (metavalue==null) {
437 // 4 param case
438 metavalue = metapos;
439 metapos = null;
440 }
441
442 this._setMetadata("",docid,metaname,metapos,metavalue);
443 this._setMetadata("-archives",docid,metaname,metapos,metavalue,"override");
444
445 }
446
447 this.setDocumentArrayMetadata = function(docArray,metamode)
448 {
449 //showDialog('Greenstone Javascript API','This sequence of changes has been commited into the system.','success', 2);
450
451 this._setDocumentArrayMetadata("",docArray,metamode);
452 this._setDocumentArrayMetadata("-archives",docArray,metamode);
453 }
454
455 this.setNewDocumentMetadata = function(docid,metaname,metavalue)
456 {
457 this._setMetadata("",docid,metaname,null,metavalue);
458 this._setMetadata("-archives",docid,metaname,null,metavalue,"accumulate");
459 }
460
461 this.setImportMetadata = function(docid,metaname,metapos,metavalue)
462 {
463 this._setMetadata("-import",docid,metaname,metapos,metavalue,"override");
464 }
465
466
467 this.explodeDocument = function(docid)
468 {
469 var exserver = this.explodeserverURL();
470
471 var url = exserver + "?a=explode-document";
472 url += "&c="+collect_;
473 url += "&d="+docid;
474
475 this.urlGetSync(url);
476 }
477
478 this.deleteDocument = function(docid,onlyAdd)
479 {
480 var exserver = this.explodeserverURL();
481
482 var url = exserver + "?a=delete-document";
483 url += "&c="+collect_;
484 params += "&onlyadd="+onlyAdd;
485 url += "&d="+docid;
486
487 this.urlGetSync(url);
488 }
489
490 this.deleteDocumentArray = function(docArray,onlyAdd)
491 {
492 docArrayJSON = JSON.stringify(docArray);
493
494 var exserver = this.explodeserverURL();
495
496 var params = "a=delete-document-array";
497 params += "&c="+collect_;
498 params += "&onlyadd="+onlyAdd;
499 params += "&json="+docArrayJSON;
500
501 this.urlGetSync(exserver + "?" + params);
502
503 }
504
505
506 this.cloneDocument = function(docid,toCollect)
507 {
508 var msserver = this.myspaceserverURL();
509
510 var url = msserver + "?a=clone";
511 url += "&c="+collect_;
512 url += "&d="+docid;
513 url += "&toCollect="+toCollect;
514
515 this.urlGetSync(url);
516 }
517
518 // consider name change to reindexDocument
519 this.documentReindex = function(docid)
520 {
521 var mdserver = this.metadataserverURL();
522
523 var url = mdserver + "?a=reindex-document";
524 url += "&c="+collect_;
525 url += "&d="+docid;
526
527 this.urlGetSync(url);
528 }
529
530
531 this.reindexCollection = function(mode,callback)
532 {
533 if (mode==null) {
534 mode = "incremental";
535 }
536
537 var idserver = this.indexserverURL();
538
539 var url = idserver + "?a=" + mode + "-rebuild";
540 url += "&c="+collect_;
541
542 this.urlGetAsync(url,callback);
543 }
544
545
546 this.buildByManifestGeneral = function(hashargs)
547 {
548 var idserver = this.buildserverURL();
549
550 var url = idserver + "?a=build-by-manifest";
551 url += "&c="+collect_;
552
553 if (hashargs["index-files"] != undefined) {
554 url += "&index-files=" + JSON.stringify(hashargs["index-files"]);
555 }
556
557 if (hashargs["reindex-files"] != undefined) {
558 url += "&reindex-files=" + JSON.stringify(hashargs["reindex-files"]);
559 }
560 if (hashargs["delete-OIDs"] != undefined) {
561 url += "&delete-OIDs=" + JSON.stringify(hashargs["delete-OIDs"]);
562 }
563
564 this.urlGetSync(url);
565 }
566
567 this.indexByManifest = function(docidArray)
568 {
569 var hashargs = {};
570 hashargs["index-files"] = docidArray;
571 this.buildByManifestGeneral(hashargs);
572 }
573
574 this.reindexByManifest = function(docidArray)
575 {
576 var hashargs = {};
577 hashargs["reindex-files"] = docidArray;
578 this.buildByManifestGeneral(hashargs);
579 }
580 this.deleteByManifest = function(docidArray)
581 {
582 var hashargs = {};
583 hashargs["delete-OIDs"] = docidArray;
584 this.buildByManifestGeneral(hashargs);
585 }
586
587 this.getLiveMetadata = function(id,metaname)
588 {
589 var mdserver = this.metadataserverURL();
590
591 var url = mdserver + "?a=get-live-metadata";
592 url += "&c="+collect_;
593 url += "&d="+id;
594 url += "&metaname=" + metaname;
595
596 var metavalue = this.urlGetSync(url);
597
598 return metavalue;
599 }
600
601 this.getDocumentMetadata = function(docoid,metaname,metapos)
602 {
603 var mdserver = this.metadataserverURL();
604
605 var url = mdserver + "?a=get-metadata";
606 url += "&c="+collect_;
607 url += "&d="+docoid;
608 url += "&metaname=" + metaname;
609 if (metapos!=null) {
610 url += "&metapos=" + metapos;
611 }
612
613 return this.urlGetSync(url);
614 }
615
616 this.removeLiveMetadata = function(id,metaname)
617 {
618 var mdserver = this.metadataserverURL();
619
620 var url = mdserver + "?a=remove-live-metadata";
621 url += "&c="+collect_;
622 url += "&d="+id;
623 url += "&metaname=" + metaname;
624
625 this.urlGetSync(url);
626 }
627
628 this.removeDocumentMetadata = function(docid,metaname,metapos)
629 {
630 var mdserver = this.metadataserverURL();
631
632 var url = mdserver + "?a=remove-metadata";
633 url += "&c="+collect_;
634 url += "&d="+docid;
635 url += "&metaname=" + metaname;
636 if (metapos!=null) {
637 url += "&metapos=" + metapos;
638 }
639
640 this.urlGetSync(url);
641 }
642
643 return true;
644
645}
Note: See TracBrowser for help on using the repository browser.