Skip to content
Snippets Groups Projects
Commit 60ceb1f3 authored by Commander-lol's avatar Commander-lol
Browse files

Updated test and semi-linted tinyDOM.js

parent b73d0242
No related branches found
No related tags found
No related merge requests found
//Prevent pollution of global namespace with closure //Prevent pollution of global namespace with closure
(function(){ (function() {
var tinyDOM = function(selector){ 'use strict';
var tinyDOM = function(selector) {
return new tinyDOMFunction(selector); return new tinyDOMFunction(selector);
} };
var tinyDOMFunction = function(selector) { var tinyDOMFunction = function(selector) {
if(selector === null || typeof(selector) === 'undefined'){ if (selector === null || typeof (selector) === 'undefined') {
this.length = 0; this.length = 0;
} else if(typeof(selector) === 'string'){ } else if(typeof(selector) === 'string') {
var elements = document.querySelectorAll(selector); var elements = document.querySelectorAll(selector);
this.length = elements.length; this.length = elements.length;
for(var i = 0; i < elements.length; i++){ for(var i = 0; i < elements.length; i++) {
var e = elements.item(i); var e = elements.item(i);
if(typeof(e.td_prop) === 'undefined'){ if(typeof(e.td_prop) === 'undefined') {
e.td_prop = { e.td_prop = {
isHidden: false isHidden: false
}; };
...@@ -31,14 +33,14 @@ ...@@ -31,14 +33,14 @@
tinyDOM.fn = tinyDOMFunction.prototype = { tinyDOM.fn = tinyDOMFunction.prototype = {
each: function(fn) { each: function(fn) {
var l = this.length; var l = this.length;
while(l--){ while(l--) {
fn(l, this[l], this); fn(l, this[l], this);
} }
return this; return this;
}, },
hide: function(){ hide: function(){
this.each(function(i, e){ this.each(function(i, e) {
if(!e.td_prop.isHidden){ if(!e.td_prop.isHidden) {
e.style.td_previousDisplay = e.style.display; e.style.td_previousDisplay = e.style.display;
e.style.display = 'none'; e.style.display = 'none';
e.td_prop.isHidden = true; e.td_prop.isHidden = true;
...@@ -46,10 +48,10 @@ ...@@ -46,10 +48,10 @@
}); });
return this; return this;
}, },
show: function(){ show: function() {
this.each(function(i, e){ this.each(function(i, e) {
if(e.td_prop.isHidden === true){ if(e.td_prop.isHidden === true){
if(typeof(e.style.td_previousDisplay) !== 'undefined'){ if(typeof(e.style.td_previousDisplay) !== 'undefined') {
e.style.display = e.style.td_previousDisplay; e.style.display = e.style.td_previousDisplay;
} else { } else {
e.style.display = 'block'; e.style.display = 'block';
...@@ -59,22 +61,22 @@ ...@@ -59,22 +61,22 @@
}); });
return this; return this;
}, },
on: function(ev, fn){ on: function(ev, fn) {
this.each(function(i, e){ this.each(function(i, e) {
e.addEventListener(ev, fn); e.addEventListener(ev, fn);
}); });
return this; return this;
}, },
first: function(){ first: function() {
if(typeof(this[0]) !== 'undefined'){ if(typeof(this[0]) !== 'undefined') {
return this[0]; return this[0];
} else { } else {
return null; return null;
} }
}, },
data: function(key, value){ data: function(key, value) {
var e = this[0]; var e = this[0];
if(typeof(value) !== 'undefined'){ if(typeof(value) !== 'undefined') {
e.setAttribute('data-'+key, value); e.setAttribute('data-'+key, value);
return this; return this;
} else { } else {
...@@ -83,16 +85,16 @@ ...@@ -83,16 +85,16 @@
} }
} }
tinyDOM.exists = function(obj){ tinyDOM.exists = function(obj) {
return obj !== null && typeof(obj) !== 'undefined'; return obj !== null && typeof(obj) !== 'undefined';
} }
tinyDOM.merge = function(json1, json2){ tinyDOM.merge = function(json1, json2) {
if(!this.exists(json1) || !this.exists(json2)){ if(!this.exists(json1) || !this.exists(json2)) {
return null; return null;
} else { } else {
for(var prop in json2) { for(var prop in json2) {
if(json2.hasOwnProperty(prop)){ if(json2.hasOwnProperty(prop)) {
json1[prop] = json2[prop]; json1[prop] = json2[prop];
} }
} }
...@@ -100,7 +102,7 @@ ...@@ -100,7 +102,7 @@
} }
} }
tinyDOM.ajax = function(options){ tinyDOM.ajax = function(options) {
var req = new XMLHttpRequest(); var req = new XMLHttpRequest();
var _this = this; var _this = this;
...@@ -113,25 +115,17 @@ ...@@ -113,25 +115,17 @@
responseType: 'text', responseType: 'text',
data: null, data: null,
headers: [], headers: [],
success: null, callbacks: {}
error: null
} }
this.merge(params, options); this.merge(params, options);
req.responseType = params.responseType; req.responseType = params.responseType;
req.onreadystatechange = function(){ if(this.exists(params.callbacks)) {
if(req.readyState > 1){ for(var ev in params.callbacks) {
if(req.status === 200){ if(params.callbacks.hasOwnProperty(ev)) {
if (req.readyState === 4 && _this.exists(params.success)){ req.addEventListener(ev, params.callbacks[ev]);
params.success(req.response, req);
}
} else {
req.abort();
if(_this.exists(params.error)){
params.error({status: req.status, message: req.statusText}, req);
}
} }
} }
} }
...@@ -144,7 +138,7 @@ ...@@ -144,7 +138,7 @@
params.password params.password
); );
for(var i = 0; i < params.headers.length; i++){ for(var i = 0; i < params.headers.length; i++) {
req.setRequestHeader(params.headers[i].header, params.headers[i].value); req.setRequestHeader(params.headers[i].header, params.headers[i].value);
} }
...@@ -152,10 +146,10 @@ ...@@ -152,10 +146,10 @@
return req; return req;
}; };
if(!window.μ){ if(!window.μ) {
window.μ = tinyDOM; window.μ = tinyDOM;
} }
if(!window.mu){ if(!window.mu) {
window.mu = tinyDOM; window.mu = tinyDOM;
} }
})(); })();
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
so it would be <span class='important'>bad</span> if any of them dissapeared. so it would be <span class='important'>bad</span> if any of them dissapeared.
</p> </p>
<p id='emptydiv'> <p id='emptydiv'>
</p> </p>
</div> </div>
...@@ -63,9 +63,11 @@ ...@@ -63,9 +63,11 @@
console.log(μ(this).data('href')); console.log(μ(this).data('href'));
μ.ajax({ μ.ajax({
url: url, url: url,
success: function(data){ callbacks: {
console.log(data); load: function(data){
μ('#emptydiv').first().innerHTML = data; console.log(data);
μ('#emptydiv').first().innerHTML = data;
}
} }
}); });
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment