JS side of the OpenSRF request stuff
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 18 Jan 2006 18:00:40 +0000 (18:00 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 18 Jan 2006 18:00:40 +0000 (18:00 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@2743 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/javascript/backend/libs/JSON.js [new file with mode: 0644]
Open-ILS/src/javascript/backend/libs/jsonopensrfrequest.js [new file with mode: 0644]

diff --git a/Open-ILS/src/javascript/backend/libs/JSON.js b/Open-ILS/src/javascript/backend/libs/JSON.js
new file mode 100644 (file)
index 0000000..1f7b805
--- /dev/null
@@ -0,0 +1,136 @@
+// in case we run on an implimentation that doesn't have "undefined";
+var undefined;
+
+function Cast (obj, class_constructor) {
+       try {
+               if (eval(class_constructor + '["_isfieldmapper"]')) {
+                       obj = eval("new " + class_constructor + "(obj)");
+               }
+       } catch( E ) {
+               alert( E + "\n");
+       } finally {
+               return obj;
+       }
+}
+
+function JSON2js (json) {
+
+       json = String(json).replace( /\/\*--\s*S\w*?\s*?\s+\w+\s*--\*\//g, 'Cast(');
+       json = String(json).replace( /\/\*--\s*E\w*?\s*?\s+(\w+)\s*--\*\//g, ', "$1")');
+
+       var obj;
+       if (json != '') {
+               try {
+                       eval( 'obj = ' + json );
+               } catch(E) {
+                       debug("Error building JSON object with string " + E + "\nString:\n" + json );
+                       return null;
+               }
+       }
+       return obj;
+}
+
+
+function object2Array(obj) {
+       if( obj == null ) return null;
+
+       var arr = new Array();
+       for( var i  = 0; i < obj.length; i++ ) {
+               arr[i] = obj[i];
+       }
+       return arr;
+}
+
+
+function js2JSON(arg) {
+       return _js2JSON(arg);
+}
+
+function _js2JSON(arg) {
+       var i, o, u, v;
+
+               switch (typeof arg) {
+                       case 'object':
+       
+                               if(arg) {
+       
+                                       if (arg._isfieldmapper) { /* magi-c-ast for fieldmapper objects */
+       
+                                               if( arg.a.constructor != Array ) {
+                                                       var arr = new Array();
+                                                       for( var i  = 0; i < arg.a.length; i++ ) {
+                                                               if( arg.a[i] == null ) {
+                                                                       arr[i] = null; continue;
+                                                               }
+       
+                                                               if( typeof arg.a[i] != 'object' ) { 
+                                                                       arr[i] = arg.a[i];
+       
+                                                               } else if( typeof arg.a[i] == 'object' 
+                                                                                       && arg.a[i]._isfieldmapper) {
+       
+                                                                       arr[i] = arg.a[i];
+       
+                                                               } else {
+                                                                       arr[i] = object2Array(arg.a[i]);                
+                                                               }
+                                                       }
+                                                       arg.a = arr;
+                                               }
+       
+                                               return "/*--S " + arg.classname + " --*/" + js2JSON(arg.a) + "/*--E " + arg.classname + " --*/";
+       
+                                       } else {
+       
+                                               if (arg.constructor == Array) {
+                                                       o = '';
+                                                       for (i = 0; i < arg.length; ++i) {
+                                                               v = js2JSON(arg[i]);
+                                                               if (o) {
+                                                                       o += ',';
+                                                               }
+                                                               if (v !== u) {
+                                                                       o += v;
+                                                               } else {
+                                                                       o += 'null';
+                                                               }
+                                                       }
+                                                       return '[' + o + ']';
+       
+                                               } else if (typeof arg.toString != 'undefined') {
+                                                       o = '';
+                                                       for (i in arg) {
+                                                               v = js2JSON(arg[i]);
+                                                               if (v !== u) {
+                                                                       if (o) {
+                                                                               o += ',';
+                                                                       }
+                                                                       o += js2JSON(i) + ':' + v;
+                                                               }
+                                                       }
+       
+                                                       o = '{' + o + '}';
+                                                       return o;
+       
+                                               } else {
+                                                       return;
+                                               }
+                                       }
+                               }
+                               return 'null';
+       
+                       case 'unknown':
+                       case 'number':
+                               if( isNaN(arg) ) throw "JSON.js encountered NaN in js2JSON()";
+                               return arg;
+       
+                       case 'undefined':
+                       case 'function':
+                               return u;
+       
+                       case 'string':
+                       default:
+                               return '"' + String(arg).replace(/(["\\])/g, '\\$1') + '"';
+               }
+
+}
diff --git a/Open-ILS/src/javascript/backend/libs/jsonopensrfrequest.js b/Open-ILS/src/javascript/backend/libs/jsonopensrfrequest.js
new file mode 100644 (file)
index 0000000..a37a095
--- /dev/null
@@ -0,0 +1,41 @@
+try {
+       load_lib('jsOO.js');
+       load_lib('JSON.js');
+} catch (e) {}
+
+var __jsonopensrfreqid = 1;
+var __jsonopensrfreq_hash = {};
+
+function JSONOpenSRFRequest () {
+        // Cache this for later ...
+        this._hash_id = __jsonopensrfreqid;
+        __jsonopensrfreqid++;
+}
+
+JSONOpenSRFRequest.method('open',function (service, method, async) {
+        this._service = service;
+        this._service = method;
+        this._async = (async ? 1 : 0);
+});
+
+JSONOpenSRFRequest.method('send', function () {
+
+        __jsonopensrfreq_hash['id' + this._hash_id] = {};
+
+       try {
+               _OILS_FUNC_jsonopensrfrequest_send(this._hash_id,this._service,this._method,this._async,js2JSON(arguments));
+       } catch (e) {
+               alert("Sorry, no JSONOpenSRFRequest support");
+       }
+
+        this.responseText = __jsonopensrfreq_hash['id' + this._hash_id].responseText;
+        this.readyState = __jsonopensrfreq_hash['id' + this._hash_id].readyState;
+        this.status = __jsonopensrfreq_hash['id' + this._hash_id].status;
+        this.statusText = __jsonopensrfreq_hash['id' + this._hash_id].statusText;
+        this.responseJSON = JSON2js(this.responseText);
+
+        if (this._async)
+                this.onreadystatechange();
+});
+
+