Skip to content

Instantly share code, notes, and snippets.

@rmccullagh
Created April 9, 2014 01:45
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 rmccullagh/10218030 to your computer and use it in GitHub Desktop.
Save rmccullagh/10218030 to your computer and use it in GitHub Desktop.
JavaScript DOM framework (not tested)
(function(window) {
'use strict';
var
CLASS_REG = /^\.[a-zA-Z_]+[a-zA-Z_0-9-]*/
,ID_REG = /^\#[a-zA-Z_]+[a-zA-Z_0-9-]*/;
var Selector = function(s) {
return new Selector.fn.init(s);
}
Selector.fn = Selector.prototype = {
constructor: Selector
};
var init = Selector.fn.init = function(selector) {
if(typeof selector === 'object') {
selector = selector;
} else if(CLASS_REG.test(selector)) {
selector = document.getElementsByClassName(selector.slice(1));
} else if(ID_REG.test(selector)) {
selector = document.getElementById(selector.slice(1));
} else if(typeof selector === 'string') {
selector = document.getElementsByTagName(selector);
} else {
selector = [];
}
var nodes = selector;
this.selector = nodes;
return this;
};
init.prototype = Selector.fn;
window.Selector = Selector;
})(this);
(function($){
$.fn.getNodeMap = function() {
var toString = Object.prototype.toString;
var type = toString.call(this.selector);
if(type === "[object NodeList]") {
return this.selector;
} else {
return [this.selector];
}
};
})(Selector);
(function($) {
$.fn.isFunction = function(arg) {
var toString = Object.prototype.toString;
var type = toString.call(arg);
if(type === "[object Function]") {
return true;
}
return false;
};
$.fn.isString = function(arg) {
var toString = Object.prototype.toString;
var type = toString.call(arg);
if(type === "[object String]") {
return true;
}
return false;
};
$.fn.isUndefined = function(arg) {
var toString = Object.prototype.toString;
var type = toString.call(arg);
if(type === "[object Undefined]") {
return true;
}
return false;
};
$.fn.isBoolean = function(arg) {
var toString = Object.prototype.toString;
var type = toString.call(arg);
if(type === "[object Boolean]") {
return true;
}
return false;
};
$.fn.isNull = function(arg) {
var toString = Object.prototype.toString;
var type = toString.call(arg);
if(type === "[object Null]") {
return true;
}
return false;
};
})(Selector);
(function($){
function classManager(type) {
var forEach = Array.prototype.forEach;
var args = Array.prototype.slice.call(arguments, 1);
forEach.call(this.getNodeMap(), function(el) {
forEach.call(args, function(name) {
(function (a) {
var self = this;
setTimeout(function() {
self.classList[type](a);
}, 0);
}).call(el, name);
});
});
return this;
};
$.fn.addClass = function() {
var start = (new Date).getTime();
var args = Array.prototype.slice.call(arguments);
args.unshift('add');
return classManager.apply(this, args);
};
$.fn.removeClass = function() {
var start = (new Date).getTime();
var args = Array.prototype.slice.call(arguments);
args.unshift('remove');
return classManager.apply(this, args);
};
})(Selector);
(function($) {
$.fn.toString = function(o) {
var toString = Object.prototype.toString;
return toString.call(o);
};
})(Selector);
(function($) {
function getEventTarget(etype, context, callback) {
var contextType = undefined;
if($.fn.isString(context)) {
contextType = Array.prototype.slice.call(context).shift();
}
this.addEventListener(etype, function(e) {
var target = e.target;
var currentNodeName = e.target.nodeName.toLowerCase();
var currentNodeClassName = e.target.className;
var currentNodeId = e.target.id;
if(typeof contextType != 'undefined') {
if(contextType === '.') {
var _className = Array.prototype.slice.call(context);
_className.shift();
_className = _className.join("");
var _currentClass = currentNodeClassName.split(" ");
var node;
while(node = _currentClass.shift()) {
if(_className === node) {
return callback.apply(e.target, arguments);
}
}
} else if(contextType === '#') {
var targetId = Array.prototype.slice.call(context);
targetId.shift();
targetId = targetId.join("");
if(currentNodeId === targetId) {
return callback.apply(e.target, arguments);
}
} else {
var contextNodeName = context.toLowerCase();
if(currentNodeName === contextNodeName) {
console.log(currentNodeClassName);
callback.apply(e.target, arguments);
}
}
} else {
return callback.apply(this, arguments);
}
});
};
$.fn.on = function(etype) {
var args = Array.prototype.slice.call(arguments, 1);
var forEach = Array.prototype.forEach;
var toString;
var context;
var callback;
var len = args.length;
while(len--) {
if(this.isFunction(args[len])) {
callback = args[len];
break;
}
}
while(len--) {
if(typeof args[len] === 'string') {
context = args[len];
break;
}
}
forEach.call(this.getNodeMap(), function(el) {
getEventTarget.apply(el, [etype, context, callback]);
});
return this;
};
})(Selector);
(function($) {
$.fn.parent = function() {
return $(this.selector.parentNode);
};
})(Selector);
(function($) {
$('.el').addClass('new').removeClass('el').addClass('el');
$('.el').on('click', function(e) {
console.log($(this));
$(this).addClass('clicked').parent().addClass('fuck-you');
},'#hello');
$(document.body).addClass('js');
console.log($('#hello').parent());
})(Selector);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment