Skip to content

Commit

Permalink
v0.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasramo committed Sep 3, 2017
1 parent e5692a4 commit 7b1198d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 177 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ Muuri uses [Velocity](http://velocityjs.org/) for animating the grid items (posi
### 1. Get Muuri

Download from GitHub:
* [muuri.js](https://raw.githubusercontent.com/haltu/muuri/0.5.0/muuri.js) - for development (not minified, with comments).
* [muuri.min.js](https://raw.githubusercontent.com/haltu/muuri/0.5.0/muuri.min.js) - for production (minified, no comments).
* [muuri.js](https://raw.githubusercontent.com/haltu/muuri/0.4.1/muuri.js) - for development (not minified, with comments).
* [muuri.min.js](https://raw.githubusercontent.com/haltu/muuri/0.4.1/muuri.min.js) - for production (minified, no comments).

Or link directly via CDNJS:

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/muuri/0.5.0/muuri.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/muuri/0.5.0/muuri.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/muuri/0.4.1/muuri.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/muuri/0.4.1/muuri.min.js"></script>
```

Or install with [npm](https://www.npmjs.com/):
Expand Down
205 changes: 38 additions & 167 deletions muuri.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Muuri v0.5.0-dev
* Muuri v0.4.1
* https://github.com/haltu/muuri
* Copyright (c) 2015, Haltu Oy
*
Expand All @@ -22,13 +22,6 @@
* SOFTWARE.
*/

// TODO: Some performance issue on Firefox. When there are a lot of items and
// you try to hide a few items, the animation is really janky.
// TODO: Debounce drag handlers: https://developers.google.com/web/fundamentals/
// performance/rendering/debounce-your-input-handlers?hl=en
// TODO: Minimize layout thrashing.
// TODO: Make sure that the offsets are correct after append procedures.

(function (global, factory) {

var namespace = 'Muuri';
Expand Down Expand Up @@ -73,10 +66,6 @@
var typeString = 'string';
var typeNumber = 'number';

// Raf loop that can be used to organize DOM write and read operations
// optimally in the next animation frame.
var rafLoop = createRafLoop();

// Drag start predicate states.
var startPredicateInactive = 0;
var startPredicatePending = 1;
Expand Down Expand Up @@ -719,10 +708,8 @@

// If there are items let's get window's width/height for the layout
// animation optimization algorithm.
rafLoop.inspect(function () {
winWidth = global.innerWidth;
winHeight = global.innerHeight;
});
winWidth = global.innerWidth;
winHeight = global.innerHeight;

// If there are items let's position them.
for (i = 0; i < items.length; i++) {
Expand Down Expand Up @@ -1931,10 +1918,8 @@
animEnabled = !instant && !inst._skipNextLayoutAnimation && animDuration > 0;

// Process current layout callback queue with interrupted flag on if the
// item is currently positioning. Also cancel the possible layout animation
// init if it has not yet been called.
// item is currently positioning.
if (isPositioning) {
inst._cancelLayoutAnimationInit();
processQueue(inst._layoutQueue, true, inst);
}

Expand Down Expand Up @@ -1971,50 +1956,35 @@
// If animations are needed, let's dive in.
else {

// Set item as positioning if it is not already.
// Get item's current left and top values.
currentLeft = getTranslateAsFloat(element, 'x') - offsetLeft;
currentTop = getTranslateAsFloat(element, 'y') - offsetTop;

// If the item is already in correct position let's quit early.
if (inst._left === currentLeft && inst._top === currentTop) {
inst._stopLayout()._finishLayout();
return;
}

// Mark as positioning and add positioning class if necessary.
if (!isPositioning) {
inst._isPositioning = true;
addClass(element, settings.itemPositioningClass);
}

// Read the item's current left and top values in the next frame.
inst._layoutAnimateInitRead = rafLoop.inspect(function () {
inst._layoutAnimateInitRead = null;
currentLeft = getTranslateAsFloat(element, 'x') - offsetLeft;
currentTop = getTranslateAsFloat(element, 'y') - offsetTop;
});

// Apply the animation logic in the next frame after we have fetched the
// current left and top values.
inst._layoutAnimateInitWrite = rafLoop.modify(function () {

inst._layoutAnimateInitWrite = null;

// If the item is already in correct position let's quit early.
if (inst._left === currentLeft && inst._top === currentTop) {
inst._stopLayout()._finishLayout();
return;
// Animate.
inst._animate.start({
translateX: (currentLeft + offsetLeft) + 'px',
translateY: (currentTop + offsetTop) + 'px'
}, {
translateX: inst._left + offsetLeft + 'px',
translateY: inst._top + offsetTop + 'px'
}, {
duration: animDuration,
easing: animEasing,
onFinish: function () {
inst._finishLayout();
}

// Mark as positioning and add positioning class if necessary.
if (!isPositioning) {
addClass(element, settings.itemPositioningClass);
}

// Animate.
inst._animate.start({
translateX: (currentLeft + offsetLeft) + 'px',
translateY: (currentTop + offsetTop) + 'px'
}, {
translateX: inst._left + offsetLeft + 'px',
translateY: inst._top + offsetTop + 'px'
}, {
duration: animDuration,
easing: animEasing,
onFinish: function () {
inst._finishLayout();
}
});

});

}
Expand Down Expand Up @@ -2077,9 +2047,6 @@
return inst;
}

// Cancel animation init.
inst._cancelLayoutAnimationInit();

// Stop animation.
inst._animate.stop();

Expand All @@ -2098,31 +2065,6 @@

};

/**
* Cancel queued layout animation init operations.
*
* @protected
* @memberof Item.prototype
* @returns {Item}
*/
Item.prototype._cancelLayoutAnimationInit = function () {

var inst = this;

if (inst._layoutAnimateInitRead) {
rafLoop.remove(inst._layoutAnimateInitRead);
inst._layoutAnimateInitRead = null;
}

if (inst._layoutAnimateInitWrite) {
rafLoop.remove(inst._layoutAnimateInitWrite);
inst._layoutAnimateInitWrite = null;
}

return inst;

};

Item.prototype._show = function (instant, onFinish) {

var inst = this;
Expand Down Expand Up @@ -3198,10 +3140,8 @@

// Position the released item and get window's width/height for the layout
// animation optimization algorithm.
rafLoop.inspect(function () {
winWidth = global.innerWidth;
winHeight = global.innerHeight;
});
winWidth = global.innerWidth;
winHeight = global.innerHeight;
item._layout(false);

return release;
Expand Down Expand Up @@ -4170,10 +4110,6 @@
var yDiff;
var axis;

// Cancel queued raf inspect and modify.
drag._moveInspect && rafLoop.remove(drag._moveInspect);
drag._moveModify && rafLoop.remove(drag._moveModify);

// If item is not active, reset drag.
if (!item._isActive) {
drag.stop();
Expand Down Expand Up @@ -4208,20 +4144,16 @@
}

// Overlap handling.
drag._moveInspect = rafLoop.inspect(function () {
drag._moveInspect = null;
settings.dragSort && drag._checkSortOverlap();
});
settings.dragSort && drag._checkSortOverlap();

// Update element's translateX/Y values and emit dragMove event.
drag._moveModify = rafLoop.modify(function () {
drag._moveModify = null;
setStyles(element, {
transform: 'translateX(' + dragData.left + 'px) translateY(' + dragData.top + 'px)'
});
grid._emit(evDragMove, item, event);
// Update element's translateX/Y values.
setStyles(element, {
transform: 'translateX(' + dragData.left + 'px) translateY(' + dragData.top + 'px)'
});

// Emit dragMove event.
grid._emit(evDragMove, item, event);

return drag;

};
Expand Down Expand Up @@ -4575,67 +4507,6 @@

}

/**
* Returns a raf loop queue system that allows pushing callbacks to either
* the read queue or the write queue.
*
* @private
* @returns {Object}
*/
function createRafLoop() {

var raf = (global.requestAnimationFrame
|| global.webkitRequestAnimationFrame
|| global.mozRequestAnimationFrame
|| global.msRequestAnimationFrame
|| function (cb) {
return global.setTimeout(cb, 16);
}
).bind(global);
var reads = [];
var writes = [];

function addWrite(cb) {
writes.push(cb);
writes.length === 1 && !reads.length && raf(flush);
return cb;
}

function addRead(cb) {
reads.push(cb);
reads.length === 1 && !writes.length && raf(flush);
return cb;
}

function remove(cb) {
[writes, reads].forEach(function (queue) {
var index = queue.indexOf(cb);
index > -1 && queue.splice(index, 1);
});
}

function flush() {
if (reads.length || writes.length) {
var currentReads = reads.splice(0, reads.length);
var currentWrites = writes.splice(0, writes.length);
var i;
for (i = 0; i < currentReads.length; i++) {
currentReads[i]();
}
for (i = 0; i < currentWrites.length; i++) {
currentWrites[i]();
}
}
}

return {
modify: addWrite,
inspect: addRead,
remove: remove
};

}

/**
* Helpers - DOM utils
* *******************
Expand Down Expand Up @@ -5605,7 +5476,7 @@
*/

/*!
* muuriLayout v0.5.0-dev
* muuriLayout v0.4.1
* Copyright (c) 2016 Niklas Rämö <[email protected]>
* Released under the MIT license
*/
Expand Down
2 changes: 1 addition & 1 deletion muuri.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "muuri",
"version": "0.5.0-dev",
"version": "0.4.1",
"description": "Responsive, sortable, filterable and draggable grid layouts.",
"keywords": [
"grid",
Expand Down
2 changes: 1 addition & 1 deletion tests/grid-events/hideEnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
grid.hide(0, {layout: false, instant: true});
grid.on('hideEnd', function (items) {
assert.strictEqual(arguments.length, 1, 'callback: should have one argument');
assert.deepEqual(utils.sortItemsById(items), utils.sortItemsById(grid.getItems([1, 2])), 'callback: first argument should be an array of all the valid items that are were hidden');
assert.deepEqual(utils.sortItemsById(items), utils.sortItemsById(grid.getItems([0, 1, 2])), 'callback: first argument should be an array of all the items that were hidden');
teardown();
});
grid.hide([0, 1, 2], {layout: false});
Expand Down
2 changes: 1 addition & 1 deletion tests/grid-events/hideStart.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
grid.hide(0, {layout: false, instant: true});
grid.on('hideStart', function (items) {
assert.strictEqual(arguments.length, 1, 'callback: should have one argument');
assert.deepEqual(utils.sortItemsById(items).length, utils.sortItemsById(grid.getItems([1, 2])).length, 'callback: first argument should be an array of all the valid items that are about to be hidden');
assert.deepEqual(utils.sortItemsById(items).length, utils.sortItemsById(grid.getItems([0, 1, 2])).length, 'callback: first argument should be an array of all the items that are about to be hidden');
});
grid.hide([0, 1, 2], {layout: false});
teardown();
Expand Down
2 changes: 1 addition & 1 deletion tests/grid-events/showEnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

grid.on('showEnd', function (items) {
assert.strictEqual(arguments.length, 1, 'callback: should have one argument');
assert.deepEqual(utils.sortItemsById(items), utils.sortItemsById(grid.getItems([0, 1])), 'callback: first argument should be an array of all the valid items that are were shown');
assert.deepEqual(utils.sortItemsById(items), utils.sortItemsById(grid.getItems([0, 1, 2])), 'callback: first argument should be an array of all the items that were shown');
teardown();
});
grid.hide([0, 1], {layout: false, instant: true});
Expand Down
2 changes: 1 addition & 1 deletion tests/grid-events/showStart.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

grid.on('showStart', function (items) {
assert.strictEqual(arguments.length, 1, 'callback: should have one argument');
assert.deepEqual(utils.sortItemsById(items), utils.sortItemsById(grid.getItems([0, 1])), 'callback: first argument should be an array of all the valid items that are about to be shown');
assert.deepEqual(utils.sortItemsById(items), utils.sortItemsById(grid.getItems([0, 1, 2])), 'callback: first argument should be an array of all the items that are about to be shown');
});
grid.hide([0, 1], {layout: false, instant: true});
grid.show([0, 1, 2], {layout: false});
Expand Down

0 comments on commit 7b1198d

Please sign in to comment.