1 /* ---------------------------------------------------------------------------
2 * Copyright (C) 2008 Georgia Public Library Service
3 * Bill Erickson <erickson@esilibrary.com>
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.
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 * ---------------------------------------------------------------------------
17 if(!dojo._hasResource["openils.CGI"]) {
19 dojo._hasResource["openils.CGI"] = true;
20 dojo.provide("openils.CGI");
21 dojo.declare('openils.CGI', null, {
23 constructor : function(args) {
25 this._keys = new Array();
26 this.data = new Object();
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
32 // if the user specifies URL components, override URL
33 // components pulled from the current page
35 if (url = args.url) { // assignment
36 this.base_url = url.replace(/(.*)\?.*/, '$1');
38 if (url.match(/\?(.*)/))
39 query = url.match(/\?(.*)/)[0];
44 query = query.replace(/^\?/, '');
52 for( var idx = 0; idx!= query.length; idx++ ) {
54 var c = query.charAt(idx);
62 if(c == "&" || c == ";") {
65 key = decodeURIComponent(key);
66 if( ! this.data[key] ) this.data[key] = [];
67 this.data[key].push(decodeURIComponent(value));
74 else if(invalue) value += c;
78 if( ! this.data[key] ) this.data[key] = [];
79 this.data[key].push(decodeURIComponent(value));
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
87 param : function(p, val, push) {
88 if (p == null || p == '') // invalid param name
92 if (arguments.length > 1) {
93 if (this.keys().indexOf(p) == -1)
96 if (dojo.isArray(this.data[p])) {
98 this.data[p].push(val);
107 if(this.data[p] == null)
109 if(this.data[p].length == 1)
110 return this.data[p][0];
114 /* returns an array of param names */
119 /* returns the URI-encoded query string */
120 queryString : function() {
124 dojo.forEach(this.keys(),
126 var params = _this.param(key);
127 if (!dojo.isArray(params))
132 if (param == null) return;
133 query += ';' + key + '=' + encodeURIComponent(param);
139 return query.replace(/^;/, '?');
143 return this.base_url + this.queryString();
146 /* debugging method */
147 toString : function() {
149 var keys = this.keys();
151 for( var k in keys ) {
152 query += keys[k] + " : ";
153 var params = this.param(keys[k]);
155 for( var p in params ) {
156 query += params[p] + " ";