Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('querystring-stringify', function (Y, NAME) {
2
 
3
/**
4
 * Provides Y.QueryString.stringify method for converting objects to Query Strings.
5
 *
6
 * @module querystring
7
 * @submodule querystring-stringify
8
 */
9
 
10
var QueryString = Y.namespace("QueryString"),
11
    stack = [],
12
    L = Y.Lang;
13
 
14
/**
15
 * Provides Y.QueryString.escape method to be able to override default encoding
16
 * method.  This is important in cases where non-standard delimiters are used, if
17
 * the delimiters would not normally be handled properly by the builtin
18
 * (en|de)codeURIComponent functions.
19
 * Default: encodeURIComponent
20
 *
21
 * @method escape
22
 * @for QueryString
23
 * @static
24
 **/
25
QueryString.escape = encodeURIComponent;
26
 
27
/**
28
 * <p>Converts an arbitrary value to a Query String representation.</p>
29
 *
30
 * <p>Objects with cyclical references will trigger an exception.</p>
31
 *
32
 * @method stringify
33
 * @for QueryString
34
 * @public
35
 * @param obj {Any} any arbitrary value to convert to query string
36
 * @param cfg {Object} (optional) Configuration object.  The three
37
 * supported configurations are:
38
 * <ul><li>sep: When defined, the value will be used as the key-value
39
 * separator.  The default value is "&".</li>
40
 * <li>eq: When defined, the value will be used to join the key to
41
 * the value.  The default value is "=".</li>
42
 * <li>arrayKey: When set to true, the key of an array will have the
43
 * '[]' notation appended to the key.  The default value is false.
44
 * </li></ul>
45
 * @param name {String} (optional) Name of the current key, for handling children recursively.
46
 * @static
47
 */
48
QueryString.stringify = function (obj, c, name) {
49
    var begin, end, i, l, n, s,
50
        sep = c && c.sep ? c.sep : "&",
51
        eq = c && c.eq ? c.eq : "=",
52
        aK = c && c.arrayKey ? c.arrayKey : false;
53
 
54
    if (L.isNull(obj) || L.isUndefined(obj) || L.isFunction(obj)) {
55
        return name ? QueryString.escape(name) + eq : '';
56
    }
57
 
58
    if (L.isBoolean(obj) || Object.prototype.toString.call(obj) === '[object Boolean]') {
59
        obj =+ obj;
60
    }
61
 
62
    if (L.isNumber(obj) || L.isString(obj)) {
63
        // Y.log("Number or string: "+obj);
64
        return QueryString.escape(name) + eq + QueryString.escape(obj);
65
    }
66
 
67
    if (L.isArray(obj)) {
68
        s = [];
69
        name = aK ? name + '[]' : name;
70
        l = obj.length;
71
        for (i = 0; i < l; i++) {
72
            s.push( QueryString.stringify(obj[i], c, name) );
73
        }
74
 
75
        return s.join(sep);
76
    }
77
    // now we know it's an object.
78
    // Y.log(
79
    //     typeof obj + (typeof obj === 'object' ? " ok" : "ONOES!")+
80
    //     Object.prototype.toString.call(obj)
81
    // );
82
 
83
    // Check for cyclical references in nested objects
84
    for (i = stack.length - 1; i >= 0; --i) {
85
        if (stack[i] === obj) {
86
            throw new Error("QueryString.stringify. Cyclical reference");
87
        }
88
    }
89
 
90
    stack.push(obj);
91
    s = [];
92
    begin = name ? name + '[' : '';
93
    end = name ? ']' : '';
94
    for (i in obj) {
95
        if (obj.hasOwnProperty(i)) {
96
            n = begin + i + end;
97
            s.push(QueryString.stringify(obj[i], c, n));
98
        }
99
    }
100
 
101
    stack.pop();
102
    s = s.join(sep);
103
    if (!s && name) {
104
        return name + "=";
105
    }
106
 
107
    return s;
108
};
109
 
110
 
111
}, '3.18.1', {"requires": ["yui-base"]});