|
| 1 | +(function(){ |
| 2 | + var vscroll, ref$; |
| 3 | + vscroll = {}; |
| 4 | + vscroll.fixed = function(opt){ |
| 5 | + opt == null && (opt = {}); |
| 6 | + this.root = opt.root; |
| 7 | + this.childNodes = Array.from(this.root.childNodes); |
| 8 | + this.init(); |
| 9 | + return this; |
| 10 | + }; |
| 11 | + vscroll.fixed.prototype = (ref$ = Object.create(Object.prototype), ref$.init = function(){ |
| 12 | + var this$ = this; |
| 13 | + this.range = [1, 0]; |
| 14 | + this.row = 1; |
| 15 | + this.count = 1; |
| 16 | + this.ph = [0, 1].map(function(){ |
| 17 | + return document.createElement('div'); |
| 18 | + }).map(function(it){ |
| 19 | + var ref$; |
| 20 | + ref$ = it.style; |
| 21 | + ref$.width = '100%'; |
| 22 | + ref$.height = '0px'; |
| 23 | + return this$.root.appendChild(it); |
| 24 | + }); |
| 25 | + this.root.addEventListener('scroll', function(){ |
| 26 | + return this$.handler(); |
| 27 | + }); |
| 28 | + return this.rbox = { |
| 29 | + height: 0 |
| 30 | + }; |
| 31 | + }, ref$.update = function(){ |
| 32 | + var ref$, ref1$, i$, to$, i, y, box; |
| 33 | + this.rbox = this.root.getBoundingClientRect(); |
| 34 | + this.row = 0; |
| 35 | + this.count = 1; |
| 36 | + this.range[0] = (ref$ = this.range[0]) < (ref1$ = this.childNodes.length - 1) ? ref$ : ref1$; |
| 37 | + this.range[1] = (ref$ = this.range[1]) < (ref1$ = this.childNodes.length - 1) ? ref$ : ref1$; |
| 38 | + for (i$ = 0, to$ = (ref$ = this.childNodes.length) < 100 ? ref$ : 100; i$ < to$; ++i$) { |
| 39 | + i = i$; |
| 40 | + if (!this.childNodes[i].parentNode) { |
| 41 | + this.root.insertBefore(this.childNodes[i], this.ph[1]); |
| 42 | + } |
| 43 | + } |
| 44 | + y = undefined; |
| 45 | + for (i$ = 0, to$ = this.childNodes.length; i$ < to$; ++i$) { |
| 46 | + i = i$; |
| 47 | + box = this.childNodes[i].getBoundingClientRect(); |
| 48 | + this.lineHeight = box.height; |
| 49 | + if (!(y != null)) { |
| 50 | + y = box.y; |
| 51 | + } else if (box.y === y) { |
| 52 | + this.count++; |
| 53 | + } else { |
| 54 | + this.lineHeight = box.y - y; |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + for (i$ = 0, to$ = this.childNodes.length; i$ < to$; ++i$) { |
| 59 | + i = i$; |
| 60 | + box = this.childNodes[i].getBoundingClientRect(); |
| 61 | + if (box.y <= this.rbox.height * 4) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + this.row = (ref$ = Math.ceil(i / this.count)) > 1 ? ref$ : 1; |
| 65 | + break; |
| 66 | + } |
| 67 | + this.delta = (ref$ = this.row * this.count) > 1 ? ref$ : 1; |
| 68 | + this.childNodes.map(function(it){ |
| 69 | + if (it.parentNode) { |
| 70 | + return it.parentNode.removeChild(it); |
| 71 | + } |
| 72 | + }); |
| 73 | + return this.handler(); |
| 74 | + }, ref$.handler = function(){ |
| 75 | + var ref$, len, delta, count, nodes, lh, root, ph, rbox, range, min, max, i$, i, j, ref1$, b1, b2; |
| 76 | + ref$ = [this.childNodes.length, this.delta, this.count, this.childNodes, this.lineHeight, this.root, this.ph, this.rbox, this.range], len = ref$[0], delta = ref$[1], count = ref$[2], nodes = ref$[3], lh = ref$[4], root = ref$[5], ph = ref$[6], rbox = ref$[7], range = ref$[8]; |
| 77 | + ref$ = [len, -1], min = ref$[0], max = ref$[1]; |
| 78 | + for (i$ = 0; delta < 0 ? i$ > len : i$ < len; i$ += delta) { |
| 79 | + i = i$; |
| 80 | + j = (ref$ = i + delta - 1) < (ref1$ = nodes.length - 1) ? ref$ : ref1$; |
| 81 | + b1 = { |
| 82 | + y: (i / count) * lh, |
| 83 | + height: lh |
| 84 | + }; |
| 85 | + b2 = { |
| 86 | + y: (j / count) * lh, |
| 87 | + height: lh |
| 88 | + }; |
| 89 | + if (b1.y - root.scrollTop <= 1.5 * rbox.height && b2.y + b2.height - root.scrollTop > -0.5 * rbox.height) { |
| 90 | + if (i < min) { |
| 91 | + min = i; |
| 92 | + } |
| 93 | + if (j > max) { |
| 94 | + max = j; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + if (root.scrollTop > root.scrollHeight / 2 && min === len && max === -1) { |
| 99 | + ref$ = [delta * Math.floor(len / delta), len - 1], min = ref$[0], max = ref$[1]; |
| 100 | + } |
| 101 | + for (i$ = (ref$ = range[0]) > 0 ? ref$ : 0; i$ < min; ++i$) { |
| 102 | + i = i$; |
| 103 | + if (nodes[i].parentNode) { |
| 104 | + root.removeChild(nodes[i]); |
| 105 | + } |
| 106 | + } |
| 107 | + for (i$ = range[0] - 1; i$ >= min; --i$) { |
| 108 | + i = i$; |
| 109 | + if (!nodes[i].parentNode) { |
| 110 | + root.insertBefore(nodes[i], ph[0].nextSibling); |
| 111 | + } |
| 112 | + } |
| 113 | + for (i$ = range[1]; i$ > max; --i$) { |
| 114 | + i = i$; |
| 115 | + if (nodes[i].parentNode) { |
| 116 | + root.removeChild(nodes[i]); |
| 117 | + } |
| 118 | + } |
| 119 | + for (i$ = range[1] + 1; i$ <= max; ++i$) { |
| 120 | + i = i$; |
| 121 | + if (!nodes[i].parentNode) { |
| 122 | + root.insertBefore(nodes[i], ph[1]); |
| 123 | + } |
| 124 | + } |
| 125 | + this.range = [min, max]; |
| 126 | + ph[0].style.height = lh * ((ref$ = min / count) > 0 ? ref$ : 0) + "px"; |
| 127 | + return ph[1].style.height = lh * Math.floor((len - max - 1) / count) + "px"; |
| 128 | + }, ref$.appendChild = function(n){ |
| 129 | + return this.childNodes.splice(this.childNodes.length, 0, n); |
| 130 | + }, ref$.insertBefore = function(n, s){ |
| 131 | + var idx; |
| 132 | + idx = this.childNodes.indexOf(s); |
| 133 | + if (idx < 0) { |
| 134 | + idx = this.childNodes.length; |
| 135 | + } |
| 136 | + return this.childNodes.splice(idx, 0, n); |
| 137 | + }, ref$.removeChild = function(n){ |
| 138 | + var idx; |
| 139 | + if (!~(idx = this.childNodes.indexOf(n))) { |
| 140 | + return; |
| 141 | + } |
| 142 | + return this.childNodes.splice(idx, 1); |
| 143 | + }, ref$); |
| 144 | + vscroll.dummy = function(opt){ |
| 145 | + opt == null && (opt = {}); |
| 146 | + this.root = opt.root; |
| 147 | + this.childNodes = []; |
| 148 | + return this; |
| 149 | + }; |
| 150 | + vscroll.dummy.prototype = (ref$ = Object.create(Object.prototype), ref$.appendChild = function(n){ |
| 151 | + this.childNodes.splice(this.childNodes.length, 0, n); |
| 152 | + return this.root.appendChild(n); |
| 153 | + }, ref$.insertBefore = function(n, s){ |
| 154 | + this.childNodes.splice(this.childNodes.indexOf(s), 0, n); |
| 155 | + return this.root.insertBefore(n, s); |
| 156 | + }, ref$.removeChild = function(n){ |
| 157 | + var idx; |
| 158 | + if (!~(idx = this.childNodes.indexOf(n))) { |
| 159 | + return; |
| 160 | + } |
| 161 | + this.childNodes.splice(idx, 1); |
| 162 | + return this.root.removeChild(n); |
| 163 | + }, ref$); |
| 164 | + if (typeof module != 'undefined' && module !== null) { |
| 165 | + module.exports = vscroll; |
| 166 | + } else if (typeof window != 'undefined' && window !== null) { |
| 167 | + window.vscroll = vscroll; |
| 168 | + } |
| 169 | +}).call(this); |
0 commit comments