Skip to content

Instantly share code, notes, and snippets.

@MatthewDLudwig
Created December 26, 2018 08:40
Show Gist options
  • Save MatthewDLudwig/011ebf39a3956ed404b41d8d10c4e693 to your computer and use it in GitHub Desktop.
Save MatthewDLudwig/011ebf39a3956ed404b41d8d10c4e693 to your computer and use it in GitHub Desktop.
function wrapJ(par) {
let foundObj = null;
if (!par) {
return {
isWrappedJ : true,
length : function() { return 0; },
get : function(i) { return this; },
parent : function() { return this; },
addClass : function (name) { return this; },
removeClass : function (name) { return this; },
outerWidth : function () { return 0; },
outerHeight : function () { return 0; },
width : function () { return 0; },
height : function () { return 0; },
css : function (name, value = null) { return this; },
attr : function (name) { return this; },
children : function () { return this; },
each : function (fn) { return this; },
ready : function (fn) { return this; },
on : function (name, handler) { return this; },
val : function (it = null) { return this; }
};
} else if (typeof par === "string") {
let foundNodes = document.querySelectorAll(par);
if (foundNodes.length == 0) {
return {
isWrappedJ : true,
length : function() { return 0; },
get : function(i) { return this; },
parent : function() { return this; },
addClass : function (name) { return this; },
removeClass : function (name) { return this; },
outerWidth : function () { return 0; },
outerHeight : function () { return 0; },
width : function () { return 0; },
height : function () { return 0; },
css : function (name, value = null) { return this; },
attr : function (name) { return this; },
children : function () { return this; },
each : function (fn) { return this; },
ready : function (fn) { return this; },
on : function (name, handler) { return this; },
val : function (it = null) { return this; }
};
} else {
foundObj = foundNodes;
}
} else if ("isWrappedJ" in par) {
return par;
} else if (par === window) {
foundObj = [window];
} else if ("length" in par) {
foundObj = [];
for (let i = 0; i < par.length; i++) {
foundObj.push(par[i]);
}
} else {
foundObj = [par];
}
return {
isWrappedJ : true,
wrappedObj : foundObj,
length : function() {
return this.wrappedObj.length;
},
get : function(i) {
return wrapJ(this.wrappedObj[i]);
},
parent : function() {
return wrapJ(this.wrappedObj[0].parentNode);
},
addClass : function (name) {
this.wrappedObj[0].classList.add(name);
return this;
},
removeClass : function (name) {
this.wrappedObj[0].classList.remove(name);
return this;
},
outerWidth : function () {
return this.wrappedObj[0].offsetWidth;
},
outerHeight : function () {
return this.wrappedObj[0].offsetHeight;
},
width : function () {
return this.wrappedObj[0].offsetWidth;
},
height : function () {
return this.wrappedObj[0].offsetHeight;
},
css : function (name, value = null) {
if (value) {
this.wrappedObj[0].style[name] = value;
return this;
} else {
return getComputedStyle(this.wrappedObj[0])[name];
}
},
attr : function (name) {
return this.wrappedObj[0].getAttribute(name);
},
children : function () {
return wrapJ(this.wrappedObj[0].children);
},
each : function (fn) {
this.wrappedObj.forEach((el, i) => {
fn(wrapJ(el));
});
return this;
},
ready : function (fn) {
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
return this;
},
on : function (name, second, third) {
if (typeof second === "string") {
this.wrappedObj[0].addEventListener(name, function(event) {
let clickedElement = event.target;
let matchingChild = clickedElement.closest(second);
third(event, matchingChild);
});
} else {
this.wrappedObj[0].addEventListener(name, second);
}
return this;
},
val : function (it = null) {
if (it) {
this.wrappedObj[0].value = it;
return this;
} else {
return this.wrappedObj[0].value;
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment