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 if( ! this.data[key] ) this.data[key] = [];
66 this.data[key].push(decodeURIComponent(value));
73 else if(invalue) value += c;
77 if( ! this.data[key] ) this.data[key] = [];
78 this.data[key].push(decodeURIComponent(value));
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
86 param : function(p, val, push) {
87 if (p == null || p == '') // invalid param name
91 if (arguments.length > 1) {
92 if (this.keys().indexOf(p) == -1)
95 if (dojo.isArray(this.data[p])) {
97 this.data[p].push(val);
106 if(this.data[p] == null)
108 if(this.data[p].length == 1)
109 return this.data[p][0];
113 /* returns an array of param names */
118 /* returns the URI-encoded query string */
119 queryString : function() {
123 dojo.forEach(this.keys(),
125 var params = _this.param(key);
126 if (!dojo.isArray(params))
131 if (param == null) return;
132 query += ';' + key + '=' + encodeURIComponent(param);
138 return query.replace(/^;/, '?');
142 return this.base_url + this.queryString();
145 /* debugging method */
146 toString : function() {
148 var keys = this.keys();
150 for( var k in keys ) {
151 query += keys[k] + " : ";
152 var params = this.param(keys[k]);
154 for( var p in params ) {
155 query += params[p] + " ";