OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 * Model for the folder shortcuts. This object is cr.ui.ArrayDataModel-like | 6 * Model for the folder shortcuts. This object is cr.ui.ArrayDataModel-like |
7 * object with additional methods for the folder shortcut feature. | 7 * object with additional methods for the folder shortcut feature. |
8 * This uses chrome.storage as backend. Items are always sorted by file path. | 8 * This uses chrome.storage as backend. Items are always sorted by file path. |
9 * | 9 * |
10 * @constructor | 10 * @constructor |
(...skipping 17 matching lines...) Expand all Loading... |
28 chrome.storage.sync.get(FolderShortcutsDataModel.NAME, function(value) { | 28 chrome.storage.sync.get(FolderShortcutsDataModel.NAME, function(value) { |
29 if (!(FolderShortcutsDataModel.NAME in value)) | 29 if (!(FolderShortcutsDataModel.NAME in value)) |
30 return; | 30 return; |
31 | 31 |
32 // Since the value comes from outer resource, we have to check it just in | 32 // Since the value comes from outer resource, we have to check it just in |
33 // case. | 33 // case. |
34 var list = value[FolderShortcutsDataModel.NAME]; | 34 var list = value[FolderShortcutsDataModel.NAME]; |
35 if (list instanceof Array) { | 35 if (list instanceof Array) { |
36 list = filter(list); | 36 list = filter(list); |
37 | 37 |
| 38 // Record metrics. |
| 39 metrics.recordSmallCount('FolderShortcut.Count', list.length); |
| 40 |
38 var permutation = this.calculatePermitation_(this.array_, list); | 41 var permutation = this.calculatePermitation_(this.array_, list); |
39 this.array_ = list; | 42 this.array_ = list; |
40 this.firePermutedEvent_(permutation); | 43 this.firePermutedEvent_(permutation); |
41 } | 44 } |
42 }.bind(this)); | 45 }.bind(this)); |
43 | 46 |
44 // Listening for changes in the storage. | 47 // Listening for changes in the storage. |
45 chrome.storage.onChanged.addListener(function(changes, namespace) { | 48 chrome.storage.onChanged.addListener(function(changes, namespace) { |
46 if (!(FolderShortcutsDataModel.NAME in changes) || namespace != 'sync') | 49 if (!(FolderShortcutsDataModel.NAME in changes) || namespace != 'sync') |
47 return; | 50 return; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 } | 156 } |
154 // If value is not added yet, add it at the last. | 157 // If value is not added yet, add it at the last. |
155 if (addedIndex == -1) { | 158 if (addedIndex == -1) { |
156 this.array_.push(value); | 159 this.array_.push(value); |
157 addedIndex = this.length; | 160 addedIndex = this.length; |
158 } | 161 } |
159 | 162 |
160 this.firePermutedEvent_( | 163 this.firePermutedEvent_( |
161 this.calculatePermitation_(oldArray, this.array_)); | 164 this.calculatePermitation_(oldArray, this.array_)); |
162 this.save_(); | 165 this.save_(); |
| 166 metrics.recordUserAction('FolderShortcut.Add'); |
163 return addedIndex; | 167 return addedIndex; |
164 }, | 168 }, |
165 | 169 |
166 /** | 170 /** |
167 * Removes the given item from the array. | 171 * Removes the given item from the array. |
168 * @param {string} value Value to be removed from the array. | 172 * @param {string} value Value to be removed from the array. |
169 * @return {number} Index in the list which the element removed from. | 173 * @return {number} Index in the list which the element removed from. |
170 */ | 174 */ |
171 remove: function(value) { | 175 remove: function(value) { |
172 var removedIndex = -1; | 176 var removedIndex = -1; |
173 var oldArray = this.array_.slice(0); // Shallow copy. | 177 var oldArray = this.array_.slice(0); // Shallow copy. |
174 for (var i = 0; i < this.length; i++) { | 178 for (var i = 0; i < this.length; i++) { |
175 // Same item check: must be exact match. | 179 // Same item check: must be exact match. |
176 if (this.array_[i] == value) { | 180 if (this.array_[i] == value) { |
177 this.array_.splice(i, 1); | 181 this.array_.splice(i, 1); |
178 removedIndex = i; | 182 removedIndex = i; |
179 break; | 183 break; |
180 } | 184 } |
181 } | 185 } |
182 | 186 |
183 if (removedIndex != -1) { | 187 if (removedIndex != -1) { |
184 this.firePermutedEvent_( | 188 this.firePermutedEvent_( |
185 this.calculatePermitation_(oldArray, this.array_)); | 189 this.calculatePermitation_(oldArray, this.array_)); |
186 this.save_(); | 190 this.save_(); |
| 191 metrics.recordUserAction('FolderShortcut.Remove'); |
187 return removedIndex; | 192 return removedIndex; |
188 } | 193 } |
189 | 194 |
190 // No item is removed. | 195 // No item is removed. |
191 return -1; | 196 return -1; |
192 }, | 197 }, |
193 | 198 |
194 /** | 199 /** |
195 * @param {string} path Path to be checked. | 200 * @param {string} path Path to be checked. |
196 * @return {boolean} True if the given |path| exists in the array. False | 201 * @return {boolean} True if the given |path| exists in the array. False |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 this.dispatchEvent(permutedEvent); | 272 this.dispatchEvent(permutedEvent); |
268 | 273 |
269 // Note: This model only fires 'permuted' event, because: | 274 // Note: This model only fires 'permuted' event, because: |
270 // 1) 'change' event is not necessary to fire since it is covered by | 275 // 1) 'change' event is not necessary to fire since it is covered by |
271 // 'permuted' event. | 276 // 'permuted' event. |
272 // 2) 'splice' and 'sorted' events are not implemented. These events are | 277 // 2) 'splice' and 'sorted' events are not implemented. These events are |
273 // not used in NavigationListModel. We have to implement them when | 278 // not used in NavigationListModel. We have to implement them when |
274 // necessary. | 279 // necessary. |
275 } | 280 } |
276 }; | 281 }; |
OLD | NEW |