|
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 keys[i] = parseInt($1, 10) | 0; |
|
71 return '.@' + (i++); |
|
72 }) |
|
73 .replace(/^\./, ''); // remove leading dot |
|
74 |
|
75 // Validate against problematic characters. |
|
76 if (!/[^\w\.\$@]/.test(locator)) { |
|
77 path = locator.split('.'); |
|
78 for (i = path.length - 1; i >= 0; --i) { |
|
79 if (path[i].charAt(0) === '@') { |
|
80 path[i] = keys[parseInt(path[i].substr(1), 10)]; |
|
81 } |
|
82 } |
|
83 |
|
84 // First look for a global function, then the Y, then try the Y |
|
85 // again from the second token (to support "callback=Y.handler") |
|
86 callback = getByPath(Y.config.win, path) || |
|
87 getByPath(Y, path) || |
|
88 getByPath(Y, path.slice(1)); |
|
89 } |
|
90 } |
|
91 |
|
92 return callback || noop; |
|
93 }, |
|
94 |
|
95 /** |
|
96 * URL formatter that looks for callback= in the url and appends it |
|
97 * if not present. The supplied proxy name will be assigned to the query |
|
98 * param. Override this method by passing a function as the |
|
99 * "format" property in the config object to the constructor. |
|
100 * |
|
101 * @method _format |
|
102 * @param url { String } the original url |
|
103 * @param proxy {String} the function name that will be used as a proxy to |
|
104 * the configured callback methods. |
|
105 * @return {String} fully qualified JSONP url |
|
106 * @protected |
|
107 */ |
|
108 _format: function (url, proxy) { |
|
109 var callbackRE = /\{callback\}/, |
|
110 callback, lastChar; |
|
111 |
|
112 if (callbackRE.test(url)) { |
|
113 return url.replace(callbackRE, proxy); |
|
114 } |
|
115 |
|
116 callback = this._template.replace(callbackRE, proxy); |
|
117 |
|
118 if (this._pattern.test(url)) { |
|
119 return url.replace(this._pattern, callback); |
|
120 } else { |
|
121 lastChar = url.slice(-1); |
|
122 if (lastChar !== '&' && lastChar !== '?') { |
|
123 url += (url.indexOf('?') > -1) ? '&' : '?'; |
|
124 } |
|
125 return url + callback; |
|
126 } |
|
127 } |
|
128 |
|
129 }, true); |
|
130 |
|
131 |
|
132 }, '@VERSION@', {"requires": ["jsonp"]}); |