Newer
Older
/*global Element, CustomEvent, HTMLElement */
/*jslint plusplus: true, nomen: true*/
var proto,
TinyDOMFunction,

Commander-lol
committed
/*
* Polyfill from https://gist.github.com/elijahmanor/6452535
*/
if (Element && !Element.prototype.matches) {
proto = Element.prototype;
proto.matches = proto.matchesSelector ||
proto.mozMatchesSelector || proto.msMatchesSelector ||
proto.oMatchesSelector || proto.webkitMatchesSelector;

Commander-lol
committed
}
/*
* End Polyfill
*/
TinyDOMFunction = function (selector) {
if (selector === null || typeof (selector) === 'undefined') {
this.length = 0;
} else if (typeof (selector) === 'string') {
elements = document.querySelectorAll(selector);

Commander-lol
committed
this.length = elements.length;
for (i = 0; i < elements.length; i++) {

Commander-lol
committed
}
} else if (selector.length) {
for (i = 0; i < selector.length; i++) {
e = selector[i];
this[i] = e;
}
} else {
this[0] = selector;
this.length = 1;

Commander-lol
committed
}
return this;
};
tinyDOM = function (selector) {
return new TinyDOMFunction(selector);
};

Commander-lol
committed
tinyDOM.fn = TinyDOMFunction.prototype = {
each: function (fn) {

Commander-lol
committed
var l = this.length;

Commander-lol
committed
fn(l, this[l], this);
}
return this;
},
on: function (ev, del, fn) {
if (typeof (del) === 'string') {
this.each(function (i, e) {
e.addEventListener(ev, function (firedevent) {
var target = firedevent.target,
matched = false;

Commander-lol
committed
do {
if (target && target.matches(del)) {

Commander-lol
committed
fn.call(target, firedevent);
matched = true;
} else {
target = target.parentNode;
if (!target || !target.matches || target === e) {

Commander-lol
committed
matched = true;
}
}
} while (matched !== true);

Commander-lol
committed
});
});
} else {
fn = del;
this.each(function (i, e) {

Commander-lol
committed
e.addEventListener(ev, fn);
});
}

Commander-lol
committed
return this;
},
first: function () {
if (typeof (this[0]) !== 'undefined') {

Commander-lol
committed
return tinyDOM(this[0]);

Commander-lol
committed
} else {
return null;
}
},
parent: function (selector) {
var e = this[0].parentNode, stn = true;
if (tinyDOM.exists(selector)) {
while (e !== null && e !== document) {
if (e.matches(selector)) {
stn = false;
break;
} else {
e = e.parentNode;
}
}
e = stn ? null : e;
}
return tinyDOM(e);
var n = this[0].childNodes,
for (i = 0; i < n.length; i++) {
if (tinyDOM.isElement(n[i])) {
}
}
return tinyDOM(a);
},
data: function (key, value) {
if (typeof (value) !== 'undefined') {
this.each(function(i, e){
e.setAttribute('data-' + key, value);
});

Commander-lol
committed
return this;
} else {
return this[0].getAttribute('data-' + key);

Commander-lol
committed
}

Commander-lol
committed
},

Commander-lol
committed
attr: function (key, value) {
if (typeof (value) !== 'undefined') {
this.each(function(i, e){
e.setAttribute(key, value);
});

Commander-lol
committed
return this;
} else {
return this[0].getAttribute(key);

Commander-lol
committed
}
},
class: function(classname, addremove){
if(tinyDOM.exists(addremove)){
this.each(function(i, e){
e.classList.toggle(classname, addremove);
});
} else {
this.each(function(i, e){
e.classList.toggle(classname);
});
}
return this;
},

Commander-lol
committed
trigger: function (eventName, data, bubbles, cancelable) {
bubbles = tinyDOM.exists(bubbles) ? bubbles : true;
cancelable = tinyDOM.exists(cancelable) ? cancelable : true;
var event = new CustomEvent(eventName, data, bubbles, cancelable);
this.each(function (i, e) {
e.dispatchEvent(event);
});

Commander-lol
committed
}

Commander-lol
committed
tinyDOM.ready = function(fn) {
document.addEventListener("DOMContentLoaded", fn);
}
tinyDOM.isElement = function (node) {
var is = false;
try {
is = node instanceof HTMLElement;
is = node.nodeType && node.nodeType === 1;
}
return is;

Commander-lol
committed
};
tinyDOM.isJson = function (obj) {
try {
JSON.parse(obj);
return obj !== null && typeof obj !== "undefined";
} catch (e) {
return false;
}
};
tinyDOM.exists = function (obj) {
return obj !== null && typeof (obj) !== 'undefined';
};
tinyDOM.merge = function (json1, json2) {
if (!this.exists(json1) || !this.exists(json2)) {
var prop;
for (prop in json2) {
if (json2.hasOwnProperty(prop)) {
json1[prop] = json2[prop];
}
}
return json1;
}
tinyDOM.byID = function (id) {

Commander-lol
committed
return tinyDOM(document.getElementById(id));

Commander-lol
committed
tinyDOM.triggerOn = function (target, eventName, data, bubbles, cancelable) {
bubbles = tinyDOM.exists(bubbles) ? bubbles : true;
cancelable = tinyDOM.exists(cancelable) ? cancelable : true;
target.dispatchEvent(new CustomEvent(eventName, data, bubbles, cancelable));
};
tinyDOM.json = {
keys: function(json) {
var kys = [],
indx;
for(indx in json){
if(json.hasOwnProperty(indx)){
kys.push(indx);
}
}
return kys;
}
};
tinyDOM.ajax = function (options) {
var req = new XMLHttpRequest(),
_this = this,
ev,
i,
params = {
method: 'GET',
url: '',
async: true,
user: null,
password: null,
responseType: 'text',
data: null,
headers: [],
callbacks: {}

Commander-lol
committed
},
makeListener = function (callback) {
return function (data) {
callback(data.currentTarget.response, data);
};
this.merge(params, options);
req.responseType = params.responseType;
if (this.exists(params.callbacks)) {
for (ev in params.callbacks) {
if (params.callbacks.hasOwnProperty(ev)) {

Commander-lol
committed
req.addEventListener(ev, makeListener(params.callbacks[ev]));
}
}
}
req.open(
params.method,
params.url,
params.async,
params.user,
params.password
);
for (i = 0; i < params.headers.length; i++) {
req.setRequestHeader(params.headers[i].header, params.headers[i].value);
}
req.send(params.data);

Commander-lol
committed
}
if (!window.mu) {