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 * FileGrid constructor.
10 * Represents grid for the Grid View in the File Manager.
12 * @extends {cr.ui.Grid}
16 throw new Error('Use FileGrid.decorate');
23 FileGrid.ThumbnailQuality = {
29 * Inherits from cr.ui.Grid.
31 FileGrid.prototype.__proto__ = cr.ui.Grid.prototype;
34 * Decorates an HTML element to be a FileGrid.
35 * @param {HTMLElement} self The grid to decorate.
36 * @param {MetadataCache} metadataCache Metadata cache to find entries
38 * @param {VolumeManagerWrapper} volumeManager Volume manager instance.
40 FileGrid.decorate = function(self, metadataCache, volumeManager) {
41 cr.ui.Grid.decorate(self);
42 self.__proto__ = FileGrid.prototype;
43 self.metadataCache_ = metadataCache;
44 self.volumeManager_ = volumeManager;
46 self.scrollBar_ = new MainPanelScrollBar();
47 self.scrollBar_.initialize(self.parentNode, self);
48 self.setBottomMarginForPanel(0);
50 self.itemConstructor = function(entry) {
51 var item = self.ownerDocument.createElement('LI');
52 FileGrid.Item.decorate(item, entry, self);
56 self.relayoutRateLimiter_ =
57 new AsyncUtil.RateLimiter(self.relayoutImmediately_.bind(self));
61 * Updates items to reflect metadata changes.
62 * @param {string} type Type of metadata changed.
63 * @param {Array.<Entry>} entries Entries whose metadata changed.
65 FileGrid.prototype.updateListItemsMetadata = function(type, entries) {
66 var urls = util.entriesToURLs(entries);
67 var boxes = this.querySelectorAll('.img-container');
68 for (var i = 0; i < boxes.length; i++) {
70 var entry = this.dataModel.item(this.getListItemAncestor(box));
71 if (!entry || !(entry.toURL() in urls))
74 FileGrid.decorateThumbnailBox(box,
78 ThumbnailLoader.FillMode.FIT,
79 FileGrid.ThumbnailQuality.LOW);
84 * Redraws the UI. Skips multiple consecutive calls.
86 FileGrid.prototype.relayout = function() {
87 this.relayoutRateLimiter_.run();
91 * Redraws the UI immediately.
94 FileGrid.prototype.relayoutImmediately_ = function() {
95 this.startBatchUpdates();
98 this.endBatchUpdates();
99 cr.dispatchSimpleEvent(this, 'relayout');
103 * Decorates thumbnail.
104 * @param {HTMLElement} li List item.
105 * @param {Entry} entry Entry to render a thumbnail for.
106 * @param {MetadataCache} metadataCache To retrieve metadata.
107 * @param {VolumeManagerWrapper} volumeManager Volume manager instance.
109 FileGrid.decorateThumbnail = function(li, entry, metadataCache, volumeManager) {
110 li.className = 'thumbnail-item';
112 filelist.decorateListItem(li, entry, metadataCache);
114 var frame = li.ownerDocument.createElement('div');
115 frame.className = 'thumbnail-frame';
116 li.appendChild(frame);
118 var box = li.ownerDocument.createElement('div');
120 FileGrid.decorateThumbnailBox(box,
124 ThumbnailLoader.FillMode.AUTO,
125 FileGrid.ThumbnailQuality.LOW);
127 frame.appendChild(box);
129 var bottom = li.ownerDocument.createElement('div');
130 bottom.className = 'thumbnail-bottom';
131 bottom.appendChild(filelist.renderFileNameLabel(li.ownerDocument, entry));
132 frame.appendChild(bottom);
136 * Decorates the box containing a centered thumbnail image.
138 * @param {HTMLDivElement} box Box to decorate.
139 * @param {Entry} entry Entry which thumbnail is generating for.
140 * @param {MetadataCache} metadataCache To retrieve metadata.
141 * @param {VolumeManagerWrapper} volumeManager Volume manager instance.
142 * @param {ThumbnailLoader.FillMode} fillMode Fill mode.
143 * @param {FileGrid.ThumbnailQuality} quality Thumbnail quality.
144 * @param {function(HTMLElement)=} opt_imageLoadCallback Callback called when
145 * the image has been loaded before inserting it into the DOM.
147 FileGrid.decorateThumbnailBox = function(
148 box, entry, metadataCache, volumeManager, fillMode, quality,
149 opt_imageLoadCallback) {
150 var locationInfo = volumeManager.getLocationInfo(entry);
151 box.className = 'img-container';
153 if (entry.isDirectory) {
154 box.setAttribute('generic-thumbnail', 'folder');
155 if (locationInfo && locationInfo.isDriveBased) {
156 metadataCache.getOne(entry, 'external', function(metadata) {
158 box.classList.add('shared');
161 if (opt_imageLoadCallback)
162 setTimeout(opt_imageLoadCallback, 0, null /* callback parameter */);
166 var metadataTypes = 'thumbnail|filesystem|external|media';
168 // Drive provides high quality thumbnails via USE_EMBEDDED, however local
169 // images usually provide very tiny thumbnails, therefore USE_EMBEDDE can't
170 // be used to obtain high quality output.
173 case FileGrid.ThumbnailQuality.LOW:
174 useEmbedded = ThumbnailLoader.UseEmbedded.USE_EMBEDDED;
176 case FileGrid.ThumbnailQuality.HIGH:
177 // TODO(mtomasz): Use Entry instead of paths.
178 useEmbedded = (locationInfo && locationInfo.isDriveBased) ?
179 ThumbnailLoader.UseEmbedded.USE_EMBEDDED :
180 ThumbnailLoader.UseEmbedded.NO_EMBEDDED;
184 metadataCache.getOne(entry, metadataTypes,
186 new ThumbnailLoader(entry,
187 ThumbnailLoader.LoaderType.IMAGE,
189 undefined, // opt_mediaType
193 ThumbnailLoader.OptimizationMode.DISCARD_DETACHED,
194 opt_imageLoadCallback);
199 * Item for the Grid View.
202 FileGrid.Item = function() {
207 * Inherits from cr.ui.ListItem.
209 FileGrid.Item.prototype.__proto__ = cr.ui.ListItem.prototype;
211 Object.defineProperty(FileGrid.Item.prototype, 'label', {
213 * @this {FileGrid.Item}
214 * @return {string} Label of the item.
217 return this.querySelector('filename-label').textContent;
222 * @param {Element} li List item element.
223 * @param {Entry} entry File entry.
224 * @param {FileGrid} grid Owner.
226 FileGrid.Item.decorate = function(li, entry, grid) {
227 li.__proto__ = FileGrid.Item.prototype;
228 // TODO(mtomasz): Pass the metadata cache and the volume manager directly
229 // instead of accessing private members of grid.
230 FileGrid.decorateThumbnail(
231 li, entry, grid.metadataCache_, grid.volumeManager_, true);
233 // Override the default role 'listitem' to 'option' to match the parent's
235 li.setAttribute('role', 'option');
239 * Sets the margin height for the transparent preview panel at the bottom.
240 * @param {number} margin Margin to be set in px.
242 FileGrid.prototype.setBottomMarginForPanel = function(margin) {
243 // +20 bottom margin is needed to match the bottom margin size with the
244 // margin between its items.
245 this.style.paddingBottom = (margin + 20) + 'px';
246 this.scrollBar_.setBottomMarginForPanel(margin);
250 * Obtains if the drag selection should be start or not by referring the mouse
252 * @param {MouseEvent} event Drag start event.
253 * @return {boolean} True if the mouse is hit to the background of the list.
255 FileGrid.prototype.shouldStartDragSelection = function(event) {
256 var pos = DragSelector.getScrolledPosition(this, event);
257 return this.getHitElements(pos.x, pos.y).length === 0;
261 * Obtains the column/row index that the coordinate points.
262 * @param {number} coordinate Vertical/horizontal coordinate value that points
264 * @param {number} step Length from a column/row to the next one.
265 * @param {number} threshold Threshold that determines whether 1 offset is added
266 * to the return value or not. This is used in order to handle the margin of
268 * @return {number} Index of hit column/row.
271 FileGrid.prototype.getHitIndex_ = function(coordinate, step, threshold) {
272 var index = ~~(coordinate / step);
273 return (coordinate % step >= threshold) ? index + 1 : index;
277 * Obtains the index list of elements that are hit by the point or the
280 * We should match its argument interface with FileList.getHitElements.
282 * @param {number} x X coordinate value.
283 * @param {number} y Y coordinate value.
284 * @param {number=} opt_width Width of the coordinate.
285 * @param {number=} opt_height Height of the coordinate.
286 * @return {Array.<number>} Index list of hit elements.
288 FileGrid.prototype.getHitElements = function(x, y, opt_width, opt_height) {
289 var currentSelection = [];
290 var right = x + (opt_width || 0);
291 var bottom = y + (opt_height || 0);
292 var itemMetrics = this.measureItem();
293 var horizontalStartIndex = this.getHitIndex_(
294 x, itemMetrics.width, itemMetrics.width - itemMetrics.marginRight);
295 var horizontalEndIndex = Math.min(this.columns, this.getHitIndex_(
296 right, itemMetrics.width, itemMetrics.marginLeft));
297 var verticalStartIndex = this.getHitIndex_(
298 y, itemMetrics.height, itemMetrics.height - itemMetrics.bottom);
299 var verticalEndIndex = this.getHitIndex_(
300 bottom, itemMetrics.height, itemMetrics.marginTop);
301 for (var verticalIndex = verticalStartIndex;
302 verticalIndex < verticalEndIndex;
304 var indexBase = this.getFirstItemInRow(verticalIndex);
305 for (var horizontalIndex = horizontalStartIndex;
306 horizontalIndex < horizontalEndIndex;
308 var index = indexBase + horizontalIndex;
309 if (0 <= index && index < this.dataModel.length)
310 currentSelection.push(index);
313 return currentSelection;