Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fatimah #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<html>
<head>

</head>
<body>
<h1 id="eyed">HELLO</h1>
<h2 class="klass shabi">World</h2>
<p>Test</p>
<script src="./lib/miniquery.js"></script>
<script>
AjaxWrapper.request({
url: 'http://localhost:3000/home',
type: 'GET',
success: data => {
console.log(data)
},
fail: function() {

}
})</script>
</body>
</html>
104 changes: 104 additions & 0 deletions lib/miniquery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var SweetSelector = {
select: function(input) {
switch(input[0]) {
case '#':
return document.querySelector(input)
break;
case '.':
return document.querySelectorAll(input)
break;
default:
return document.querySelectorAll(input)
}
}
};

var DOM = {
hide: function(input) {
switch(input[0]) {
case '#':
let id = input.substring(1)
let div = document.getElementById(id)
div.style.display = 'none'
break;
case '.':
let klass = document.querySelectorAll(input)
for(let i = 0; i < klass.length; i++) {
klass[i].style.display = 'none'
}
break;
default:
let tag = document.querySelectorAll(input)
for(let i = 0; i < tag.length; i++) {
tag[i].style.display = 'none'
}
}
},
show: function(input) {
switch(input[0]) {
case '#':
let id = input.substring(1)
let div = document.getElementById(id)
div.style.display = 'block'
break;
case '.':
let klass = document.querySelectorAll(input)
for(let i = 0; i < klass.length; i++) {
klass[i].style.display = 'block'
}
break;
default:
let tag = document.querySelectorAll(input)
for(let i = 0; i < tag.length; i++) {
tag[i].style.display = 'block'
}
}
},

addClass: function(input, option) {
let klass = document.querySelectorAll(input)
for(let i = 0; i < klass.length; i++) {
klass[i].className += ` ${option}`
// console.log(klass[i])
}
},

removeClass: function(input, option) {
let klass = document.querySelectorAll(input)
for(let i = 0; i < klass.length; i++) {
klass[i].className = input
// console.log(klass[i])
}
}
};

var EventDispatcher = {
on: function(input, event, callback) {
let klass = document.querySelectorAll(input)
for(let i = 0; i < klass.length; i++) {
klass[i].addEventListener(event, callback)
}
},

trigger: function(input, event) {
let klass = document.querySelectorAll(input)
for(let i = 0; i < klass.length; i++) {
klass[i].dispatchEvent(new Event(event))
}
}
}

var AjaxWrapper = {
request: function(obj) {
var xhr = new XMLHttpRequest();
xhr.open(obj.type, obj.url, true)
xhr.onreadystatechange = function() {
if(xhr.status >= 200 && xhr.status < 400) {
obj.success(xhr.responseText)
} else {
obj.fail()
}
}
xhr.send()
}
}