| Index: components/physical_web/webui/resources/physical_web.js
|
| diff --git a/components/physical_web/webui/resources/physical_web.js b/components/physical_web/webui/resources/physical_web.js
|
| index 418dc1d6e90c1b5e8daffb35b8fbd841900eae08..b653afb64a7aa67ae7c55919b28df51bc8c514f4 100644
|
| --- a/components/physical_web/webui/resources/physical_web.js
|
| +++ b/components/physical_web/webui/resources/physical_web.js
|
| @@ -9,12 +9,25 @@
|
| * nearby devices
|
| */
|
| function renderTemplate(nearbyUrlsData) {
|
| + prepareNearbyUrls(nearbyUrlsData);
|
| +
|
| + // This is a workaround with jstemplate. Jstemplate render only works on empty
|
| + // node. When we need to rerender things, we have to remove previous nodes.
|
| + let renderContainer = document.getElementById('render-container');
|
| + // Remove existing childNode.
|
| + while (renderContainer.hasChildNodes()) {
|
| + renderContainer.removeChild(renderContainer.lastChild);
|
| + }
|
| +
|
| + let templateDiv = document.getElementById('render-template').cloneNode(true);
|
| + renderContainer.appendChild(templateDiv);
|
| +
|
| // This is the javascript code that processes the template:
|
| - jstProcess(new JsEvalContext(nearbyUrlsData), $('physicalWebTemplate'));
|
| + jstProcess(new JsEvalContext(nearbyUrlsData), templateDiv);
|
| }
|
|
|
| -function requestNearbyURLs() {
|
| - chrome.send('requestNearbyURLs');
|
| +function physicalWebPageLoaded() {
|
| + chrome.send('physicalWebPageLoaded');
|
| }
|
|
|
| function physicalWebItemClicked(index) {
|
| @@ -22,10 +35,44 @@ function physicalWebItemClicked(index) {
|
| }
|
|
|
| function returnNearbyURLs(nearbyUrlsData) {
|
| - var bodyContainer = $('body-container');
|
| + let bodyContainer = $('body-container');
|
| renderTemplate(nearbyUrlsData);
|
|
|
| bodyContainer.style.visibility = 'visible';
|
| }
|
|
|
| -document.addEventListener('DOMContentLoaded', requestNearbyURLs);
|
| +var currentNearbyUrlsData = [];
|
| +function findNewUrls(nearbyUrlsData) {
|
| + var newUrlsMetadata = [];
|
| + for (let i = 0; i < nearbyUrlsData['metadata'].length; i++) {
|
| + let current = nearbyUrlsData['metadata'][i];
|
| + let found = false;
|
| + for (let j = 0; j < currentNearbyUrlsData.length; j++) {
|
| + if (current.resolvedUrl == currentNearbyUrlsData[j].resolvedUrl) {
|
| + found = true;
|
| + break;
|
| + }
|
| + }
|
| + if (!found) {
|
| + newUrlsMetadata.push(current);
|
| + }
|
| + }
|
| + return newUrlsMetadata;
|
| +}
|
| +
|
| +// When we have new URLs, we want to append it to the end of the result page to
|
| +// prevent misclick.
|
| +function prepareNearbyUrls(nearbyUrlsData) {
|
| + currentNearbyUrlsData =
|
| + currentNearbyUrlsData.concat(findNewUrls(nearbyUrlsData));
|
| + nearbyUrlsData.metadata = currentNearbyUrlsData;
|
| + resetUrlIndex(nearbyUrlsData);
|
| +}
|
| +
|
| +function resetUrlIndex(nearbyUrlsData) {
|
| + for (let i = 0; i < nearbyUrlsData['metadata'].length; i++) {
|
| + nearbyUrlsData['metadata'][i].index = i;
|
| + }
|
| +}
|
| +
|
| +document.addEventListener('DOMContentLoaded', physicalWebPageLoaded);
|
|
|