Skip to content

Instantly share code, notes, and snippets.

@fmarcia
Last active October 26, 2017 18:04
Show Gist options
  • Save fmarcia/9353db5af997ba69bc54 to your computer and use it in GitHub Desktop.
Save fmarcia/9353db5af997ba69bc54 to your computer and use it in GitHub Desktop.
// Tiny jQuery-like toolbox
// Author: Franck Marcia
/*global document,window,XMLHttpRequest*/
var $ = (function () {
"use strict";
function Query(selector, context, content) {
if (selector !== void 0) {
if (selector.constructor === String) {
content = [].slice.call((context || document).querySelectorAll(selector));
} else if (selector.nodeType) {
content = [ selector ];
}
}
[].push.apply(this, content || []);
}
function query(selector, context) {
return new Query(selector, context);
}
function explode(str) {
if (str === void 0 || str.constructor !== String) {
return [];
}
return str.replace(/(^\s+|\s+$)/g, "").split(/\s+/);
}
function delegate(selector, handler) {
return function (event) {
event = event || window.event;
var target = event.target || event.srcElement;
var elt = target.nodeType == 3 ? target.parentNode : target;
if ($(elt).is(selector)) {
handler.apply(elt, event);
}
};
}
Query.prototype = query.prototype = {
length: 0,
addClass: function (name) {
return this.each(function (elt) {
var c = explode(elt.className);
if (c.indexOf(name) === -1) {
c.push(name);
elt.className = c.join(" ");
}
});
},
append: function (data) {
if (data.constructor === String) {
return this.each(function (elt) {
elt.insertAdjacentHTML("beforeend", data);
});
}
if (data.nodeType) {
return this.each(function (elt) {
elt.appendChild(data.cloneNode(true));
});
}
return this;
},
attr: function (name, value) {
if (value === void 0) {
if (this[0]) {
return this[0].getAttribute(name);
}
return void 0;
}
return this.each(function (elt) {
elt.setAttribute(name, value);
});
},
css: function (name, value) {
if (value === void 0) {
return this[0] ? this[0].style[name] : void 0;
}
return this.each(function (elt) {
elt.style[name] = value;
});
},
each: function (handler) {
[].forEach.call(this, function (elt, i) {
return handler.call(elt, elt, i) !== false;
});
return this;
},
empty: function () {
return this.each(function (elt) {
elt.innerHTML = "";
});
},
find: function (selector) {
if (this[0]) {
return query(selector, this[0]);
}
return query();
},
hasClass: function (name) {
//
return explode(this[0].className).indexOf(name) !== -1;
},
html: function (content) {
if (content === void 0) {
if (this[0]) {
return this[0].innerHTML;
}
return void 0;
}
return this.each(function (elt) {
elt.innerHTML = content;
});
},
is: function (selector, context) {
if (this[0]) {
return [].indexOf.call(query(selector, context), this[0]) > -1;
}
return false;
},
off: function (events, selector, handler) {
events = explode(events || "");
if (handler === void 0) {
return this.each(function (elt) {
events.forEach(function (event) {
elt.removeEventListener(event, selector, false);
});
});
}
return this.each(function (elt) {
events.forEach(function (event) {
elt.removeEventListener(event, delegate(selector, handler), false);
});
});
},
on: function (events, selector, handler) {
events = explode(events || "");
if (handler === void 0) {
return this.each(function (elt) {
events.forEach(function (event) {
elt.addEventListener(event, selector, false);
});
});
}
return this.each(function (elt) {
events.forEach(function (event) {
elt.addEventListener(event, delegate(selector, handler), false);
});
});
},
parent: function () {
if (this[0] && this[0].parentNode) {
return query(this[0].parentNode);
}
return query();
},
remove: function () {
return this.each(function (elt) {
if (elt.parentNode) {
elt.parentNode.removeChild(elt);
}
});
},
removeAttr: function (name) {
return this.each(function (elt) {
elt.removeAttribute(name);
});
},
removeClass: function (target) {
return this.each(function (elt) {
var c = explode(elt.className).filter(function (name) {
return name !== target;
});
elt.className = c.join(" ");
});
},
val: function (value) {
if (value === void 0) {
if (this[0]) {
return this[0].value;
}
return void 0;
}
return this.each(function (elt) {
elt.value = value;
});
}
};
query.ajax = function (settings) {
var request = new XMLHttpRequest();
request.withCredentials = settings.credentials;
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200) {
if (settings.success) {
settings.success(request.responseText);
}
} else if (settings.failure) {
settings.failure(request);
}
}
};
var method = settings.method || "POST";
request.open(method, settings.url, true);
if (method === "POST") {
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
request.setRequestHeader("Connection", "close");
request.send(settings.body || "");
};
return query;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment