src/cm/media/js/lib/yui/yui3-3.15.0/build/yql/yql.js
changeset 602 e16a97fb364a
equal deleted inserted replaced
601:d334a616c023 602:e16a97fb364a
       
     1 YUI.add('yql', function (Y, NAME) {
       
     2 
       
     3 /**
       
     4  * This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
       
     5  * @module yql
       
     6  */
       
     7 /**
       
     8  * Utility Class used under the hood by the YQL class
       
     9  * @class YQLRequest
       
    10  * @constructor
       
    11  * @param {String} sql The SQL statement to execute
       
    12  * @param {Function/Object} callback The callback to execute after the query (Falls through to JSONP).
       
    13  * @param {Object} params An object literal of extra parameters to pass along (optional).
       
    14  * @param {Object} opts An object literal of configuration options (optional): proto (http|https), base (url)
       
    15  */
       
    16 var YQLRequest = function (sql, callback, params, opts) {
       
    17 
       
    18     if (!params) {
       
    19         params = {};
       
    20     }
       
    21     params.q = sql;
       
    22     //Allow format override.. JSON-P-X
       
    23     if (!params.format) {
       
    24         params.format = Y.YQLRequest.FORMAT;
       
    25     }
       
    26     if (!params.env) {
       
    27         params.env = Y.YQLRequest.ENV;
       
    28     }
       
    29 
       
    30     this._context = this;
       
    31 
       
    32     if (opts && opts.context) {
       
    33         this._context = opts.context;
       
    34         delete opts.context;
       
    35     }
       
    36 
       
    37     if (params && params.context) {
       
    38         this._context = params.context;
       
    39         delete params.context;
       
    40     }
       
    41 
       
    42     this._params = params;
       
    43     this._opts = opts;
       
    44     this._callback = callback;
       
    45 
       
    46 };
       
    47 
       
    48 YQLRequest.prototype = {
       
    49     /**
       
    50     * @private
       
    51     * @property _jsonp
       
    52     * @description Reference to the JSONP instance used to make the queries
       
    53     */
       
    54     _jsonp: null,
       
    55     /**
       
    56     * @private
       
    57     * @property _opts
       
    58     * @description Holder for the opts argument
       
    59     */
       
    60     _opts: null,
       
    61     /**
       
    62     * @private
       
    63     * @property _callback
       
    64     * @description Holder for the callback argument
       
    65     */
       
    66     _callback: null,
       
    67     /**
       
    68     * @private
       
    69     * @property _params
       
    70     * @description Holder for the params argument
       
    71     */
       
    72     _params: null,
       
    73     /**
       
    74     * @private
       
    75     * @property _context
       
    76     * @description The context to execute the callback in
       
    77     */
       
    78     _context: null,
       
    79     /**
       
    80     * @private
       
    81     * @method _internal
       
    82     * @description Internal Callback Handler
       
    83     */
       
    84     _internal: function () {
       
    85         this._callback.apply(this._context, arguments);
       
    86     },
       
    87     /**
       
    88     * @method send
       
    89     * @description The method that executes the YQL Request.
       
    90     * @chainable
       
    91     * @return {YQLRequest}
       
    92     */
       
    93     send: function () {
       
    94         var qs = [], url = ((this._opts && this._opts.proto) ? this._opts.proto : Y.YQLRequest.PROTO), o;
       
    95 
       
    96         Y.Object.each(this._params, function (v, k) {
       
    97             qs.push(k + '=' + encodeURIComponent(v));
       
    98         });
       
    99 
       
   100         qs = qs.join('&');
       
   101 
       
   102         url += ((this._opts && this._opts.base) ? this._opts.base : Y.YQLRequest.BASE_URL) + qs;
       
   103 
       
   104         o = (!Y.Lang.isFunction(this._callback)) ? this._callback : { on: { success: this._callback } };
       
   105 
       
   106         o.on = o.on || {};
       
   107         this._callback = o.on.success;
       
   108 
       
   109         o.on.success = Y.bind(this._internal, this);
       
   110 
       
   111         this._send(url, o);
       
   112         return this;
       
   113     },
       
   114     /**
       
   115     * Private method to send the request, overwritten in plugins
       
   116     * @method _send
       
   117     * @private
       
   118     * @param {String} url The URL to request
       
   119     * @param {Object} o The config object
       
   120     */
       
   121     _send: function() {
       
   122         //Overwritten in plugins
       
   123     }
       
   124 };
       
   125 
       
   126 /**
       
   127 * @static
       
   128 * @property FORMAT
       
   129 * @description Default format to use: json
       
   130 */
       
   131 YQLRequest.FORMAT = 'json';
       
   132 /**
       
   133 * @static
       
   134 * @property PROTO
       
   135 * @description Default protocol to use: http
       
   136 */
       
   137 YQLRequest.PROTO = 'http';
       
   138 /**
       
   139 * @static
       
   140 * @property BASE_URL
       
   141 * @description The base URL to query: query.yahooapis.com/v1/public/yql?
       
   142 */
       
   143 YQLRequest.BASE_URL = ':/' + '/query.yahooapis.com/v1/public/yql?';
       
   144 /**
       
   145 * @static
       
   146 * @property ENV
       
   147 * @description The environment file to load: http://datatables.org/alltables.env
       
   148 */
       
   149 YQLRequest.ENV = 'http:/' + '/datatables.org/alltables.env';
       
   150 
       
   151 Y.YQLRequest = YQLRequest;
       
   152 
       
   153 /**
       
   154  * This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
       
   155  * @class YQL
       
   156  * @constructor
       
   157  * @param {String} sql The SQL statement to execute
       
   158  * @param {Function} callback The callback to execute after the query (optional).
       
   159  * @param {Object} params An object literal of extra parameters to pass along (optional).
       
   160  * @param {Object} opts An object literal of configuration options (optional): proto (http|https), base (url)
       
   161  */
       
   162 Y.YQL = function (sql, callback, params, opts) {
       
   163     return new Y.YQLRequest(sql, callback, params, opts).send();
       
   164 };
       
   165 
       
   166 
       
   167 }, '@VERSION@', {"requires": ["oop"]});