| OLD | NEW |
| 1 | |
| 2 // Helpers | |
| 3 | |
| 4 function findAncestorByClass(el, className) { | |
| 5 return findAncestor(el, function(el) { | |
| 6 return hasClass(el, className); | |
| 7 }); | |
| 8 } | |
| 9 | |
| 10 /** | |
| 11 * Return the first ancestor for which the {@code predicate} returns true. | |
| 12 * @param {Node} node The node to check. | |
| 13 * @param {function(Node) : boolean} predicate The function that tests the | |
| 14 * nodes. | |
| 15 * @return {Node} The found ancestor or null if not found. | |
| 16 */ | |
| 17 function findAncestor(node, predicate) { | |
| 18 var last = false; | |
| 19 while (node != null && !(last = predicate(node))) { | |
| 20 node = node.parentNode; | |
| 21 } | |
| 22 return last ? node : null; | |
| 23 } | |
| 24 | |
| 25 // WebKit does not have Node.prototype.swapNode | |
| 26 // https://bugs.webkit.org/show_bug.cgi?id=26525 | |
| 27 function swapDomNodes(a, b) { | |
| 28 var afterA = a.nextSibling; | |
| 29 if (afterA == b) { | |
| 30 swapDomNodes(b, a); | |
| 31 return; | |
| 32 } | |
| 33 var aParent = a.parentNode; | |
| 34 b.parentNode.replaceChild(a, b); | |
| 35 aParent.insertBefore(b, afterA); | |
| 36 } | |
| 37 | |
| 38 function bind(fn, selfObj, var_args) { | |
| 39 var boundArgs = Array.prototype.slice.call(arguments, 2); | |
| 40 return function() { | |
| 41 var args = Array.prototype.slice.call(arguments); | |
| 42 args.unshift.apply(args, boundArgs); | |
| 43 return fn.apply(selfObj, args); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 const IS_MAC = /$Mac/.test(navigator.platform); | |
| 48 | |
| 49 var loading = true; | |
| 50 var mostVisitedData = []; | |
| 51 var gotMostVisited = false; | |
| 52 | |
| 53 function mostVisitedPages(data, firstRun) { | |
| 54 logEvent('received most visited pages'); | |
| 55 | |
| 56 // We append the class name with the "filler" so that we can style fillers | |
| 57 // differently. | |
| 58 var maxItems = 8; | |
| 59 data.length = Math.min(maxItems, data.length); | |
| 60 var len = data.length; | |
| 61 for (var i = len; i < maxItems; i++) { | |
| 62 data[i] = {filler: true}; | |
| 63 } | |
| 64 | |
| 65 mostVisitedData = data; | |
| 66 renderMostVisited(data); | |
| 67 | |
| 68 gotMostVisited = true; | |
| 69 onDataLoaded(); | |
| 70 | |
| 71 // Only show the first run notification if first run. | |
| 72 if (firstRun) { | |
| 73 showFirstRunNotification(); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 function getAppsCallback(data) { | |
| 78 var appsSection = $('apps-section'); | |
| 79 appsSection.innerHTML = ''; | |
| 80 appsSection.style.display = data.length ? 'block' : ''; | |
| 81 | |
| 82 data.forEach(function(app) { | |
| 83 appsSection.appendChild(apps.createElement(app)); | |
| 84 }); | |
| 85 } | |
| 86 | |
| 87 var apps = { | |
| 88 /** | |
| 89 * @this {!HTMLAnchorElement} | |
| 90 */ | |
| 91 handleClick_: function() { | |
| 92 chrome.send('launchApp', [this.id]); | |
| 93 return false; | |
| 94 }, | |
| 95 | |
| 96 createElement: function(app) { | |
| 97 var a = document.createElement('a'); | |
| 98 a.xtitle = a.textContent = app['name']; | |
| 99 a.href = app['launch_url']; | |
| 100 a.id = app['id']; | |
| 101 a.onclick = apps.handleClick_; | |
| 102 a.style.backgroundImage = url(app['icon']); | |
| 103 return a; | |
| 104 } | |
| 105 }; | |
| 106 | |
| 107 var tipCache = {}; | |
| 108 | |
| 109 function tips(data) { | |
| 110 logEvent('received tips'); | |
| 111 tipCache = data; | |
| 112 renderTip(); | |
| 113 } | |
| 114 | |
| 115 function createTip(data) { | |
| 116 if (data.length) { | |
| 117 if (data[0].set_homepage_tip) { | |
| 118 var homepageButton = document.createElement('button'); | |
| 119 homepageButton.className = 'link'; | |
| 120 homepageButton.textContent = data[0].set_homepage_tip; | |
| 121 homepageButton.addEventListener('click', setAsHomePageLinkClicked); | |
| 122 return homepageButton; | |
| 123 } else { | |
| 124 try { | |
| 125 return parseHtmlSubset(data[0].tip_html_text); | |
| 126 } catch (parseErr) { | |
| 127 console.error('Error parsing tips: ' + parseErr.message); | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 // Return an empty DF in case of failure. | |
| 132 return document.createDocumentFragment(); | |
| 133 } | |
| 134 | |
| 135 function clearTipLine() { | |
| 136 var tipElement = $('tip-line'); | |
| 137 // There should always be only one tip. | |
| 138 tipElement.textContent = ''; | |
| 139 tipElement.removeEventListener('click', setAsHomePageLinkClicked); | |
| 140 } | |
| 141 | |
| 142 function renderTip() { | |
| 143 clearTipLine(); | |
| 144 var tipElement = $('tip-line'); | |
| 145 tipElement.appendChild(createTip(tipCache)); | |
| 146 fixLinkUnderlines(tipElement); | |
| 147 } | |
| 148 | |
| 149 function recentlyClosedTabs(data) { | |
| 150 logEvent('received recently closed tabs'); | |
| 151 // We need to store the recent items so we can update the layout on a resize. | |
| 152 recentItems = data; | |
| 153 renderRecentlyClosed(); | |
| 154 } | |
| 155 | |
| 156 var recentItems = []; | |
| 157 | |
| 158 function renderRecentlyClosed() { | |
| 159 // We remove all items but the header and the nav | |
| 160 var recentlyClosedElement = $('recently-closed'); | |
| 161 var headerEl = recentlyClosedElement.firstElementChild; | |
| 162 var navEl = recentlyClosedElement.lastElementChild.lastElementChild; | |
| 163 var parentEl = navEl.parentNode; | |
| 164 | |
| 165 for (var el = navEl.previousElementSibling; el; | |
| 166 el = navEl.previousElementSibling) { | |
| 167 parentEl.removeChild(el); | |
| 168 } | |
| 169 | |
| 170 // Create new items | |
| 171 recentItems.forEach(function(item) { | |
| 172 var el = createRecentItem(item); | |
| 173 parentEl.insertBefore(el, navEl); | |
| 174 }); | |
| 175 | |
| 176 layoutRecentlyClosed(); | |
| 177 } | |
| 178 | |
| 179 function createRecentItem(data) { | |
| 180 var isWindow = data.type == 'window'; | |
| 181 var el; | |
| 182 if (isWindow) { | |
| 183 el = document.createElement('span'); | |
| 184 el.className = 'item link window'; | |
| 185 el.tabItems = data.tabs; | |
| 186 el.tabIndex = 0; | |
| 187 el.textContent = formatTabsText(data.tabs.length); | |
| 188 } else { | |
| 189 el = document.createElement('a'); | |
| 190 el.className = 'item'; | |
| 191 el.href = data.url; | |
| 192 el.style.backgroundImage = url('chrome://favicon/' + data.url); | |
| 193 el.dir = data.direction; | |
| 194 el.textContent = data.title; | |
| 195 } | |
| 196 el.sessionId = data.sessionId; | |
| 197 el.xtitle = data.title; | |
| 198 var wrapperEl = document.createElement('span'); | |
| 199 wrapperEl.appendChild(el); | |
| 200 return wrapperEl; | |
| 201 } | |
| 202 | |
| 203 function onShownSections(mask) { | |
| 204 logEvent('received shown sections'); | |
| 205 if (mask != shownSections) { | |
| 206 var oldShownSections = shownSections; | |
| 207 shownSections = mask; | |
| 208 | |
| 209 // Only invalidate most visited if needed. | |
| 210 if ((mask & Section.THUMB) != (oldShownSections & Section.THUMB)) { | |
| 211 mostVisited.invalidate(); | |
| 212 } | |
| 213 | |
| 214 mostVisited.updateDisplayMode(); | |
| 215 renderRecentlyClosed(); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 function saveShownSections() { | |
| 220 chrome.send('setShownSections', [String(shownSections)]); | |
| 221 } | |
| 222 | |
| 223 function getThumbnailClassName(data) { | |
| 224 return 'thumbnail-container' + | |
| 225 (data.pinned ? ' pinned' : '') + | |
| 226 (data.filler ? ' filler' : ''); | |
| 227 } | |
| 228 | |
| 229 function url(s) { | |
| 230 // http://www.w3.org/TR/css3-values/#uris | |
| 231 // Parentheses, commas, whitespace characters, single quotes (') and double | |
| 232 // quotes (") appearing in a URI must be escaped with a backslash | |
| 233 var s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1'); | |
| 234 // WebKit has a bug when it comes to URLs that end with \ | |
| 235 // https://bugs.webkit.org/show_bug.cgi?id=28885 | |
| 236 if (/\\\\$/.test(s2)) { | |
| 237 // Add a space to work around the WebKit bug. | |
| 238 s2 += ' '; | |
| 239 } | |
| 240 return 'url("' + s2 + '")'; | |
| 241 } | |
| 242 | |
| 243 function renderMostVisited(data) { | |
| 244 var parent = $('most-visited'); | |
| 245 var children = parent.children; | |
| 246 for (var i = 0; i < data.length; i++) { | |
| 247 var d = data[i]; | |
| 248 var t = children[i]; | |
| 249 | |
| 250 // If we have a filler continue | |
| 251 var oldClassName = t.className; | |
| 252 var newClassName = getThumbnailClassName(d); | |
| 253 if (oldClassName != newClassName) { | |
| 254 t.className = newClassName; | |
| 255 } | |
| 256 | |
| 257 // No need to continue if this is a filler. | |
| 258 if (newClassName == 'thumbnail-container filler') { | |
| 259 // Make sure the user cannot tab to the filler. | |
| 260 t.tabIndex = -1; | |
| 261 continue; | |
| 262 } | |
| 263 // Allow focus. | |
| 264 t.tabIndex = 1; | |
| 265 | |
| 266 t.href = d.url; | |
| 267 t.querySelector('.pin').title = localStrings.getString(d.pinned ? | |
| 268 'unpinthumbnailtooltip' : 'pinthumbnailtooltip'); | |
| 269 t.querySelector('.remove').title = | |
| 270 localStrings.getString('removethumbnailtooltip'); | |
| 271 | |
| 272 // There was some concern that a malformed malicious URL could cause an XSS | |
| 273 // attack but setting style.backgroundImage = 'url(javascript:...)' does | |
| 274 // not execute the JavaScript in WebKit. | |
| 275 | |
| 276 var thumbnailUrl = d.thumbnailUrl || 'chrome://thumb/' + d.url; | |
| 277 t.querySelector('.thumbnail-wrapper').style.backgroundImage = | |
| 278 url(thumbnailUrl); | |
| 279 var titleDiv = t.querySelector('.title > div'); | |
| 280 titleDiv.xtitle = titleDiv.textContent = d.title; | |
| 281 var faviconUrl = d.faviconUrl || 'chrome://favicon/' + d.url; | |
| 282 titleDiv.style.backgroundImage = url(faviconUrl); | |
| 283 titleDiv.dir = d.direction; | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 /** | |
| 288 * Calls chrome.send with a callback and restores the original afterwards. | |
| 289 */ | |
| 290 function chromeSend(name, params, callbackName, callback) { | |
| 291 var old = global[callbackName]; | |
| 292 global[callbackName] = function() { | |
| 293 // restore | |
| 294 global[callbackName] = old; | |
| 295 | |
| 296 var args = Array.prototype.slice.call(arguments); | |
| 297 return callback.apply(global, args); | |
| 298 }; | |
| 299 chrome.send(name, params); | |
| 300 } | |
| 301 | |
| 302 var LayoutMode = { | |
| 303 SMALL: 1, | |
| 304 NORMAL: 2 | |
| 305 }; | |
| 306 | |
| 307 var layoutMode = useSmallGrid() ? LayoutMode.SMALL : LayoutMode.NORMAL; | |
| 308 | |
| 309 function handleWindowResize() { | |
| 310 if (window.innerWidth < 10) { | |
| 311 // We're probably a background tab, so don't do anything. | |
| 312 return; | |
| 313 } | |
| 314 | |
| 315 var oldLayoutMode = layoutMode; | |
| 316 layoutMode = useSmallGrid() ? LayoutMode.SMALL : LayoutMode.NORMAL | |
| 317 | |
| 318 if (layoutMode != oldLayoutMode){ | |
| 319 mostVisited.invalidate(); | |
| 320 mostVisited.layout(); | |
| 321 renderRecentlyClosed(); | |
| 322 } | |
| 323 } | |
| 324 | |
| 325 function showSection(section) { | |
| 326 if (!(section & shownSections)) { | |
| 327 shownSections |= section; | |
| 328 | |
| 329 switch (section) { | |
| 330 case Section.THUMB: | |
| 331 mostVisited.invalidate(); | |
| 332 mostVisited.updateDisplayMode(); | |
| 333 mostVisited.layout(); | |
| 334 break; | |
| 335 case Section.RECENT: | |
| 336 renderRecentlyClosed(); | |
| 337 break; | |
| 338 case Section.TIPS: | |
| 339 removeClass($('tip-line'), 'hidden'); | |
| 340 break; | |
| 341 } | |
| 342 } | |
| 343 } | |
| 344 | |
| 345 function hideSection(section) { | |
| 346 if (section & shownSections) { | |
| 347 shownSections &= ~section; | |
| 348 | |
| 349 switch (section) { | |
| 350 case Section.THUMB: | |
| 351 mostVisited.invalidate(); | |
| 352 mostVisited.updateDisplayMode(); | |
| 353 mostVisited.layout(); | |
| 354 break; | |
| 355 case Section.RECENT: | |
| 356 renderRecentlyClosed(); | |
| 357 break; | |
| 358 case Section.TIPS: | |
| 359 addClass($('tip-line'), 'hidden'); | |
| 360 break; | |
| 361 } | |
| 362 } | |
| 363 } | |
| 364 | |
| 365 var mostVisited = { | |
| 366 addPinnedUrl_: function(data, index) { | |
| 367 chrome.send('addPinnedURL', [data.url, data.title, data.faviconUrl || '', | |
| 368 data.thumbnailUrl || '', String(index)]); | |
| 369 }, | |
| 370 getItem: function(el) { | |
| 371 return findAncestorByClass(el, 'thumbnail-container'); | |
| 372 }, | |
| 373 | |
| 374 getHref: function(el) { | |
| 375 return el.href; | |
| 376 }, | |
| 377 | |
| 378 togglePinned: function(el) { | |
| 379 var index = this.getThumbnailIndex(el); | |
| 380 var data = mostVisitedData[index]; | |
| 381 data.pinned = !data.pinned; | |
| 382 if (data.pinned) { | |
| 383 this.addPinnedUrl_(data, index); | |
| 384 } else { | |
| 385 chrome.send('removePinnedURL', [data.url]); | |
| 386 } | |
| 387 this.updatePinnedDom_(el, data.pinned); | |
| 388 }, | |
| 389 | |
| 390 updatePinnedDom_: function(el, pinned) { | |
| 391 el.querySelector('.pin').title = localStrings.getString(pinned ? | |
| 392 'unpinthumbnailtooltip' : 'pinthumbnailtooltip'); | |
| 393 if (pinned) { | |
| 394 addClass(el, 'pinned'); | |
| 395 } else { | |
| 396 removeClass(el, 'pinned'); | |
| 397 } | |
| 398 }, | |
| 399 | |
| 400 getThumbnailIndex: function(el) { | |
| 401 var nodes = el.parentNode.querySelectorAll('.thumbnail-container'); | |
| 402 return Array.prototype.indexOf.call(nodes, el); | |
| 403 }, | |
| 404 | |
| 405 swapPosition: function(source, destination) { | |
| 406 var nodes = source.parentNode.querySelectorAll('.thumbnail-container'); | |
| 407 var sourceIndex = this.getThumbnailIndex(source); | |
| 408 var destinationIndex = this.getThumbnailIndex(destination); | |
| 409 swapDomNodes(source, destination); | |
| 410 | |
| 411 var sourceData = mostVisitedData[sourceIndex]; | |
| 412 this.addPinnedUrl_(sourceData, destinationIndex); | |
| 413 sourceData.pinned = true; | |
| 414 this.updatePinnedDom_(source, true); | |
| 415 | |
| 416 var destinationData = mostVisitedData[destinationIndex]; | |
| 417 // Only update the destination if it was pinned before. | |
| 418 if (destinationData.pinned) { | |
| 419 this.addPinnedUrl_(destinationData, sourceIndex); | |
| 420 } | |
| 421 mostVisitedData[destinationIndex] = sourceData; | |
| 422 mostVisitedData[sourceIndex] = destinationData; | |
| 423 }, | |
| 424 | |
| 425 blacklist: function(el) { | |
| 426 var self = this; | |
| 427 var url = this.getHref(el); | |
| 428 chrome.send('blacklistURLFromMostVisited', [url]); | |
| 429 | |
| 430 addClass(el, 'hide'); | |
| 431 | |
| 432 // Find the old item. | |
| 433 var oldUrls = {}; | |
| 434 var oldIndex = -1; | |
| 435 var oldItem; | |
| 436 for (var i = 0; i < mostVisitedData.length; i++) { | |
| 437 if (mostVisitedData[i].url == url) { | |
| 438 oldItem = mostVisitedData[i]; | |
| 439 oldIndex = i; | |
| 440 } | |
| 441 oldUrls[mostVisitedData[i].url] = true; | |
| 442 } | |
| 443 | |
| 444 // Send 'getMostVisitedPages' with a callback since we want to find the new | |
| 445 // page and add that in the place of the removed page. | |
| 446 chromeSend('getMostVisited', [], 'mostVisitedPages', function(data) { | |
| 447 // Find new item. | |
| 448 var newItem; | |
| 449 for (var i = 0; i < data.length; i++) { | |
| 450 if (!(data[i].url in oldUrls)) { | |
| 451 newItem = data[i]; | |
| 452 break; | |
| 453 } | |
| 454 } | |
| 455 | |
| 456 if (!newItem) { | |
| 457 // If no other page is available to replace the blacklisted item, | |
| 458 // we need to reorder items s.t. all filler items are in the rightmost | |
| 459 // indices. | |
| 460 mostVisitedPages(data); | |
| 461 | |
| 462 // Replace old item with new item in the mostVisitedData array. | |
| 463 } else if (oldIndex != -1) { | |
| 464 mostVisitedData.splice(oldIndex, 1, newItem); | |
| 465 mostVisitedPages(mostVisitedData); | |
| 466 addClass(el, 'fade-in'); | |
| 467 } | |
| 468 | |
| 469 // We wrap the title in a <span class=blacklisted-title>. We pass an empty | |
| 470 // string to the notifier function and use DOM to insert the real string. | |
| 471 var actionText = localStrings.getString('undothumbnailremove'); | |
| 472 | |
| 473 // Show notification and add undo callback function. | |
| 474 var wasPinned = oldItem.pinned; | |
| 475 showNotification('', actionText, function() { | |
| 476 self.removeFromBlackList(url); | |
| 477 if (wasPinned) { | |
| 478 self.addPinnedUrl_(oldItem, oldIndex); | |
| 479 } | |
| 480 chrome.send('getMostVisited'); | |
| 481 }); | |
| 482 | |
| 483 // Now change the DOM. | |
| 484 var removeText = localStrings.getString('thumbnailremovednotification'); | |
| 485 var notifySpan = document.querySelector('#notification > span'); | |
| 486 notifySpan.textContent = removeText; | |
| 487 | |
| 488 // Focus the undo link. | |
| 489 var undoLink = document.querySelector( | |
| 490 '#notification > .link > [tabindex]'); | |
| 491 undoLink.focus(); | |
| 492 }); | |
| 493 }, | |
| 494 | |
| 495 removeFromBlackList: function(url) { | |
| 496 chrome.send('removeURLsFromMostVisitedBlacklist', [url]); | |
| 497 }, | |
| 498 | |
| 499 clearAllBlacklisted: function() { | |
| 500 chrome.send('clearMostVisitedURLsBlacklist', []); | |
| 501 hideNotification(); | |
| 502 }, | |
| 503 | |
| 504 updateDisplayMode: function() { | |
| 505 if (!this.dirty_) { | |
| 506 return; | |
| 507 } | |
| 508 updateSimpleSection('most-visited-section', Section.THUMB); | |
| 509 }, | |
| 510 | |
| 511 dirty_: false, | |
| 512 | |
| 513 invalidate: function() { | |
| 514 this.dirty_ = true; | |
| 515 }, | |
| 516 | |
| 517 layout: function() { | |
| 518 if (!this.dirty_) { | |
| 519 return; | |
| 520 } | |
| 521 var d0 = Date.now(); | |
| 522 | |
| 523 var mostVisitedElement = $('most-visited'); | |
| 524 var thumbnails = mostVisitedElement.children; | |
| 525 var hidden = !(shownSections & Section.THUMB); | |
| 526 | |
| 527 | |
| 528 // We set overflow to hidden so that the most visited element does not | |
| 529 // "leak" when we hide and show it. | |
| 530 if (hidden) { | |
| 531 mostVisitedElement.style.overflow = 'hidden'; | |
| 532 } | |
| 533 | |
| 534 applyMostVisitedRects(); | |
| 535 | |
| 536 // Only set overflow to visible if the element is shown. | |
| 537 if (!hidden) { | |
| 538 afterTransition(function() { | |
| 539 mostVisitedElement.style.overflow = ''; | |
| 540 }); | |
| 541 } | |
| 542 | |
| 543 this.dirty_ = false; | |
| 544 | |
| 545 logEvent('mostVisited.layout: ' + (Date.now() - d0)); | |
| 546 }, | |
| 547 | |
| 548 getRectByIndex: function(index) { | |
| 549 return getMostVisitedLayoutRects()[index]; | |
| 550 } | |
| 551 }; | |
| 552 | |
| 553 // Recently closed | |
| 554 | |
| 555 function layoutRecentlyClosed() { | |
| 556 var recentShown = shownSections & Section.RECENT; | |
| 557 updateSimpleSection('recently-closed', Section.RECENT); | |
| 558 | |
| 559 if (recentShown) { | |
| 560 var recentElement = $('recently-closed'); | |
| 561 var style = recentElement.style; | |
| 562 // We cannot use clientWidth here since the width has a transition. | |
| 563 var spacing = 20; | |
| 564 var headerEl = recentElement.firstElementChild; | |
| 565 var navEl = recentElement.lastElementChild.lastElementChild; | |
| 566 var navWidth = navEl.offsetWidth; | |
| 567 // Subtract 10 for the padding | |
| 568 var availWidth = (useSmallGrid() ? 690 : 918) - navWidth - 10; | |
| 569 | |
| 570 // Now go backwards and hide as many elements as needed. | |
| 571 var elementsToHide = []; | |
| 572 for (var el = navEl.previousElementSibling; el; | |
| 573 el = el.previousElementSibling) { | |
| 574 if (el.offsetLeft + el.offsetWidth + spacing > availWidth) { | |
| 575 elementsToHide.push(el); | |
| 576 } | |
| 577 } | |
| 578 | |
| 579 elementsToHide.forEach(function(el) { | |
| 580 el.parentNode.removeChild(el); | |
| 581 }); | |
| 582 } | |
| 583 } | |
| 584 | |
| 585 /** | |
| 586 * This function is called by the backend whenever the sync status section | |
| 587 * needs to be updated to reflect recent sync state changes. The backend passes | |
| 588 * the new status information in the newMessage parameter. The state includes | |
| 589 * the following: | |
| 590 * | |
| 591 * syncsectionisvisible: true if the sync section needs to show up on the new | |
| 592 * tab page and false otherwise. | |
| 593 * title: the header for the sync status section. | |
| 594 * msg: the actual message (e.g. "Synced to foo@gmail.com"). | |
| 595 * linkisvisible: true if the link element should be visible within the sync | |
| 596 * section and false otherwise. | |
| 597 * linktext: the text to display as the link in the sync status (only used if | |
| 598 * linkisvisible is true). | |
| 599 * linkurlisset: true if an URL should be set as the href for the link and false | |
| 600 * otherwise. If this field is false, then clicking on the link | |
| 601 * will result in sending a message to the backend (see | |
| 602 * 'SyncLinkClicked'). | |
| 603 * linkurl: the URL to use as the element's href (only used if linkurlisset is | |
| 604 * true). | |
| 605 */ | |
| 606 function syncMessageChanged(newMessage) { | |
| 607 var syncStatusElement = $('sync-status'); | |
| 608 var style = syncStatusElement.style; | |
| 609 | |
| 610 // Hide the section if the message is emtpy. | |
| 611 if (!newMessage['syncsectionisvisible']) { | |
| 612 style.display = 'none'; | |
| 613 return; | |
| 614 } | |
| 615 style.display = 'block'; | |
| 616 | |
| 617 // Set the sync section background color based on the state. | |
| 618 if (newMessage.msgtype == 'error') { | |
| 619 style.backgroundColor = 'tomato'; | |
| 620 } else { | |
| 621 style.backgroundColor = ''; | |
| 622 } | |
| 623 | |
| 624 // Set the text for the header and sync message. | |
| 625 var titleElement = syncStatusElement.firstElementChild; | |
| 626 titleElement.textContent = newMessage.title; | |
| 627 var messageElement = titleElement.nextElementSibling; | |
| 628 messageElement.textContent = newMessage.msg; | |
| 629 | |
| 630 // Remove what comes after the message | |
| 631 while (messageElement.nextSibling) { | |
| 632 syncStatusElement.removeChild(messageElement.nextSibling); | |
| 633 } | |
| 634 | |
| 635 if (newMessage.linkisvisible) { | |
| 636 var el; | |
| 637 if (newMessage.linkurlisset) { | |
| 638 // Use a link | |
| 639 el = document.createElement('a'); | |
| 640 el.href = newMessage.linkurl; | |
| 641 } else { | |
| 642 el = document.createElement('button'); | |
| 643 el.className = 'link'; | |
| 644 el.addEventListener('click', syncSectionLinkClicked); | |
| 645 } | |
| 646 el.textContent = newMessage.linktext; | |
| 647 syncStatusElement.appendChild(el); | |
| 648 fixLinkUnderline(el); | |
| 649 } | |
| 650 } | |
| 651 | |
| 652 /** | |
| 653 * Invoked when the link in the sync status section is clicked. | |
| 654 */ | |
| 655 function syncSectionLinkClicked(e) { | |
| 656 chrome.send('SyncLinkClicked'); | |
| 657 e.preventDefault(); | |
| 658 } | |
| 659 | |
| 660 /** | |
| 661 * Invoked when link to start sync in the promo message is clicked, and Chrome | |
| 662 * has already been synced to an account. | |
| 663 */ | |
| 664 function syncAlreadyEnabled(message) { | |
| 665 showNotification(message.syncEnabledMessage, | |
| 666 localStrings.getString('close')); | |
| 667 } | |
| 668 | |
| 669 /** | |
| 670 * Returns the text used for a recently closed window. | |
| 671 * @param {number} numTabs Number of tabs in the window. | |
| 672 * @return {string} The text to use. | |
| 673 */ | |
| 674 function formatTabsText(numTabs) { | |
| 675 if (numTabs == 1) | |
| 676 return localStrings.getString('closedwindowsingle'); | |
| 677 return localStrings.getStringF('closedwindowmultiple', numTabs); | |
| 678 } | |
| 679 | |
| 680 /** | |
| 681 * We need both most visited and the shown sections to be considered loaded. | |
| 682 * @return {boolean} | |
| 683 */ | |
| 684 function onDataLoaded() { | |
| 685 if (gotMostVisited) { | |
| 686 mostVisited.layout(); | |
| 687 loading = false; | |
| 688 // Remove class name in a timeout so that changes done in this JS thread are | |
| 689 // not animated. | |
| 690 window.setTimeout(function() { | |
| 691 ensureSmallGridCorrect(); | |
| 692 removeClass(document.body, 'loading'); | |
| 693 }, 1); | |
| 694 } | |
| 695 } | |
| 696 | |
| 697 // Theme related | |
| 698 | |
| 699 function themeChanged() { | |
| 700 $('themecss').href = 'chrome://theme/css/newtab.css?' + Date.now(); | |
| 701 updateAttribution(); | |
| 702 } | |
| 703 | |
| 704 function updateAttribution() { | |
| 705 $('attribution-img').src = 'chrome://theme/theme_ntp_attribution?' + | |
| 706 Date.now(); | |
| 707 } | |
| 708 | |
| 709 function bookmarkBarAttached() { | |
| 710 document.documentElement.setAttribute('bookmarkbarattached', 'true'); | |
| 711 } | |
| 712 | |
| 713 function bookmarkBarDetached() { | |
| 714 document.documentElement.setAttribute('bookmarkbarattached', 'false'); | |
| 715 } | |
| 716 | |
| 717 function viewLog() { | |
| 718 var lines = []; | |
| 719 var start = log[0][1]; | |
| 720 | |
| 721 for (var i = 0; i < log.length; i++) { | |
| 722 lines.push((log[i][1] - start) + ': ' + log[i][0]); | |
| 723 } | |
| 724 | |
| 725 console.log(lines.join('\n')); | |
| 726 } | |
| 727 | |
| 728 // Updates the visibility of the menu items. | |
| 729 function updateOptionMenu() { | |
| 730 var menuItems = $('option-menu').children; | |
| 731 for (var i = 0; i < menuItems.length; i++) { | |
| 732 var item = menuItems[i]; | |
| 733 var command = item.getAttribute('command'); | |
| 734 if (command == 'show' || command == 'hide') { | |
| 735 var section = Section[item.getAttribute('section')]; | |
| 736 var visible = shownSections & section; | |
| 737 item.setAttribute('command', visible ? 'hide' : 'show'); | |
| 738 } | |
| 739 } | |
| 740 } | |
| 741 | |
| 742 // We apply the size class here so that we don't trigger layout animations | |
| 743 // onload. | |
| 744 | |
| 745 handleWindowResize(); | |
| 746 | |
| 747 var localStrings = new LocalStrings(); | |
| 748 | |
| 749 /////////////////////////////////////////////////////////////////////////////// | |
| 750 // Things we know are not needed at startup go below here | |
| 751 | |
| 752 function afterTransition(f) { | |
| 753 if (loading) { | |
| 754 // Make sure we do not use a timer during load since it slows down the UI. | |
| 755 f(); | |
| 756 } else { | |
| 757 // The duration of all transitions are .15s | |
| 758 window.setTimeout(f, 150); | |
| 759 } | |
| 760 } | |
| 761 | |
| 762 // Notification | |
| 763 | |
| 764 | |
| 765 var notificationTimeout; | |
| 766 | |
| 767 function showNotification(text, actionText, opt_f, opt_delay) { | |
| 768 var notificationElement = $('notification'); | |
| 769 var f = opt_f || function() {}; | |
| 770 var delay = opt_delay || 10000; | |
| 771 | |
| 772 function show() { | |
| 773 window.clearTimeout(notificationTimeout); | |
| 774 addClass(notificationElement, 'show'); | |
| 775 addClass(document.body, 'notification-shown'); | |
| 776 } | |
| 777 | |
| 778 function delayedHide() { | |
| 779 notificationTimeout = window.setTimeout(hideNotification, delay); | |
| 780 } | |
| 781 | |
| 782 function doAction() { | |
| 783 f(); | |
| 784 hideNotification(); | |
| 785 } | |
| 786 | |
| 787 // Remove any possible first-run trails. | |
| 788 removeClass(notification, 'first-run'); | |
| 789 | |
| 790 var actionLink = notificationElement.querySelector('.link-color'); | |
| 791 notificationElement.firstElementChild.textContent = text; | |
| 792 actionLink.textContent = actionText; | |
| 793 | |
| 794 actionLink.onclick = doAction; | |
| 795 actionLink.onkeydown = handleIfEnterKey(doAction); | |
| 796 notificationElement.onmouseover = show; | |
| 797 notificationElement.onmouseout = delayedHide; | |
| 798 actionLink.onfocus = show; | |
| 799 actionLink.onblur = delayedHide; | |
| 800 // Enable tabbing to the link now that it is shown. | |
| 801 actionLink.tabIndex = 0; | |
| 802 | |
| 803 show(); | |
| 804 delayedHide(); | |
| 805 } | |
| 806 | |
| 807 /** | |
| 808 * Hides the notifier. | |
| 809 */ | |
| 810 function hideNotification() { | |
| 811 var notificationElement = $('notification'); | |
| 812 removeClass(notificationElement, 'show'); | |
| 813 removeClass(document.body, 'notification-shown'); | |
| 814 var actionLink = notificationElement.querySelector('.link-color'); | |
| 815 // Prevent tabbing to the hidden link. | |
| 816 actionLink.tabIndex = -1; | |
| 817 // Setting tabIndex to -1 only prevents future tabbing to it. If, however, the | |
| 818 // user switches window or a tab and then moves back to this tab the element | |
| 819 // may gain focus. We therefore make sure that we blur the element so that the | |
| 820 // element focus is not restored when coming back to this window. | |
| 821 actionLink.blur(); | |
| 822 } | |
| 823 | |
| 824 function showFirstRunNotification() { | |
| 825 showNotification(localStrings.getString('firstrunnotification'), | |
| 826 localStrings.getString('closefirstrunnotification'), | |
| 827 null, 30000); | |
| 828 var notificationElement = $('notification'); | |
| 829 addClass(notification, 'first-run'); | |
| 830 } | |
| 831 | |
| 832 | |
| 833 /** | |
| 834 * This handles the option menu. | |
| 835 * @param {Element} button The button element. | |
| 836 * @param {Element} menu The menu element. | |
| 837 * @constructor | |
| 838 */ | |
| 839 function OptionMenu(button, menu) { | |
| 840 this.button = button; | |
| 841 this.menu = menu; | |
| 842 this.button.onmousedown = bind(this.handleMouseDown, this); | |
| 843 this.button.onkeydown = bind(this.handleKeyDown, this); | |
| 844 this.boundHideMenu_ = bind(this.hide, this); | |
| 845 this.boundMaybeHide_ = bind(this.maybeHide_, this); | |
| 846 this.menu.onmouseover = bind(this.handleMouseOver, this); | |
| 847 this.menu.onmouseout = bind(this.handleMouseOut, this); | |
| 848 this.menu.onmouseup = bind(this.handleMouseUp, this); | |
| 849 } | |
| 850 | |
| 851 OptionMenu.prototype = { | |
| 852 show: function() { | |
| 853 updateOptionMenu(); | |
| 854 this.positionMenu_(); | |
| 855 this.menu.style.display = 'block'; | |
| 856 addClass(this.button, 'open'); | |
| 857 this.button.focus(); | |
| 858 | |
| 859 // Listen to document and window events so that we hide the menu when the | |
| 860 // user clicks outside the menu or tabs away or the whole window is blurred. | |
| 861 document.addEventListener('focus', this.boundMaybeHide_, true); | |
| 862 document.addEventListener('mousedown', this.boundMaybeHide_, true); | |
| 863 }, | |
| 864 | |
| 865 positionMenu_: function() { | |
| 866 this.menu.style.top = this.button.getBoundingClientRect().bottom + 'px'; | |
| 867 }, | |
| 868 | |
| 869 hide: function() { | |
| 870 this.menu.style.display = 'none'; | |
| 871 removeClass(this.button, 'open'); | |
| 872 this.setSelectedIndex(-1); | |
| 873 | |
| 874 document.removeEventListener('focus', this.boundMaybeHide_, true); | |
| 875 document.removeEventListener('mousedown', this.boundMaybeHide_, true); | |
| 876 }, | |
| 877 | |
| 878 isShown: function() { | |
| 879 return this.menu.style.display == 'block'; | |
| 880 }, | |
| 881 | |
| 882 /** | |
| 883 * Callback for document mousedown and focus. It checks if the user tried to | |
| 884 * navigate to a different element on the page and if so hides the menu. | |
| 885 * @param {Event} e The mouse or focus event. | |
| 886 * @private | |
| 887 */ | |
| 888 maybeHide_: function(e) { | |
| 889 if (!this.menu.contains(e.target) && !this.button.contains(e.target)) { | |
| 890 this.hide(); | |
| 891 } | |
| 892 }, | |
| 893 | |
| 894 handleMouseDown: function(e) { | |
| 895 if (this.isShown()) { | |
| 896 this.hide(); | |
| 897 } else { | |
| 898 this.show(); | |
| 899 } | |
| 900 }, | |
| 901 | |
| 902 handleMouseOver: function(e) { | |
| 903 var el = e.target; | |
| 904 if (!el.hasAttribute('command')) { | |
| 905 this.setSelectedIndex(-1); | |
| 906 } else { | |
| 907 var index = Array.prototype.indexOf.call(this.menu.children, el); | |
| 908 this.setSelectedIndex(index); | |
| 909 } | |
| 910 }, | |
| 911 | |
| 912 handleMouseOut: function(e) { | |
| 913 this.setSelectedIndex(-1); | |
| 914 }, | |
| 915 | |
| 916 handleMouseUp: function(e) { | |
| 917 var item = this.getSelectedItem(); | |
| 918 if (item) { | |
| 919 this.executeItem(item); | |
| 920 } | |
| 921 }, | |
| 922 | |
| 923 handleKeyDown: function(e) { | |
| 924 var item = this.getSelectedItem(); | |
| 925 | |
| 926 var self = this; | |
| 927 function selectNextVisible(m) { | |
| 928 var children = self.menu.children; | |
| 929 var len = children.length; | |
| 930 var i = self.selectedIndex_; | |
| 931 if (i == -1 && m == -1) { | |
| 932 // Edge case when we need to go the last item fisrt. | |
| 933 i = 0; | |
| 934 } | |
| 935 while (true) { | |
| 936 i = (i + m + len) % len; | |
| 937 item = children[i]; | |
| 938 if (item && item.hasAttribute('command') && | |
| 939 item.style.display != 'none') { | |
| 940 break; | |
| 941 } | |
| 942 } | |
| 943 if (item) { | |
| 944 self.setSelectedIndex(i); | |
| 945 } | |
| 946 } | |
| 947 | |
| 948 switch (e.keyIdentifier) { | |
| 949 case 'Down': | |
| 950 if (!this.isShown()) { | |
| 951 this.show(); | |
| 952 } | |
| 953 selectNextVisible(1); | |
| 954 e.preventDefault(); | |
| 955 break; | |
| 956 case 'Up': | |
| 957 if (!this.isShown()) { | |
| 958 this.show(); | |
| 959 } | |
| 960 selectNextVisible(-1); | |
| 961 e.preventDefault(); | |
| 962 break; | |
| 963 case 'Esc': | |
| 964 case 'U+001B': // Maybe this is remote desktop playing a prank? | |
| 965 this.hide(); | |
| 966 break; | |
| 967 case 'Enter': | |
| 968 case 'U+0020': // Space | |
| 969 if (this.isShown()) { | |
| 970 if (item) { | |
| 971 this.executeItem(item); | |
| 972 } else { | |
| 973 this.hide(); | |
| 974 } | |
| 975 } else { | |
| 976 this.show(); | |
| 977 } | |
| 978 e.preventDefault(); | |
| 979 break; | |
| 980 } | |
| 981 }, | |
| 982 | |
| 983 selectedIndex_: -1, | |
| 984 setSelectedIndex: function(i) { | |
| 985 if (i != this.selectedIndex_) { | |
| 986 var items = this.menu.children; | |
| 987 var oldItem = items[this.selectedIndex_]; | |
| 988 if (oldItem) { | |
| 989 oldItem.removeAttribute('selected'); | |
| 990 } | |
| 991 var newItem = items[i]; | |
| 992 if (newItem) { | |
| 993 newItem.setAttribute('selected', 'selected'); | |
| 994 } | |
| 995 this.selectedIndex_ = i; | |
| 996 } | |
| 997 }, | |
| 998 | |
| 999 getSelectedItem: function() { | |
| 1000 return this.menu.children[this.selectedIndex_] || null; | |
| 1001 }, | |
| 1002 | |
| 1003 executeItem: function(item) { | |
| 1004 var command = item.getAttribute('command'); | |
| 1005 if (command in this.commands) { | |
| 1006 this.commands[command].call(this, item); | |
| 1007 } | |
| 1008 | |
| 1009 this.hide(); | |
| 1010 } | |
| 1011 }; | |
| 1012 | |
| 1013 var optionMenu = new OptionMenu($('option-button'), $('option-menu')); | |
| 1014 optionMenu.commands = { | |
| 1015 'clear-all-blacklisted' : function() { | |
| 1016 mostVisited.clearAllBlacklisted(); | |
| 1017 chrome.send('getMostVisited'); | |
| 1018 }, | |
| 1019 'show': function(item) { | |
| 1020 var section = Section[item.getAttribute('section')]; | |
| 1021 showSection(section); | |
| 1022 saveShownSections(); | |
| 1023 }, | |
| 1024 'hide': function(item) { | |
| 1025 var section = Section[item.getAttribute('section')]; | |
| 1026 hideSection(section); | |
| 1027 saveShownSections(); | |
| 1028 } | |
| 1029 }; | |
| 1030 | |
| 1031 $('most-visited').addEventListener('click', function(e) { | |
| 1032 var target = e.target; | |
| 1033 if (hasClass(target, 'pin')) { | |
| 1034 mostVisited.togglePinned(mostVisited.getItem(target)); | |
| 1035 e.preventDefault(); | |
| 1036 } else if (hasClass(target, 'remove')) { | |
| 1037 mostVisited.blacklist(mostVisited.getItem(target)); | |
| 1038 e.preventDefault(); | |
| 1039 } | |
| 1040 }); | |
| 1041 | |
| 1042 // Allow blacklisting most visited site using the keyboard. | |
| 1043 $('most-visited').addEventListener('keydown', function(e) { | |
| 1044 if (!IS_MAC && e.keyCode == 46 || // Del | |
| 1045 IS_MAC && e.metaKey && e.keyCode == 8) { // Cmd + Backspace | |
| 1046 mostVisited.blacklist(e.target); | |
| 1047 } | |
| 1048 }); | |
| 1049 | |
| 1050 $('main').addEventListener('click', function(e) { | |
| 1051 if (e.target.tagName == 'H2') { | |
| 1052 var p = e.target.parentNode; | |
| 1053 var section = p.getAttribute('section'); | |
| 1054 if (section) { | |
| 1055 if (shownSections & Section[section]) | |
| 1056 hideSection(Section[section]); | |
| 1057 else | |
| 1058 showSection(Section[section]); | |
| 1059 saveShownSections(); | |
| 1060 } | |
| 1061 } | |
| 1062 }); | |
| 1063 | |
| 1064 function handleIfEnterKey(f) { | |
| 1065 return function(e) { | |
| 1066 if (e.keyIdentifier == 'Enter') { | |
| 1067 f(e); | |
| 1068 } | |
| 1069 }; | |
| 1070 } | |
| 1071 | |
| 1072 function maybeReopenTab(e) { | |
| 1073 var el = findAncestor(e.target, function(el) { | |
| 1074 return el.sessionId !== undefined; | |
| 1075 }); | |
| 1076 if (el) { | |
| 1077 chrome.send('reopenTab', [String(el.sessionId)]); | |
| 1078 e.preventDefault(); | |
| 1079 | |
| 1080 // HACK(arv): After the window onblur event happens we get a mouseover event | |
| 1081 // on the next item and we want to make sure that we do not show a tooltip | |
| 1082 // for that. | |
| 1083 window.setTimeout(function() { | |
| 1084 windowTooltip.hide(); | |
| 1085 }, 2 * WindowTooltip.DELAY); | |
| 1086 } | |
| 1087 } | |
| 1088 | |
| 1089 function maybeShowWindowTooltip(e) { | |
| 1090 var f = function(el) { | |
| 1091 return el.tabItems !== undefined; | |
| 1092 }; | |
| 1093 var el = findAncestor(e.target, f); | |
| 1094 var relatedEl = findAncestor(e.relatedTarget, f); | |
| 1095 if (el && el != relatedEl) { | |
| 1096 windowTooltip.handleMouseOver(e, el, el.tabItems); | |
| 1097 } | |
| 1098 } | |
| 1099 | |
| 1100 | |
| 1101 var recentlyClosedElement = $('recently-closed'); | |
| 1102 | |
| 1103 recentlyClosedElement.addEventListener('click', maybeReopenTab); | |
| 1104 recentlyClosedElement.addEventListener('keydown', | |
| 1105 handleIfEnterKey(maybeReopenTab)); | |
| 1106 | |
| 1107 recentlyClosedElement.addEventListener('mouseover', maybeShowWindowTooltip); | |
| 1108 recentlyClosedElement.addEventListener('focus', maybeShowWindowTooltip, true); | |
| 1109 | |
| 1110 /** | |
| 1111 * This object represents a tooltip representing a closed window. It is | |
| 1112 * shown when hovering over a closed window item or when the item is focused. It | |
| 1113 * gets hidden when blurred or when mousing out of the menu or the item. | |
| 1114 * @param {Element} tooltipEl The element to use as the tooltip. | |
| 1115 * @constructor | |
| 1116 */ | |
| 1117 function WindowTooltip(tooltipEl) { | |
| 1118 this.tooltipEl = tooltipEl; | |
| 1119 this.boundHide_ = bind(this.hide, this); | |
| 1120 this.boundHandleMouseOut_ = bind(this.handleMouseOut, this); | |
| 1121 } | |
| 1122 | |
| 1123 WindowTooltip.trackMouseMove_ = function(e) { | |
| 1124 WindowTooltip.clientX = e.clientX; | |
| 1125 WindowTooltip.clientY = e.clientY; | |
| 1126 }; | |
| 1127 | |
| 1128 /** | |
| 1129 * Time in ms to delay before the tooltip is shown. | |
| 1130 * @type {number} | |
| 1131 */ | |
| 1132 WindowTooltip.DELAY = 300; | |
| 1133 | |
| 1134 WindowTooltip.prototype = { | |
| 1135 timer: 0, | |
| 1136 handleMouseOver: function(e, linkEl, tabs) { | |
| 1137 this.linkEl_ = linkEl; | |
| 1138 if (e.type == 'mouseover') { | |
| 1139 this.linkEl_.addEventListener('mousemove', WindowTooltip.trackMouseMove_); | |
| 1140 this.linkEl_.addEventListener('mouseout', this.boundHandleMouseOut_); | |
| 1141 } else { // focus | |
| 1142 this.linkEl_.addEventListener('blur', this.boundHide_); | |
| 1143 } | |
| 1144 this.timer = window.setTimeout(bind(this.show, this, e.type, linkEl, tabs), | |
| 1145 WindowTooltip.DELAY); | |
| 1146 }, | |
| 1147 show: function(type, linkEl, tabs) { | |
| 1148 window.addEventListener('blur', this.boundHide_); | |
| 1149 this.linkEl_.removeEventListener('mousemove', | |
| 1150 WindowTooltip.trackMouseMove_); | |
| 1151 window.clearTimeout(this.timer); | |
| 1152 | |
| 1153 this.renderItems(tabs); | |
| 1154 var rect = linkEl.getBoundingClientRect(); | |
| 1155 var bodyRect = document.body.getBoundingClientRect(); | |
| 1156 var rtl = document.documentElement.dir == 'rtl'; | |
| 1157 | |
| 1158 this.tooltipEl.style.display = 'block'; | |
| 1159 var tooltipRect = this.tooltipEl.getBoundingClientRect(); | |
| 1160 var x, y; | |
| 1161 | |
| 1162 // When focused show below, like a drop down menu. | |
| 1163 if (type == 'focus') { | |
| 1164 x = rtl ? | |
| 1165 rect.left + bodyRect.left + rect.width - this.tooltipEl.offsetWidth : | |
| 1166 rect.left + bodyRect.left; | |
| 1167 y = rect.top + bodyRect.top + rect.height; | |
| 1168 } else { | |
| 1169 x = bodyRect.left + (rtl ? | |
| 1170 WindowTooltip.clientX - this.tooltipEl.offsetWidth : | |
| 1171 WindowTooltip.clientX); | |
| 1172 // Offset like a tooltip | |
| 1173 y = 20 + WindowTooltip.clientY + bodyRect.top; | |
| 1174 } | |
| 1175 | |
| 1176 // We need to ensure that the tooltip is inside the window viewport. | |
| 1177 x = Math.min(x, bodyRect.width - tooltipRect.width); | |
| 1178 x = Math.max(x, 0); | |
| 1179 y = Math.min(y, bodyRect.height - tooltipRect.height); | |
| 1180 y = Math.max(y, 0); | |
| 1181 | |
| 1182 this.tooltipEl.style.left = x + 'px'; | |
| 1183 this.tooltipEl.style.top = y + 'px'; | |
| 1184 }, | |
| 1185 handleMouseOut: function(e) { | |
| 1186 // Don't hide when move to another item in the link. | |
| 1187 var f = function(el) { | |
| 1188 return el.tabItems !== undefined; | |
| 1189 }; | |
| 1190 var el = findAncestor(e.target, f); | |
| 1191 var relatedEl = findAncestor(e.relatedTarget, f); | |
| 1192 if (el && el != relatedEl) { | |
| 1193 this.hide(); | |
| 1194 } | |
| 1195 }, | |
| 1196 hide: function() { | |
| 1197 window.clearTimeout(this.timer); | |
| 1198 window.removeEventListener('blur', this.boundHide_); | |
| 1199 this.linkEl_.removeEventListener('mousemove', | |
| 1200 WindowTooltip.trackMouseMove_); | |
| 1201 this.linkEl_.removeEventListener('mouseout', this.boundHandleMouseOut_); | |
| 1202 this.linkEl_.removeEventListener('blur', this.boundHide_); | |
| 1203 this.linkEl_ = null; | |
| 1204 | |
| 1205 this.tooltipEl.style.display = 'none'; | |
| 1206 }, | |
| 1207 renderItems: function(tabs) { | |
| 1208 var tooltip = this.tooltipEl; | |
| 1209 tooltip.textContent = ''; | |
| 1210 | |
| 1211 tabs.forEach(function(tab) { | |
| 1212 var span = document.createElement('span'); | |
| 1213 span.className = 'item'; | |
| 1214 span.style.backgroundImage = url('chrome://favicon/' + tab.url); | |
| 1215 span.dir = tab.direction; | |
| 1216 span.textContent = tab.title; | |
| 1217 tooltip.appendChild(span); | |
| 1218 }); | |
| 1219 } | |
| 1220 }; | |
| 1221 | |
| 1222 var windowTooltip = new WindowTooltip($('window-tooltip')); | |
| 1223 | |
| 1224 window.addEventListener('load', bind(logEvent, global, 'Tab.NewTabOnload', | |
| 1225 true)); | |
| 1226 window.addEventListener('load', onDataLoaded); | |
| 1227 | |
| 1228 window.addEventListener('resize', handleWindowResize); | |
| 1229 document.addEventListener('DOMContentLoaded', | |
| 1230 bind(logEvent, global, 'Tab.NewTabDOMContentLoaded', true)); | |
| 1231 | |
| 1232 // Whether or not we should send the initial 'GetSyncMessage' to the backend | |
| 1233 // depends on the value of the attribue 'syncispresent' which the backend sets | |
| 1234 // to indicate if there is code in the backend which is capable of processing | |
| 1235 // this message. This attribute is loaded by the JSTemplate and therefore we | |
| 1236 // must make sure we check the attribute after the DOM is loaded. | |
| 1237 document.addEventListener('DOMContentLoaded', | |
| 1238 callGetSyncMessageIfSyncIsPresent); | |
| 1239 | |
| 1240 // Set up links and text-decoration for promotional message. | |
| 1241 document.addEventListener('DOMContentLoaded', setUpPromoMessage); | |
| 1242 | |
| 1243 // Work around for http://crbug.com/25329 | |
| 1244 function ensureSmallGridCorrect() { | |
| 1245 if (wasSmallGrid != useSmallGrid()) { | |
| 1246 applyMostVisitedRects(); | |
| 1247 } | |
| 1248 } | |
| 1249 document.addEventListener('DOMContentLoaded', ensureSmallGridCorrect); | |
| 1250 | |
| 1251 /** | |
| 1252 * The sync code is not yet built by default on all platforms so we have to | |
| 1253 * make sure we don't send the initial sync message to the backend unless the | |
| 1254 * backend told us that the sync code is present. | |
| 1255 */ | |
| 1256 function callGetSyncMessageIfSyncIsPresent() { | |
| 1257 if (document.documentElement.getAttribute('syncispresent') == 'true') { | |
| 1258 chrome.send('GetSyncMessage'); | |
| 1259 } | |
| 1260 } | |
| 1261 | |
| 1262 function setAsHomePageLinkClicked(e) { | |
| 1263 chrome.send('setHomePage'); | |
| 1264 e.preventDefault(); | |
| 1265 } | |
| 1266 | |
| 1267 function onHomePageSet(data) { | |
| 1268 showNotification(data[0], data[1]); | |
| 1269 // Removes the "make this my home page" tip. | |
| 1270 clearTipLine(); | |
| 1271 } | |
| 1272 | |
| 1273 function hideAllMenus() { | |
| 1274 optionMenu.hide(); | |
| 1275 } | |
| 1276 | |
| 1277 window.addEventListener('blur', hideAllMenus); | |
| 1278 window.addEventListener('keydown', function(e) { | |
| 1279 if (e.keyIdentifier == 'Alt' || e.keyIdentifier == 'Meta') { | |
| 1280 hideAllMenus(); | |
| 1281 } | |
| 1282 }, true); | |
| 1283 | |
| 1284 // Tooltip for elements that have text that overflows. | |
| 1285 document.addEventListener('mouseover', function(e) { | |
| 1286 // We don't want to do this while we are dragging because it makes things very | |
| 1287 // janky | |
| 1288 if (dnd.dragItem) { | |
| 1289 return; | |
| 1290 } | |
| 1291 | |
| 1292 var el = findAncestor(e.target, function(el) { | |
| 1293 return el.xtitle; | |
| 1294 }); | |
| 1295 if (el && el.xtitle != el.title) { | |
| 1296 if (el.scrollWidth > el.clientWidth) { | |
| 1297 el.title = el.xtitle; | |
| 1298 } else { | |
| 1299 el.title = ''; | |
| 1300 } | |
| 1301 } | |
| 1302 }); | |
| 1303 | |
| 1304 // DnD | |
| 1305 | |
| 1306 var dnd = { | |
| 1307 currentOverItem_: null, | |
| 1308 get currentOverItem() { | |
| 1309 return this.currentOverItem_; | |
| 1310 }, | |
| 1311 set currentOverItem(item) { | |
| 1312 var style; | |
| 1313 if (item != this.currentOverItem_) { | |
| 1314 if (this.currentOverItem_) { | |
| 1315 style = this.currentOverItem_.firstElementChild.style; | |
| 1316 style.left = style.top = ''; | |
| 1317 } | |
| 1318 this.currentOverItem_ = item; | |
| 1319 | |
| 1320 if (item) { | |
| 1321 // Make the drag over item move 15px towards the source. The movement is | |
| 1322 // done by only moving the edit-mode-border (as in the mocks) and it is | |
| 1323 // done with relative positioning so that the movement does not change | |
| 1324 // the drop target. | |
| 1325 var dragIndex = mostVisited.getThumbnailIndex(this.dragItem); | |
| 1326 var overIndex = mostVisited.getThumbnailIndex(item); | |
| 1327 if (dragIndex == -1 || overIndex == -1) { | |
| 1328 return; | |
| 1329 } | |
| 1330 | |
| 1331 var dragRect = mostVisited.getRectByIndex(dragIndex); | |
| 1332 var overRect = mostVisited.getRectByIndex(overIndex); | |
| 1333 | |
| 1334 var x = dragRect.left - overRect.left; | |
| 1335 var y = dragRect.top - overRect.top; | |
| 1336 var z = Math.sqrt(x * x + y * y); | |
| 1337 var z2 = 15; | |
| 1338 var x2 = x * z2 / z; | |
| 1339 var y2 = y * z2 / z; | |
| 1340 | |
| 1341 style = this.currentOverItem_.firstElementChild.style; | |
| 1342 style.left = x2 + 'px'; | |
| 1343 style.top = y2 + 'px'; | |
| 1344 } | |
| 1345 } | |
| 1346 }, | |
| 1347 dragItem: null, | |
| 1348 startX: 0, | |
| 1349 startY: 0, | |
| 1350 startScreenX: 0, | |
| 1351 startScreenY: 0, | |
| 1352 dragEndTimer: null, | |
| 1353 | |
| 1354 handleDragStart: function(e) { | |
| 1355 var thumbnail = mostVisited.getItem(e.target); | |
| 1356 if (thumbnail) { | |
| 1357 // Don't set data since HTML5 does not allow setting the name for | |
| 1358 // url-list. Instead, we just rely on the dragging of link behavior. | |
| 1359 this.dragItem = thumbnail; | |
| 1360 addClass(this.dragItem, 'dragging'); | |
| 1361 this.dragItem.style.zIndex = 2; | |
| 1362 e.dataTransfer.effectAllowed = 'copyLinkMove'; | |
| 1363 } | |
| 1364 }, | |
| 1365 | |
| 1366 handleDragEnter: function(e) { | |
| 1367 if (this.canDropOnElement(this.currentOverItem)) { | |
| 1368 e.preventDefault(); | |
| 1369 } | |
| 1370 }, | |
| 1371 | |
| 1372 handleDragOver: function(e) { | |
| 1373 var item = mostVisited.getItem(e.target); | |
| 1374 this.currentOverItem = item; | |
| 1375 if (this.canDropOnElement(item)) { | |
| 1376 e.preventDefault(); | |
| 1377 e.dataTransfer.dropEffect = 'move'; | |
| 1378 } | |
| 1379 }, | |
| 1380 | |
| 1381 handleDragLeave: function(e) { | |
| 1382 var item = mostVisited.getItem(e.target); | |
| 1383 if (item) { | |
| 1384 e.preventDefault(); | |
| 1385 } | |
| 1386 | |
| 1387 this.currentOverItem = null; | |
| 1388 }, | |
| 1389 | |
| 1390 handleDrop: function(e) { | |
| 1391 var dropTarget = mostVisited.getItem(e.target); | |
| 1392 if (this.canDropOnElement(dropTarget)) { | |
| 1393 dropTarget.style.zIndex = 1; | |
| 1394 mostVisited.swapPosition(this.dragItem, dropTarget); | |
| 1395 // The timeout below is to allow WebKit to see that we turned off | |
| 1396 // pointer-event before moving the thumbnails so that we can get out of | |
| 1397 // hover mode. | |
| 1398 window.setTimeout(function() { | |
| 1399 mostVisited.invalidate(); | |
| 1400 mostVisited.layout(); | |
| 1401 }, 10); | |
| 1402 e.preventDefault(); | |
| 1403 if (this.dragEndTimer) { | |
| 1404 window.clearTimeout(this.dragEndTimer); | |
| 1405 this.dragEndTimer = null; | |
| 1406 } | |
| 1407 afterTransition(function() { | |
| 1408 dropTarget.style.zIndex = ''; | |
| 1409 }); | |
| 1410 } | |
| 1411 }, | |
| 1412 | |
| 1413 handleDragEnd: function(e) { | |
| 1414 var dragItem = this.dragItem; | |
| 1415 if (dragItem) { | |
| 1416 dragItem.style.pointerEvents = ''; | |
| 1417 removeClass(dragItem, 'dragging'); | |
| 1418 | |
| 1419 afterTransition(function() { | |
| 1420 // Delay resetting zIndex to let the animation finish. | |
| 1421 dragItem.style.zIndex = ''; | |
| 1422 // Same for overflow. | |
| 1423 dragItem.parentNode.style.overflow = ''; | |
| 1424 }); | |
| 1425 | |
| 1426 mostVisited.invalidate(); | |
| 1427 mostVisited.layout(); | |
| 1428 this.dragItem = null; | |
| 1429 } | |
| 1430 }, | |
| 1431 | |
| 1432 handleDrag: function(e) { | |
| 1433 // Moves the drag item making sure that it is not displayed outside the | |
| 1434 // browser viewport. | |
| 1435 var item = mostVisited.getItem(e.target); | |
| 1436 var rect = document.querySelector('#most-visited').getBoundingClientRect(); | |
| 1437 item.style.pointerEvents = 'none'; | |
| 1438 | |
| 1439 var x = this.startX + e.screenX - this.startScreenX; | |
| 1440 var y = this.startY + e.screenY - this.startScreenY; | |
| 1441 | |
| 1442 // The position of the item is relative to #most-visited so we need to | |
| 1443 // subtract that when calculating the allowed position. | |
| 1444 x = Math.max(x, -rect.left); | |
| 1445 x = Math.min(x, document.body.clientWidth - rect.left - item.offsetWidth - | |
| 1446 2); | |
| 1447 // The shadow is 2px | |
| 1448 y = Math.max(-rect.top, y); | |
| 1449 y = Math.min(y, document.body.clientHeight - rect.top - item.offsetHeight - | |
| 1450 2); | |
| 1451 | |
| 1452 // Override right in case of RTL. | |
| 1453 item.style.right = 'auto'; | |
| 1454 item.style.left = x + 'px'; | |
| 1455 item.style.top = y + 'px'; | |
| 1456 item.style.zIndex = 2; | |
| 1457 }, | |
| 1458 | |
| 1459 // We listen to mousedown to get the relative position of the cursor for dnd. | |
| 1460 handleMouseDown: function(e) { | |
| 1461 var item = mostVisited.getItem(e.target); | |
| 1462 if (item) { | |
| 1463 this.startX = item.offsetLeft; | |
| 1464 this.startY = item.offsetTop; | |
| 1465 this.startScreenX = e.screenX; | |
| 1466 this.startScreenY = e.screenY; | |
| 1467 | |
| 1468 // We don't want to focus the item on mousedown. However, to prevent focus | |
| 1469 // one has to call preventDefault but this also prevents the drag and drop | |
| 1470 // (sigh) so we only prevent it when the user is not doing a left mouse | |
| 1471 // button drag. | |
| 1472 if (e.button != 0) // LEFT | |
| 1473 e.preventDefault(); | |
| 1474 } | |
| 1475 }, | |
| 1476 | |
| 1477 canDropOnElement: function(el) { | |
| 1478 return this.dragItem && el && hasClass(el, 'thumbnail-container') && | |
| 1479 !hasClass(el, 'filler'); | |
| 1480 }, | |
| 1481 | |
| 1482 init: function() { | |
| 1483 var el = $('most-visited'); | |
| 1484 el.addEventListener('dragstart', bind(this.handleDragStart, this)); | |
| 1485 el.addEventListener('dragenter', bind(this.handleDragEnter, this)); | |
| 1486 el.addEventListener('dragover', bind(this.handleDragOver, this)); | |
| 1487 el.addEventListener('dragleave', bind(this.handleDragLeave, this)); | |
| 1488 el.addEventListener('drop', bind(this.handleDrop, this)); | |
| 1489 el.addEventListener('dragend', bind(this.handleDragEnd, this)); | |
| 1490 el.addEventListener('drag', bind(this.handleDrag, this)); | |
| 1491 el.addEventListener('mousedown', bind(this.handleMouseDown, this)); | |
| 1492 } | |
| 1493 }; | |
| 1494 | |
| 1495 dnd.init(); | |
| 1496 | |
| 1497 /** | 1 /** |
| 1498 * Whitelist of tag names allowed in parseHtmlSubset. | 2 * Whitelist of tag names allowed in parseHtmlSubset. |
| 1499 * @type {[string]} | 3 * @type {[string]} |
| 1500 */ | 4 */ |
| 1501 var allowedTags = ['A', 'B', 'STRONG']; | 5 var allowedTags = ['A', 'B', 'STRONG']; |
| 1502 | 6 |
| 1503 /** | 7 /** |
| 1504 * Parse a very small subset of HTML. | 8 * Parse a very small subset of HTML. |
| 1505 * @param {string} s The string to parse. | 9 * @param {string} s The string to parse. |
| 1506 * @throws {Error} In case of non supported markup. | 10 * @throws {Error} In case of non supported markup. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1566 case Node.DOCUMENT_FRAGMENT_NODE: | 70 case Node.DOCUMENT_FRAGMENT_NODE: |
| 1567 case Node.TEXT_NODE: | 71 case Node.TEXT_NODE: |
| 1568 break; | 72 break; |
| 1569 | 73 |
| 1570 default: | 74 default: |
| 1571 throw Error('Node type ' + node.nodeType + ' is not supported'); | 75 throw Error('Node type ' + node.nodeType + ' is not supported'); |
| 1572 } | 76 } |
| 1573 }); | 77 }); |
| 1574 return df; | 78 return df; |
| 1575 } | 79 } |
| 1576 | |
| 1577 /** | |
| 1578 * Makes links and buttons support a different underline color. | |
| 1579 * @param {Node} node The node to search for links and buttons in. | |
| 1580 */ | |
| 1581 function fixLinkUnderlines(node) { | |
| 1582 var elements = node.querySelectorAll('a,button'); | |
| 1583 Array.prototype.forEach.call(elements, fixLinkUnderline); | |
| 1584 } | |
| 1585 | |
| 1586 /** | |
| 1587 * Wraps the content of an element in a a link-color span. | |
| 1588 * @param {Element} el The element to wrap. | |
| 1589 */ | |
| 1590 function fixLinkUnderline(el) { | |
| 1591 var span = document.createElement('span'); | |
| 1592 span.className = 'link-color'; | |
| 1593 while (el.hasChildNodes()) { | |
| 1594 span.appendChild(el.firstChild); | |
| 1595 } | |
| 1596 el.appendChild(span); | |
| 1597 } | |
| 1598 | |
| 1599 updateAttribution(); | |
| 1600 | |
| 1601 // Closes the promo line when close button is clicked. | |
| 1602 $('promo-close').onclick = function (e) { | |
| 1603 addClass($('promo-line'), 'hidden'); | |
| 1604 chrome.send('stopPromoLineMessage'); | |
| 1605 e.preventDefault(); | |
| 1606 }; | |
| 1607 | |
| 1608 // Set bookmark sync button to start bookmark sync process on click; also set | |
| 1609 // link underline colors correctly. | |
| 1610 function setUpPromoMessage() { | |
| 1611 var syncButton = document.querySelector('#promo-message button'); | |
| 1612 syncButton.className = 'sync-button link'; | |
| 1613 syncButton.onclick = syncSectionLinkClicked; | |
| 1614 fixLinkUnderlines($('promo-message')); | |
| 1615 } | |
| OLD | NEW |