MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / datatable-datasource / datatable-datasource.js
blob7c46ac6dec52a4259d9089a845a45cd98dddd45b
1 /*
2 YUI 3.5.1 (build 22)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('datatable-datasource', function(Y) {
9 /**
10  * Plugs DataTable with DataSource integration.
11  *
12  * @module datatable
13  * @submodule datatable-datasource
14  */
16 /**
17  * Adds DataSource integration to DataTable.
18  * @class Plugin.DataTableDataSource
19  * @extends Plugin.Base
20  */
21 function DataTableDataSource() {
22     DataTableDataSource.superclass.constructor.apply(this, arguments);
25 /////////////////////////////////////////////////////////////////////////////
27 // STATIC PROPERTIES
29 /////////////////////////////////////////////////////////////////////////////
30 Y.mix(DataTableDataSource, {
31     /**
32      * The namespace for the plugin. This will be the property on the host which
33      * references the plugin instance.
34      *
35      * @property NS
36      * @type String
37      * @static
38      * @final
39      * @value "datasource"
40      */
41     NS: "datasource",
43     /**
44      * Class name.
45      *
46      * @property NAME
47      * @type String
48      * @static
49      * @final
50      * @value "dataTableDataSource"
51      */
52     NAME: "dataTableDataSource",
54 /////////////////////////////////////////////////////////////////////////////
56 // ATTRIBUTES
58 /////////////////////////////////////////////////////////////////////////////
59     ATTRS: {
60         /**
61         * @attribute datasource
62         * @description Pointer to DataSource instance.
63         * @type {DataSource}
64         */
65         datasource: {
66             setter: "_setDataSource"
67         },
68         
69         /**
70         * @attribute initialRequest
71         * @description Request sent to DataSource immediately upon initialization.
72         * @type Object
73         */
74         initialRequest: {
75             setter: "_setInitialRequest"
76         }
77     }
78 });
80 /////////////////////////////////////////////////////////////////////////////
82 // PROTOTYPE
84 /////////////////////////////////////////////////////////////////////////////
85 Y.extend(DataTableDataSource, Y.Plugin.Base, {
86     /////////////////////////////////////////////////////////////////////////////
87     //
88     // ATTRIBUTE HELPERS
89     //
90     /////////////////////////////////////////////////////////////////////////////
91     /**
92     * @method _setDataSource
93     * @description Creates new DataSource instance if one is not provided.
94     * @param ds {Object | Y.DataSource}
95     * @return {DataSource}
96     * @private
97     */
98     _setDataSource: function(ds) {
99         return ds || new Y.DataSource.Local(ds);
100     },
102     /**
103     * @method _setInitialRequest
104     * @description Sends request to DataSource.
105     * @param request {Object} DataSource request.
106     * @private
107     */
108     _setInitialRequest: function(request) {
109     },
111     /////////////////////////////////////////////////////////////////////////////
112     //
113     // METHODS
114     //
115     /////////////////////////////////////////////////////////////////////////////
116     /**
117     * Initializer.
118     *
119     * @method initializer
120     * @param config {Object} Config object.
121     * @private
122     */
123     initializer: function(config) {
124         if(!Y.Lang.isUndefined(config.initialRequest)) {
125             this.load({request:config.initialRequest});
126         }
127     },
129     ////////////////////////////////////////////////////////////////////////////
130     //
131     // DATA
132     //
133     ////////////////////////////////////////////////////////////////////////////
135     /**
136      * Load data by calling DataSource's sendRequest() method under the hood.
137      *
138      * @method load
139      * @param config {object} Optional configuration parameters:
140      *
141      * <dl>
142      * <dt>request</dt><dd>Pass in a new request, or initialRequest is used.</dd>
143      * <dt>callback</dt><dd>Pass in DataSource callback object, or the following default is used:
144      *    <dl>
145      *      <dt>success</dt><dd>datatable.onDataReturnInitializeTable</dd>
146      *      <dt>failure</dt><dd>datatable.onDataReturnInitializeTable</dd>
147      *      <dt>scope</dt><dd>datatable</dd>
148      *      <dt>argument</dt><dd>datatable.getState()</dd>
149      *    </dl>
150      * </dd>
151      * <dt>datasource</dt><dd>Pass in a new DataSource instance to override the current DataSource for this transaction.</dd>
152      * </dl>
153      */
154     load: function(config) {
155         config = config || {};
156         config.request = config.request || this.get("initialRequest");
157         config.callback = config.callback || {
158             success: Y.bind(this.onDataReturnInitializeTable, this),
159             failure: Y.bind(this.onDataReturnInitializeTable, this),
160             argument: this.get("host").get("state") //TODO
161         };
163         var ds = (config.datasource || this.get("datasource"));
164         if(ds) {
165             ds.sendRequest(config);
166         }
167     },
169     /**
170      * Callback function passed to DataSource's sendRequest() method populates
171      * an entire DataTable with new data, clearing previous data, if any.
172      *
173      * @method onDataReturnInitializeTable
174      * @param e {Event.Facade} DataSource Event Facade object.
175      */
176     onDataReturnInitializeTable : function(e) {
177         var records = (e.response && e.response.results) || [];
179         this.get("host").set("data", records);
180     }
183 Y.namespace("Plugin").DataTableDataSource = DataTableDataSource;
186 }, '3.5.1' ,{requires:['datatable-base','plugin','datasource-local']});