1+ /*
2+ * Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a
5+ * copy of this software and associated documentation files (the "Software"),
6+ * to deal in the Software without restriction, including without limitation
7+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+ * and/or sell copies of the Software, and to permit persons to whom the
9+ * Software is furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in
12+ * all copies or substantial portions of the Software.
13+ *
14+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+ * DEALINGS IN THE SOFTWARE.
21+ *
22+ */
23+
24+
25+ /*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
26+ /*global define, window */
27+
28+ define ( function ( require , exports , module ) {
29+ "use strict" ;
30+
31+ /**
32+ * Convert between URL querystring and name/value pairs. Decodes and encodes URL parameters.
33+ */
34+ function UrlParams ( ) {
35+ this . _store = { } ;
36+ }
37+
38+ /**
39+ * Parse the window location by default. Optionally specify a URL to parse.
40+ * @param {string } url
41+ */
42+ UrlParams . prototype . parse = function ( url ) {
43+ if ( url ) {
44+ url = url . substring ( indexOf ( "?" ) + 1 ) ;
45+ } else {
46+ url = window . document . location . search . substring ( 1 ) ;
47+ }
48+
49+ var urlParams = url . split ( "&" ) ,
50+ p ,
51+ self = this ;
52+
53+ urlParams . forEach ( function ( param ) {
54+ p = param . split ( "=" ) ;
55+ self . _store [ decodeURIComponent ( p [ 0 ] ) ] = decodeURIComponent ( p [ 1 ] ) ;
56+ } ) ;
57+ } ;
58+
59+ /**
60+ * Store a name/value string pair
61+ * @param {!string } name
62+ * @param {!string } value
63+ */
64+ UrlParams . prototype . put = function ( name , value ) {
65+ this . _store [ name ] = value ;
66+ } ;
67+
68+ /**
69+ * Retreive a value by name
70+ * @param {!string } name
71+ */
72+ UrlParams . prototype . get = function ( name ) {
73+ return this . _store [ name ] ;
74+ } ;
75+
76+ /**
77+ * Encode name/value pairs as URI components.
78+ */
79+ UrlParams . prototype . toString = function ( ) {
80+ var strs = [ ] ,
81+ self = this ;
82+
83+ Object . keys ( self . _store ) . forEach ( function ( key ) {
84+ strs . push ( encodeURIComponent ( key ) + "=" + encodeURIComponent ( self . _store [ key ] ) ) ;
85+ } ) ;
86+
87+ return strs . join ( "&" ) ;
88+ } ;
89+
90+ // Define public API
91+ exports . UrlParams = UrlParams ;
92+ } ) ;
0 commit comments