-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (60 loc) · 2.44 KB
/
Copy pathscript.js
File metadata and controls
68 lines (60 loc) · 2.44 KB
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
//Dom elements with observers
const btnsHolder = document.getElementById('styleBtns'),
removeBtnsHolder = document.getElementById('removeBtns'),
charUpdate = document.getElementById('charUpdates'),
// child elements updated in observed parent elements
btnsStyle = [].slice.call(document.querySelectorAll('.styledBtn')),
btnsRemoveStyle = [].slice.call(document.querySelectorAll('.styledBtnRemoved'));
// observer config objects
//you could reuse one that had all options but I wanted to show the minmium options needed for each record type.
const observerConfigAttribute = {attributes: true, subtree: true},
observerConfigChildList = {childList: true},
observerConfigCharData = {characterData: true, characterDataOldValue: true, subtree: true};
//create an instance of a Mutation obsever which checks for mutation record type and appends text message to dom element
const observer = new MutationObserver(function(mutations) {
let pTag = document.createElement('P'),
resultStlElm = document.getElementById('styleMo'),
resultRemElm = document.getElementById('removeMo'),
resultcharDataElm = document.getElementById('charDataMo'),
resultTxt = 'This mutation record is of type ';
mutations.forEach(function(mutation) {
switch(mutation.type){
case 'attributes':
pTag.innerText = resultTxt + mutation.type +'.';
resultStlElm.appendChild(pTag);
break;
case 'childList':
pTag.innerText = resultTxt + mutation.type +'.';
resultRemElm.appendChild(pTag);
break;
case 'characterData':
pTag.innerText = resultTxt + mutation.type +'.';
resultcharDataElm.appendChild(pTag);
break;
}
});
});
//set up observers with target(dom element) and observer config object
observer.observe(btnsHolder, observerConfigAttribute);
observer.observe(removeBtnsHolder, observerConfigChildList);
observer.observe(charUpdate, observerConfigCharData);
//Timers - used to simulate dynamic updates
// style change timer on first set of buttons
timer = (elm, time) => {
time = time * 2000;
setTimeout(() => {
elm.classList.add("styledBtnChange");
}, time);
}
setTimeout(() => {
btnsStyle.forEach((btn, index) => {
timer(btn, index);
});
}, 500);
// timers to add remove button is second set
setTimeout(function(){
btnsRemoveStyle[1].parentNode.removeChild(btnsRemoveStyle[1]);
}, 5000);
setTimeout(function(){
btnsRemoveStyle[0].parentNode.insertBefore(btnsRemoveStyle[1], btnsRemoveStyle[0].nextSibling);
}, 7000);