-
Notifications
You must be signed in to change notification settings - Fork 0
/
owldom.js
66 lines (58 loc) · 1.93 KB
/
owldom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @author Ashu Sharma ([email protected])
* @license MIT
* @copyright Copyright 2022 - Nift* (github.com/Niftproj/owldom).
*/
/**
* Easier manage the DOM. Don't mess with it.
* @class OwlDOM
*/
function OwlDOM() {}
/**
* Get Object with metadata (only for inner use of OwlDOM)
* @param {String} method by which method (i.e. className, tagName, id) datas are collected
* @param {*} data The data that is selected, mainly in Node, NodeList, Array
* @returns {Object} Returns an Object of given parameters 'method' and 'data'
*/
function getRoundObject(method, data)
{
return {method: method, data: data};
}
/**
* Check the given value is really given or not (only for inner use of OwlDOM)
* @param {String} data The data that has to be runned checks
* @returns {Boolean} Returns boolean value for checks passed or not
*/
function valueExist(data)
{
if(data !== undefined && data !== '' && data !== null)
return true;
return false;
}
/**
* Grab any element of DOM
* @example .grabElement('*') to get All elements even childs in array
* @param {String} tagName to find elements with provided tagName (Optional)
* @param {String} id to find elements with specified Id (Optional)
* @param {Array[String,String]} classList to select all elements with given classList (Optional)
* @returns {Array} Returns an Array of {getRoundObject} function with metadata
*/
OwlDOM.prototype.grabElement = function(tagName, id, classList = [])
{
let roundSearch = [];
if(valueExist(tagName))
{
roundSearch.push(getRoundObject('tagName', document.getElementsByTagName(tagName)));
}
if(valueExist(id))
{
roundSearch.push(getRoundObject('id', document.getElementById(id)));
}
if(classList.length > 0)
{
classList.forEach(classN => {
roundSearch.push(getRoundObject('className', document.querySelectorAll('.'+classN)));
});
}
return roundSearch;
}