| 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 prepareNearbyUrls(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 physicalWebPageLoaded() { |
| 17 chrome.send('requestNearbyURLs'); | 30 chrome.send('physicalWebPageLoaded'); |
| 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 |
| 31 document.addEventListener('DOMContentLoaded', requestNearbyURLs); | 44 var currentNearbyUrlsData = []; |
| 45 function findNewUrls(nearbyUrlsData) { |
| 46 var newUrlsMetadata = []; |
| 47 for (let i = 0; i < nearbyUrlsData['metadata'].length; i++) { |
| 48 let current = nearbyUrlsData['metadata'][i]; |
| 49 let found = false; |
| 50 for (let j = 0; j < currentNearbyUrlsData.length; j++) { |
| 51 if (current.resolvedUrl == currentNearbyUrlsData[j].resolvedUrl) { |
| 52 found = true; |
| 53 break; |
| 54 } |
| 55 } |
| 56 if (!found) { |
| 57 newUrlsMetadata.push(current); |
| 58 } |
| 59 } |
| 60 return newUrlsMetadata; |
| 61 } |
| 62 |
| 63 // When we have new URLs, we want to append it to the end of the result page to |
| 64 // prevent misclick. |
| 65 function prepareNearbyUrls(nearbyUrlsData) { |
| 66 currentNearbyUrlsData = |
| 67 currentNearbyUrlsData.concat(findNewUrls(nearbyUrlsData)); |
| 68 nearbyUrlsData.metadata = currentNearbyUrlsData; |
| 69 resetUrlIndex(nearbyUrlsData); |
| 70 } |
| 71 |
| 72 function resetUrlIndex(nearbyUrlsData) { |
| 73 for (let i = 0; i < nearbyUrlsData['metadata'].length; i++) { |
| 74 nearbyUrlsData['metadata'][i].index = i; |
| 75 } |
| 76 } |
| 77 |
| 78 document.addEventListener('DOMContentLoaded', physicalWebPageLoaded); |
| OLD | NEW |