-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 620fac4
Showing
11 changed files
with
1,052 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 David Toews | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Layout Queue | ||
|
||
This library was created to eliminate race conditions when multiple JavaScript applied style changes need to occur on window resize. By adding actions to the queue it can be assured that each action will be called in the order they were added every time the queue is triggerd. By default the queue will execute on window load and window resize, but additional triggers can be added, for example on the loading of ajax content that affects the layout. | ||
|
||
## Installation | ||
|
||
Install via npm, or copy bundle.js to your project. | ||
|
||
## Use | ||
|
||
Add a function to the queue with the `add()` method, by passing in the function and an array of it's argument. | ||
|
||
LayoutQueue.add(nameOfYourFunction, [argument1, argument2]); | ||
|
||
### Adding other triggers | ||
|
||
If you need the queue to execute on other events, for example when loading ajax content, or when an iframe is loaded, the `trigger()` method can be used. Example: | ||
|
||
document.querySelectorAll('iframe').forEach(function(element) { | ||
element.addEventListener('load', function() { | ||
LayoutQueue.trigger(); | ||
}); | ||
}); | ||
|
||
### Additional methods | ||
|
||
LayoutQueue.list() // Returns an array of queued functions. | ||
LayoutQueue.clear() // Empties the queue. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
|
||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
|
||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) | ||
/******/ return installedModules[moduleId].exports; | ||
|
||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ i: moduleId, | ||
/******/ l: false, | ||
/******/ exports: {} | ||
/******/ }; | ||
|
||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
|
||
/******/ // Flag the module as loaded | ||
/******/ module.l = true; | ||
|
||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
|
||
|
||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
|
||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
|
||
/******/ // identity function for calling harmony imports with the correct context | ||
/******/ __webpack_require__.i = function(value) { return value; }; | ||
|
||
/******/ // define getter function for harmony exports | ||
/******/ __webpack_require__.d = function(exports, name, getter) { | ||
/******/ if(!__webpack_require__.o(exports, name)) { | ||
/******/ Object.defineProperty(exports, name, { | ||
/******/ configurable: false, | ||
/******/ enumerable: true, | ||
/******/ get: getter | ||
/******/ }); | ||
/******/ } | ||
/******/ }; | ||
|
||
/******/ // getDefaultExport function for compatibility with non-harmony modules | ||
/******/ __webpack_require__.n = function(module) { | ||
/******/ var getter = module && module.__esModule ? | ||
/******/ function getDefault() { return module['default']; } : | ||
/******/ function getModuleExports() { return module; }; | ||
/******/ __webpack_require__.d(getter, 'a', getter); | ||
/******/ return getter; | ||
/******/ }; | ||
|
||
/******/ // Object.prototype.hasOwnProperty.call | ||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | ||
|
||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
|
||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 1); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
|
||
"use strict"; | ||
|
||
|
||
var LayoutQueue = (function () { | ||
var queue = []; | ||
|
||
function processQueue() { | ||
queue.forEach(function(task) { | ||
task.action.apply(this, task.args); | ||
}); | ||
} | ||
|
||
window.addEventListener('load', function(){ | ||
processQueue(); | ||
}); | ||
|
||
window.addEventListener('resize', function() { | ||
processQueue(); | ||
}); | ||
|
||
return { | ||
add: function (action, args) { | ||
queue.push({ | ||
action: action, | ||
args: args | ||
}); | ||
}, | ||
trigger: function () { | ||
processQueue(); | ||
}, | ||
list: function () { | ||
return queue; | ||
}, | ||
clear: function () { | ||
queue = []; | ||
} | ||
}; | ||
})(); | ||
|
||
module.exports = LayoutQueue; | ||
|
||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
|
||
window.LayoutQueue = __webpack_require__(0); | ||
|
||
/***/ }) | ||
/******/ ]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!doctype html> | ||
<html class="no-js" lang=""> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="x-ua-compatible" content="ie=edge"> | ||
<title>Layout Queue</title> | ||
|
||
</head> | ||
<body> | ||
|
||
<h1>Layout Queue</h1> | ||
|
||
<p>The actions assigned to the queue will be executed in the order they have been added. By default the queue will be executed on window load and on window resize. For this page, the queue will also be executed when the maps iframe loads.</p> | ||
<p>Two functions have been assigned to the queue on this page. One which waits half a second and then outputs "hello" to the textarea below. And one which immediately outputs "world" to the text area below.</p> | ||
<p>You can trigger the queue by resizing the browser window (or changing the orientation of a mobile device). Note that though the "hello" function takes longer, it always executes before the "world" function.</p> | ||
|
||
<textarea style="height: 200px;"></textarea> | ||
<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m12!1m3!1d187667.68211669664!2d-114.03672458087165!3d51.037060414667884!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!5e0!3m2!1sen!2sca!4v1485455760909" style="height: 200px;"></iframe> | ||
|
||
|
||
<script src="bundle.js"></script> | ||
<script> | ||
function print(text) { | ||
document.querySelector('textarea').value = document.querySelector('textarea').value + text; | ||
} | ||
|
||
function wait(ms) { | ||
var start = Date.now(), | ||
now = start; | ||
while (now - start < ms) { | ||
now = Date.now(); | ||
} | ||
} | ||
|
||
function delayedLog(text) { | ||
wait(500); | ||
print(text); | ||
} | ||
|
||
LayoutQueue.add(delayedLog, ['hello\n']); | ||
LayoutQueue.add(print, ['world\n']); | ||
console.log(LayoutQueue.list()); | ||
|
||
document.querySelectorAll('iframe').forEach(function(element) { | ||
element.addEventListener('load', function() { | ||
LayoutQueue.trigger(); | ||
}); | ||
}); | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "layout-queue", | ||
"version": "0.1.0", | ||
"description": "A queue for javascript actions that affect layout, which need to execute on browser window resize and need to execute in a specific order.", | ||
"main": "src/LayoutQueue.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"keywords": [ | ||
"layout", | ||
"queue", | ||
"window", | ||
"resize" | ||
], | ||
"author": "Dave Toews (http://davejtoews.com)", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"jsdom": "^9.9.1", | ||
"jsdom-global": "^2.1.1", | ||
"mocha": "^3.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"use strict"; | ||
|
||
var LayoutQueue = (function () { | ||
var queue = []; | ||
|
||
function processQueue() { | ||
queue.forEach(function(task) { | ||
task.action.apply(this, task.args); | ||
}); | ||
} | ||
|
||
window.addEventListener('load', function(){ | ||
processQueue(); | ||
}); | ||
|
||
window.addEventListener('resize', function() { | ||
processQueue(); | ||
}); | ||
|
||
return { | ||
add: function (action, args) { | ||
queue.push({ | ||
action: action, | ||
args: args | ||
}); | ||
}, | ||
trigger: function () { | ||
processQueue(); | ||
}, | ||
list: function () { | ||
return queue; | ||
}, | ||
clear: function () { | ||
queue = []; | ||
} | ||
}; | ||
})(); | ||
|
||
module.exports = LayoutQueue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
window.LayoutQueue = require('./LayoutQueue.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require('jsdom-global')(); | ||
var assert = require('chai').assert; | ||
var LayoutQueue = require('../src/LayoutQueue.js'); | ||
|
||
describe('LayoutQueue', function() { | ||
var i = 0; | ||
var counter = function() { i++; } | ||
|
||
it('should start with empty queue', function() { | ||
assert.equal(LayoutQueue.list().length, 0); | ||
}); | ||
it('should add an item to queue', function() { | ||
LayoutQueue.add(console.log, ['argument']); | ||
var list = LayoutQueue.list(); | ||
assert.equal(list[0].action, console.log); | ||
assert.equal(list[0].args[0], 'argument'); | ||
}); | ||
it('should clear an item from queue', function() { | ||
LayoutQueue.clear(); | ||
assert.equal(LayoutQueue.list().length, 0); | ||
}); | ||
it('should process queue on load', function() { | ||
LayoutQueue.add(counter, []); | ||
window.dispatchEvent(new Event('load')); | ||
assert.equal(i, 1); | ||
}); | ||
it('should process queue on resize', function() { | ||
window.dispatchEvent(new Event('resize')); | ||
assert.equal(i, 2); | ||
}); | ||
it('should process queue on trigger', function() { | ||
LayoutQueue.trigger(); | ||
assert.equal(i, 3); | ||
}); | ||
}); |
Oops, something went wrong.