Last active
December 16, 2015 19:49
-
-
Save kaneel/5487338 to your computer and use it in GitHub Desktop.
resizeCollection, a tiny helper I've made so we can keep a trace of the resize events. Needs jQuery for its custom events + pub/sub model so you can have one resize event only that will trigger the resize-trigger inside resizeCollection.
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
;(function(root) { | |
/* | |
this module return methods for having resize collections bound to a timer; | |
use like this: | |
resizeCollection.register(function, ms) | |
If ms is null, will push the function to the 500ms timer. | |
If ms is given, will create a new resize timer. | |
*/ | |
var lastTimer = 2000, | |
baseTimer = 500, | |
collection = {500: [], 2000: []}, | |
timers = [], | |
map = [], | |
chains = {}, | |
Chain = function() {this.array = []}, | |
obj = null, | |
w = root.window, | |
jWin = $(w); | |
Chain.prototype.push = function(func) { | |
this.array.push(func); | |
return this; | |
} | |
Chain.prototype.execute = function() { | |
for (var i = 0, chain = this.array, max = chain.length; i < max; i++) { | |
chain[i](); | |
} | |
return this; | |
} | |
var removeFromCollection = function(func) { | |
// must REDO if needed; | |
// collection.splice(collection.indexOf(func), 1); | |
} | |
var resizeBehavior = function(ms) { | |
//console.log("resizing at: ", ms); | |
var func = null, msCollection = collection[ms], max = msCollection.length; | |
while(max--) if( func = msCollection[max], !!func && typeof func === "function") { | |
func(chains); | |
} | |
/*for (var i = 0; i < max; i++) if( func = msCollection[max], !!func && typeof func === "function") { | |
func(chains); | |
} */ | |
} | |
var pushToCollection = function(func, ms) { | |
if (!!ms && !collection[ms]) { | |
collection[ms] = []; | |
// create the resize behaviour according to the ms timer value | |
addResizeListener(ms); | |
} else { | |
ms = ms || baseTimer; | |
} | |
collection[ms].push(func); | |
func(); | |
} | |
w.resizeCollection = { | |
"methods": { | |
"register": pushToCollection, // register AND execute the function | |
"remove": removeFromCollection, | |
"chainer": { | |
"create": createChain, | |
"get": getChain | |
} | |
}, | |
"debug": function() { | |
console.log(collection); | |
} | |
}) | |
// make the first listener | |
addResizeListener(baseTimer); | |
addResizeListener(lastTimer); | |
var index = 0, indexOfMap = 0, indexOfMap = 0; | |
function addResizeListener(ms) { | |
indexOfMap = map.indexOf(ms) | |
if (!!~indexOfMap) { | |
index = indexOfMap; | |
} else { | |
index = map.length; // next index is obviously inserted so... it's the length of map :) | |
map.push(ms); | |
} | |
jWin.on("resize-trigger", function(index) { | |
return function(e, tempWidth, tempHeight, pre, after) { | |
var firstTimer = 0, lastTimer = 0, prevTimer = 0; | |
firstTimer = (function() { | |
var arr = map.sort(function(a, b) { | |
return a - b; | |
}); | |
return arr[0]; | |
}()); | |
if (ms == firstTimer) { | |
pre(); | |
} | |
clearTimeout(timers[index]); | |
timers[index] = setTimeout(function() { | |
resizeBehavior(ms); | |
lastTimer = (function() { | |
var arr = map.sort(function(a, b) { | |
return a + b; | |
}); | |
return arr[0]; | |
}()); | |
if (ms == lastTimer) { | |
after(); | |
} | |
}, ms); | |
} | |
}(index)); | |
} | |
function createChain(id) { | |
var chain = new Chain; | |
chains[id] = chain; | |
return chain; | |
} | |
function getChain(id) { | |
if (!!chains[id]) return chains[id]; | |
else return false; | |
} | |
}(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment