Skip to content

Commit

Permalink
refactor(infiniteGrid): remove es5 polyfill (naver#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
sculove committed Sep 2, 2016
1 parent cfd8764 commit 62c3cca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
76 changes: 44 additions & 32 deletions src/infiniteGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
this._reset();
this._refreshViewport();
if (this.el.children.length > 0) {
this.items = this._itemize([].slice.apply(this.el.children), this.options.defaultGroupKey, true);
this.items = this._itemize($.makeArray(this.el.children), this.options.defaultGroupKey, true);
this.layout(this.items, true);
}

Expand Down Expand Up @@ -205,7 +205,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
return {
prop: data,
options: $.extend({}, this.options),
items: this.items.map(function(v) {
items: $.map(this.items, function(v) {
var clone = $.extend({}, v);
delete clone.el;
return clone;
Expand All @@ -229,7 +229,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
this.el.style.cssText = status.cssText;
this.el.innerHTML = status.html;
$.extend(this, status.prop);
this.items = [].slice.apply(this.el.children).map(function(v, i) {
this.items = $.map(this.el.children, function(v, i) {
status.items[i].el = v;
return status.items[i];
});
Expand Down Expand Up @@ -261,7 +261,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
* @return {Array} groupKeys
*/
getGroupKeys: function() {
return this.items.map(function(v) {
return $.map(this.items, function(v) {
return v.groupKey;
});
},
Expand All @@ -275,24 +275,29 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
items = items || this.items;
isRefresh = typeof isRefresh === "undefined" ? true : isRefresh;
this._isProcessing = true;
isRefresh && (items = items.map(function(v) {
isRefresh && (items = $.map(items, function(v) {
v.isAppend = true;
return v;
}));
this._waitResource(items, isRefresh);
return this;
},
_layoutItems: function(items) {
items.map(function(v) {
v.position = this._getItemLayoutPosition(v);
return v;
}, this).forEach(function(v) {
if (v.el) {
var style = v.el.style;
style.left = v.position.x + "px";
style.top = v.position.y + "px";
}
});
var self = this;

// for performance
$.each(
$.map(items, function(v) {
v.position = self._getItemLayoutPosition(v);
return v;
}),
function(i, v) {
if (v.el) {
var style = v.el.style;
style.left = v.position.x + "px";
style.top = v.position.y + "px";
}
});
},
/**
* Append elements
Expand Down Expand Up @@ -360,7 +365,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
_getTopItem: function() {
var item = null;
var min = Infinity;
this._getColItems(false).forEach(function(v) {
$.each(this._getColItems(false), function(i, v) {
if (v && v.position.y < min) {
min = v.position.y;
item = v;
Expand All @@ -384,7 +389,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
_getBottomItem: function() {
var item = null;
var max = -Infinity;
this._getColItems(true).forEach(function(v) {
$.each(this._getColItems(true), function(i, v) {
if (v && v.position.y + v.size.height > max) {
max = v.position.y + v.size.height;
item = v;
Expand Down Expand Up @@ -462,7 +467,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
var elements = $elements.toArray();
var $cloneElements = $(elements);
var dummy = -this._clientHeight + "px";
elements.map(function(v) {
$.each(elements, function(i, v) {
v.style.position = "absolute";
v.style.top = dummy;
});
Expand All @@ -476,7 +481,6 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
this.isRecycling() && this._adjustRange(isAppend, $cloneElements);

this.$el[isAppend ? "append" : "prepend"]($cloneElements);

this.layout(items, false);
},
_waitResource: function(items, isRefresh) {
Expand All @@ -485,7 +489,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
var callback = function() {
if (self._isProcessing) {
if (isRefresh || !self._appendCols.length) {
items.forEach(function(v) {
$.each(items, function(i, v) {
v.el.style.position = "absolute";
});
self._measureColumns();
Expand Down Expand Up @@ -518,7 +522,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
}

// @todo improve performance
targets.forEach(function(v) {
$.each(targets, function(i, v) {
idx = $elements.index(v.el);
if (idx !== -1) {
$elements.splice(idx, 1);
Expand Down Expand Up @@ -572,7 +576,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
return false;
}
var y = this._updateCols(); // for prepend
this.items.forEach(function(v) {
$.each(this.items, function(i, v) {
v.position.y -= y;
applyDom && (v.el.style.top = v.position.y + "px");
});
Expand Down Expand Up @@ -611,7 +615,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
},
_checkImageLoaded: function() {
return this.$el.find("img").filter(function(k, v) {
if (v.nodeType && ([1,9,11].indexOf(v.nodeType) !== -1)) {
if (v.nodeType && ($.inArray(v.nodeType, [1,9,11]) !== -1)) {
return !v.complete;
}
}).toArray();
Expand All @@ -624,11 +628,18 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
checkCount <= 0 && callback && callback();
};
var $el;
needCheck.forEach(function(v) {
var self = this;
$.each(needCheck, function(i, v) {
$el = $(v);
this._isIE10lower && $el.attr("src", $el.attr("src"));

// for IE10 lower
if (self._isIE10lower) {
var url = v.getAttribute("src");
v.setAttribute("src", "");
v.setAttribute("src", url);
}
$el.on("load error", onCheck);
}, this);
});
},
_measureColumns: function() {
this.el.style.width = null;
Expand All @@ -645,9 +656,10 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
},
_resetCols: function(count) {
count = typeof count === "undefined" ? 0 : count;
var arr = Array.apply(null, new Array(count)).map(function() {
return 0;
});
var arr = [];
while (count--) {
arr.push(0);
}
this._appendCols = arr.concat();
this._prependCols = arr.concat();
},
Expand Down Expand Up @@ -689,7 +701,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
return base;
},
_getMinY: function(items) {
return Math.min.apply(Math, items.map(function(v) {
return Math.min.apply(Math, $.map(items, function(v) {
return v ? v.position.y : 0;
}));
},
Expand All @@ -716,7 +728,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
return colItems;
},
_itemize: function(elements, groupKey, isAppend) {
return elements.map(function(v) {
return $.map(elements, function(v) {
return {
el: v,
position: {
Expand All @@ -742,7 +754,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
var y = Math[isAppend ? "min" : "max"].apply(Math, cols);
var shortColIndex;
if (isAppend) {
shortColIndex = cols.indexOf(y);
shortColIndex = $.inArray(y, cols);
} else {
var i = cols.length;
while (i-- >= 0) {
Expand Down
3 changes: 3 additions & 0 deletions test/unit/infiniteGrid.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, target-densitydpi=medium-dpi">

<link rel="stylesheet" href="../../bower_components/qunit/qunit/qunit.css">
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.min.js"></script>
<![endif]-->
<!-- user css start -->
<style type="text/css">
body,p,h1,h2,h3,h4,h5,h6,ul,ol,li,dl,dt,dd,table,th,td,form,fieldset,legend,input,textarea,button,select {
Expand Down

0 comments on commit 62c3cca

Please sign in to comment.