dyn.js (3184B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- 2 * vim: sw=2 ts=2 sts=2 et filetype=javascript 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 function loadURL(url,callback) { 8 var xhttp = new XMLHttpRequest(); 9 xhttp.onreadystatechange = function() { 10 if (xhttp.readyState == 4 && xhttp.status == 200) { 11 callback(xhttp.responseText); 12 } 13 }; 14 xhttp.open("GET", url, true); 15 xhttp.send(); 16 } 17 18 function dyn1(selector) { 19 // get an array of elements matching |selector| 20 var elems = Array.prototype.slice.call(document.querySelectorAll(selector)) 21 22 // remove the first item in each grid 23 var removed = elems.map(function(e) { 24 var child = e.children[0]; 25 if (child) { 26 var next = child.nextSibling; 27 e.removeChild(child); 28 return [ e, child, next ]; 29 } else { 30 return null; 31 } 32 }); 33 34 document.body.style.display = 'block'; 35 document.body.offsetHeight; 36 37 // insert the removed item 38 removed.map(function(a) { 39 if (a) { 40 a[0].insertBefore(a[1],a[2]); 41 } 42 }); 43 } 44 45 function dyn2(selector) { 46 // get an array of elements matching |selector| 47 var elems = Array.prototype.slice.call(document.querySelectorAll(selector)) 48 49 // inject a new first item in each grid 50 var inserted = elems.map(function(e) { 51 var child = document.createElement('span'); 52 e.insertBefore(child, e.firstChild); 53 return [ e, child ]; 54 }); 55 56 document.body.style.display = 'block'; 57 document.body.offsetHeight; 58 59 // remove the inserted item 60 inserted.map(function(a) { 61 a[0].removeChild(a[1]); 62 }); 63 } 64 65 function dyn3(selector) { 66 // get an array of elements matching |selector| 67 var elems = Array.prototype.slice.call(document.querySelectorAll(selector)) 68 69 // remove the second item in each grid 70 var removed = elems.map(function(e) { 71 var child = e.children[1]; 72 if (child) { 73 var next = child.nextSibling; 74 e.removeChild(child); 75 return [ e, child, next ]; 76 } else { 77 return null; 78 } 79 }); 80 81 document.body.style.display = 'block'; 82 document.body.offsetHeight; 83 84 // insert the removed items 85 removed.map(function(a) { 86 if (a) { 87 a[0].insertBefore(a[1],a[2]); 88 } 89 }); 90 } 91 92 function dyn4(selector) { 93 dyn3(selector); 94 dyn2(selector); 95 } 96 97 function dyn5(selector) { 98 // get an array of elements matching |selector| 99 var elems = Array.prototype.slice.call(document.querySelectorAll(selector)) 100 101 // inject 20 new items in each grid 102 var inserted = elems.map(function(e) { 103 var a = new Array; 104 for (var i = 0; i < 20; ++i) { 105 var child = document.createElement('span'); 106 e.insertBefore(child, e.firstChild); 107 a.push(child); 108 } 109 return [ e, a ]; 110 }); 111 112 document.body.style.display = 'block'; 113 document.body.offsetHeight; 114 115 // remove the inserted item 116 inserted.map(function(a) { 117 a[1].forEach(function(child) { 118 a[0].removeChild(child); 119 }); 120 }); 121 } 122 123 function dynamicTest(url, callback) { 124 document.body.style.display=''; 125 document.body.offsetHeight; 126 loadURL(url,callback); 127 }