1 |
efrain |
1 |
YUI.add('jsonp-url', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
var JSONPRequest = Y.JSONPRequest,
|
|
|
4 |
getByPath = Y.Object.getValue,
|
|
|
5 |
noop = function () {};
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* Adds support for parsing complex callback identifiers from the jsonp url.
|
|
|
9 |
* This includes callback=foo[1]bar.baz["goo"] as well as referencing methods
|
|
|
10 |
* in the YUI instance.
|
|
|
11 |
*
|
|
|
12 |
* @module jsonp
|
|
|
13 |
* @submodule jsonp-url
|
|
|
14 |
* @for JSONPRequest
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
Y.mix(JSONPRequest.prototype, {
|
|
|
18 |
/**
|
|
|
19 |
* RegExp used by the default URL formatter to insert the generated callback
|
|
|
20 |
* name into the JSONP url. Looks for a query param callback=. If a value
|
|
|
21 |
* is assigned, it will be clobbered.
|
|
|
22 |
*
|
|
|
23 |
* @property _pattern
|
|
|
24 |
* @type RegExp
|
|
|
25 |
* @default /\bcallback=.*?(?=&|$)/i
|
|
|
26 |
* @protected
|
|
|
27 |
*/
|
|
|
28 |
_pattern: /\bcallback=(.*?)(?=&|$)/i,
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Template used by the default URL formatter to add the callback function
|
|
|
32 |
* name to the url.
|
|
|
33 |
*
|
|
|
34 |
* @property _template
|
|
|
35 |
* @type String
|
|
|
36 |
* @default "callback={callback}"
|
|
|
37 |
* @protected
|
|
|
38 |
*/
|
|
|
39 |
_template: "callback={callback}",
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* <p>Parses the url for a callback named explicitly in the string.
|
|
|
43 |
* Override this if the target JSONP service uses a different query
|
|
|
44 |
* parameter or url format.</p>
|
|
|
45 |
*
|
|
|
46 |
* <p>If the callback is declared inline, the corresponding function will
|
|
|
47 |
* be returned. Otherwise null.</p>
|
|
|
48 |
*
|
|
|
49 |
* @method _defaultCallback
|
|
|
50 |
* @param url {String} the url to search in
|
|
|
51 |
* @return {Function} the callback function if found, or null
|
|
|
52 |
* @protected
|
|
|
53 |
*/
|
|
|
54 |
_defaultCallback: function (url) {
|
|
|
55 |
var match = url.match(this._pattern),
|
|
|
56 |
keys = [],
|
|
|
57 |
i = 0,
|
|
|
58 |
locator, path, callback;
|
|
|
59 |
|
|
|
60 |
if (match) {
|
|
|
61 |
// Strip the ["string keys"] and [1] array indexes
|
|
|
62 |
locator = match[1]
|
|
|
63 |
.replace(/\[(['"])(.*?)\1\]/g,
|
|
|
64 |
function (x, $1, $2) {
|
|
|
65 |
keys[i] = $2;
|
|
|
66 |
return '.@' + (i++);
|
|
|
67 |
})
|
|
|
68 |
.replace(/\[(\d+)\]/g,
|
|
|
69 |
function (x, $1) {
|
|
|
70 |
/*jslint bitwise: true */
|
|
|
71 |
keys[i] = parseInt($1, 10) | 0;
|
|
|
72 |
return '.@' + (i++);
|
|
|
73 |
})
|
|
|
74 |
.replace(/^\./, ''); // remove leading dot
|
|
|
75 |
|
|
|
76 |
// Validate against problematic characters.
|
|
|
77 |
if (!/[^\w\.\$@]/.test(locator)) {
|
|
|
78 |
path = locator.split('.');
|
|
|
79 |
for (i = path.length - 1; i >= 0; --i) {
|
|
|
80 |
if (path[i].charAt(0) === '@') {
|
|
|
81 |
path[i] = keys[parseInt(path[i].substr(1), 10)];
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
// First look for a global function, then the Y, then try the Y
|
|
|
86 |
// again from the second token (to support "callback=Y.handler")
|
|
|
87 |
callback = getByPath(Y.config.win, path) ||
|
|
|
88 |
getByPath(Y, path) ||
|
|
|
89 |
getByPath(Y, path.slice(1));
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
return callback || noop;
|
|
|
94 |
},
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* URL formatter that looks for callback= in the url and appends it
|
|
|
98 |
* if not present. The supplied proxy name will be assigned to the query
|
|
|
99 |
* param. Override this method by passing a function as the
|
|
|
100 |
* "format" property in the config object to the constructor.
|
|
|
101 |
*
|
|
|
102 |
* @method _format
|
|
|
103 |
* @param url { String } the original url
|
|
|
104 |
* @param proxy {String} the function name that will be used as a proxy to
|
|
|
105 |
* the configured callback methods.
|
|
|
106 |
* @return {String} fully qualified JSONP url
|
|
|
107 |
* @protected
|
|
|
108 |
*/
|
|
|
109 |
_format: function (url, proxy) {
|
|
|
110 |
var callbackRE = /\{callback\}/,
|
|
|
111 |
callback, lastChar;
|
|
|
112 |
|
|
|
113 |
if (callbackRE.test(url)) {
|
|
|
114 |
return url.replace(callbackRE, proxy);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
callback = this._template.replace(callbackRE, proxy);
|
|
|
118 |
|
|
|
119 |
if (this._pattern.test(url)) {
|
|
|
120 |
return url.replace(this._pattern, callback);
|
|
|
121 |
} else {
|
|
|
122 |
lastChar = url.slice(-1);
|
|
|
123 |
if (lastChar !== '&' && lastChar !== '?') {
|
|
|
124 |
url += (url.indexOf('?') > -1) ? '&' : '?';
|
|
|
125 |
}
|
|
|
126 |
return url + callback;
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
}, true);
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
}, '3.18.1', {"requires": ["jsonp"]});
|