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
(function(){
var tinyDOM = function(selector){
(function() {
'use strict';
var tinyDOM = function(selector) {
return new tinyDOMFunction(selector);
}
};
var tinyDOMFunction = function(selector) {
if(selector === null || typeof(selector) === 'undefined'){
if (selector === null || typeof (selector) === 'undefined') {
this.length = 0;
} else if(typeof(selector) === 'string'){
} else if(typeof(selector) === 'string') {
var elements = document.querySelectorAll(selector);
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);
if(typeof(e.td_prop) === 'undefined'){
if(typeof(e.td_prop) === 'undefined') {
e.td_prop = {
isHidden: false
};
......@@ -31,14 +33,14 @@
tinyDOM.fn = tinyDOMFunction.prototype = {
each: function(fn) {
var l = this.length;
while(l--){
while(l--) {
fn(l, this[l], this);
}
return this;
},
hide: function(){
this.each(function(i, e){
if(!e.td_prop.isHidden){
this.each(function(i, e) {
if(!e.td_prop.isHidden) {
e.style.td_previousDisplay = e.style.display;
e.style.display = 'none';
e.td_prop.isHidden = true;
......@@ -46,10 +48,10 @@
});
return this;
},
show: function(){
this.each(function(i, e){
show: function() {
this.each(function(i, e) {
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;
} else {
e.style.display = 'block';
......@@ -59,22 +61,22 @@
});
return this;
},
on: function(ev, fn){
this.each(function(i, e){
on: function(ev, fn) {
this.each(function(i, e) {
e.addEventListener(ev, fn);
});
return this;
},
first: function(){
if(typeof(this[0]) !== 'undefined'){
first: function() {
if(typeof(this[0]) !== 'undefined') {
return this[0];
} else {
return null;
}
},
data: function(key, value){
data: function(key, value) {
var e = this[0];
if(typeof(value) !== 'undefined'){
if(typeof(value) !== 'undefined') {
e.setAttribute('data-'+key, value);
return this;
} else {
......@@ -83,16 +85,16 @@
}
}
tinyDOM.exists = function(obj){
tinyDOM.exists = function(obj) {
return obj !== null && typeof(obj) !== 'undefined';
}
tinyDOM.merge = function(json1, json2){
if(!this.exists(json1) || !this.exists(json2)){
tinyDOM.merge = function(json1, json2) {
if(!this.exists(json1) || !this.exists(json2)) {
return null;
} else {
for(var prop in json2) {
if(json2.hasOwnProperty(prop)){
if(json2.hasOwnProperty(prop)) {
json1[prop] = json2[prop];
}
}
......@@ -100,7 +102,7 @@
}
}
tinyDOM.ajax = function(options){
tinyDOM.ajax = function(options) {
var req = new XMLHttpRequest();
var _this = this;
......@@ -113,25 +115,17 @@
responseType: 'text',
data: null,
headers: [],
success: null,
error: null
callbacks: {}
}
this.merge(params, options);
req.responseType = params.responseType;
req.onreadystatechange = function(){
if(req.readyState > 1){
if(req.status === 200){
if (req.readyState === 4 && _this.exists(params.success)){
params.success(req.response, req);
}
} else {
req.abort();
if(_this.exists(params.error)){
params.error({status: req.status, message: req.statusText}, req);
}
if(this.exists(params.callbacks)) {
for(var ev in params.callbacks) {
if(params.callbacks.hasOwnProperty(ev)) {
req.addEventListener(ev, params.callbacks[ev]);
}
}
}
......@@ -144,7 +138,7 @@
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);
}
......@@ -152,10 +146,10 @@
return req;
};
if(!window.μ){
if(!window.μ) {
window.μ = tinyDOM;
}
if(!window.mu){
if(!window.mu) {
window.mu = tinyDOM;
}
})();
......@@ -13,7 +13,7 @@
so it would be <span class='important'>bad</span> if any of them dissapeared.
</p>
<p id='emptydiv'>
</p>
</div>
......@@ -63,9 +63,11 @@
console.log(μ(this).data('href'));
μ.ajax({
url: url,
success: function(data){
console.log(data);
μ('#emptydiv').first().innerHTML = data;
callbacks: {
load: function(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