Skip to content

Instantly share code, notes, and snippets.

@orenmizr
Created January 7, 2017 01:27
Show Gist options
  • Save orenmizr/850e7b645595e2e1fbae031b1e266eb4 to your computer and use it in GitHub Desktop.
Save orenmizr/850e7b645595e2e1fbae031b1e266eb4 to your computer and use it in GitHub Desktop.
extend
// merge two objects to one merged object;
var extend = function(defaults, options) {
var extended = {};
copyObj(extended, defaults);
copyObj(extended, options);
// var prop;
// for (prop in defaults) {
// if (Object.prototype.hasOwnProperty.call(defaults, prop)) {
// extended[prop] = defaults[prop];
// }
// }
// for (prop in options) {
// if (Object.prototype.hasOwnProperty.call(options, prop)) {
// extended[prop] = options[prop];
// }
// }
function copyObj(to, from) {
for (var key in from) {
if (Object.prototype.hasOwnProperty.call(from, key)) {
to[key] = from[key];
}
}
}
return extended;
};
// simple is element within frame
function isVisible(element) {
var rect = element.getBoundingClientRect();
return rect.top >= 0 && rect.left >= 0 && rect.bottom <= window.innerHeight && rect.right <= window.innerWidth;
}
var on = function(event, selector, handler, state) {
state.boxElements = document.querySelectorAll(selector); // throw error + change to element querySelectorAll
if (!state.boxElements) throw ("Error (on) - no box elements found");
state.cachedDom[selector] = state.boxElements;
[].forEach.call(state.boxElements, function(box) {
box.addEventListener(event, handler);
});
return true;
};
var off = function(event, selector, handler, state) {
var els = state.cachedDom[selector];
if (!els) throw ("Error (off) - no cached elements found");
[].forEach.call(els, function(box) {
box.removeEventListener(event, handler);
});
return true;
};
// underscore debounce cause it's late...
var debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this,
args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var pubsub = (function() {
var topics = {};
var hOP = topics.hasOwnProperty;
return {
subscribe: function(topic, listener) {
// Create the topic's object if not yet created
if (!hOP.call(topics, topic)) topics[topic] = [];
// Add the listener to queue
var index = topics[topic].push(listener) - 1;
// Provide handle back for removal of topic
return {
remove: function() {
delete topics[topic][index];
}
};
},
publish: function(topic, info) {
// If the topic doesn't exist, or there's no listeners in queue, just leave
if (!hOP.call(topics, topic)) return;
// Cycle through topics queue, fire!
topics[topic].forEach(function(item) {
item(info != undefined ? info : {});
});
}
};
})();
// flatten object to one level
var flatten = function(obj) {
var flattened = {};
for (var key in obj) {
if (!obj.hasOwnProperty(key)) continue;
if ((typeof obj[key]) == 'object') {
var _flattened = flatten(obj[key]);
for (var _key in _flattened) {
if (!_flattened.hasOwnProperty(_key)) continue;
flattened[key + '.' + _key] = _flattened[_key];
}
} else {
flattened[key] = obj[key];
}
}
return flattened;
};
module.exports = {
extend: extend,
flatten: flatten,
isVisible: isVisible,
on: on,
off: off,
debounce: debounce,
pubsub: pubsub
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment