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