Skip to content

Commit

Permalink
Prerender/img (#320)
Browse files Browse the repository at this point in the history
* fix: #195

* release: v1.0.84

* fix: 修复测试报错
  • Loading branch information
VenyXiong authored May 22, 2018
1 parent 8e86a85 commit 5dbdf45
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mip",
"version": "1.0.83",
"version": "1.0.84",
"description": "mobile instant page",
"main": "dist/mip.js",
"dependencies": {},
Expand Down
14 changes: 14 additions & 0 deletions src/components/mip-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,19 @@ define(function (require) {
return true;
};

/**
* Advance one screen loading
*
* @param {Object} offset Element offset
* @param {Object} viewportOffset Window offset
* @return {boolean}
*/
customElem.prototype.prerenderAllowed = function (offset, viewRect) {
var threshold = viewRect.height;

return viewRect.top + viewRect.height + threshold >= offset.top
&& offset.top + offset.height + threshold >= viewRect.top;
};

return customElem;
});
4 changes: 2 additions & 2 deletions src/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ define(function (require) {
*
* @return {boolean}
*/
proto.prerenderAllowed = function () {
return this.customElement.prerenderAllowed();
proto.prerenderAllowed = function (offset, viewport) {
return this.customElement.prerenderAllowed(offset, viewport);
};

/**
Expand Down
5 changes: 3 additions & 2 deletions src/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ define(function (require) {
for (var i in resources) {
// Compute the viewport state of current element.
// If current element`s prerenderAllowed returns `true` always set the state to be `true`.
var inViewport = resources[i].prerenderAllowed()
|| rect.overlapping(rect.getElementRect(resources[i]), viewportRect);
var elementRect = rect.getElementRect(resources[i]);
var inViewport = resources[i].prerenderAllowed(elementRect, viewportRect)
|| rect.overlapping(elementRect, viewportRect);
this.setInViewport(resources[i], inViewport);
}
}
Expand Down
12 changes: 11 additions & 1 deletion test/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,18 @@ define(function (require) {
sinon.stub(app, 'getResources', function () {
return {
MIP: {
prerenderAllowed: function () {
prerenderAllowed: function (offset, viewportRect) {
expect(offset).to.be.a('object').and.not.empty;
expect(viewportRect).to.be.a('object').and.not.empty;
return true;
},
getBoundingClientRect: function () {
return {
left: 0,
top: 0,
width: 0,
height: 0
};
}
}
};
Expand Down

0 comments on commit 5dbdf45

Please sign in to comment.