Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(674)

Side by Side Diff: chrome/browser/resources/settings/device_page/layout_behavior.js

Issue 2651483002: MD Settings: Eliminate use of ES6 for Chrome OS (Closed)
Patch Set: . Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 Behavior for handling display layout, specifically 6 * @fileoverview Behavior for handling display layout, specifically
7 * edge snapping and collisions. 7 * edge snapping and collisions.
8 */ 8 */
9 9
10 /** @polymerBehavior */ 10 /** @polymerBehavior */
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 * @param {!Array<!chrome.system.display.DisplayUnitInfo>} displays 51 * @param {!Array<!chrome.system.display.DisplayUnitInfo>} displays
52 * @param {!Array<!chrome.system.display.DisplayLayout>} layouts 52 * @param {!Array<!chrome.system.display.DisplayLayout>} layouts
53 */ 53 */
54 initializeDisplayLayout: function(displays, layouts) { 54 initializeDisplayLayout: function(displays, layouts) {
55 this.dragLayoutId = ''; 55 this.dragLayoutId = '';
56 this.dragParentId_ = ''; 56 this.dragParentId_ = '';
57 57
58 this.mirroring = displays.length > 0 && !!displays[0].mirroringSourceId; 58 this.mirroring = displays.length > 0 && !!displays[0].mirroringSourceId;
59 59
60 this.displayBoundsMap_.clear(); 60 this.displayBoundsMap_.clear();
61 for (let display of displays) 61 for (var d = 0; d < displays.length; ++d) {
62 var display = displays[d];
62 this.displayBoundsMap_.set(display.id, display.bounds); 63 this.displayBoundsMap_.set(display.id, display.bounds);
63 64 }
64 this.displayLayoutMap_.clear(); 65 this.displayLayoutMap_.clear();
65 for (let layout of layouts) 66 for (var l = 0; l < layouts.length; ++l) {
67 var layout = layouts[l];
66 this.displayLayoutMap_.set(layout.id, layout); 68 this.displayLayoutMap_.set(layout.id, layout);
67 69 }
68 this.calculatedBoundsMap_.clear(); 70 this.calculatedBoundsMap_.clear();
69 for (let display of displays) { 71 for (d = 0; d < displays.length; ++d) {
72 display = displays[d];
70 if (!this.calculatedBoundsMap_.has(display.id)) { 73 if (!this.calculatedBoundsMap_.has(display.id)) {
71 let bounds = display.bounds; 74 var bounds = display.bounds;
72 this.calculateBounds_(display.id, bounds.width, bounds.height); 75 this.calculateBounds_(display.id, bounds.width, bounds.height);
73 } 76 }
74 } 77 }
75 }, 78 },
76 79
77 /** 80 /**
78 * Called when a drag event occurs. Checks collisions and updates the layout. 81 * Called when a drag event occurs. Checks collisions and updates the layout.
79 * @param {string} id 82 * @param {string} id
80 * @param {!chrome.system.display.Bounds} newBounds The new calculated 83 * @param {!chrome.system.display.Bounds} newBounds The new calculated
81 * bounds for the display. 84 * bounds for the display.
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 var layout = this.displayLayoutMap_.get(id); 142 var layout = this.displayLayoutMap_.get(id);
140 143
141 var orphanIds; 144 var orphanIds;
142 if (!layout || layout.parentId == '') { 145 if (!layout || layout.parentId == '') {
143 // Primary display. Set the calculated position to |dragBounds_|. 146 // Primary display. Set the calculated position to |dragBounds_|.
144 this.setCalculatedDisplayBounds_(id, this.dragBounds_); 147 this.setCalculatedDisplayBounds_(id, this.dragBounds_);
145 148
146 // We cannot re-parent the primary display, so instead make all other 149 // We cannot re-parent the primary display, so instead make all other
147 // displays orphans and clear their calculated bounds. 150 // displays orphans and clear their calculated bounds.
148 orphanIds = this.findChildren_(id, true /* recurse */); 151 orphanIds = this.findChildren_(id, true /* recurse */);
149 for (let o in orphanIds) 152 for (var o in orphanIds)
dpapad 2017/01/23 19:12:54 Iterating an array with for..in is discouraged (ve
stevenjb 2017/01/24 02:02:04 Good catch. This is in fact incorrect, does nothin
dpapad 2017/01/24 02:38:10 Ok. I believe you, although reading the comment ma
stevenjb 2017/01/24 02:59:52 Yeah, this is not as robust as it should be, but t
150 this.calculatedBoundsMap_.delete(o); 153 this.calculatedBoundsMap_.delete(o);
151 154
152 // Re-parent |dragParentId_|. It will be forced to parent to the dragged 155 // Re-parent |dragParentId_|. It will be forced to parent to the dragged
153 // display since it is the only non-orphan. 156 // display since it is the only non-orphan.
154 this.reparentOrphan_(this.dragParentId_, orphanIds); 157 this.reparentOrphan_(this.dragParentId_, orphanIds);
155 orphanIds.splice(orphanIds.indexOf(this.dragParentId_), 1); 158 orphanIds.splice(orphanIds.indexOf(this.dragParentId_), 1);
156 } else { 159 } else {
157 // All immediate children of |layout| will need to be re-parented. 160 // All immediate children of |layout| will need to be re-parented.
158 orphanIds = this.findChildren_(id, false /* do not recurse */); 161 orphanIds = this.findChildren_(id, false /* do not recurse */);
159 for (let o in orphanIds) 162 for (var oo in orphanIds)
dpapad 2017/01/23 19:12:54 Same here and elsewhere in this file.
stevenjb 2017/01/24 02:02:03 Ditto.
160 this.calculatedBoundsMap_.delete(o); 163 this.calculatedBoundsMap_.delete(oo);
161 164
162 // When re-parenting to a descendant, also parent any immediate child to 165 // When re-parenting to a descendant, also parent any immediate child to
163 // drag display's current parent. 166 // drag display's current parent.
164 var topLayout = this.displayLayoutMap_.get(this.dragParentId_); 167 var topLayout = this.displayLayoutMap_.get(this.dragParentId_);
165 while (topLayout && topLayout.parentId != '') { 168 while (topLayout && topLayout.parentId != '') {
166 if (topLayout.parentId == id) { 169 if (topLayout.parentId == id) {
167 topLayout.parentId = layout.parentId; 170 topLayout.parentId = layout.parentId;
168 break; 171 break;
169 } 172 }
170 topLayout = this.displayLayoutMap_.get(topLayout.parentId); 173 topLayout = this.displayLayoutMap_.get(topLayout.parentId);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 Object.assign({}, bounds))); 216 Object.assign({}, bounds)));
214 }, 217 },
215 218
216 /** 219 /**
217 * Re-parents all entries in |orphanIds| and any children. 220 * Re-parents all entries in |orphanIds| and any children.
218 * @param {!Array<string>} orphanIds The list of ids affected by the move. 221 * @param {!Array<string>} orphanIds The list of ids affected by the move.
219 * @private 222 * @private
220 */ 223 */
221 updateOrphans_: function(orphanIds) { 224 updateOrphans_: function(orphanIds) {
222 var orphans = orphanIds.slice(); 225 var orphans = orphanIds.slice();
223 for (let orphan of orphanIds) { 226 for (var i = 0; i < orphanIds.length; ++i) {
227 var orphan = orphanIds[i];
224 var newOrphans = this.findChildren_(orphan, true /* recurse */); 228 var newOrphans = this.findChildren_(orphan, true /* recurse */);
225 // If the dragged display was re-parented to one of its children, 229 // If the dragged display was re-parented to one of its children,
226 // there may be duplicates so merge the lists. 230 // there may be duplicates so merge the lists.
227 for (let o of newOrphans) { 231 for (var ii = 0; ii < newOrphans.length; ++ii) {
232 var o = newOrphans[ii];
228 if (!orphans.includes(o)) 233 if (!orphans.includes(o))
229 orphans.push(o); 234 orphans.push(o);
230 } 235 }
231 } 236 }
232 237
233 // Remove each orphan from the list as it is re-parented so that 238 // Remove each orphan from the list as it is re-parented so that
234 // subsequent orphans can be parented to it. 239 // subsequent orphans can be parented to it.
235 while (orphans.length) { 240 while (orphans.length) {
236 var orphanId = orphans.shift(); 241 var orphanId = orphans.shift();
237 this.reparentOrphan_(orphanId, orphans); 242 this.reparentOrphan_(orphanId, orphans);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 }, 290 },
286 291
287 /** 292 /**
288 * @param {string} parentId 293 * @param {string} parentId
289 * @param {boolean} recurse Include descendants of children. 294 * @param {boolean} recurse Include descendants of children.
290 * @return {!Array<string>} 295 * @return {!Array<string>}
291 * @private 296 * @private
292 */ 297 */
293 findChildren_: function(parentId, recurse) { 298 findChildren_: function(parentId, recurse) {
294 var children = []; 299 var children = [];
295 for (let childId of this.displayLayoutMap_.keys()) { 300 this.displayLayoutMap_.forEach(function(value, key) {
296 if (childId == parentId) 301 var childId = key;
dpapad 2017/01/23 19:12:54 Nit: Why not mirror the previous logic more closel
stevenjb 2017/01/24 02:02:03 I find 'return' inside an inlined forEach super co
297 continue; 302 if (childId != parentId && value.parentId == parentId) {
298 if (this.displayLayoutMap_.get(childId).parentId == parentId) {
299 // Insert immediate children at the front of the array. 303 // Insert immediate children at the front of the array.
300 children.unshift(childId); 304 children.unshift(childId);
301 if (recurse) { 305 if (recurse) {
302 // Descendants get added to the end of the list. 306 // Descendants get added to the end of the list.
303 children = children.concat(this.findChildren_(childId, true)); 307 children = children.concat(this.findChildren_(childId, true));
304 } 308 }
305 } 309 }
306 } 310 }.bind(this));
307 return children; 311 return children;
308 }, 312 },
309 313
310 /** 314 /**
311 * Recursively calculates the absolute bounds of a display. 315 * Recursively calculates the absolute bounds of a display.
312 * Caches the display bounds so that parent bounds are only calculated once. 316 * Caches the display bounds so that parent bounds are only calculated once.
313 * @param {string} id 317 * @param {string} id
314 * @param {number} width 318 * @param {number} width
315 * @param {number} height 319 * @param {number} height
316 * @private 320 * @private
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 * @param {!chrome.system.display.Bounds} bounds 367 * @param {!chrome.system.display.Bounds} bounds
364 * @param {Array<string>=} opt_ignoreIds Ids to ignore. 368 * @param {Array<string>=} opt_ignoreIds Ids to ignore.
365 * @return {string} 369 * @return {string}
366 * @private 370 * @private
367 */ 371 */
368 findClosest_: function(displayId, bounds, opt_ignoreIds) { 372 findClosest_: function(displayId, bounds, opt_ignoreIds) {
369 var x = bounds.left + bounds.width / 2; 373 var x = bounds.left + bounds.width / 2;
370 var y = bounds.top + bounds.height / 2; 374 var y = bounds.top + bounds.height / 2;
371 var closestId = ''; 375 var closestId = '';
372 var closestDelta2 = 0; 376 var closestDelta2 = 0;
373 for (let otherId of this.calculatedBoundsMap_.keys()) { 377 var keys = this.calculatedBoundsMap_.keys();
378 for (var o = 0; o < this.calculatedBoundsMap_.size; ++o) {
379 var otherId = keys.next().value;
dpapad 2017/01/23 19:12:54 (Here and elsewhere): Iterating over a map using b
stevenjb 2017/01/24 02:02:04 I din't know about next.done. that makes sense. Do
374 if (otherId == displayId) 380 if (otherId == displayId)
375 continue; 381 continue;
376 if (opt_ignoreIds && opt_ignoreIds.includes(otherId)) 382 if (opt_ignoreIds && opt_ignoreIds.includes(otherId))
377 continue; 383 continue;
378 var otherBounds = this.getCalculatedDisplayBounds(otherId); 384 var otherBounds = this.getCalculatedDisplayBounds(otherId);
379 var left = otherBounds.left; 385 var left = otherBounds.left;
380 var top = otherBounds.top; 386 var top = otherBounds.top;
381 var width = otherBounds.width; 387 var width = otherBounds.width;
382 var height = otherBounds.height; 388 var height = otherBounds.height;
383 if (x >= left && x < left + width && y >= top && y < top + height) 389 if (x >= left && x < left + width && y >= top && y < top + height)
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 * @param {!chrome.system.display.Bounds} bounds 550 * @param {!chrome.system.display.Bounds} bounds
545 * @param {!{x: number, y: number}} deltaPos 551 * @param {!{x: number, y: number}} deltaPos
546 */ 552 */
547 collideAndModifyDelta_: function(id, bounds, deltaPos) { 553 collideAndModifyDelta_: function(id, bounds, deltaPos) {
548 var keys = this.calculatedBoundsMap_.keys(); 554 var keys = this.calculatedBoundsMap_.keys();
549 var others = new Set(keys); 555 var others = new Set(keys);
550 others.delete(id); 556 others.delete(id);
551 var checkCollisions = true; 557 var checkCollisions = true;
552 while (checkCollisions) { 558 while (checkCollisions) {
553 checkCollisions = false; 559 checkCollisions = false;
554 for (let otherId of others) { 560 var othersValues = others.values();
561 for (var o = 0; o < others.size; ++o) {
562 var otherId = othersValues.next().value;
555 var otherBounds = this.getCalculatedDisplayBounds(otherId); 563 var otherBounds = this.getCalculatedDisplayBounds(otherId);
556 if (this.collideWithBoundsAndModifyDelta_( 564 if (this.collideWithBoundsAndModifyDelta_(
557 bounds, otherBounds, deltaPos)) { 565 bounds, otherBounds, deltaPos)) {
558 if (deltaPos.x == 0 && deltaPos.y == 0) 566 if (deltaPos.x == 0 && deltaPos.y == 0)
559 return; 567 return;
560 others.delete(otherId); 568 others.delete(otherId);
561 checkCollisions = true; 569 checkCollisions = true;
562 break; 570 break;
563 } 571 }
564 } 572 }
(...skipping 19 matching lines...) Expand all
584 (newY >= otherBounds.top + otherBounds.height)) { 592 (newY >= otherBounds.top + otherBounds.height)) {
585 return false; 593 return false;
586 } 594 }
587 595
588 // |deltaPos| should already be restricted to X or Y. This shortens the 596 // |deltaPos| should already be restricted to X or Y. This shortens the
589 // delta to stay outside the bounds, however it does not change the sign of 597 // delta to stay outside the bounds, however it does not change the sign of
590 // the delta, i.e. it does not "push" the point outside the bounds if 598 // the delta, i.e. it does not "push" the point outside the bounds if
591 // the point is already inside. 599 // the point is already inside.
592 if (Math.abs(deltaPos.x) > Math.abs(deltaPos.y)) { 600 if (Math.abs(deltaPos.x) > Math.abs(deltaPos.y)) {
593 deltaPos.y = 0; 601 deltaPos.y = 0;
594 let snapDeltaX; 602 var snapDeltaX;
595 if (deltaPos.x > 0) { 603 if (deltaPos.x > 0) {
596 let x = otherBounds.left - bounds.width; 604 snapDeltaX =
597 snapDeltaX = Math.max(0, x - bounds.left); 605 Math.max(0, (otherBounds.left - bounds.width) - bounds.left);
598 } else { 606 } else {
599 let x = otherBounds.left + otherBounds.width; 607 snapDeltaX =
600 snapDeltaX = Math.min(x - bounds.left, 0); 608 Math.min(0, (otherBounds.left + otherBounds.width) - bounds.left);
601 } 609 }
602 deltaPos.x = snapDeltaX; 610 deltaPos.x = snapDeltaX;
603 } else { 611 } else {
604 deltaPos.x = 0; 612 deltaPos.x = 0;
605 let snapDeltaY; 613 var snapDeltaY;
606 if (deltaPos.y > 0) { 614 if (deltaPos.y > 0) {
607 let y = otherBounds.top - bounds.height; 615 snapDeltaY =
608 snapDeltaY = Math.min(0, y - bounds.top); 616 Math.min(0, (otherBounds.top - bounds.height) - bounds.top);
609 } else if (deltaPos.y < 0) { 617 } else if (deltaPos.y < 0) {
610 let y = otherBounds.top + otherBounds.height; 618 snapDeltaY =
611 snapDeltaY = Math.max(y - bounds.top, 0); 619 Math.max(0, (otherBounds.top + otherBounds.height) - bounds.top);
612 } else { 620 } else {
613 snapDeltaY = 0; 621 snapDeltaY = 0;
614 } 622 }
615 deltaPos.y = snapDeltaY; 623 deltaPos.y = snapDeltaY;
616 } 624 }
617 625
618 return true; 626 return true;
619 }, 627 },
620 628
621 /** 629 /**
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 689
682 /** 690 /**
683 * Highlights the edge of the div associated with |id| based on 691 * Highlights the edge of the div associated with |id| based on
684 * |layoutPosition| and removes any other highlights. If |layoutPosition| is 692 * |layoutPosition| and removes any other highlights. If |layoutPosition| is
685 * undefined, removes all highlights. 693 * undefined, removes all highlights.
686 * @param {string} id 694 * @param {string} id
687 * @param {chrome.system.display.LayoutPosition|undefined} layoutPosition 695 * @param {chrome.system.display.LayoutPosition|undefined} layoutPosition
688 * @private 696 * @private
689 */ 697 */
690 highlightEdge_: function(id, layoutPosition) { 698 highlightEdge_: function(id, layoutPosition) {
691 for (let layout of this.layouts) { 699 for (var l = 0; l < this.layouts.length; ++l) {
700 var layout = this.layouts[l];
692 var highlight = (layout.id == id) ? layoutPosition : undefined; 701 var highlight = (layout.id == id) ? layoutPosition : undefined;
693 var div = this.$$('#_' + layout.id); 702 var div = this.$$('#_' + layout.id);
694 div.classList.toggle( 703 div.classList.toggle(
695 'highlight-right', 704 'highlight-right',
696 highlight == chrome.system.display.LayoutPosition.RIGHT); 705 highlight == chrome.system.display.LayoutPosition.RIGHT);
697 div.classList.toggle( 706 div.classList.toggle(
698 'highlight-left', 707 'highlight-left',
699 highlight == chrome.system.display.LayoutPosition.LEFT); 708 highlight == chrome.system.display.LayoutPosition.LEFT);
700 div.classList.toggle( 709 div.classList.toggle(
701 'highlight-top', 710 'highlight-top',
702 highlight == chrome.system.display.LayoutPosition.TOP); 711 highlight == chrome.system.display.LayoutPosition.TOP);
703 div.classList.toggle( 712 div.classList.toggle(
704 'highlight-bottom', 713 'highlight-bottom',
705 highlight == chrome.system.display.LayoutPosition.BOTTOM); 714 highlight == chrome.system.display.LayoutPosition.BOTTOM);
706 } 715 }
707 }, 716 },
708 }; 717 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698