src/cm/media/js/lib/yui/yui_3.10.3/build/datasource-local/datasource-local-debug.js
changeset 525 89ef5ed3c48b
equal deleted inserted replaced
524:322d0feea350 525:89ef5ed3c48b
       
     1 /*
       
     2 YUI 3.10.3 (build 2fb5187)
       
     3 Copyright 2013 Yahoo! Inc. All rights reserved.
       
     4 Licensed under the BSD License.
       
     5 http://yuilibrary.com/license/
       
     6 */
       
     7 
       
     8 YUI.add('datasource-local', function (Y, NAME) {
       
     9 
       
    10 /**
       
    11  * The DataSource utility provides a common configurable interface for widgets to
       
    12  * access a variety of data, from JavaScript arrays to online database servers.
       
    13  *
       
    14  * @module datasource
       
    15  * @main datasource
       
    16  */
       
    17     
       
    18 /**
       
    19  * Provides the base DataSource implementation, which can be extended to
       
    20  * create DataSources for specific data protocols, such as the IO Utility, the
       
    21  * Get Utility, or custom functions.
       
    22  *
       
    23  * @module datasource
       
    24  * @submodule datasource-local
       
    25  */
       
    26 
       
    27 /**
       
    28  * Base class for the DataSource Utility.
       
    29  * @class DataSource.Local
       
    30  * @extends Base
       
    31  * @constructor
       
    32  */    
       
    33 var LANG = Y.Lang,
       
    34 
       
    35 DSLocal = function() {
       
    36     DSLocal.superclass.constructor.apply(this, arguments);
       
    37 };
       
    38     
       
    39     /////////////////////////////////////////////////////////////////////////////
       
    40     //
       
    41     // DataSource static properties
       
    42     //
       
    43     /////////////////////////////////////////////////////////////////////////////
       
    44 Y.mix(DSLocal, {
       
    45     /**
       
    46      * Class name.
       
    47      *
       
    48      * @property NAME
       
    49      * @type String
       
    50      * @static     
       
    51      * @final
       
    52      * @value "dataSourceLocal"
       
    53      */
       
    54     NAME: "dataSourceLocal",
       
    55 
       
    56     /////////////////////////////////////////////////////////////////////////////
       
    57     //
       
    58     // DataSource Attributes
       
    59     //
       
    60     /////////////////////////////////////////////////////////////////////////////
       
    61 
       
    62     ATTRS: {
       
    63         /**
       
    64         * @attribute source
       
    65         * @description Pointer to live data.
       
    66         * @type MIXED
       
    67         * @default null        
       
    68         */
       
    69         source: {
       
    70             value: null
       
    71         }
       
    72     },
       
    73 
       
    74     /**
       
    75      * Global transaction counter.
       
    76      *
       
    77      * @property _tId
       
    78      * @type Number
       
    79      * @static
       
    80      * @private
       
    81      * @default 0
       
    82      */
       
    83     _tId: 0,
       
    84 
       
    85     /**
       
    86      * Global in-progress transaction objects.
       
    87      *
       
    88      * @property transactions
       
    89      * @type Object
       
    90      * @static
       
    91      */
       
    92     transactions: {},
       
    93 
       
    94     /**
       
    95      * Returns data to callback.
       
    96      *
       
    97      * @method issueCallback
       
    98      * @param e {EventFacade} Event Facade.
       
    99      * @param caller {DataSource} Calling DataSource instance.
       
   100      * @static
       
   101      */
       
   102     issueCallback: function (e, caller) {
       
   103         var callbacks = e.on || e.callback,
       
   104             callback = callbacks && callbacks.success,
       
   105             payload = e.details[0];
       
   106 
       
   107         payload.error = (e.error || e.response.error);
       
   108 
       
   109         if (payload.error) {
       
   110             caller.fire("error", payload);
       
   111             callback = callbacks && callbacks.failure;
       
   112         }
       
   113 
       
   114         if (callback) {
       
   115             //TODO: this should be executed from a specific context
       
   116             callback(payload);
       
   117         }
       
   118     }
       
   119 });
       
   120     
       
   121 Y.extend(DSLocal, Y.Base, {
       
   122     /**
       
   123     * Internal init() handler.
       
   124     *
       
   125     * @method initializer
       
   126     * @param config {Object} Config object.
       
   127     * @private        
       
   128     */
       
   129     initializer: function(config) {
       
   130         this._initEvents();
       
   131     },
       
   132 
       
   133     /**
       
   134     * This method creates all the events for this module.
       
   135     * @method _initEvents
       
   136     * @private        
       
   137     */
       
   138     _initEvents: function() {
       
   139         /**
       
   140          * Fired when a data request is received.
       
   141          *
       
   142          * @event request
       
   143          * @param e {Event.Facade} Event Facade with the following properties:
       
   144          * <dl>                          
       
   145          * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
       
   146          * <dt>request (Object)</dt> <dd>The request.</dd>
       
   147          * <dt>callback (Object)</dt> <dd>The callback object
       
   148          *   (deprecated, refer to <strong>on</strong></dd>
       
   149          * <dt>on (Object)</dt> <dd>The map of configured callback
       
   150          *   functions.</dd>
       
   151          * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
       
   152          * </dl>
       
   153          * @preventable _defRequestFn
       
   154          */
       
   155         this.publish("request", {defaultFn: Y.bind("_defRequestFn", this), queuable:true});
       
   156          
       
   157         /**
       
   158          * Fired when raw data is received.
       
   159          *
       
   160          * @event data
       
   161          * @param e {Event.Facade} Event Facade with the following properties:
       
   162          * <dl>
       
   163          * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
       
   164          * <dt>request (Object)</dt> <dd>The request.</dd>
       
   165          * <dt>callback (Object)</dt> <dd>Deprecated alias for the
       
   166          *   <strong>on</strong> property</dd>
       
   167          * <dt>on (Object)</dt> <dd>The map of configured transaction
       
   168          *   callbacks.  An object with the following properties:
       
   169          *     <dl>
       
   170          *         <dt>success (Function)</dt> <dd>Success handler.</dd>
       
   171          *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
       
   172          *     </dl>
       
   173          * </dd>
       
   174          * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
       
   175          * <dt>data (Object)</dt> <dd>Raw data.</dd>
       
   176          * </dl>
       
   177          * @preventable _defDataFn
       
   178          */
       
   179         this.publish("data", {defaultFn: Y.bind("_defDataFn", this), queuable:true});
       
   180 
       
   181         /**
       
   182          * Fired when response is returned.
       
   183          *
       
   184          * @event response
       
   185          * @param e {Event.Facade} Event Facade with the following properties:
       
   186          * <dl>
       
   187          * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
       
   188          * <dt>request (Object)</dt> <dd>The request.</dd>
       
   189          * <dt>callback (Object)</dt> <dd>Deprecated alias for the
       
   190          *   <strong>on</strong> property</dd>
       
   191          * <dt>on (Object)</dt> <dd>The map of configured transaction
       
   192          *   callbacks.  An object with the following properties:
       
   193          *     <dl>
       
   194          *         <dt>success (Function)</dt> <dd>Success handler.</dd>
       
   195          *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
       
   196          *     </dl>
       
   197          * </dd>
       
   198          * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
       
   199          * <dt>data (Object)</dt> <dd>Raw data.</dd>
       
   200          * <dt>response (Object)</dt>
       
   201          *     <dd>Normalized response object with the following properties:
       
   202          *         <dl>
       
   203          *             <dt>results (Object)</dt> <dd>Parsed results.</dd>
       
   204          *             <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
       
   205          *             <dt>error (Boolean)</dt> <dd>Error flag.</dd>
       
   206          *         </dl>
       
   207          *     </dd>
       
   208          * <dt>error</dt>
       
   209          *     <dd>Any error that occurred along the transaction lifecycle.</dd>
       
   210          * </dl>
       
   211          * @preventable _defResponseFn
       
   212          */
       
   213          this.publish("response", {defaultFn: Y.bind("_defResponseFn", this), queuable:true});
       
   214 
       
   215         /**
       
   216          * Fired when an error is encountered.
       
   217          *
       
   218          * @event error
       
   219          * @param e {Event.Facade} Event Facade with the following properties:
       
   220          * <dl>
       
   221          * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
       
   222          * <dt>request (Object)</dt> <dd>The request.</dd>
       
   223          * <dt>callback (Object)</dt> <dd>Deprecated alias for the
       
   224          *   <strong>on</strong> property</dd>
       
   225          * <dt>on (Object)</dt> <dd>The map of configured transaction
       
   226          *   callbacks.  An object with the following properties:
       
   227          *     <dl>
       
   228          *         <dt>success (Function)</dt> <dd>Success handler.</dd>
       
   229          *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
       
   230          *     </dl>
       
   231          * </dd>
       
   232          * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
       
   233          * <dt>data (Object)</dt> <dd>Raw data.</dd>
       
   234          * <dt>response (Object)</dt>
       
   235          *     <dd>Normalized response object with the following properties:
       
   236          *         <dl>
       
   237          *             <dt>results (Object)</dt> <dd>Parsed results.</dd>
       
   238          *             <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
       
   239          *             <dt>error (Object)</dt> <dd>Error object.</dd>
       
   240          *         </dl>
       
   241          *     </dd>
       
   242          * <dt>error</dt>
       
   243          *     <dd>Any error that occurred along the transaction lifecycle.</dd>
       
   244          * </dl>
       
   245          */
       
   246 
       
   247     },
       
   248 
       
   249     /**
       
   250      * Manages request/response transaction. Must fire <code>response</code>
       
   251      * event when response is received. This method should be implemented by
       
   252      * subclasses to achieve more complex behavior such as accessing remote data.
       
   253      *
       
   254      * @method _defRequestFn
       
   255      * @param e {Event.Facade} Event Facadewith the following properties:
       
   256      * <dl>
       
   257      * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
       
   258      * <dt>request (Object)</dt> <dd>The request.</dd>
       
   259      * <dt>callback (Object)</dt> <dd>Deprecated alias for the
       
   260      *   <strong>on</strong> property</dd>
       
   261      * <dt>on (Object)</dt> <dd>The map of configured transaction
       
   262      *   callbacks.  An object with the following properties:
       
   263      *     <dl>
       
   264      *         <dt>success (Function)</dt> <dd>Success handler.</dd>
       
   265      *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
       
   266      *     </dl>
       
   267      * </dd>
       
   268      * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
       
   269      * </dl>
       
   270      * @protected
       
   271      */
       
   272     _defRequestFn: function(e) {
       
   273         var data = this.get("source"),
       
   274             payload = e.details[0];
       
   275         
       
   276         // Problematic data
       
   277         if(LANG.isUndefined(data)) {
       
   278             payload.error = new Error("Local source undefined");
       
   279             Y.log("Local source undefined", "error", "datasource-local");
       
   280         }
       
   281 
       
   282         payload.data = data;
       
   283         this.fire("data", payload);
       
   284         Y.log("Transaction " + e.tId + " complete. Request: " +
       
   285                 Y.dump(e.request) + " . Response: " + Y.dump(e.response), "info", "datasource-local");
       
   286     },
       
   287 
       
   288     /**
       
   289      * Normalizes raw data into a response that includes results and meta properties.
       
   290      *
       
   291      * @method _defDataFn
       
   292      * @param e {Event.Facade} Event Facade with the following properties:
       
   293      * <dl>
       
   294      * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
       
   295      * <dt>request (Object)</dt> <dd>The request.</dd>
       
   296      * <dt>callback (Object)</dt> <dd>Deprecated alias for the
       
   297      *   <strong>on</strong> property</dd>
       
   298      * <dt>on (Object)</dt> <dd>The map of configured transaction
       
   299      *   callbacks.  An object with the following properties:
       
   300      *     <dl>
       
   301      *         <dt>success (Function)</dt> <dd>Success handler.</dd>
       
   302      *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
       
   303      *     </dl>
       
   304      * </dd>
       
   305      * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
       
   306      * <dt>data (Object)</dt> <dd>Raw data.</dd>
       
   307      * </dl>
       
   308      * @protected
       
   309      */
       
   310     _defDataFn: function(e) {
       
   311         var data = e.data,
       
   312             meta = e.meta,
       
   313             response = {
       
   314                 results: (LANG.isArray(data)) ? data : [data],
       
   315                 meta: (meta) ? meta : {}
       
   316             },
       
   317             payload = e.details[0];
       
   318 
       
   319         payload.response = response;
       
   320         this.fire("response", payload);
       
   321     },
       
   322 
       
   323     /**
       
   324      * Sends data as a normalized response to callback.
       
   325      *
       
   326      * @method _defResponseFn
       
   327      * @param e {Event.Facade} Event Facade with the following properties:
       
   328      * <dl>
       
   329      * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
       
   330      * <dt>request (Object)</dt> <dd>The request.</dd>
       
   331      * <dt>callback (Object)</dt> <dd>Deprecated alias for the
       
   332      *   <strong>on</strong> property</dd>
       
   333      * <dt>on (Object)</dt> <dd>The map of configured transaction
       
   334      *   callbacks.  An object with the following properties:
       
   335      *     <dl>
       
   336      *         <dt>success (Function)</dt> <dd>Success handler.</dd>
       
   337      *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
       
   338      *     </dl>
       
   339      * </dd>
       
   340      * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
       
   341      * <dt>data (Object)</dt> <dd>Raw data.</dd>
       
   342      * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
       
   343      *     <dl>
       
   344      *         <dt>results (Object)</dt> <dd>Parsed results.</dd>
       
   345      *         <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
       
   346      *         <dt>error (Boolean)</dt> <dd>Error flag.</dd>
       
   347      *     </dl>
       
   348      * </dd>
       
   349      * </dl>
       
   350      * @protected
       
   351      */
       
   352     _defResponseFn: function(e) {
       
   353         // Send the response back to the callback
       
   354         DSLocal.issueCallback(e, this);
       
   355     },
       
   356     
       
   357     /**
       
   358      * Generates a unique transaction ID and fires <code>request</code> event.
       
   359      * <strong>Note</strong>: the property <code>callback</code> is a
       
   360      * deprecated alias for the <code>on</code> transaction configuration
       
   361      * property described below.
       
   362      *
       
   363      * @method sendRequest
       
   364      * @param [request] {Object} An object literal with the following properties:
       
   365      *     <dl>
       
   366      *     <dt><code>request</code></dt>
       
   367      *     <dd>The request to send to the live data source, if any.</dd>
       
   368      *     <dt><code>on</code></dt>
       
   369      *     <dd>An object literal with the following properties:
       
   370      *         <dl>
       
   371      *         <dt><code>success</code></dt>
       
   372      *         <dd>The function to call when the data is ready.</dd>
       
   373      *         <dt><code>failure</code></dt>
       
   374      *         <dd>The function to call upon a response failure condition.</dd>
       
   375      *         <dt><code>argument</code></dt>
       
   376      *         <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
       
   377      *         </dl>
       
   378      *     </dd>
       
   379      *     <dt><code>cfg</code></dt>
       
   380      *     <dd>Configuration object, if any.</dd>
       
   381      *     </dl>
       
   382      * @return {Number} Transaction ID.
       
   383      */
       
   384     sendRequest: function(request) {
       
   385         var tId = DSLocal._tId++,
       
   386             callbacks;
       
   387 
       
   388         request = request || {};
       
   389 
       
   390         callbacks = request.on || request.callback;
       
   391 
       
   392         this.fire("request", {
       
   393             tId: tId,
       
   394             request: request.request,
       
   395             on: callbacks,
       
   396             callback: callbacks,
       
   397             cfg: request.cfg || {}
       
   398         });
       
   399 
       
   400         Y.log("Transaction " + tId + " sent request: " + Y.dump(request.request), "info", "datasource-local");
       
   401 
       
   402         return tId;
       
   403     }
       
   404 });
       
   405     
       
   406 Y.namespace("DataSource").Local = DSLocal;
       
   407 
       
   408 
       
   409 }, '3.10.3', {"requires": ["base"]});