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
3 // found in the LICENSE file.
8 * The current selection object.
10 * @param {FileManager} fileManager FileManager instance.
11 * @param {Array.<number>} indexes Selected indexes.
14 function FileSelection(fileManager, indexes) {
15 this.fileManager_ = fileManager;
16 this.computeBytesSequence_ = 0;
17 this.indexes = indexes;
21 this.directoryCount = 0;
23 this.showBytes = false;
24 this.allDriveFilesPresent = false,
26 this.bytesKnown = false;
27 this.mustBeHidden_ = false;
28 this.mimeTypes = null;
30 // Synchronously compute what we can.
31 for (var i = 0; i < this.indexes.length; i++) {
32 var entry = fileManager.getFileList().item(this.indexes[i]);
36 this.entries.push(entry);
38 if (this.iconType == null) {
39 this.iconType = FileType.getIcon(entry);
40 } else if (this.iconType != 'unknown') {
41 var iconType = FileType.getIcon(entry);
42 if (this.iconType != iconType)
43 this.iconType = 'unknown';
49 this.directoryCount += 1;
54 this.tasks = new FileTasks(this.fileManager_);
60 * Computes data required to get file tasks and requests the tasks.
62 * @param {function} callback The callback.
64 FileSelection.prototype.createTasks = function(callback) {
65 if (!this.fileManager_.isOnDrive()) {
66 this.tasks.init(this.entries);
71 this.fileManager_.metadataCache_.get(this.entries, 'drive', function(props) {
72 var present = props.filter(function(p) { return p && p.availableOffline });
73 this.allDriveFilesPresent = present.length == props.length;
75 // Collect all of the mime types and push that info into the selection.
76 this.mimeTypes = props.map(function(value) {
77 return (value && value.contentMimeType) || '';
80 this.tasks.init(this.entries, this.mimeTypes);
86 * Computes the total size of selected files.
88 * @param {function} callback Completion callback. Not called when cancelled,
89 * or a new call has been invoked in the meantime.
91 FileSelection.prototype.computeBytes = function(callback) {
92 if (this.entries.length == 0) {
93 this.bytesKnown = true;
94 this.showBytes = false;
99 var computeBytesSequence = ++this.computeBytesSequence_;
100 var pendingMetadataCount = 0;
102 var maybeDone = function() {
103 if (pendingMetadataCount == 0) {
104 this.bytesKnown = true;
109 var onProps = function(properties) {
110 // Ignore if the call got cancelled, or there is another new one fired.
111 if (computeBytesSequence != this.computeBytesSequence_)
114 // It may happen that the metadata is not available because a file has been
115 // deleted in the meantime.
117 this.bytes += properties.size;
118 pendingMetadataCount--;
122 for (var index = 0; index < this.entries.length; index++) {
123 var entry = this.entries[index];
125 this.showBytes |= !FileType.isHosted(entry);
126 pendingMetadataCount++;
127 this.fileManager_.metadataCache_.get(entry, 'filesystem', onProps);
128 } else if (entry.isDirectory) {
129 // Don't compute the directory size as it's expensive.
131 this.showBytes = false;
139 * Cancels any async computation by increasing the sequence number. Results
140 * of any previous call to computeBytes() will be discarded.
144 FileSelection.prototype.cancelComputing_ = function() {
145 this.computeBytesSequence_++;
149 * This object encapsulates everything related to current selection.
151 * @param {FileManager} fileManager File manager instance.
152 * @extends {cr.EventTarget}
155 function FileSelectionHandler(fileManager) {
156 this.fileManager_ = fileManager;
157 // TODO(dgozman): create a shared object with most of UI elements.
158 this.okButton_ = fileManager.okButton_;
159 this.filenameInput_ = fileManager.filenameInput_;
160 this.previewPanel_ = fileManager.previewPanel_;
161 this.taskItems_ = fileManager.taskItems_;
165 * Create the temporary disabled action menu item.
166 * @return {Object} Created disabled item.
169 FileSelectionHandler.createTemporaryDisabledActionMenuItem_ = function() {
170 if (!FileSelectionHandler.cachedDisabledActionMenuItem_) {
171 FileSelectionHandler.cachedDisabledActionMenuItem_ = {
172 label: str('ACTION_OPEN'),
177 return FileSelectionHandler.cachedDisabledActionMenuItem_;
181 * Cached the temporary disabled action menu item. Used inside
182 * FileSelectionHandler.createTemporaryDisabledActionMenuItem_().
185 FileSelectionHandler.cachedDisabledActionMenuItem_ = null;
188 * FileSelectionHandler extends cr.EventTarget.
190 FileSelectionHandler.prototype.__proto__ = cr.EventTarget.prototype;
193 * Maximum amount of thumbnails in the preview pane.
198 FileSelectionHandler.MAX_PREVIEW_THUMBNAIL_COUNT = 4;
201 * Maximum width or height of an image what pops up when the mouse hovers
202 * thumbnail in the bottom panel (in pixels).
207 FileSelectionHandler.IMAGE_HOVER_PREVIEW_SIZE = 200;
210 * Update the UI when the selection model changes.
212 * @param {Event} event The change event.
214 FileSelectionHandler.prototype.onFileSelectionChanged = function(event) {
216 this.fileManager_.getCurrentList().selectionModel.selectedIndexes;
217 if (this.selection) this.selection.cancelComputing_();
218 var selection = new FileSelection(this.fileManager_, indexes);
219 this.selection = selection;
221 if (this.fileManager_.dialogType == DialogType.SELECT_SAVEAS_FILE) {
222 // If this is a save-as dialog, copy the selected file into the filename
224 if (this.selection.totalCount == 1 &&
225 this.selection.entries[0].isFile &&
226 this.filenameInput_.value != this.selection.entries[0].name) {
227 this.filenameInput_.value = this.selection.entries[0].name;
231 this.updateOkButton();
233 if (this.selectionUpdateTimer_) {
234 clearTimeout(this.selectionUpdateTimer_);
235 this.selectionUpdateTimer_ = null;
238 // The rest of the selection properties are computed via (sometimes lengthy)
239 // asynchronous calls. We initiate these calls after a timeout. If the
240 // selection is changing quickly we only do this once when it slows down.
242 var updateDelay = 200;
243 var now = Date.now();
244 if (now > (this.lastFileSelectionTime_ || 0) + updateDelay) {
245 // The previous selection change happened a while ago. Update the UI soon.
248 this.lastFileSelectionTime_ = now;
250 if (this.fileManager_.dialogType === DialogType.FULL_PAGE &&
251 selection.directoryCount === 0 && selection.fileCount > 0) {
252 // Show disabled items for position calculation of the menu. They will be
253 // overridden in this.updateFileSelectionAsync().
254 this.fileManager_.updateContextMenuActionItems(
255 FileSelectionHandler.createTemporaryDisabledActionMenuItem_(), true);
257 // Update context menu.
258 this.fileManager_.updateContextMenuActionItems(null, false);
261 this.selectionUpdateTimer_ = setTimeout(function() {
262 this.selectionUpdateTimer_ = null;
263 if (this.selection == selection)
264 this.updateFileSelectionAsync(selection);
265 }.bind(this), updateDelay);
269 * Updates the Ok button enabled state.
271 * @return {boolean} Whether button is enabled.
273 FileSelectionHandler.prototype.updateOkButton = function() {
275 var dialogType = this.fileManager_.dialogType;
277 if (DialogType.isFolderDialog(dialogType)) {
278 // In SELECT_FOLDER mode, we allow to select current directory
279 // when nothing is selected.
280 selectable = this.selection.directoryCount <= 1 &&
281 this.selection.fileCount == 0;
282 } else if (dialogType == DialogType.SELECT_OPEN_FILE) {
283 selectable = (this.isFileSelectionAvailable() &&
284 this.selection.directoryCount == 0 &&
285 this.selection.fileCount == 1);
286 } else if (dialogType == DialogType.SELECT_OPEN_MULTI_FILE) {
287 selectable = (this.isFileSelectionAvailable() &&
288 this.selection.directoryCount == 0 &&
289 this.selection.fileCount >= 1);
290 } else if (dialogType == DialogType.SELECT_SAVEAS_FILE) {
291 if (this.fileManager_.isOnReadonlyDirectory()) {
294 selectable = !!this.filenameInput_.value;
296 } else if (dialogType == DialogType.FULL_PAGE) {
297 // No "select" buttons on the full page UI.
300 throw new Error('Unknown dialog type');
303 this.okButton_.disabled = !selectable;
308 * Check if all the files in the current selection are available. The only
309 * case when files might be not available is when the selection contains
310 * uncached Drive files and the browser is offline.
312 * @return {boolean} True if all files in the current selection are
315 FileSelectionHandler.prototype.isFileSelectionAvailable = function() {
316 return !this.fileManager_.isOnDrive() ||
317 !this.fileManager_.isDriveOffline() ||
318 this.selection.allDriveFilesPresent;
322 * Calculates async selection stats and updates secondary UI elements.
324 * @param {FileSelection} selection The selection object.
326 FileSelectionHandler.prototype.updateFileSelectionAsync = function(selection) {
327 if (this.selection != selection) return;
329 // Update the file tasks.
330 if (this.fileManager_.dialogType === DialogType.FULL_PAGE &&
331 selection.directoryCount === 0 && selection.fileCount > 0) {
332 selection.createTasks(function() {
333 if (this.selection != selection)
335 selection.tasks.display(this.taskItems_);
336 selection.tasks.updateMenuItem();
339 this.taskItems_.hidden = true;
342 // Update preview panels.
343 var wasVisible = this.previewPanel_.visible;
344 this.previewPanel_.setSelection(selection);
347 if (!wasVisible && this.selection.totalCount == 1) {
348 var list = this.fileManager_.getCurrentList();
349 list.scrollIndexIntoView(list.selectionModel.selectedIndex);
352 // Sync the commands availability.
353 if (this.fileManager_.commandHandler)
354 this.fileManager_.commandHandler.updateAvailability();
356 // Inform tests it's OK to click buttons now.
357 if (selection.totalCount > 0) {
358 chrome.test.sendMessage('selection-change-complete');