| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 * Data model for gallery. | 6 * Data model for gallery. |
| 7 * | 7 * |
| 8 * @param {!MetadataModel} metadataModel | 8 * @param {!MetadataModel} metadataModel |
| 9 * @param {!EntryListWatcher=} opt_watcher Entry list watcher. | 9 * @param {!EntryListWatcher=} opt_watcher Entry list watcher. |
| 10 * @constructor | 10 * @constructor |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 * | 57 * |
| 58 * @param {!VolumeManager} volumeManager Volume manager instance. | 58 * @param {!VolumeManager} volumeManager Volume manager instance. |
| 59 * @param {!Gallery.Item} item Original gallery item. | 59 * @param {!Gallery.Item} item Original gallery item. |
| 60 * @param {!HTMLCanvasElement} canvas Canvas containing new image. | 60 * @param {!HTMLCanvasElement} canvas Canvas containing new image. |
| 61 * @param {boolean} overwrite Whether to overwrite the image to the item or not. | 61 * @param {boolean} overwrite Whether to overwrite the image to the item or not. |
| 62 * @return {!Promise} Promise to be fulfilled with when the operation completes. | 62 * @return {!Promise} Promise to be fulfilled with when the operation completes. |
| 63 */ | 63 */ |
| 64 GalleryDataModel.prototype.saveItem = function( | 64 GalleryDataModel.prototype.saveItem = function( |
| 65 volumeManager, item, canvas, overwrite) { | 65 volumeManager, item, canvas, overwrite) { |
| 66 var oldEntry = item.getEntry(); | 66 var oldEntry = item.getEntry(); |
| 67 var oldMetadataItem = item.getMetadataItem(); | |
| 68 var oldThumbnailMetadataItem = item.getThumbnailMetadataItem(); | |
| 69 var oldLocationInfo = item.getLocationInfo(); | 67 var oldLocationInfo = item.getLocationInfo(); |
| 70 return new Promise(function(fulfill, reject) { | 68 return new Promise(function(fulfill, reject) { |
| 71 item.saveToFile( | 69 item.saveToFile( |
| 72 volumeManager, | 70 volumeManager, |
| 73 this.metadataModel_, | 71 this.metadataModel_, |
| 74 this.fallbackSaveDirectory, | 72 this.fallbackSaveDirectory, |
| 75 overwrite, | 73 overwrite, |
| 76 canvas, | 74 canvas, |
| 77 function(success) { | 75 function(success) { |
| 78 if (!success) { | 76 if (!success) { |
| 79 reject('Failed to save the image.'); | 77 reject('Failed to save the image.'); |
| 80 return; | 78 return; |
| 81 } | 79 } |
| 82 | 80 |
| 83 // Current entry is updated. | 81 // Current entry is updated. |
| 84 // Dispatch an event. | 82 // Dispatch an event. |
| 85 var event = new Event('content'); | 83 var event = new Event('content'); |
| 86 event.item = item; | 84 event.item = item; |
| 87 event.oldEntry = oldEntry; | 85 event.oldEntry = oldEntry; |
| 88 event.thumbnailChanged = true; | 86 event.thumbnailChanged = true; |
| 89 this.dispatchEvent(event); | 87 this.dispatchEvent(event); |
| 90 | 88 |
| 91 if (!util.isSameEntry(oldEntry, item.getEntry())) { | 89 if (!util.isSameEntry(oldEntry, item.getEntry())) { |
| 92 // New entry is added and the item now tracks it. | 90 Promise.all([ |
| 93 // Add another item for the old entry. | 91 this.metadataModel_.get( |
| 94 var anotherItem = new Gallery.Item( | 92 [oldEntry], Gallery.PREFETCH_PROPERTY_NAMES), |
| 95 oldEntry, | 93 new ThumbnailModel(this.metadataModel_).get([oldEntry]) |
| 96 oldLocationInfo, | 94 ]).then(function(itemLists) { |
| 97 oldMetadataItem, | 95 // New entry is added and the item now tracks it. |
| 98 oldThumbnailMetadataItem, | 96 // Add another item for the old entry. |
| 99 item.isOriginal()); | 97 var anotherItem = new Gallery.Item( |
| 100 // The item must be added behind the existing item so that it does | 98 oldEntry, |
| 101 // not change the index of the existing item. | 99 oldLocationInfo, |
| 102 // TODO(hirono): Update the item index of the selection model | 100 itemLists[0][0], |
| 103 // correctly. | 101 itemLists[1][0], |
| 104 this.splice(this.indexOf(item) + 1, 0, anotherItem); | 102 item.isOriginal()); |
| 103 // The item must be added behind the existing item so that it does |
| 104 // not change the index of the existing item. |
| 105 // TODO(hirono): Update the item index of the selection model |
| 106 // correctly. |
| 107 this.splice(this.indexOf(item) + 1, 0, anotherItem); |
| 108 }.bind(this)).then(fulfill, reject); |
| 109 } else { |
| 110 fulfill(); |
| 105 } | 111 } |
| 106 | |
| 107 fulfill(); | |
| 108 }.bind(this)); | 112 }.bind(this)); |
| 109 }.bind(this)); | 113 }.bind(this)); |
| 110 }; | 114 }; |
| 111 | 115 |
| 112 /** | 116 /** |
| 113 * Evicts image caches in the items. | 117 * Evicts image caches in the items. |
| 114 */ | 118 */ |
| 115 GalleryDataModel.prototype.evictCache = function() { | 119 GalleryDataModel.prototype.evictCache = function() { |
| 116 // Sort the item by the last accessed date. | 120 // Sort the item by the last accessed date. |
| 117 var sorted = this.slice().sort(function(a, b) { | 121 var sorted = this.slice().sort(function(a, b) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 141 } else { | 145 } else { |
| 142 // Force to free the buffer of the canvas by assigning zero size. | 146 // Force to free the buffer of the canvas by assigning zero size. |
| 143 sorted[i].screenImage.width = 0; | 147 sorted[i].screenImage.width = 0; |
| 144 sorted[i].screenImage.height = 0; | 148 sorted[i].screenImage.height = 0; |
| 145 sorted[i].screenImage = null; | 149 sorted[i].screenImage = null; |
| 146 } | 150 } |
| 147 } | 151 } |
| 148 } | 152 } |
| 149 } | 153 } |
| 150 }; | 154 }; |
| OLD | NEW |