source: main/trunk/greenstone2/web/script/gsajaxapi.js@ 27322

Last change on this file since 27322 was 27322, checked in by ak19, 11 years ago

Added Ajax PostAsync method urlPostAsync and tested it works.

File size: 16.7 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 // New, an Ajax Synchronous Post method.
158// http://www.degraeve.com/reference/simple-ajax-example.php
159// Async vs Sync: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
160// Also:
161// http://stackoverflow.com/questions/6312447/in-an-ajax-post-do-i-need-to-urlencode-parameters-before-sending
162// http://api.jquery.com/jQuery.post/
163// http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
164 this.urlPostSync = function(scriptURL, params) {
165 var xmlHttp=false;
166 try {
167 // Firefox, Opera 8.0+, Safari
168 xmlHttp=new XMLHttpRequest();
169 }
170 catch (e) {
171 // Internet Explorer
172 try {
173 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
174 }
175 catch (e) {
176 try {
177 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
178 }
179 catch (e) {
180 alert("Your browser does not support AJAX!");
181 return false;
182 }
183 }
184 }
185
186 // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
187 xmlHttp.open('POST', scriptURL, false); // false means synchronous
188 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
189
190 if(un_ != null) {
191 params += "&un=" + un_;
192 }
193 if(ky_ != null) {
194 params += "&ky=" + ky_;
195 }
196
197 xmlHttp.send(params); // needs to be escaped/encoded
198
199 //alert(scriptURL + "?" + params);
200 //alert(xmlHttp.responseText); // if synchronous, process xmlHttp.responseText AFTER send() call
201 return xmlHttp.responseText;
202}
203
204 // New, an Ajax Asynchronous Post method.
205 // For helpful links, see the urlPostSync() method above
206 this.urlPostAsync = function(scriptURL, params, callback) {
207 var xmlHttp=false;
208 try {
209 // Firefox, Opera 8.0+, Safari
210 xmlHttp=new XMLHttpRequest();
211 }
212 catch (e) {
213 // Internet Explorer
214 try {
215 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
216 }
217 catch (e) {
218 try {
219 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
220 }
221 catch (e) {
222 alert("Your browser does not support AJAX!");
223 return false;
224 }
225 }
226 }
227
228
229
230 // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
231 xmlHttp.open('POST', scriptURL, true); // true means asynchronous
232 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
233
234
235 // If asynchronous:
236 // If the callback param is a function, we will set it up to get called when
237 // the async post has finished (is ready)
238 // if the callback parameter isn't a function, the param represents a field
239 // that we want to dynamically update when the async post process has finished
240
241 var typeof_callback = typeof(callback);
242 if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) {
243 var locid = callback;
244
245 xmlHttp.onreadystatechange=function() {
246 if(xmlHttp.readyState==4) {
247 if (locelem != null) {
248 var locelem = document.getElementById(locid);
249
250 locelem.innerHTML = xmlHttp.responseText;
251 }
252 }
253 }
254 }
255 else if (typeof_callback == "function") {
256 xmlHttp.onreadystatechange=function() {
257 if(xmlHttp.readyState==4) {
258 callback(xmlHttp); // e.g. this might do: updatepage(xmlHttp.responseText);
259 }
260 }
261 }
262 else {
263 alert("Unrecognized type of callback value: " + typeof_callback);
264 }
265
266 if(un_ != null) {
267 params += "&un=" + un_;
268 }
269 if(ky_ != null) {
270 params += "&ky=" + ky_;
271 }
272 //alert("Posting Async: " + scriptURL + "?" + params);
273
274 xmlHttp.send(params); // needs to be escaped/encoded
275 // if synchronous, would process xmlHttp AFTER send() call, such as by
276 // accessing xmlHttp.responseText to return that to the caller at this point.
277}
278
279
280 this.setLiveMetadata = function(id,metaname,metavalue)
281 {
282 var mdserver = this.metadataserverURL();
283
284 var url = mdserver + "?a=set-live-metadata";
285 url += "&c="+collect_;
286 url += "&d="+id;
287 url += "&metaname=" + metaname;
288 url += "&metavalue=" + metavalue;
289
290 this.urlGetSync(url);
291 }
292
293 // New
294 // The where parameter can be specified as one or more of: import, archives, index, live
295 // separated by |. If null, it is assumed to be index which is the original default
296 // behaviour of calling set-metadata. E.g. where=import|archives|index
297 this.setMetadata = function(docid,metaname,metapos,metavalue,metamode,where)
298 {
299 var mdserver = this.metadataserverURL();
300
301 var params = "a=set-metadata";
302 if(where != null) {
303 params += "&where=" + where; // if where not specified, meta-server will default to setting index meta
304 //} else {
305 // params += "&where=import|archives|index";
306 }
307 params += "&c="+collect_;
308 params += "&d="+docid;
309 params += "&metaname=" + metaname;
310 if (metapos!=null) {
311 params += "&metapos=" + metapos;
312 }
313 params += "&metavalue=" + metavalue;
314 if (metamode!=null) {
315 params += "&metamode=" + metamode;
316 }
317
318 //this.urlGetSync(mdserver + "?" + params);
319 this.urlPostSync(mdserver,params);
320 }
321
322 // New
323 // The where parameter can be specified as one or more of: import, archives, index, live
324 // separated by |. If null, it is assumed to be index which is the original default
325 // behaviour of calling set-metadata-array). E.g. where=import|archives|index
326 this.setMetadataArray = function(docArray,metamode,where)
327 {
328 docArrayJSON = JSON.stringify(docArray);
329
330 var mdserver = this.metadataserverURL();
331
332 var params = "a=" + escape("set-metadata-array"); //"a=set-metadata-array";
333 if(where != null) {
334 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
335 //} else {
336 // params += "&where=import|archives|index";
337 }
338 params += "&c="+escape(collect_);
339 params += "&json="+escape(docArrayJSON);
340
341 if (metamode!=null) {
342 params += "&metamode=" + escape(metamode);
343 }
344
345 //this.urlGetSync(mdserver + "?" + params);
346 this.urlPostSync(mdserver,params);
347 }
348
349 // New
350 this.getArchivesMetadata = function(docoid,metaname,metapos)
351 {
352 var mdserver = this.metadataserverURL();
353
354 var url = mdserver + "?a=get-archives-metadata";
355 url += "&c="+collect_;
356 url += "&d="+docoid;
357 url += "&metaname=" + metaname;
358 if (metapos!=null) {
359 url += "&metapos=" + metapos;
360 }
361
362 //alert("In getArchivesMeta. URL: " + url)
363 return this.urlGetSync(url); //Once this works, make it POST
364 }
365
366 this.getMetadataArray = function(docArray,where)
367 {
368 docArrayJSON = JSON.stringify(docArray);
369
370 var mdserver = this.metadataserverURL();
371
372 var params = "a=" + escape("get-metadata-array"); //"a=set-metadata-array";
373 if(where != null) {
374 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
375 //} else {
376 // params += "&where=import|archives|index";
377 }
378 params += "&c="+escape(collect_);
379 params += "&json="+escape(docArrayJSON);
380
381 //this.urlGetSync(mdserver + "?" + params);
382 return this.urlPostSync(mdserver,params);
383 }
384
385
386 this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode)
387 {
388 var mdserver = this.metadataserverURL();
389
390 var params = "a=set" + mode + "-metadata";
391 params += "&c="+collect_;
392 params += "&d="+docid;
393 params += "&metaname=" + metaname;
394 if (metapos!=null) {
395 params += "&metapos=" + metapos;
396 }
397 params += "&metavalue=" + metavalue;
398 if (metamode!=null) {
399 params += "&metamode=" + metamode;
400 }
401
402 this.urlGetSync(mdserver + "?" + params);
403 //this.urlPostSync(mdserver,params);
404 }
405
406
407 this._setDocumentArrayMetadata = function(mode,docArray,metamode)
408 {
409 docArrayJSON = JSON.stringify(docArray);
410
411 var mdserver = this.metadataserverURL();
412
413 var params = "a=set" + mode + "-metadata-array";
414 params += "&c="+collect_;
415 params += "&json="+docArrayJSON;
416
417 if (metamode!=null) {
418 params += "&metamode=" + metamode;
419 }
420
421 this.urlGetSync(mdserver + "?" + params);
422
423 }
424
425
426 this.setDocumentMetadata = function(docid,metaname,metapos,metavalue)
427 {
428 // Allow for three param call to function, where metapos is missed out
429 if (metavalue==null) {
430 // 4 param case
431 metavalue = metapos;
432 metapos = null;
433 }
434
435 this._setMetadata("",docid,metaname,metapos,metavalue);
436 this._setMetadata("-archives",docid,metaname,metapos,metavalue,"override");
437
438 }
439
440 this.setDocumentArrayMetadata = function(docArray,metamode)
441 {
442 //showDialog('Greenstone Javascript API','This sequence of changes has been commited into the system.','success', 2);
443
444 this._setDocumentArrayMetadata("",docArray,metamode);
445 this._setDocumentArrayMetadata("-archives",docArray,metamode);
446 }
447
448 this.setNewDocumentMetadata = function(docid,metaname,metavalue)
449 {
450 this._setMetadata("",docid,metaname,null,metavalue);
451 this._setMetadata("-archives",docid,metaname,null,metavalue,"accumulate");
452 }
453
454 this.setImportMetadata = function(docid,metaname,metapos,metavalue)
455 {
456 this._setMetadata("-import",docid,metaname,metapos,metavalue,"override");
457 }
458
459
460 this.explodeDocument = function(docid)
461 {
462 var exserver = this.explodeserverURL();
463
464 var url = exserver + "?a=explode-document";
465 url += "&c="+collect_;
466 url += "&d="+docid;
467
468 this.urlGetSync(url);
469 }
470
471 this.deleteDocument = function(docid,onlyAdd)
472 {
473 var exserver = this.explodeserverURL();
474
475 var url = exserver + "?a=delete-document";
476 url += "&c="+collect_;
477 params += "&onlyadd="+onlyAdd;
478 url += "&d="+docid;
479
480 this.urlGetSync(url);
481 }
482
483 this.deleteDocumentArray = function(docArray,onlyAdd)
484 {
485 docArrayJSON = JSON.stringify(docArray);
486
487 var exserver = this.explodeserverURL();
488
489 var params = "a=delete-document-array";
490 params += "&c="+collect_;
491 params += "&onlyadd="+onlyAdd;
492 params += "&json="+docArrayJSON;
493
494 this.urlGetSync(exserver + "?" + params);
495
496 }
497
498
499 this.cloneDocument = function(docid,toCollect)
500 {
501 var msserver = this.myspaceserverURL();
502
503 var url = msserver + "?a=clone";
504 url += "&c="+collect_;
505 url += "&d="+docid;
506 url += "&toCollect="+toCollect;
507
508 this.urlGetSync(url);
509 }
510
511 // consider name change to reindexDocument
512 this.documentReindex = function(docid)
513 {
514 var mdserver = this.metadataserverURL();
515
516 var url = mdserver + "?a=reindex-document";
517 url += "&c="+collect_;
518 url += "&d="+docid;
519
520 this.urlGetSync(url);
521 }
522
523
524 this.reindexCollection = function(mode,callback)
525 {
526 if (mode==null) {
527 mode = "incremental";
528 }
529
530 var idserver = this.indexserverURL();
531
532 var url = idserver + "?a=" + mode + "-rebuild";
533 url += "&c="+collect_;
534
535 this.urlGetAsync(url,callback);
536 }
537
538
539 this.buildByManifestGeneral = function(hashargs)
540 {
541 var idserver = this.buildserverURL();
542
543 var url = idserver + "?a=build-by-manifest";
544 url += "&c="+collect_;
545
546 if (hashargs["index-files"] != undefined) {
547 url += "&index-files=" + JSON.stringify(hashargs["index-files"]);
548 }
549
550 if (hashargs["reindex-files"] != undefined) {
551 url += "&reindex-files=" + JSON.stringify(hashargs["reindex-files"]);
552 }
553 if (hashargs["delete-OIDs"] != undefined) {
554 url += "&delete-OIDs=" + JSON.stringify(hashargs["delete-OIDs"]);
555 }
556
557 this.urlGetSync(url);
558 }
559
560 this.indexByManifest = function(docidArray)
561 {
562 var hashargs = {};
563 hashargs["index-files"] = docidArray;
564 this.buildByManifestGeneral(hashargs);
565 }
566
567 this.reindexByManifest = function(docidArray)
568 {
569 var hashargs = {};
570 hashargs["reindex-files"] = docidArray;
571 this.buildByManifestGeneral(hashargs);
572 }
573 this.deleteByManifest = function(docidArray)
574 {
575 var hashargs = {};
576 hashargs["delete-OIDs"] = docidArray;
577 this.buildByManifestGeneral(hashargs);
578 }
579
580 this.getLiveMetadata = function(id,metaname)
581 {
582 var mdserver = this.metadataserverURL();
583
584 var url = mdserver + "?a=get-live-metadata";
585 url += "&c="+collect_;
586 url += "&d="+id;
587 url += "&metaname=" + metaname;
588
589 var metavalue = this.urlGetSync(url);
590
591 return metavalue;
592 }
593
594 this.getDocumentMetadata = function(docoid,metaname,metapos)
595 {
596 var mdserver = this.metadataserverURL();
597
598 var url = mdserver + "?a=get-metadata";
599 url += "&c="+collect_;
600 url += "&d="+docoid;
601 url += "&metaname=" + metaname;
602 if (metapos!=null) {
603 url += "&metapos=" + metapos;
604 }
605
606 return this.urlGetSync(url);
607 }
608
609 this.removeLiveMetadata = function(id,metaname)
610 {
611 var mdserver = this.metadataserverURL();
612
613 var url = mdserver + "?a=remove-live-metadata";
614 url += "&c="+collect_;
615 url += "&d="+id;
616 url += "&metaname=" + metaname;
617
618 this.urlGetSync(url);
619 }
620
621 this.removeDocumentMetadata = function(docid,metaname,metapos)
622 {
623 var mdserver = this.metadataserverURL();
624
625 var url = mdserver + "?a=remove-metadata";
626 url += "&c="+collect_;
627 url += "&d="+docid;
628 url += "&metaname=" + metaname;
629 if (metapos!=null) {
630 url += "&metapos=" + metapos;
631 }
632
633 this.urlGetSync(url);
634 }
635
636 return true;
637
638}
Note: See TracBrowser for help on using the repository browser.