|
23 | 23 | // List of MutationSelectorObservers. |
24 | 24 | var msobservers = []; |
25 | 25 | msobservers.initialize = function (selector, callback, options) { |
| 26 | + var target = options.target || document.documentElement; |
26 | 27 |
|
27 | 28 | // Wrap the callback so that we can ensure that it is only |
28 | 29 | // called once per element. |
|
38 | 39 | $(selector).each(callbackOnce); |
39 | 40 |
|
40 | 41 | // Then, add it to the list of selector observers. |
41 | | - this.push(new MutationSelectorObserver(selector, callbackOnce, options)); |
42 | | - }; |
| 42 | + var msobserver = new MutationSelectorObserver(selector, callbackOnce, options) |
| 43 | + this.push(msobserver); |
| 44 | + |
| 45 | + // The MutationObserver watches for when new elements are added to the DOM. |
| 46 | + var observer = new MutationObserver(function (mutations) { |
| 47 | + |
| 48 | + // For each mutation. |
| 49 | + for (var m = 0; m < mutations.length; m++) { |
| 50 | + console.log(mutations[m]); |
| 51 | + |
| 52 | + // Do we observe this mutation type? |
| 53 | + if ($.inArray(mutations[m].type, mtypes) == -1) continue; |
| 54 | + |
| 55 | + if (msobserver.options.scanDocument) { |
43 | 56 |
|
44 | | - // The MutationObserver watches for when new elements are added to the DOM. |
45 | | - var observer = new MutationObserver(function (mutations) { |
46 | | - |
47 | | - // For each MutationSelectorObserver currently registered. |
48 | | - for (var j = 0; j < msobservers.length; j++) { |
49 | | - if (msobservers[j].options.scanDocument) { |
50 | | - |
51 | | - // Scan the entire document. |
52 | | - $(msobservers[j].selector) |
53 | | - .each(msobservers[j].callback); |
54 | | - } else { |
55 | | - |
56 | | - // For each mutation. |
57 | | - for (var m = 0; m < mutations.length; m++) { |
58 | | - // If mutation type is observed. |
59 | | - if ($.inArray(mutations[m].type, mtypes) != -1) { |
60 | | - for (var n = 0; n < mutations[m].addedNodes.length; n++) { |
61 | | - $(mutations[m].addedNodes[n]).find(msobservers[j].selector) |
62 | | - .addBack(msobservers[j].selector) |
63 | | - .each(msobservers[j].callback); |
64 | | - } |
| 57 | + // Search within the observed node for elements matching the selector. |
| 58 | + // This can take longer, but we are more likely to find a match with |
| 59 | + // complex selectors. |
| 60 | + $(target).find(msobserver.selector) |
| 61 | + .each(msobserver.callback); |
| 62 | + } else { |
| 63 | + |
| 64 | + // If this is an attributes mutation, then the target is the node upon which the mutation occurred. |
| 65 | + if (mutations[m].type == 'attributes') { |
| 66 | + $(mutations[m].target) |
| 67 | + .find(msobserver.selector) // Find any descendent nodes matching selector |
| 68 | + .addBack(msobserver.selector) // Include the mutated node itself. |
| 69 | + .each(msobserver.callback); // initialize with the callback. |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + // Otherwise, search for added nodes. |
| 74 | + // Search added nodes only for matching selectors. |
| 75 | + for (var n = 0; n < mutations[m].addedNodes.length; n++) { |
| 76 | + $(mutations[m].addedNodes[n]) |
| 77 | + .find(msobserver.selector) // Find any descendent nodes matching selector |
| 78 | + .addBack(msobserver.selector) // Include the added node itself. |
| 79 | + .each(msobserver.callback); // initialize with the callback. |
65 | 80 | } |
66 | 81 | } |
67 | 82 | } |
68 | | - } |
69 | | - }); |
| 83 | + }); |
70 | 84 |
|
71 | | - // Observe the entire document. |
72 | | - observer.observe(document.documentElement, {childList: true, subtree: true, attributes: true}); |
| 85 | + // Observe the target element. |
| 86 | + observer.observe(target, {childList: true, subtree: true, attributes: true}); |
| 87 | + }; |
73 | 88 |
|
74 | 89 | // Deprecated API (does not work with jQuery >= 3.1.1): |
75 | 90 | $.fn.initialize = function (callback, options) { |
|
82 | 97 | }; |
83 | 98 |
|
84 | 99 | $.initialize.defaults = { |
85 | | - scanDocument: true |
| 100 | + scanDocument: true, |
| 101 | + target: null // Defaults observe the entire document. |
86 | 102 | } |
87 | 103 |
|
88 | 104 | })(jQuery); |
0 commit comments