Skip to content

Commit 371a5a0

Browse files
committed
- fix bug: tweak execution order of host in ld-each initialization
- add @loadingio/vscroll package for demonstration
1 parent 6905d0b commit 371a5a0

File tree

15 files changed

+248
-35
lines changed

15 files changed

+248
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Logs
22

3+
## v0.2.5
4+
5+
- fix bug: tweak execution order of host in ld-each initialization
6+
37
## v0.2.4
48

59
- support virtual container

dist/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
}).filter(function(it){
142142
return !in$(it, eachesNodes);
143143
}).map(function(n){
144-
var name, c, i, ret, that, p;
144+
var name, c, i, ret, p, that;
145145
if (!n.parentNode) {
146146
return null;
147147
}
@@ -155,19 +155,19 @@
155155
c = n.parentNode;
156156
i = Array.from(c.childNodes).indexOf(n);
157157
ret = {
158-
container: (that = this$.handler[name].host) ? new that({
159-
root: c
160-
}) : c,
161158
idx: i,
162159
node: n,
163160
name: name,
164161
nodes: []
165162
};
166-
p = document.createComment(" " + this$.ld + "-each=" + ret.name + " ");
167-
p._data = ret;
163+
p = document.createComment(" " + this$.ld + "-each=" + name + " ");
168164
c.insertBefore(p, n);
169-
ret.proxy = p;
170165
c.removeChild(n);
166+
p._data = ret;
167+
ret.proxy = p;
168+
ret.container = (that = this$.handler[name].host) ? new that({
169+
root: c
170+
}) : c;
171171
return ret;
172172
}).filter(function(it){
173173
return it;

dist/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 41 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "MIT",
55
"main": "dist/index.js",
66
"description": "view template micro framework",
7-
"version": "0.2.3",
7+
"version": "0.2.5",
88
"files": [
99
"dist/**/*"
1010
],
@@ -24,6 +24,7 @@
2424
"devDependencies": {
2525
"@loadingio/bootstrap.ext": "^0.0.1",
2626
"@loadingio/ldquery": "^2.2.0",
27+
"@loadingio/vscroll": "^0.0.1",
2728
"@zbryikt/template": "^2.3.22",
2829
"bootstrap": "^4.5.2",
2930
"deploy": "github:zbryikt/deploy",
@@ -36,7 +37,8 @@
3637
"modules": [
3738
"bootstrap",
3839
"@loadingio/bootstrap.ext",
39-
"@loadingio/ldquery"
40+
"@loadingio/ldquery",
41+
"@loadingio/vscroll"
4042
]
4143
}
4244
}

src/ldview.ls

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,13 @@ ldview.prototype = Object.create(Object.prototype) <<< do
7676
if !@handler[name] => return null
7777
c = n.parentNode
7878
i = Array.from(c.childNodes).indexOf(n)
79-
ret = {
80-
container: if @handler[name].host => new that(root: c) else c
81-
idx: i, node: n, name: name, nodes: []
82-
}
83-
p = document.createComment " #{@ld}-each=#{ret.name} "
84-
p._data = ret
79+
ret = { idx: i, node: n, name: name, nodes: [] }
80+
p = document.createComment " #{@ld}-each=#{name} "
8581
c.insertBefore p, n
86-
ret.proxy = p
8782
c.removeChild n
83+
p._data = ret
84+
ret.proxy = p
85+
ret.container = if @handler[name].host => new that(root: c) else c
8886
return ret
8987
.filter -> it
9088
# TODO

web/.view/long/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/src/pug/long/index.ls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ view = new ldview do
99
root: '[ld-scope=ldview]'
1010
handler: do
1111
item: do
12-
host: virtual-container-clustered
12+
host: vscroll.fixed
1313
list: ->
1414
#start = Math.floor(Math.random!*data.length)
1515
#end = ( 1 + start + Math.floor((data.length - start) * Math.random!)) <? data.length

web/src/pug/long/index.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ html
2525
.desc(ld="description")
2626
.text-muted.text-sm(ld="date")
2727
+script("/assets/lib/@loadingio/ldquery/main/ldq.min.js")
28+
+script("/assets/lib/@loadingio/vscroll/main/index.js")
2829
+script("/assets/lib/ldview/dev/index.js")
29-
+script("/js/container.js")
3030
script: include:lsc index.ls
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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

Comments
 (0)