|
7 | 7 | * https://github.com/timpler/jquery.initialize/blob/master/LICENSE |
8 | 8 | */ |
9 | 9 | ;(function ($) { |
10 | | - |
| 10 | + |
11 | 11 | "use strict"; |
12 | | - |
| 12 | + |
13 | 13 | // MutationSelectorObserver represents a selector and it's associated initialization callback. |
14 | 14 | var MutationSelectorObserver = function (selector, callback, options) { |
15 | 15 | this.selector = selector; |
|
23 | 23 | // List of MutationSelectorObservers. |
24 | 24 | var msobservers = []; |
25 | 25 | msobservers.initialize = function (selector, callback, options) { |
26 | | - var target = options.target || document.documentElement; |
27 | 26 |
|
28 | 27 | // Wrap the callback so that we can ensure that it is only |
29 | 28 | // called once per element. |
|
36 | 35 | }; |
37 | 36 |
|
38 | 37 | // See if the selector matches any elements already on the page. |
39 | | - $(selector).each(callbackOnce); |
| 38 | + $(options.target).find(selector).each(callbackOnce); |
40 | 39 |
|
41 | 40 | // Then, add it to the list of selector observers. |
42 | 41 | var msobserver = new MutationSelectorObserver(selector, callbackOnce, options) |
|
45 | 44 | // The MutationObserver watches for when new elements are added to the DOM. |
46 | 45 | var observer = new MutationObserver(function (mutations) { |
47 | 46 |
|
| 47 | + var matches = []; |
| 48 | + function add(match) { |
| 49 | + matches.push(match); |
| 50 | + } |
| 51 | + |
48 | 52 | // For each mutation. |
49 | 53 | for (var m = 0; m < mutations.length; m++) { |
50 | | - console.log(mutations[m]); |
51 | 54 |
|
52 | 55 | // Do we observe this mutation type? |
53 | 56 | if ($.inArray(mutations[m].type, mtypes) == -1) continue; |
54 | 57 |
|
55 | | - if (msobserver.options.scanDocument) { |
| 58 | + if (msobserver.options.scanMode == 'target') { |
56 | 59 |
|
57 | 60 | // Search within the observed node for elements matching the selector. |
58 | 61 | // This can take longer, but we are more likely to find a match with |
59 | 62 | // complex selectors. |
60 | | - $(target).find(msobserver.selector) |
61 | | - .each(msobserver.callback); |
62 | | - } else { |
| 63 | + msobserver.options.target.querySelectorAll(msobserver.selector).forEach(add); |
| 64 | + } else if (msobserver.options.scanMode == 'descendants') { |
63 | 65 |
|
64 | 66 | // If this is an attributes mutation, then the target is the node upon which the mutation occurred. |
65 | 67 | 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; |
| 68 | + mutations[m].target.querySelectorAll(msobserver.selector).forEach(add); |
| 69 | + if (mutations[m].target.matches(msobserver.selector)) { |
| 70 | + matches.push(mutations[m].target); |
| 71 | + } |
| 72 | + } else if (mutations[m].type == 'childList') { |
| 73 | + |
| 74 | + // Otherwise, search for added nodes. |
| 75 | + // Search added nodes only for matching selectors. |
| 76 | + for (var n = 0; n < mutations[m].addedNodes.length; n++) { |
| 77 | + |
| 78 | + mutations[m].addedNodes[n].querySelectorAll(msobserver.selector).forEach(add); |
| 79 | + if (mutations[m].addedNodes[n].matches(msobserver.selector)) { |
| 80 | + matches.push(mutations[m].addedNodes[n]); |
| 81 | + } |
| 82 | + } |
71 | 83 | } |
| 84 | + } else if (msobserver.options.scanMode == 'exact') { |
72 | 85 |
|
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. |
| 86 | + // Similar to descendant scan mode, except it will not search within child nodes. |
| 87 | + // This offers the most performance. |
| 88 | + if (mutations[m].type == 'attributes') { |
| 89 | + if (mutations[m].target.matches(msobserver.selector)) |
| 90 | + matches.push(mutations[m].target); |
| 91 | + } else if (mutations[m].type == 'childList') { |
| 92 | + for (var n = 0; n < mutations[m].addedNodes.length; n++) { |
| 93 | + if (mutations[m].addedNodes[n].matches(msobserver.selector)) |
| 94 | + matches.push(mutations[m].addedNodes[n]); |
| 95 | + } |
80 | 96 | } |
| 97 | + |
81 | 98 | } |
82 | 99 | } |
| 100 | + |
| 101 | + matches.forEach(function(match) { |
| 102 | + $(match).each(msobserver.callback); |
| 103 | + }); |
83 | 104 | }); |
84 | 105 |
|
85 | 106 | // Observe the target element. |
86 | | - observer.observe(target, {childList: true, subtree: true, attributes: true}); |
| 107 | + observer.observe(options.target, {childList: true, subtree: true, attributes: true}); |
87 | 108 | }; |
88 | 109 |
|
89 | 110 | // Deprecated API (does not work with jQuery >= 3.1.1): |
|
97 | 118 | }; |
98 | 119 |
|
99 | 120 | $.initialize.defaults = { |
100 | | - scanDocument: true, |
101 | | - target: null // Defaults observe the entire document. |
| 121 | + scanMode: 'target', // Can be either: 'target', 'descendants', or 'exact' |
| 122 | + target: document.documentElement // Defaults observe the entire document. |
102 | 123 | } |
103 | 124 |
|
104 | 125 | })(jQuery); |
0 commit comments