]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/CGI.js
LP#1350371 PO name on create w/ dupe detect
[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                     key = decodeURIComponent(key);
66                     if( ! this.data[key] ) this.data[key] = [];
67                     this.data[key].push(decodeURIComponent(value));
68                     this._keys.push(key);
69                     key = ""; value = "";
70                     continue;
71                 }
72
73                 if(inkey) key += c;
74                 else if(invalue) value += c;
75             }
76
77             if (key.length) {
78                 if( ! this.data[key] ) this.data[key] = [];
79                 this.data[key].push(decodeURIComponent(value));
80                 this._keys.push(key);
81             }
82         },
83
84         /* returns the value for the given param.  If there is only one value for the
85            given param, it returns that value.  Otherwise it returns an array of values
86          */
87         param : function(p, val, push) {
88             if (p == null || p == '') // invalid param name
89                 return null;
90
91             // set param value
92             if (arguments.length > 1) { 
93                 if (this.keys().indexOf(p) == -1)
94                     this._keys.push(p);
95
96                 if (dojo.isArray(this.data[p])) {
97                     if (push) {
98                         this.data[p].push(val);
99                     } else {
100                         this.data[p] = val;
101                     }
102                 } else {
103                     this.data[p] = val;
104                 }
105             }
106
107             if(this.data[p] == null)
108                 return null;
109             if(this.data[p].length == 1)
110                 return this.data[p][0];
111             return this.data[p];
112         },
113
114         /* returns an array of param names */
115         keys : function() {
116             return this._keys;
117         },
118
119         /* returns the URI-encoded query string */
120         queryString : function() {
121             var query = "";
122             var _this = this;
123
124             dojo.forEach(this.keys(),
125                 function(key) {
126                     var params = _this.param(key);
127                     if (!dojo.isArray(params))
128                         params = [params];
129
130                     dojo.forEach(params,
131                         function(param) {
132                             if (param == null) return;
133                             query += ';' + key + '=' + encodeURIComponent(param);
134                         }
135                     );
136                 }
137             );
138
139             return query.replace(/^;/, '?');
140         },
141
142         url : function() {
143             return this.base_url + this.queryString();
144         },
145
146         /* debugging method */
147         toString : function() {
148             var query = "";
149             var keys = this.keys();
150
151             for( var k in keys ) {
152                 query += keys[k] + " : ";
153                 var params = this.param(keys[k]);
154
155                 for( var p in params ) {
156                     query +=  params[p] + " ";
157                 }
158                 query += "\n";
159             }
160             return query;
161         }
162     });
163 }
164
165