Newer
Older

Commander-lol
committed
//Prevent pollution of global namespace with closure
(function() {
'use strict';
var tinyDOM = function(selector) {

Commander-lol
committed
return new tinyDOMFunction(selector);

Commander-lol
committed
var tinyDOMFunction = function(selector) {
if (selector === null || typeof (selector) === 'undefined') {
this.length = 0;
} else if(typeof(selector) === 'string') {
var elements = document.querySelectorAll(selector);

Commander-lol
committed
this.length = elements.length;
for(var i = 0; i < elements.length; i++) {
var e = elements.item(i);
if(typeof(e.td_prop) === 'undefined') {
e.td_prop = {
isHidden: false
};
}
this[i] = elements.item(i);

Commander-lol
committed
}
} else {
this[0] = selector;
this.length = 1;

Commander-lol
committed
}
return this;
}
tinyDOM.fn = tinyDOMFunction.prototype = {
each: function(fn) {
var l = this.length;

Commander-lol
committed
fn(l, this[l], this);
}
return this;
},
hide: function(){
this.each(function(i, e) {
if(!e.td_prop.isHidden) {

Commander-lol
committed
e.style.td_previousDisplay = e.style.display;
e.style.display = 'none';
e.td_prop.isHidden = true;
}
});
return this;
},
show: function() {
this.each(function(i, e) {

Commander-lol
committed
if(e.td_prop.isHidden === true){
if(typeof(e.style.td_previousDisplay) !== 'undefined') {

Commander-lol
committed
e.style.display = e.style.td_previousDisplay;
} else {
e.style.display = 'block';
}
e.td_prop.isHidden = false;
}
});
return this;
},
on: function(ev, fn) {
this.each(function(i, e) {

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

Commander-lol
committed
return this[0];
} else {
return null;
}
},

Commander-lol
committed
var e = this[0];
if(typeof(value) !== 'undefined') {

Commander-lol
committed
e.setAttribute('data-'+key, value);
return this;
} else {
return e.getAttribute('data-'+key);
}
}
}
return obj !== null && typeof(obj) !== 'undefined';
}
tinyDOM.merge = function(json1, json2) {
if(!this.exists(json1) || !this.exists(json2)) {
return null;
} else {
for(var prop in json2) {
json1[prop] = json2[prop];
}
}
return json1;
}
}
tinyDOM.ajax = function(options) {
var req = new XMLHttpRequest();
var _this = this;
var params = {
method: 'GET',
url: '',
async: true,
user: null,
password: null,
responseType: 'text',
data: null,
headers: [],
}
this.merge(params, options);
req.responseType = params.responseType;
if(this.exists(params.callbacks)) {
for(var ev in params.callbacks) {
if(params.callbacks.hasOwnProperty(ev)) {
req.addEventListener(ev, params.callbacks[ev]);
}
}
}
req.open(
params.method,
params.url,
params.async,
params.user,
params.password
);
for(var i = 0; i < params.headers.length; i++) {
req.setRequestHeader(params.headers[i].header, params.headers[i].value);
}
req.send(params.data);

Commander-lol
committed
}