DOMinion is a vanilla javascript project inspired by jQuery to traverse and manipulate the DOM, handle event listeners, and provide a promised Ajax request/response
Weather app using yahoo weahter APIs and DOMinion $l.ajax
Download the library into your project and load the DOMinion.js file in your code.
<head>
<script type="text/javascript" src="./DOMinion.js"></script>
</head>
DOMinion provides traversing and manipulating the DOM using vanilla JavaScript. It serves as a wrapper for DOM nodes / HTML elements; furthermore, it provides event listeners using vanilla JavaScript.
DOMinion provides the following features:
This selector can receive either a single HTMLElement or a string with a CSS selector and in either case the return value will be a DOMNodeCollection
If it receives an argument, this will become the innerHTML (hint hint) of the each of the nodes. If it does not receive an argument, it returns the innerHTML of the first node in the array.
This method iterates over the DOMinion elements.
This method clears out the content of all nodes in the internal array.
This method accept either a DOMinion wrapped collection, an HTML element, or a string. Append the outerHTML of each element in the argument to the innerHTML of each element in the DOMNodeCollection.
This method returns a DOMNodeCollection of all attribute nodes registered to the specified node.
This method take a string and add the class on each of the elements in the DOMNodeCollection.
This method take a string and remove the class on each of the elements in the DOMNodeCollection.
This method returns a DOMNodeCollection of ALL children of all nodes in the array.
This method returns a DOMNodeCollection of the parents of each of the nodes
This method returns a DOMNodeCollection of all the nodes matching the selector passed in as an argument that are descendants of the nodes.
This method removes the html of all the nodes in the array from the DOM.
This method adds event handlers.
This method removes event handlers.
This method merges JavaScript objects.
This method receives one options object argument and performs an asynchronous HTTP (Ajax) request.
All features in this game would be implemented using native JavaScript DOM manipulation.
This project will be implemented with the following technologies:
JavaScript
for traversing & manipulating logicYahoo API
for demonstrating DOMinionWebpack
for bundling scripts
$l.ajax = function(options){
const defaults = {
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
method: 'GET',
success: ()=> {},
error: ()=>{},
url: '',
data: {}
};
const params = $l.extend(defaults, options);
params.method = params.method.toUpperCase();
if (params.method === "GET") {
params.url += `?${toQueryString(params.data)}`;
}
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(params.method, params.url);
xhr.onload = () => resolve(xhr.responseText);
xhr.onerror = () => reject(xhr.statusText);
xhr.send(params.data);
});
};
class DOMNodeCollection {
...
children(){
let newArr = [];
this.nodes.forEach( (node) => {
Array.from(node.children).forEach( c => newArr.push(c));
})
return new DOMNodeCollection(newArr);
}
...
on(type, options){
const func = options;
if (Array.isArray(this.nodes[0][type])) {
this.nodes[0][type].push(func);
} else {
this.nodes[0][type] = [func];
}
this.nodes.forEach(node => {
node.addEventListener(type, func);
});
}