]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/CGI.js
ACQ : lineitem recovers focus from various interfaces
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / CGI.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Bill Erickson <erickson@esilibrary.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * ---------------------------------------------------------------------------
15  */
16
17 if(!dojo._hasResource["openils.CGI"]) {
18
19     dojo._hasResource["openils.CGI"] = true;
20     dojo.provide("openils.CGI");
21     dojo.declare('openils.CGI', null, {
22
23         constructor : function(args) {
24
25             this._keys = new Array();
26             this.data = new Object();
27
28             var query = location.search.replace(/^\?/,"");
29             this.server_name = location.href.replace(/^https?:\/\/([^\/]+).+$/,"$1");
30             this.base_url = location.href.replace(/(.*)\?.*/, '$1'); // proto://hostname/full/path
31
32             // if the user specifies URL components, override URL
33             // components pulled from the current page
34             if (args) {
35                 if (url = args.url) { // assignment
36                     this.base_url = url.replace(/(.*)\?.*/, '$1');
37                     query = '';
38                     if (url.match(/\?(.*)/))
39                         query = url.match(/\?(.*)/)[0];
40                 }
41                 if (args.query)
42                     query = args.query;
43
44                 query = query.replace(/^\?/, '');
45             }
46
47             var key = ""; 
48             var value = "";
49             var inkey = true;
50             var invalue = false;
51
52             for( var idx = 0; idx!= query.length; idx++ ) {
53
54                 var c = query.charAt(idx);
55
56                 if( c == "=" )  {
57                     invalue = true;
58                     inkey = false;
59                     continue;
60                 } 
61
62                 if(c == "&" || c == ";") {
63                     inkey = 1;
64                     invalue = 0;
65                     if( ! this.data[key] ) this.data[key] = [];
66                     this.data[key].push(decodeURIComponent(value));
67                     this._keys.push(key);
68                     key = ""; value = "";
69                     continue;
70                 }
71
72                 if(inkey) key += c;
73                 else if(invalue) value += c;
74             }
75
76             if (key.length) {
77                 if( ! this.data[key] ) this.data[key] = [];
78                 this.data[key].push(decodeURIComponent(value));
79                 this._keys.push(key);
80             }
81         },
82
83         /* returns the value for the given param.  If there is only one value for the
84            given param, it returns that value.  Otherwise it returns an array of values
85          */
86         param : function(p, val, push) {
87             if (p == null || p == '') // invalid param name
88                 return null;
89
90             // set param value
91             if (arguments.length > 1) { 
92                 if (this.keys().indexOf(p) == -1)
93                     this._keys.push(p);
94
95                 if (dojo.isArray(this.data[p])) {
96                     if (push) {
97                         this.data[p].push(val);
98                     } else {
99                         this.data[p] = val;
100                     }
101                 } else {
102                     this.data[p] = val;
103                 }
104             }
105
106             if(this.data[p] == null)
107                 return null;
108             if(this.data[p].length == 1)
109                 return this.data[p][0];
110             return this.data[p];
111         },
112
113         /* returns an array of param names */
114         keys : function() {
115             return this._keys;
116         },
117
118         /* returns the URI-encoded query string */
119         queryString : function() {
120             var query = "";
121             var _this = this;
122
123             dojo.forEach(this.keys(),
124                 function(key) {
125                     var params = _this.param(key);
126                     if (!dojo.isArray(params))
127                         params = [params];
128
129                     dojo.forEach(params,
130                         function(param) {
131                             if (param == null) return;
132                             query += ';' + key + '=' + encodeURIComponent(param);
133                         }
134                     );
135                 }
136             );
137
138             return query.replace(/^;/, '?');
139         },
140
141         url : function() {
142             return this.base_url + this.queryString();
143         },
144
145         /* debugging method */
146         toString : function() {
147             var query = "";
148             var keys = this.keys();
149
150             for( var k in keys ) {
151                 query += keys[k] + " : ";
152                 var params = this.param(keys[k]);
153
154                 for( var p in params ) {
155                     query +=  params[p] + " ";
156                 }
157                 query += "\n";
158             }
159             return query;
160         }
161     });
162 }
163
164