resources/lib.js 1 /* Copyright 2017 Google Inc. */ 2 /** 3 * Swaps the display settings of two elements. Before calling, exactly one 4 * of the two elements should have style="display:none" and it shows the one 5 * that is hidden and hides the one that is shown. 6 */ 7 function _showHide(id1, id2) { 8 var x1 = document.getElementById(id1); 9 var x2 = document.getElementById(id2); 10 var shown = x1.style.display != ''; 11 x1.style.display = shown ? '' : 'none'; 12 x2.style.display = shown ? 'none' : ''; 13 }; 14 15 /** 16 * Refreshes the current page by loading the url and then passing the 17 * json response object to the callback. 18 */ 19 function _refresh(url, callback) { 20 var httpRequest = window.XMLHttpRequest ? new XMLHttpRequest() 21 : new ActiveXObject("Microsoft.XMLHTTP"); 22 httpRequest.onreadystatechange = function() { 23 if (httpRequest.readyState == 4) { 24 _feed = callback; 25 eval('(' + httpRequest.responseText + ')'); 26 httpRequest = null; 27 } 28 } 29 httpRequest.open("GET", url, true); 30 httpRequest.send(null); 31 }; 32 33 /** 34 * Processes refresh response {'private_snippet':snippet, user:snippet, ...} 35 */ 36 function _finishRefreshHome(response) { 37 for (var uid in response) { 38 var element = document.getElementById(uid); 39 if (element) { 40 element.innerHTML = response[uid]; 41 } 42 } 43 }; 44 45 function _refreshHome(uniqueId) { 46 _refresh("/" + uniqueId + "/feed.gtl", _finishRefreshHome); 47 }; 48 49 /** 50 * Processes refresh response [name, snippet, ...] 51 */ 52 function _finishRefreshSnippets(response) { 53 var n = response.length; 54 var element = document.getElementById('user_name'); 55 if (n && element) { 56 element.innerHTML = response[0]; 57 } 58 59 // The snippet divs are numbered 0, 1, ... 60 for (var i = 1; i < n; ++i) { 61 var element = document.getElementById(i - 1); 62 if (element) { 63 element.innerHTML = response[i]; 64 } 65 } 66 }; 67 68 function _refreshSnippets(uniqueId, uid) { 69 _refresh("/" + uniqueId + "/feed.gtl?uid=" + uid, _finishRefreshSnippets); 70 };