NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / recordset-indexer / recordset-indexer-debug.js
blob593f7ddb20023b824b805406b82903411e5c1aff
1 /*
2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
8 YUI.add('recordset-indexer', function (Y, NAME) {
10 /**
11  * Provides the ability to store multiple custom hash tables referencing records in the recordset.
12  * @module recordset
13  * @submodule recordset-indexer
14  */
15 /**
16  * Plugin that provides the ability to store multiple custom hash tables referencing records in the recordset.
17  * This utility does not support any collision handling. New hash table entries with a used key overwrite older ones.
18  * @class RecordsetIndexer
19  */
20 function RecordsetIndexer(config) {
21     RecordsetIndexer.superclass.constructor.apply(this, arguments);
24 Y.mix(RecordsetIndexer, {
25     NS: "indexer",
27     NAME: "recordsetIndexer",
29     ATTRS: {
30         /**
31         * @description Collection of all the hashTables created by the plugin.
32         * The individual tables can be accessed by the key they are hashing against.
33         *
34         * @attribute hashTables
35         * @public
36         * @type object
37         */
38         hashTables: {
39             value: {
41             }
42         },
45         keys: {
46             value: {
48             }
49         }
50     }
51 });
54 Y.extend(RecordsetIndexer, Y.Plugin.Base, {
56     initializer: function(config) {
57         var host = this.get('host');
59         //setup listeners on recordset events
60         this.onHostEvent('add', Y.bind("_defAddHash", this), host);
61         this.onHostEvent('remove', Y.bind('_defRemoveHash', this), host);
62         this.onHostEvent('update', Y.bind('_defUpdateHash', this), host);
64     },
66     destructor: function(config) {
68     },
71     /**
72      * @description Setup the hash table for a given key with all existing records in the recordset
73      *
74      * @method _setHashTable
75      * @param key {string} A key to hash by.
76      * @return obj {object} The created hash table
77      * @private
78      */
79     _setHashTable: function(key) {
80         var host = this.get('host'),
81         obj = {},
82         i = 0,
83         len = host.getLength();
85         for (; i < len; i++) {
86             obj[host._items[i].getValue(key)] = host._items[i];
87         }
88         return obj;
89     },
91     //---------------------------------------------
92     // Syncing Methods
93     //---------------------------------------------
95     /**
96      * @description Updates all hash tables when a record is added to the recordset
97      *
98      * @method _defAddHash
99      * @private
100      */
101     _defAddHash: function(e) {
102         var tbl = this.get('hashTables');
105         //Go through every hashtable that is stored.
106         //in each hashtable, look to see if the key is represented in the object being added.
107         Y.each(tbl,
108         function(v, key) {
109             Y.each(e.added || e.updated,
110             function(o) {
111                 //if the object being added has a key which is being stored by hashtable v, add it into the table.
112                 if (o.getValue(key)) {
113                     v[o.getValue(key)] = o;
114                 }
115             });
116         });
118     },
120     /**
121      * @description Updates all hash tables when a record is removed from the recordset
122      *
123      * @method _defRemoveHash
124      * @private
125      */
126     _defRemoveHash: function(e) {
127         var tbl = this.get('hashTables'),
128         reckey;
130         //Go through every hashtable that is stored.
131         //in each hashtable, look to see if the key is represented in the object being deleted.
132         Y.each(tbl,
133         function(v, key) {
134             Y.each(e.removed || e.overwritten,
135             function(o) {
136                 reckey = o.getValue(key);
138                 //if the hashtable has a key storing a record, and the key and the record both match the record being deleted, delete that row from the hashtable
139                 if (reckey && v[reckey] === o) {
140                     delete v[reckey];
141                 }
142             });
143         });
144     },
146     /**
147      * @description Updates all hash tables when the recordset is updated (a combination of add and remove)
148      *
149      * @method _defUpdateHash
150      * @private
151      */
152     _defUpdateHash: function(e) {
154         //TODO: It will be more performant to create a new method rather than using _defAddHash, _defRemoveHash, due to the number of loops. See commented code.
155         e.added = e.updated;
156         e.removed = e.overwritten;
157         this._defAddHash(e);
158         this._defRemoveHash(e);
160         /*
161                     var tbl = this.get('hashTables'), reckey;
163                     Y.each(tbl, function(v, key) {
164                         Y.each(e.updated, function(o, i) {
166                             //delete record from hashtable if it has been overwritten
167                             reckey = o.getValue(key);
169                             if (reckey) {
170                                 v[reckey] = o;
171                             }
173                             //the undefined case is if more records are updated than currently exist in the recordset.
174                             if (e.overwritten[i] && (v[e.overwritten[i].getValue(key)] === e.overwritten[i])) {
175                                 delete v[e.overwritten[i].getValue(key)];
176                             }
178                             // if (v[reckey] === o) {
179                             //  delete v[reckey];
180                             // }
181                             //
182                             // //add the new updated record if it has a key that corresponds to a hash table
183                             // if (o.getValue(key)) {
184                             //  v[o.getValue(key)] = o;
185                             // }
187                         });
188                     });
189             */
190     },
192     //---------------------------------------------
193     // Public Methods
194     //---------------------------------------------
196     /**
197      * @description Creates a new hash table.
198      *
199      * @method createTable
200      * @param key {string} A key to hash by.
201      * @return tbls[key] {object} The created hash table
202      * @public
203      */
204     createTable: function(key) {
205         var tbls = this.get('hashTables');
206         tbls[key] = this._setHashTable(key);
207         this.set('hashTables', tbls);
209         return tbls[key];
210     },
213     /**
214      * @description Get a hash table that hashes records by a given key.
215      *
216      * @method getTable
217      * @param key {string} A key to hash by.
218      * @return table {object} The created hash table
219      * @public
220      */
221     getTable: function(key) {
222         return this.get('hashTables')[key];
223     }
230 Y.namespace("Plugin").RecordsetIndexer = RecordsetIndexer;
234 }, '3.13.0', {"requires": ["recordset-base", "plugin"]});