Permalink
Browse files

add faster forEach, minor code style adjustments, eslintrc

  • Loading branch information...
1 parent c2ca9e9 commit cafea535340cc360eb9fbedb9f83cfdba080ff9b @voronianski voronianski committed Sep 9, 2015
Showing with 62 additions and 23 deletions.
  1. +1 โˆ’1 .editorconfig
  2. +23 โˆ’0 .eslintrc
  3. +34 โˆ’18 index.js
  4. +2 โˆ’2 minigrid.min.js
  5. +1 โˆ’1 package.json
  6. +1 โˆ’1 test.js
View
@@ -9,4 +9,4 @@ trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
-trim_trailing_whitespace = false
+trim_trailing_whitespace = false
View
@@ -0,0 +1,23 @@
+// http://eslint.org/docs/rules
+{
+ "env": {
+ "browser": true,
+ "node": true
+ },
+ "rules": {
+ "strict": 0,
+ "quotes": [2, "single"],
+ "no-underscore-dangle": 0,
+ "no-unused-vars": 1,
+ "no-unused-expressions": 0,
+ "new-cap": 0,
+ "no-extra-boolean-cast": 0,
+ "yoda": 0,
+ "no-empty": 0,
+ "no-use-before-define": 0,
+ "camelcase": 0
+ },
+ "globals": {
+ "define": true
+ }
+}
View
@@ -1,5 +1,5 @@
-/* @license minigrid v1.6.3 - minimal cascading grid layout http://alves.im/minigrid */
-(function(exports){
+/* @license minigrid v1.6.4 - minimal cascading grid layout http://alves.im/minigrid */
+(function (exports) {
'use strict';
@@ -16,16 +16,24 @@
if (style[prop = 'transform'] !== undefined) {
transformProp = prop;
}
- } ());
+ }());
- function minigrid(gridContainer, itemSelector, gutter, animate, done) {
- var forEach = Array.prototype.forEach;
+ function forEach (arr, cb) {
+ if (arr) {
+ for (var i = 0, len = arr.length; i < len; i++) {
+ if (arr[i]) {
+ cb(arr[i], i, arr);
+ }
+ }
+ }
+ }
+
+ function minigrid (gridContainer, itemSelector, gutter, animate, done) {
var containerEle = gridContainer instanceof Node ? gridContainer : document.querySelector(gridContainer);
if (!containerEle) { return false; }
var itemsNodeList = containerEle.querySelectorAll(itemSelector);
if (itemsNodeList.length === 0) { return false; }
- gutter = (typeof gutter === "number" && isFinite(gutter)
- && Math.floor(gutter) === gutter) ? gutter : 6;
+ gutter = (typeof gutter === 'number' && isFinite(gutter) && Math.floor(gutter) === gutter) ? gutter : 6;
containerEle.style.width = '';
var containerWidth = containerEle.getBoundingClientRect().width;
var firstChildWidth = itemsNodeList[0].getBoundingClientRect().width + gutter;
@@ -35,21 +43,26 @@
containerEle.style.width = containerWidth;
containerEle.style.position = 'relative';
- for (var itemsGutter = [], itemsPosX = [], g = 0; g < cols; g++) {
+ var itemsGutter = [];
+ var itemsPosX = [];
+ forEach(cols, function (col, g) {
itemsPosX.push(g * firstChildWidth + gutter);
itemsGutter.push(gutter);
- }
+ });
- forEach.call(itemsNodeList, function(item){
- var itemIndex = itemsGutter.slice(0).sort(function (a, b) {
- return a - b;
- }).shift();
+ forEach(itemsNodeList, function (item) {
+ var itemIndex = itemsGutter
+ .slice(0)
+ .sort(function (a, b) {
+ return a - b;
+ })
+ .shift();
itemIndex = itemsGutter.indexOf(itemIndex);
var posX = itemsPosX[itemIndex];
var posY = itemsGutter[itemIndex];
item.style.position = 'absolute';
if (!animate && transformProp) {
- item.style[transformProp] = 'translate3D(' + posX + 'px,' + posY + 'px,0)';
+ item.style[transformProp] = 'translate3D(' + posX + 'px,' + posY + 'px, 0)';
}
itemsGutter[itemIndex] += item.getBoundingClientRect().height + gutter;
count = count + 1;
@@ -58,13 +71,16 @@
}
});
- var containerHeight = itemsGutter.slice(0).sort(function (a, b) {
- return a - b;
- }).pop();
+ var containerHeight = itemsGutter
+ .slice(0)
+ .sort(function (a, b) {
+ return a - b;
+ })
+ .pop();
containerEle.style.height = containerHeight + 'px';
- if (typeof done === 'function'){
+ if (typeof done === 'function') {
done(itemsNodeList);
}
}
View
@@ -1,2 +1,2 @@
-/* @license minigrid v1.6.3 - minimal cascading grid layout http://alves.im/minigrid */
-!function(t){"use strict";function e(t,e,i,o,r){var u=Array.prototype.forEach,f=t instanceof Node?t:document.querySelector(t);if(!f)return!1;var s=f.querySelectorAll(e);if(0===s.length)return!1;i="number"==typeof i&&isFinite(i)&&Math.floor(i)===i?i:6,f.style.width="";var l=f.getBoundingClientRect().width,a=s[0].getBoundingClientRect().width+i,d=Math.max(Math.floor((l-i)/a),1),c=0;l=a*d+i+"px",f.style.width=l,f.style.position="relative";for(var p=[],h=[],y=0;d>y;y++)h.push(y*a+i),p.push(i);u.call(s,function(t){var e=p.slice(0).sort(function(t,e){return t-e}).shift();e=p.indexOf(e);var r=h[e],u=p[e];return t.style.position="absolute",!o&&n&&(t.style[n]="translate3D("+r+"px,"+u+"px,0)"),p[e]+=t.getBoundingClientRect().height+i,c+=1,o?o(t,r,u,c):void 0});var m=p.slice(0).sort(function(t,e){return t-e}).pop();f.style.height=m+"px","function"==typeof r&&r(s)}var n;!function(){var t,e=document.createElement("a").style;void 0!==e[t="webkitTransform"]&&(n=t),void 0!==e[t="msTransform"]&&(n=t),void 0!==e[t="transform"]&&(n=t)}(),"function"==typeof define&&define.amd?define(function(){return e}):"undefined"!=typeof module&&module.exports?module.exports=e:t.minigrid=e}(this);
+/* @license minigrid v1.6.4 - minimal cascading grid layout http://alves.im/minigrid */
+!function(t){"use strict";function e(t,e){if(t)for(var n=0,i=t.length;i>n;n++)t[n]&&e(t[n],n,t)}function n(t,n,o,r,u){var f=t instanceof Node?t:document.querySelector(t);if(!f)return!1;var s=f.querySelectorAll(n);if(0===s.length)return!1;o="number"==typeof o&&isFinite(o)&&Math.floor(o)===o?o:6,f.style.width="";var l=f.getBoundingClientRect().width,a=s[0].getBoundingClientRect().width+o,d=Math.max(Math.floor((l-o)/a),1),c=0;l=a*d+o+"px",f.style.width=l,f.style.position="relative";var h=[],p=[];e(d,function(t,e){p.push(e*a+o),h.push(o)}),e(s,function(t){var e=h.slice(0).sort(function(t,e){return t-e}).shift();e=h.indexOf(e);var n=p[e],u=h[e];return t.style.position="absolute",!r&&i&&(t.style[i]="translate3D("+n+"px,"+u+"px, 0)"),h[e]+=t.getBoundingClientRect().height+o,c+=1,r?r(t,n,u,c):void 0});var v=h.slice(0).sort(function(t,e){return t-e}).pop();f.style.height=v+"px","function"==typeof u&&u(s)}var i;!function(){var t,e=document.createElement("a").style;void 0!==e[t="webkitTransform"]&&(i=t),void 0!==e[t="msTransform"]&&(i=t),void 0!==e[t="transform"]&&(i=t)}(),"function"==typeof define&&define.amd?define(function(){return n}):"undefined"!=typeof module&&module.exports?module.exports=n:t.minigrid=n}(this);
View
@@ -1,6 +1,6 @@
{
"name": "minigrid",
- "version": "1.6.3",
+ "version": "1.6.4",
"description": "Minimal 2kb zero dependency cascading grid layout",
"main": "index.js",
"scripts": {
View
@@ -25,4 +25,4 @@ test('minigrid done callback', function(t){
t.equal(nodeList.length > 0, true, nodeList.length + ' grid items');
t.end();
});
-});
+});

0 comments on commit cafea53

Please sign in to comment.