Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 * Takes the |nearbyUrlsData| input argument which holds metadata for web pages | 6 * Takes the |nearbyUrlsData| input argument which holds metadata for web pages |
| 7 * broadcast by nearby devices. | 7 * broadcast by nearby devices. |
| 8 * @param {Object} nearbyUrlsData Information about web pages broadcast by | 8 * @param {Object} nearbyUrlsData Information about web pages broadcast by |
| 9 * nearby devices | 9 * nearby devices |
| 10 */ | 10 */ |
| 11 function renderTemplate(nearbyUrlsData) { | 11 function renderTemplate(nearbyUrlsData) { |
| 12 prepareURLS(nearbyUrlsData); | |
| 13 | |
| 14 // This is a workaround with jstemplate. Jstemplate render only works on empty | |
| 15 // node. When we need to rerender things, we have to remove previous nodes. | |
| 16 let renderContainer = document.getElementById('render-container'); | |
| 17 // Remove existing childNode. | |
| 18 while (renderContainer.hasChildNodes()) { | |
| 19 renderContainer.removeChild(renderContainer.lastChild); | |
| 20 } | |
| 21 | |
| 22 let templateDiv = document.getElementById('render-template').cloneNode(true); | |
| 23 renderContainer.appendChild(templateDiv); | |
| 24 | |
| 12 // This is the javascript code that processes the template: | 25 // This is the javascript code that processes the template: |
| 13 jstProcess(new JsEvalContext(nearbyUrlsData), $('physicalWebTemplate')); | 26 jstProcess(new JsEvalContext(nearbyUrlsData), templateDiv); |
| 14 } | 27 } |
| 15 | 28 |
| 16 function requestNearbyURLs() { | 29 function requestNearbyURLs() { |
| 17 chrome.send('requestNearbyURLs'); | 30 chrome.send('requestNearbyURLs'); |
| 18 } | 31 } |
| 19 | 32 |
| 20 function physicalWebItemClicked(index) { | 33 function physicalWebItemClicked(index) { |
| 21 chrome.send('physicalWebItemClicked', [index]); | 34 chrome.send('physicalWebItemClicked', [index]); |
| 22 } | 35 } |
| 23 | 36 |
| 24 function returnNearbyURLs(nearbyUrlsData) { | 37 function returnNearbyURLs(nearbyUrlsData) { |
| 25 var bodyContainer = $('body-container'); | 38 let bodyContainer = $('body-container'); |
| 26 renderTemplate(nearbyUrlsData); | 39 renderTemplate(nearbyUrlsData); |
| 27 | 40 |
| 28 bodyContainer.style.visibility = 'visible'; | 41 bodyContainer.style.visibility = 'visible'; |
| 29 } | 42 } |
| 30 | 43 |
| 44 function prepareURLS(nearbyUrlsData) { | |
| 45 dedupURLs(nearbyUrlsData); | |
| 46 reorderURLs(nearbyUrlsData); | |
| 47 resetUrlIndex(nearbyUrlsData); | |
| 48 } | |
| 49 | |
| 50 // When we have new URLs, we want to append it to the end of the result page to | |
| 51 // prevent misclick. | |
| 52 var ShownUrls = []; | |
| 53 function reorderURLs(nearbyUrlsData) { | |
|
mmocny
2017/03/22 16:03:50
This seems like an error prone hand rolled solutio
Ran
2017/03/22 19:10:47
Done.
| |
| 54 let newUrlsMetadata = []; | |
| 55 for (let i = nearbyUrlsData['metadata'].length - 1; i >= 0; i--) { | |
| 56 let resolvedUrl = nearbyUrlsData['metadata'][i].resolvedUrl; | |
| 57 if (ShownUrls.indexOf(resolvedUrl) != -1) { // already shown in page | |
| 58 continue; | |
| 59 } else { | |
| 60 ShownUrls.push(resolvedUrl); | |
| 61 newUrlsMetadata.push(nearbyUrlsData['metadata'][i]); | |
| 62 nearbyUrlsData['metadata'].splice(i, 1); | |
| 63 } | |
| 64 } | |
| 65 nearbyUrlsData['metadata'] = | |
| 66 nearbyUrlsData['metadata'].concat(newUrlsMetadata); | |
| 67 } | |
| 68 | |
| 69 function dedupURLs(nearbyUrlsData) { | |
|
mmocny
2017/03/22 16:03:50
As I mentioned in person, I think URLs are (or sho
Ran
2017/03/22 19:10:47
Done.
| |
| 70 let set = new Set(); | |
| 71 for (let i = nearbyUrlsData['metadata'].length - 1; i >= 0; i--) { | |
| 72 if (set.has(nearbyUrlsData['metadata'][i].resolvedUrl)) { | |
| 73 nearbyUrlsData['metadata'].splice(i, 1); | |
| 74 } else { | |
| 75 set.add(nearbyUrlsData['metadata'][i].resolvedUrl); | |
| 76 } | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 function resetUrlIndex(nearbyUrlsData) { | |
|
mmocny
2017/03/22 16:03:50
Do you use .index for anything?
mattreynolds
2017/03/22 18:33:10
Only for UMA. If we can remove it that would be gr
| |
| 81 for (let i = 0; i < nearbyUrlsData['metadata'].length; i++) { | |
| 82 nearbyUrlsData['metadata'][i].index = i; | |
| 83 } | |
| 84 } | |
| 85 | |
| 31 document.addEventListener('DOMContentLoaded', requestNearbyURLs); | 86 document.addEventListener('DOMContentLoaded', requestNearbyURLs); |
| OLD | NEW |