]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/yql/yql.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / yql / yql.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('yql', function(Y) {
9
10     /**
11      * This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
12      * @module yql
13      */     
14     /**
15      * Utility Class used under the hood my the YQL class
16      * @class YQLRequest
17      * @constructor
18      * @param {String} sql The SQL statement to execute
19      * @param {Function/Object} callback The callback to execute after the query (Falls through to JSONP).
20      * @param {Object} params An object literal of extra parameters to pass along (optional).
21      * @param {Object} opts An object literal of configuration options (optional): proto (http|https), base (url)
22      */
23     var YQLRequest = function (sql, callback, params, opts) {
24         
25         if (!params) {
26             params = {};
27         }
28         params.q = sql;
29         //Allow format override.. JSON-P-X
30         if (!params.format) {
31             params.format = Y.YQLRequest.FORMAT;
32         }
33         if (!params.env) {
34             params.env = Y.YQLRequest.ENV;
35         }
36
37         this._params = params;
38         this._opts = opts;
39         this._callback = callback;
40
41     };
42     
43     YQLRequest.prototype = {
44         /**
45         * @private
46         * @property _jsonp
47         * @description Reference to the JSONP instance used to make the queries
48         */
49         _jsonp: null,
50         /**
51         * @private
52         * @property _opts
53         * @description Holder for the opts argument
54         */
55         _opts: null,
56         /**
57         * @private
58         * @property _callback
59         * @description Holder for the callback argument
60         */
61         _callback: null,
62         /**
63         * @private
64         * @property _params
65         * @description Holder for the params argument
66         */
67         _params: null,
68         /**
69         * @method send
70         * @description The method that executes the YQL Request.
71         * @chainable
72         * @returns {YQLRequest}
73         */
74         send: function() {
75             var qs = '', url = ((this._opts && this._opts.proto) ? this._opts.proto : Y.YQLRequest.PROTO);
76
77             Y.each(this._params, function(v, k) {
78                 qs += k + '=' + encodeURIComponent(v) + '&';
79             });
80             
81             url += ((this._opts && this._opts.base) ? this._opts.base : Y.YQLRequest.BASE_URL) + qs;
82             
83             var o = (!Y.Lang.isFunction(this._callback)) ? this._callback : { on: { success: this._callback } };
84             if (o.allowCache !== false) {
85                 o.allowCache = true;
86             }
87             
88             if (!this._jsonp) {
89                 this._jsonp = Y.jsonp(url, o);
90             } else {
91                 this._jsonp.url = url;
92                 if (o.on && o.on.success) {
93                     this._jsonp._config.on.success = o.on.success;
94                 }
95                 this._jsonp.send();
96             }
97             return this;
98         }
99     };
100
101     /**
102     * @static
103     * @property FORMAT
104     * @description Default format to use: json
105     */
106     YQLRequest.FORMAT = 'json';
107     /**
108     * @static
109     * @property PROTO
110     * @description Default protocol to use: http
111     */
112     YQLRequest.PROTO = 'http';
113     /**
114     * @static
115     * @property BASE_URL
116     * @description The base URL to query: query.yahooapis.com/v1/public/yql?
117     */
118     YQLRequest.BASE_URL = ':/'+'/query.yahooapis.com/v1/public/yql?';
119     /**
120     * @static
121     * @property ENV
122     * @description The environment file to load: http://datatables.org/alltables.env
123     */
124     YQLRequest.ENV = 'http:/'+'/datatables.org/alltables.env';
125     
126     Y.YQLRequest = YQLRequest;
127         
128     /**
129      * This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
130      * @class YQL
131      * @constructor
132      * @param {String} sql The SQL statement to execute
133      * @param {Function} callback The callback to execute after the query (optional).
134      * @param {Object} params An object literal of extra parameters to pass along (optional).
135      */
136         Y.YQL = function(sql, callback, params) {
137         return new Y.YQLRequest(sql, callback, params).send();
138     };
139
140
141
142 }, '3.3.0' ,{requires:['jsonp']});