| OLD | NEW |
| 1 | 1 |
| 2 // Helpers | 2 // Helpers |
| 3 | 3 |
| 4 function $(id) { | 4 function $(id) { |
| 5 return document.getElementById(id); | 5 return document.getElementById(id); |
| 6 } | 6 } |
| 7 | 7 |
| 8 // TODO(arv): Remove these when classList is available in HTML5. | 8 // TODO(arv): Remove these when classList is available in HTML5. |
| 9 // https://bugs.webkit.org/show_bug.cgi?id=20709 | 9 // https://bugs.webkit.org/show_bug.cgi?id=20709 |
| 10 function hasClass(el, name) { | 10 function hasClass(el, name) { |
| (...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 } else { | 504 } else { |
| 505 thumbCheckbox.checked = false; | 505 thumbCheckbox.checked = false; |
| 506 listCheckbox.checked = false; | 506 listCheckbox.checked = false; |
| 507 } | 507 } |
| 508 }, | 508 }, |
| 509 | 509 |
| 510 dirty_: false, | 510 dirty_: false, |
| 511 | 511 |
| 512 invalidate: function() { | 512 invalidate: function() { |
| 513 this.dirty_ = true; | 513 this.dirty_ = true; |
| 514 this.calculationsDirty_ = true; |
| 514 }, | 515 }, |
| 515 | 516 |
| 516 layout: function() { | 517 layout: function() { |
| 517 if (!this.dirty_) { | 518 if (!this.dirty_) { |
| 518 return; | 519 return; |
| 519 } | 520 } |
| 520 var d0 = Date.now(); | 521 var d0 = Date.now(); |
| 522 |
| 523 this.calculateLayout_(); |
| 524 |
| 521 var mostVisitedElement = $('most-visited'); | 525 var mostVisitedElement = $('most-visited'); |
| 522 var thumbnails = mostVisitedElement.children; | 526 var thumbnails = mostVisitedElement.children; |
| 523 | 527 |
| 528 if (shownSections & Section.LIST) { |
| 529 addClass(mostVisitedElement, 'list'); |
| 530 } else if (shownSections & Section.THUMB) { |
| 531 removeClass(mostVisitedElement, 'list'); |
| 532 } |
| 533 |
| 534 var cache = this.layoutCache_; |
| 535 mostVisitedElement.style.height = cache.sumHeight + 'px'; |
| 536 mostVisitedElement.style.opacity = cache.opacity; |
| 537 // We set overflow to hidden so that the most visited element does not |
| 538 // "leak" when we hide and show it. |
| 539 if (!cache.opacity) { |
| 540 mostVisitedElement.style.overflow = 'hidden'; |
| 541 } |
| 542 |
| 543 if (shownSections & Section.THUMB || shownSections & Section.LIST) { |
| 544 for (var i = 0; i < thumbnails.length; i++) { |
| 545 var t = thumbnails[i]; |
| 546 |
| 547 // Remove temporary ID that was used during startup layout. |
| 548 t.id = ''; |
| 549 |
| 550 var rect = cache.rects[i]; |
| 551 t.style.left = rect.left + 'px'; |
| 552 t.style.top = rect.top + 'px'; |
| 553 t.style.width = rect.width != undefined ? rect.width + 'px' : ''; |
| 554 var innerStyle = t.firstElementChild.style; |
| 555 innerStyle.left = innerStyle.top = ''; |
| 556 } |
| 557 } |
| 558 |
| 559 afterTransition(function() { |
| 560 // Only set overflow to visible if the element is shown. |
| 561 if (cache.opacity) { |
| 562 mostVisitedElement.style.overflow = ''; |
| 563 } |
| 564 }); |
| 565 |
| 566 this.dirty_ = false; |
| 567 |
| 568 logEvent('mostVisited.layout: ' + (Date.now() - d0)); |
| 569 }, |
| 570 |
| 571 layoutCache_: {}, |
| 572 calculationsDirty_: true, |
| 573 |
| 574 /** |
| 575 * Calculates and caches the layout positions for the thumbnails. |
| 576 */ |
| 577 calculateLayout_: function() { |
| 578 if (!this.calculationsDirty_) { |
| 579 return; |
| 580 } |
| 581 |
| 524 var small = useSmallGrid(); | 582 var small = useSmallGrid(); |
| 525 | 583 |
| 526 var cols = 4; | 584 var cols = 4; |
| 527 var rows = 2; | 585 var rows = 2; |
| 528 var marginWidth = 10; | 586 var marginWidth = 10; |
| 529 var marginHeight = 7; | 587 var marginHeight = 7; |
| 530 var borderWidth = 4; | 588 var borderWidth = 4; |
| 531 var thumbWidth = small ? 150 : 207; | 589 var thumbWidth = small ? 150 : 207; |
| 532 var thumbHeight = small ? 93 : 129; | 590 var thumbHeight = small ? 93 : 129; |
| 533 var w = thumbWidth + 2 * borderWidth + 2 * marginWidth; | 591 var w = thumbWidth + 2 * borderWidth + 2 * marginWidth; |
| 534 var h = thumbHeight + 40 + 2 * marginHeight; | 592 var h = thumbHeight + 40 + 2 * marginHeight; |
| 535 var sumWidth = cols * w - 2 * marginWidth; | 593 var sumWidth = cols * w - 2 * marginWidth; |
| 536 var sumHeight = rows * h; | 594 var sumHeight = rows * h; |
| 537 var opacity = 1; | 595 var opacity = 1; |
| 538 | 596 |
| 539 if (shownSections & Section.LIST) { | 597 if (shownSections & Section.LIST) { |
| 540 w = (sumWidth + 2 * marginWidth) / 2; | 598 w = (sumWidth + 2 * marginWidth) / 2; |
| 541 h = 45; | 599 h = 45; |
| 542 rows = 4; | 600 rows = 4; |
| 543 cols = 2; | 601 cols = 2; |
| 544 sumHeight = rows * h; | 602 sumHeight = rows * h; |
| 545 addClass(mostVisitedElement, 'list'); | 603 } else if (!(shownSections & Section.THUMB)) { |
| 546 } else if (shownSections & Section.THUMB) { | |
| 547 removeClass(mostVisitedElement, 'list'); | |
| 548 } else { | |
| 549 sumHeight = 0; | 604 sumHeight = 0; |
| 550 opacity = 0; | 605 opacity = 0; |
| 551 } | 606 } |
| 552 | 607 |
| 553 mostVisitedElement.style.height = sumHeight + 'px'; | |
| 554 mostVisitedElement.style.opacity = opacity; | |
| 555 // We set overflow to hidden so that the most visited element does not | |
| 556 // "leak" when we hide and show it. | |
| 557 if (!opacity) { | |
| 558 mostVisitedElement.style.overflow = 'hidden'; | |
| 559 } | |
| 560 | |
| 561 var rtl = document.documentElement.dir == 'rtl'; | 608 var rtl = document.documentElement.dir == 'rtl'; |
| 609 var rects = []; |
| 562 | 610 |
| 563 if (shownSections & Section.THUMB || shownSections & Section.LIST) { | 611 if (shownSections & Section.THUMB || shownSections & Section.LIST) { |
| 564 for (var i = 0; i < thumbnails.length; i++) { | 612 for (var i = 0; i < rows * cols; i++) { |
| 565 var t = thumbnails[i]; | 613 var row, col, left, top, width; |
| 566 | |
| 567 // Remove temporary ID that was used during startup layout. | |
| 568 t.id = ''; | |
| 569 | |
| 570 var row, col; | |
| 571 if (shownSections & Section.THUMB) { | 614 if (shownSections & Section.THUMB) { |
| 572 row = Math.floor(i / cols); | 615 row = Math.floor(i / cols); |
| 573 col = i % cols; | 616 col = i % cols; |
| 574 } else { | 617 } else { |
| 575 col = Math.floor(i / rows); | 618 col = Math.floor(i / rows); |
| 576 row = i % rows; | 619 row = i % rows; |
| 577 } | 620 } |
| 578 | 621 |
| 579 if (shownSections & Section.THUMB) { | 622 if (shownSections & Section.THUMB) { |
| 580 t.style.left = (rtl ? | 623 left = rtl ? sumWidth - col * w - thumbWidth - 2 * borderWidth : |
| 581 sumWidth - col * w - thumbWidth - 2 * borderWidth : | 624 col * w; |
| 582 col * w) + 'px'; | |
| 583 } else { | 625 } else { |
| 584 t.style.left = (rtl ? | 626 left = rtl ? sumWidth - col * w - w + 2 * marginWidth : col * w; |
| 585 sumWidth - col * w - w + 2 * marginWidth : | |
| 586 col * w) + 'px'; | |
| 587 } | 627 } |
| 588 t.style.top = row * h + 'px'; | 628 top = row * h; |
| 589 | 629 |
| 590 if (shownSections & Section.LIST) { | 630 if (shownSections & Section.LIST) { |
| 591 t.style.width = w - 2 * marginWidth + 'px'; | 631 width = w - 2 * marginWidth; |
| 592 } else { | |
| 593 t.style.width = ''; | |
| 594 } | 632 } |
| 633 |
| 634 rects[i] = {left: left, top: top, width: width}; |
| 595 } | 635 } |
| 596 } | 636 } |
| 597 | 637 |
| 598 afterTransition(function() { | 638 this.layoutCache_ = { |
| 599 // Only set overflow to visible if the element is shown. | 639 opacity: opacity, |
| 600 if (opacity) { | 640 sumHeight: sumHeight, |
| 601 mostVisitedElement.style.overflow = ''; | 641 rects: rects |
| 602 } | 642 } |
| 603 }); | |
| 604 | 643 |
| 605 this.dirty_ = false; | 644 this.calculationsDirty_ = false; |
| 645 }, |
| 606 | 646 |
| 607 logEvent('mostVisited.layout: ' + (Date.now() - d0)); | 647 getRectByIndex: function(index) { |
| 648 this.calculateLayout_(); |
| 649 return this.layoutCache_.rects[index] |
| 608 } | 650 } |
| 609 }; | 651 }; |
| 610 | 652 |
| 611 function recentChangedSize(large) { | 653 function recentChangedSize(large) { |
| 612 if (large) { | 654 if (large) { |
| 613 addClass($('recent-activities'), 'large'); | 655 addClass($('recent-activities'), 'large'); |
| 614 } else { | 656 } else { |
| 615 removeClass($('recent-activities'), 'large'); | 657 removeClass($('recent-activities'), 'large'); |
| 616 } | 658 } |
| 617 | 659 |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1149 el.title = el.xtitle; | 1191 el.title = el.xtitle; |
| 1150 } else { | 1192 } else { |
| 1151 el.title = ''; | 1193 el.title = ''; |
| 1152 } | 1194 } |
| 1153 } | 1195 } |
| 1154 }); | 1196 }); |
| 1155 | 1197 |
| 1156 // DnD | 1198 // DnD |
| 1157 | 1199 |
| 1158 var dnd = { | 1200 var dnd = { |
| 1159 currentOverItem: null, | 1201 currentOverItem_: null, |
| 1202 get currentOverItem() { |
| 1203 return this.currentOverItem_; |
| 1204 }, |
| 1205 set currentOverItem(item) { |
| 1206 var style; |
| 1207 if (item != this.currentOverItem_) { |
| 1208 if (this.currentOverItem_) { |
| 1209 style = this.currentOverItem_.firstElementChild.style; |
| 1210 style.left = style.top = ''; |
| 1211 } |
| 1212 this.currentOverItem_ = item; |
| 1213 |
| 1214 if (item) { |
| 1215 // Make the drag over item move 15px towards the source. The movement is |
| 1216 // done by only moving the edit-mode-border (as in the mocks) and it is |
| 1217 // done with relative positioning so that the movement does not change |
| 1218 // the drop target. |
| 1219 var dragIndex = mostVisited.getThumbnailIndex(this.dragItem); |
| 1220 var overIndex = mostVisited.getThumbnailIndex(item); |
| 1221 if (dragIndex == -1 || overIndex == -1) { |
| 1222 return; |
| 1223 } |
| 1224 |
| 1225 var dragRect = mostVisited.getRectByIndex(dragIndex); |
| 1226 var overRect = mostVisited.getRectByIndex(overIndex); |
| 1227 |
| 1228 var x = dragRect.left - overRect.left; |
| 1229 var y = dragRect.top - overRect.top; |
| 1230 var z = Math.sqrt(x * x + y * y); |
| 1231 var z2 = 15; |
| 1232 var x2 = x * z2 / z; |
| 1233 var y2 = y * z2 / z; |
| 1234 |
| 1235 style = this.currentOverItem_.firstElementChild.style; |
| 1236 style.left = x2 + 'px'; |
| 1237 style.top = y2 + 'px'; |
| 1238 } |
| 1239 } |
| 1240 }, |
| 1160 dragItem: null, | 1241 dragItem: null, |
| 1161 startX: 0, | 1242 startX: 0, |
| 1162 startY: 0, | 1243 startY: 0, |
| 1163 startScreenX: 0, | 1244 startScreenX: 0, |
| 1164 startScreenY: 0, | 1245 startScreenY: 0, |
| 1165 dragEndTimer: null, | 1246 dragEndTimer: null, |
| 1166 | 1247 |
| 1167 handleDragStart: function(e) { | 1248 handleDragStart: function(e) { |
| 1168 var thumbnail = mostVisited.getItem(e.target); | 1249 var thumbnail = mostVisited.getItem(e.target); |
| 1169 if (thumbnail) { | 1250 if (thumbnail) { |
| 1170 // Don't set data since HTML5 does not allow setting the name for | 1251 // Don't set data since HTML5 does not allow setting the name for |
| 1171 // url-list. Instead, we just rely on the dragging of link behavior. | 1252 // url-list. Instead, we just rely on the dragging of link behavior. |
| 1172 this.dragItem = thumbnail; | 1253 this.dragItem = thumbnail; |
| 1173 addClass(this.dragItem, 'dragging'); | 1254 addClass(this.dragItem, 'dragging'); |
| 1174 this.dragItem.style.zIndex = 2; | 1255 this.dragItem.style.zIndex = 2; |
| 1175 } | 1256 } |
| 1176 }, | 1257 }, |
| 1177 | 1258 |
| 1178 handleDragEnter: function(e) { | 1259 handleDragEnter: function(e) { |
| 1179 this.currentOverItem = mostVisited.getItem(e.target); | |
| 1180 if (this.canDropOnElement(this.currentOverItem)) { | 1260 if (this.canDropOnElement(this.currentOverItem)) { |
| 1181 e.preventDefault(); | 1261 e.preventDefault(); |
| 1182 } | 1262 } |
| 1183 }, | 1263 }, |
| 1184 | 1264 |
| 1185 handleDragOver: function(e) { | 1265 handleDragOver: function(e) { |
| 1186 var item = mostVisited.getItem(e.target); | 1266 var item = mostVisited.getItem(e.target); |
| 1267 this.currentOverItem = item; |
| 1187 if (this.canDropOnElement(item)) { | 1268 if (this.canDropOnElement(item)) { |
| 1188 e.preventDefault(); | 1269 e.preventDefault(); |
| 1189 } | 1270 } |
| 1190 }, | 1271 }, |
| 1191 | 1272 |
| 1192 handleDragLeave: function(e) { | 1273 handleDragLeave: function(e) { |
| 1193 var item = mostVisited.getItem(e.target); | 1274 var item = mostVisited.getItem(e.target); |
| 1194 if (item) { | 1275 if (item) { |
| 1195 e.preventDefault(); | 1276 e.preventDefault(); |
| 1196 } | 1277 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1277 el.addEventListener('dragover', bind(this.handleDragOver, this)); | 1358 el.addEventListener('dragover', bind(this.handleDragOver, this)); |
| 1278 el.addEventListener('dragleave', bind(this.handleDragLeave, this)); | 1359 el.addEventListener('dragleave', bind(this.handleDragLeave, this)); |
| 1279 el.addEventListener('drop', bind(this.handleDrop, this)); | 1360 el.addEventListener('drop', bind(this.handleDrop, this)); |
| 1280 el.addEventListener('dragend', bind(this.handleDragEnd, this)); | 1361 el.addEventListener('dragend', bind(this.handleDragEnd, this)); |
| 1281 el.addEventListener('drag', bind(this.handleDrag, this)); | 1362 el.addEventListener('drag', bind(this.handleDrag, this)); |
| 1282 el.addEventListener('mousedown', bind(this.handleMouseDown, this)); | 1363 el.addEventListener('mousedown', bind(this.handleMouseDown, this)); |
| 1283 } | 1364 } |
| 1284 }; | 1365 }; |
| 1285 | 1366 |
| 1286 dnd.init(); | 1367 dnd.init(); |
| OLD | NEW |