| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview This is a data model representin | 6 * @fileoverview This is a data model representin |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 // The include directives are put into Javascript-style comments to prevent | 9 // The include directives are put into Javascript-style comments to prevent |
| 10 // parsing errors in non-flattened mode. The flattener still sees them. | 10 // parsing errors in non-flattened mode. The flattener still sees them. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 | 36 |
| 37 ArrayDataModel.prototype = { | 37 ArrayDataModel.prototype = { |
| 38 __proto__: EventTarget.prototype, | 38 __proto__: EventTarget.prototype, |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * The length of the data model. | 41 * The length of the data model. |
| 42 * @type {number} | 42 * @type {number} |
| 43 */ | 43 */ |
| 44 get length() { return this.array_.length; }, | 44 get length() { |
| 45 return this.array_.length; |
| 46 }, |
| 45 | 47 |
| 46 /** | 48 /** |
| 47 * Returns the item at the given index. | 49 * Returns the item at the given index. |
| 48 * This implementation returns the item at the given index in the sorted | 50 * This implementation returns the item at the given index in the sorted |
| 49 * array. | 51 * array. |
| 50 * @param {number} index The index of the element to get. | 52 * @param {number} index The index of the element to get. |
| 51 * @return {*} The element at the given index. | 53 * @return {*} The element at the given index. |
| 52 */ | 54 */ |
| 53 item: function(index) { | 55 item: function(index) { |
| 54 if (index >= 0 && index < this.length) | 56 if (index >= 0 && index < this.length) |
| 55 return this.array_[this.indexes_[index]]; | 57 return this.array_[this.indexes_[index]]; |
| 56 return undefined; | 58 return undefined; |
| 57 }, | 59 }, |
| 58 | 60 |
| 59 /** | 61 /** |
| 60 * Returns compare function set for given field. | 62 * Returns compare function set for given field. |
| 61 * @param {string} field The field to get compare function for. | 63 * @param {string} field The field to get compare function for. |
| 62 * @return {function(*, *): number} Compare function set for given field. | 64 * @return {function(*, *): number} Compare function set for given field. |
| 63 */ | 65 */ |
| 64 compareFunction: function(field) { return this.compareFunctions_[field]; }, | 66 compareFunction: function(field) { |
| 67 return this.compareFunctions_[field]; |
| 68 }, |
| 65 | 69 |
| 66 /** | 70 /** |
| 67 * Sets compare function for given field. | 71 * Sets compare function for given field. |
| 68 * @param {string} field The field to set compare function. | 72 * @param {string} field The field to set compare function. |
| 69 * @param {function(*, *): number} compareFunction Compare function to set | 73 * @param {function(*, *): number} compareFunction Compare function to set |
| 70 * for given field. | 74 * for given field. |
| 71 */ | 75 */ |
| 72 setCompareFunction: function(field, compareFunction) { | 76 setCompareFunction: function(field, compareFunction) { |
| 73 if (!this.compareFunctions_) { | 77 if (!this.compareFunctions_) { |
| 74 this.compareFunctions_ = {}; | 78 this.compareFunctions_ = {}; |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 this.updateIndex(index); | 240 this.updateIndex(index); |
| 237 }, | 241 }, |
| 238 | 242 |
| 239 /** | 243 /** |
| 240 * Use this to update a given item in the array. This does not remove and | 244 * Use this to update a given item in the array. This does not remove and |
| 241 * reinsert a new item. | 245 * reinsert a new item. |
| 242 * This dispatches a change event. | 246 * This dispatches a change event. |
| 243 * This runs sort after updating. | 247 * This runs sort after updating. |
| 244 * @param {number} index The index of the item to update. | 248 * @param {number} index The index of the item to update. |
| 245 */ | 249 */ |
| 246 updateIndex: function(index) { this.updateIndexes([index]); }, | 250 updateIndex: function(index) { |
| 251 this.updateIndexes([index]); |
| 252 }, |
| 247 | 253 |
| 248 /** | 254 /** |
| 249 * Notifies of update of the items in the array. This does not remove and | 255 * Notifies of update of the items in the array. This does not remove and |
| 250 * reinsert new items. | 256 * reinsert new items. |
| 251 * This dispatches one or more change events. | 257 * This dispatches one or more change events. |
| 252 * This runs sort after updating. | 258 * This runs sort after updating. |
| 253 * @param {Array<number>} indexes The index list of items to update. | 259 * @param {Array<number>} indexes The index list of items to update. |
| 254 */ | 260 */ |
| 255 updateIndexes: function(indexes) { | 261 updateIndexes: function(indexes) { |
| 256 indexes.forEach(function(index) { | 262 indexes.forEach(function(index) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 287 }, | 293 }, |
| 288 | 294 |
| 289 /** | 295 /** |
| 290 * Called before a sort happens so that you may fetch additional data | 296 * Called before a sort happens so that you may fetch additional data |
| 291 * required for the sort. | 297 * required for the sort. |
| 292 * | 298 * |
| 293 * @param {string} field Sort field. | 299 * @param {string} field Sort field. |
| 294 * @param {function()} callback The function to invoke when preparation | 300 * @param {function()} callback The function to invoke when preparation |
| 295 * is complete. | 301 * is complete. |
| 296 */ | 302 */ |
| 297 prepareSort: function(field, callback) { callback(); }, | 303 prepareSort: function(field, callback) { |
| 304 callback(); |
| 305 }, |
| 298 | 306 |
| 299 /** | 307 /** |
| 300 * Sorts data model according to given field and direction and dispathes | 308 * Sorts data model according to given field and direction and dispathes |
| 301 * sorted event with delay. If no need to delay, use sort() instead. | 309 * sorted event with delay. If no need to delay, use sort() instead. |
| 302 * @param {string} field Sort field. | 310 * @param {string} field Sort field. |
| 303 * @param {string} direction Sort direction. | 311 * @param {string} direction Sort direction. |
| 304 * @private | 312 * @private |
| 305 */ | 313 */ |
| 306 delayedSort_: function(field, direction) { | 314 delayedSort_: function(field, direction) { |
| 307 var self = this; | 315 var self = this; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 if (a < b) | 437 if (a < b) |
| 430 return -1; | 438 return -1; |
| 431 if (a > b) | 439 if (a > b) |
| 432 return 1; | 440 return 1; |
| 433 return 0; | 441 return 0; |
| 434 } | 442 } |
| 435 }; | 443 }; |
| 436 | 444 |
| 437 return {ArrayDataModel: ArrayDataModel}; | 445 return {ArrayDataModel: ArrayDataModel}; |
| 438 }); | 446 }); |
| OLD | NEW |