]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/recordset/recordset-indexer.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / recordset / recordset-indexer.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('recordset-indexer', function(Y) {
9
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);
22 }
23
24 Y.mix(RecordsetIndexer, {
25     NS: "indexer",
26
27     NAME: "recordsetIndexer",
28
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: {
40
41             }
42         },
43
44
45         keys: {
46             value: {
47
48             }
49         }
50     }
51 });
52
53
54 Y.extend(RecordsetIndexer, Y.Plugin.Base, {
55
56     initializer: function(config) {
57         var host = this.get('host');
58
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);
63
64     },
65
66     destructor: function(config) {
67     
68     },
69
70
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();
84
85         for (; i < len; i++) {
86             obj[host._items[i].getValue(key)] = host._items[i];
87         }
88         return obj;
89     },
90
91     //---------------------------------------------
92     // Syncing Methods
93     //---------------------------------------------
94
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');
103
104
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         });
117
118     },
119
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;
129
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);
137
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     },
145
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) {
153
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);
159
160         /*
161                                         var tbl = this.get('hashTables'), reckey;
162                                         
163                                         Y.each(tbl, function(v, key) {
164                                                 Y.each(e.updated, function(o, i) {
165                                                         
166                                                         //delete record from hashtable if it has been overwritten
167                                                         reckey = o.getValue(key);
168                                                         
169                                                         if (reckey) {
170                                                                 v[reckey] = o;
171                                                         }
172                                                         
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                                                         }
177                                                         
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                                                         // }
186                                                                                                                         
187                                                 });
188                                         });
189                         */
190     },
191
192     //---------------------------------------------
193     // Public Methods
194     //---------------------------------------------
195
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);
208
209         return tbls[key];
210     },
211
212
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     }
224
225
226
227
228
229 });
230 Y.namespace("Plugin").RecordsetIndexer = RecordsetIndexer;
231
232
233
234
235 }, '3.3.0' ,{requires:['recordset-base','plugin']});