summaryrefslogtreecommitdiff
path: root/mini-jquery.js
blob: 1db716247bc45e6c4bbe86c091fb83caec8565fe (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 
// mini-jquery.js
//
// Matthew Kosarek
// 
// 2021-08-08
//
// JQuery is huge and I use the smallest portion of it.
// This is my attempt to make a JQuery lookalike that does
// enough for me, but doesn't bloat things.
// 

function MiniJQueryObject(arg) {
    this.elementList = [];
    if (typeof arg === 'string') {
        arg = arg.trim();

        if (arg[0] === '<' && arg[arg.length - 1] === '>') {
            // Trying to create an html element
            arg = arg.substring(1, arg.length - 1);
            var element = document.createElement(arg);
            this.elementList.push(element);
        } else {
            // Trying to query an html element
            
            switch (arg[0]) {
                case '#': {
                    arg = arg.substring(1);
                    var idElement = document.getElementById(arg);
                    if (idElement) {
                        this.elementList.push(idElement);
                    }
                    break;
                }
                case '.': {
                    arg = arg.substring(1);
                    this.fromHTMLCollection(document.getElementsByClassName(arg));
                    break;
                }
                default: {
                    this.fromHTMLCollection(document.getElementsByTagName(arg));
                    break;
                }
            }
        }
    } else if (arg instanceof HTMLElement) {
        this.elementList.push(arg);
    } else {
        console.error('Unknown object: ', arg);
    }
}

MiniJQueryObject.prototype.fromHTMLCollection = function(collection) {
    for (var i = 0; i < collection.length; i++) {
        this.elementList.push(collection.item(i));
    }
};

MiniJQueryObject.prototype.append = function(otherMinJqueryObject) {
    this.elementList.forEach(function(element) {
        otherMinJqueryObject.elementList.forEach(function(otherElement) {
            element.appendChild(otherElement);
        });
    });
    return this;
};

MiniJQueryObject.prototype.appendTo = function(otherMinJqueryObject) {
    otherMinJqueryObject.append(this);
    return this;
};

MiniJQueryObject.prototype.empty = function() {
    this.elementList.forEach(function(element) {
        element.innerHTML = '';
    });
    return this;
};

MiniJQueryObject.prototype._set = function(field, value) {
    this.elementList.forEach(function(element) {
        element[field] = value;
    });
    return this;
};

MiniJQueryObject.prototype.type = function(type) {
    return this._set('type', type);
};

MiniJQueryObject.prototype.val = function(val) {
    return this._set('value', val);
};

MiniJQueryObject.prototype.disabled = function(val) {
    return this._set('disabled', val ? 'true' : undefined);
};

MiniJQueryObject.prototype.on = function(event, callback) {
    this.elementList.forEach(function(element) {
        element.addEventListener(event, callback);
    });
    
    return this;
};

MiniJQueryObject.prototype.addClass = function(className) {
    this.elementList.forEach(function(element) {
        element.classList.add(className);
    });
    return this;
};

MiniJQueryObject.prototype.removeClass = function(className) {
    this.elementList.forEach(function(element) {
        element.classList.remove(className);
    });
    return this;
};

var $ = function(arg) {
    return new MiniJQueryObject(arg);
};
window.$ = $;