-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindly.js
130 lines (130 loc) · 5.82 KB
/
bindly.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
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
125
126
127
128
129
130
class ElmBind {
constructor(params) {
this.params = params
this.awaitDOM()
}
awaitDOM() {
if (document.readyState === 'loading') {
window.addEventListener('DOMContentLoaded', async (e) => {
this.awaitReadyStateComplete()
})
}
else if (document.readyState === 'interactive') { this.awaitReadyStateComplete() }
else { this.waitForElm() }
}
awaitReadyStateComplete() {
if (document.readyState === 'complete' || this.params.runBeforeComplete) { return this.waitForElm() }
console.log('waiting completeion')
let observer = new MutationObserver(mutations => {
if (document.readyState === 'complete') {
observer.disconnect()
this.waitForElm()
}
});
observer.observe(document.body, { childList: true, subtree: true })
}
waitForElm() {
new Promise(resolve => {
if (this.params.mode == 'jquery') {
if ($(`${this.params.el}:not([bindly="bound"])`).length >= 1) { return resolve($(`${this.params.el}:not([bindly="bound"])`)[0]) }
let observer = new MutationObserver(mutations => {
if ($(`${this.params.el}:not([bindly="bound"])`).length >= 1) {
resolve($(`${this.params.el}:not([bindly="bound"])`)[0])
observer.disconnect();
}
});
observer.observe(document.body, { childList: true, subtree: true })
}
else {
if (document.querySelector(`${this.params.el}:not([bindly="bound"])`)) { return resolve(document.querySelector(`${this.params.el}:not([bindly="bound"])`)) }
let observer = new MutationObserver(mutations => {
if (document.querySelector(`${this.params.el}:not([bindly="bound"])`)) {
resolve(document.querySelector(`${this.params.el}:not([bindly="bound"])`))
observer.disconnect();
}
});
observer.observe(document.body, { childList: true, subtree: true })
}
}).then((element) => {
element.setAttribute('bindly', 'bound')
if (this.params.parentToBind) {
this.params.elToClone = element.closest(this.params.parentToBind)
if (this.params.elToClone) {
element.closest(this.params.parentToBind).setAttribute('bindly', 'bound')
this.initializeElm()
!this.params.bindAll && this.trackElmDeletion(element)
this.params.bindAll && this.waitForElm()
} else {
// this checks if the element has the desired parent class, if not, we need to re run the waitForElm routine regardless of bindAll status.
this.waitForElm()
}
} else {
this.params.elToClone = element
this.initializeElm()
!this.params.bindAll && this.trackElmDeletion(element)
this.params.bindAll && this.waitForElm()
}
})
}
trackElmDeletion(target) {
new Promise(resolve => {
let observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var nodes = Array.from(mutation.removedNodes)
var directMatch = nodes.indexOf(target) > -1
var parentMatch = nodes.some(parent => parent.contains(target))
if (directMatch || parentMatch) {
observer.disconnect()
resolve('Element Deleted')
}
})
})
var config = { subtree: true, childList: true }
observer.observe(document.body, config)
}).then((msg) => {
this.waitForElm()
})
}
initializeElm() {
this.dupliacteElm()
this.params.hideOriginal && this.hideOriginal()
this.params.id && this.setNewElmId()
this.params.className && this.setClass()
this.params.addClasses && this.addClasses()
this.params.addAttributes && (() => {for (let i=0; i < Object.keys(this.params.addAttributes).length; i++) { this.addAttribute(Object.keys(this.params.addAttributes)[i], Object.values(this.params.addAttributes)[i]) }})()
this.params.addListeners && (() => {for (let i=0; i < Object.keys(this.params.addListeners).length; i++) { this.addListener(Object.keys(this.params.addListeners)[i], Object.values(this.params.addListeners)[i]) }})()
typeof this.params.insert === 'string' ? this.params.insert.toLowerCase() == 'before' ? this.insertBefore() : this.insertAfter() : this.insertAfter()
this.params.adjustElm && this.adjustElm()
}
dupliacteElm() {
this.newElm = this.params.elToClone.cloneNode(true)
}
hideOriginal() {
this.params.elToClone.style.display = 'none'
}
setNewElmId() {
if (this.params.id) { this.newElm.id = this.params.id }
}
setClass() {
this.newElm.className = this.params.className
}
addClasses() {
this.newElm.classList.add(...this.params.addClasses)
}
addAttribute(attrName, attrValue) {
this.newElm.setAttribute(attrName, attrValue)
}
addListener(listFor, callback) {
this.newElm.addEventListener(listFor, callback)
}
insertAfter() {
this.params.elToClone.parentNode.insertBefore(this.newElm, this.params.elToClone.nextSibling)
}
insertBefore() {
this.params.elToClone.parentNode.insertBefore(this.newElm, this.params.elToClone)
}
adjustElm() {
this.params.adjustElm(this.newElm)
}
}
function Bindly(params) { return new ElmBind(params) }