Skip to content

Instantly share code, notes, and snippets.

@rrfaria
Created June 30, 2019 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rrfaria/dad5128e3a8e5a9e3a776d8248ae4361 to your computer and use it in GitHub Desktop.
Save rrfaria/dad5128e3a8e5a9e3a776d8248ae4361 to your computer and use it in GitHub Desktop.
var __ = function () {
this.context = [];
var self = this;
this.selector = function (_elem, _sel) {
return _elem.querySelectorAll(_sel);
}
this.on = function (_event, _element, _function) {
this.context = self.selector(document, _element);
document.addEventListener(_event, function (e) {
var elem = e.target;
while (elem != null) {
if ("#" + elem.id == _element || self.isClass(elem, _element) || self.elemEqal(elem)) {
_function(e, elem);
}
elem = elem.parentElement;
}
}, false);
};
this.isClass = function (_elem, _class) {
var names = _elem.className.trim().split(" ");
for (this.it = 0; this.it < names.length; this.it++) {
names[this.it] = "." + names[this.it];
}
return names.indexOf(_class) != -1 ? true : false;
};
this.elemEqal = function (_elem) {
var flg = false;
for (this.it = 0; this.it < this.context.length; this.it++) {
if (this.context[this.it] === _elem && !flg) {
flg = true;
}
}
return flg;
};
}
function _(_sel_string) {
var new_selc = new __(_sel_string);
return new_selc;
}
_(document).on("click", "#brnPrepend", function (_event, _element) {
console.log(_event);
console.log(_element);
// Todo
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment