Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed jQuery dependency. #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions responsive-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,33 @@
// THE SOFTWARE.
//

// Messing witht prototype is problematic, maybe theres a simpler solution?
DOMTokenList.prototype.addClass = function(classes) {
var classes = classes.split(' '),
i = 0,
ii = classes.length;

for(i; i<ii; i++) {
this.add(classes[i]);
}
}

DOMTokenList.prototype.removeClass = function(classes) {
var classes = classes.split(' '),
i = 0,
ii = classes.length;

for(i; i<ii; i++) {
remove.add(classes[i]);
}
}


var ResponsiveElements = {
q: function ( selector ) {
var search = document.querySelectorAll( selector );
return search.length > 1 ? search : search[0];
},
elementsAttributeName: 'data-respond',
maxRefreshRate: 5,
defaults: {
Expand All @@ -36,8 +62,8 @@ var ResponsiveElements = {
var self = this;
$(function() {
self.el = {
window: $(window),
responsive_elements: $('[' + self.elementsAttributeName + ']')
window: window,
responsive_elements: q('[' + self.elementsAttributeName + ']')
};

self.events();
Expand Down Expand Up @@ -74,14 +100,14 @@ var ResponsiveElements = {
},
generateBreakpointsOnAllElements: function() {
var self = ResponsiveElements;
self.el.responsive_elements.each(function(i, _el) {
self.generateBreakpointsOnElement($(_el));
self.el.responsive_elements.forEach(function(_el, i) {
self.generateBreakpointsOnElement(q(_el));
});
},
generateBreakpointsOnElement: function(_el) {
var options_string = _el.attr(this.elementsAttributeName),
var options_string = _el.getAttribute(this.elementsAttributeName),
options = this.parseOptions(options_string) || this.defaults,
breakpoints = this.generateBreakpoints(_el.width(), options);
breakpoints = this.generateBreakpoints(_el.offsetWidth, options);

this.cleanUpBreakpoints(_el);
_el.addClass(breakpoints.join(' '));
Expand All @@ -107,21 +133,21 @@ var ResponsiveElements = {
var classes = breakpoints_string.split(/\s+/),
breakpointClasses = [];

$(classes).each(function(i, className) {
classes.forEach(function(className, i) {
if (className.match(/^gt\d+|lt\d+$/)) breakpointClasses.push(className);
});

return breakpointClasses;
},
cleanUpBreakpoints: function(_el) {
var classesToCleanup = this.parseBreakpointClasses(_el.attr('class'));
_el.removeClass(classesToCleanup.join(' '));
var classesToCleanup = this.parseBreakpointClasses(_el.getAttribute('class'));
_el.classList.removeMany(classesToCleanup.join(' '));
},
events: function() {
this.generateBreakpointsOnAllElements();

this.el.window.bind('resize', this.utils.debounce(
this.generateBreakpointsOnAllElements, this.maxRefreshRate));
this.el.window.addEventListener('resize', this.utils.debounce(
this.generateBreakpointsOnAllElements, this.maxRefreshRate), false);
},
utils: {
// Debounce is part of Underscore.js 1.5.2 http://underscorejs.org
Expand Down