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

Haidar #4

Open
wants to merge 7 commits 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
66 changes: 66 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>MiniQuery</title>

</head>
<body>
<div id="eyed">
eyed
</div>
<div class="klass">
klass
</div>
<a href="#hello">click me</a>

<script src="/lib/miniquery.js"></script>
<script type="text/javascript">
let eyed = SweetSelector.select('#eyed')
console.log(eyed);
let klass = SweetSelector.select('.klass')
console.log(klass);
let a = SweetSelector.select('a')
console.log(a);
// DOM.hide('#eyed')
miniquery('#eyed').hide();
miniquery('#eyed').show();
$('#eyed').hide();
// DOM.show('#eyed')
// DOM.addClass('.klass','shadi')
miniquery('.klass').addClass('shadi');
// DOM.removeClass('.klass','shadi')
miniquery('.klass').removeClass('shadi');

// EventDispatcher.on('.klass','click',() => {
// console.log("awesome");
// });
miniquery('.klass').on('click',() => {
console.log("awesome");
});
// EventDispatcher.trigger('.klass','click');
miniquery('.klass').trigger('click');
// AjaxWrapper.request({
// url: 'http://localhost:3000/search?q=hacktiv8',
// type: 'GET',
// success: () => {
// console.log('berhasil');
// },
// fail: () => {
// console.log('gagal');
// }
// })
miniquery().ajax({
url: 'http://localhost:3000/search?q=hacktiv8',
type: 'GET',
success: (data) => {
console.log('berhasil');
},
fail: () => {
console.log('gagal');
}
})

</script>
</body>
</html>
136 changes: 136 additions & 0 deletions lib/miniquery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

class SweetSelector {
static select(input){
if (input == undefined) {
return null
}
if (input[0] == '#') {
return document.querySelector(input);
} else if (input[0] == '.') {
let doc = document.querySelectorAll(input);
if (doc.length == 1) {
return document.querySelector(input);
}
return doc;
} else if (input[0] != '.' && input[0] != '#') {
return document.querySelector(input);
}
}
}

class DOM {
static hide(selector){
let element = SweetSelector.select(selector);
element.style.display = "none"
}
static show(selector){
let element = SweetSelector.select(selector);
element.style.display = "block"
}
static addClass(selector,className){
let element = SweetSelector.select(selector);
element.classList.add(className);
}
static removeClass(selector,className){
let element = SweetSelector.select(selector);
element.classList.remove(className);
}
}

class EventDispatcher {
static on(selector,event,callback){
const element = SweetSelector.select(selector);
element.addEventListener(event, callback);
}
static trigger(selector,event){
const element = SweetSelector.select(selector);
element.dispatchEvent(new Event(event));
}
}
class AjaxWrapper {
static request(obj){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
obj.success(this.responseText);
} else {
obj.fail();
}
};
xhttp.open(obj.type,obj.url, true);
xhttp.send();
}

}
var miniquery = function(context) {
if (this.__proto__.constructor !== miniquery) {
return new miniquery(context);
}
this.element = SweetSelector.select(context) ;
this.hide = function(){
this.element.style.display = "none"
}
this.show = function(){
this.element.style.display = "block"
}
this.addClass = function(className){
this.element.classList.add(className);
}
this.removeClass = function(className){
this.element.classList.remove(className);
}
this.on = function(event,callback){
this.element.addEventListener(event, callback);
}
this.trigger = function(event){
this.element.dispatchEvent(new Event(event));
}
this.ajax = function (obj){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
obj.success(this.responseText);
} else {
obj.fail();
}
};
xhttp.open(obj.type,obj.url, true);
xhttp.send();
}
};
var $ = function(context) {
if (this.__proto__.constructor !== miniquery) {
return new miniquery(context);
}
this.element = SweetSelector.select(context) ;
this.hide = function(){
this.element.style.display = "none"
}
this.show = function(){
this.element.style.display = "block"
}
this.addClass = function(className){
this.element.classList.add(className);
}
this.removeClass = function(className){
this.element.classList.remove(className);
}
this.on = function(event,callback){
this.element.addEventListener(event, callback);
}
this.trigger = function(event){
this.element.dispatchEvent(new Event(event));
}
this.ajax = function (obj){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
obj.success(this.responseText);
} else {
obj.fail();
}
};
xhttp.open(obj.type,obj.url, true);
xhttp.send();
}
};