|
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 | | - var MutationSelectorObserver = function (selector, callback) { |
| 14 | + var MutationSelectorObserver = function (selector, callback, options) { |
15 | 15 | this.selector = selector; |
16 | 16 | this.callback = callback; |
| 17 | + this.options = options; |
17 | 18 | }; |
18 | 19 |
|
| 20 | + // List of mutation types that are observable. |
| 21 | + var mtypes = ['childList', 'attributes']; |
| 22 | + |
19 | 23 | // List of MutationSelectorObservers. |
20 | 24 | var msobservers = []; |
21 | | - msobservers.initialize = function (selector, callback) { |
| 25 | + msobservers.initialize = function (selector, callback, options) { |
22 | 26 |
|
23 | 27 | // Wrap the callback so that we can ensure that it is only |
24 | 28 | // called once per element. |
|
31 | 35 | }; |
32 | 36 |
|
33 | 37 | // See if the selector matches any elements already on the page. |
34 | | - $(selector).each(callbackOnce); |
| 38 | + $(options.target).find(selector).each(callbackOnce); |
35 | 39 |
|
36 | 40 | // Then, add it to the list of selector observers. |
37 | | - this.push(new MutationSelectorObserver(selector, callbackOnce)); |
38 | | - }; |
| 41 | + var msobserver = new MutationSelectorObserver(selector, callbackOnce, options) |
| 42 | + this.push(msobserver); |
| 43 | + |
| 44 | + // The MutationObserver watches for when new elements are added to the DOM. |
| 45 | + var observer = new MutationObserver(function (mutations) { |
| 46 | + |
| 47 | + var matches = []; |
| 48 | + function add(match) { |
| 49 | + matches.push(match); |
| 50 | + } |
| 51 | + |
| 52 | + // For each mutation. |
| 53 | + for (var m = 0; m < mutations.length; m++) { |
| 54 | + |
| 55 | + // Do we observe this mutation type? |
| 56 | + if ($.inArray(mutations[m].type, mtypes) == -1) continue; |
| 57 | + |
| 58 | + if (msobserver.options.scanMode == 'target') { |
39 | 59 |
|
40 | | - // The MutationObserver watches for when new elements are added to the DOM. |
41 | | - var observer = new MutationObserver(function (mutations) { |
| 60 | + // Search within the observed node for elements matching the selector. |
| 61 | + // This can take longer, but we are more likely to find a match with |
| 62 | + // complex selectors. |
| 63 | + msobserver.options.target.querySelectorAll(msobserver.selector).forEach(add); |
| 64 | + } else if (msobserver.options.scanMode == 'descendants') { |
42 | 65 |
|
43 | | - // For each MutationSelectorObserver currently registered. |
44 | | - for (var j = 0; j < msobservers.length; j++) { |
45 | | - $(msobservers[j].selector).each(msobservers[j].callback); |
46 | | - } |
47 | | - }); |
| 66 | + // If this is an attributes mutation, then the target is the node upon which the mutation occurred. |
| 67 | + if (mutations[m].type == 'attributes') { |
| 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') { |
48 | 73 |
|
49 | | - // Observe the entire document. |
50 | | - observer.observe(document.documentElement, {childList: true, subtree: true, attributes: true}); |
| 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 | + if (!(mutations[m].addedNodes[n] instanceof Element)) continue; |
| 78 | + |
| 79 | + mutations[m].addedNodes[n].querySelectorAll(msobserver.selector).forEach(add); |
| 80 | + if (mutations[m].addedNodes[n].matches(msobserver.selector)) { |
| 81 | + matches.push(mutations[m].addedNodes[n]); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } else if (msobserver.options.scanMode == 'exact') { |
| 86 | + |
| 87 | + // Similar to descendant scan mode, except it will not search within child nodes. |
| 88 | + // This offers the most performance. |
| 89 | + if (mutations[m].type == 'attributes') { |
| 90 | + if (mutations[m].target.matches(msobserver.selector)) |
| 91 | + matches.push(mutations[m].target); |
| 92 | + } else if (mutations[m].type == 'childList') { |
| 93 | + for (var n = 0; n < mutations[m].addedNodes.length; n++) { |
| 94 | + if (!(mutations[m].addedNodes[n] instanceof Element)) continue; |
| 95 | + if (mutations[m].addedNodes[n].matches(msobserver.selector)) |
| 96 | + matches.push(mutations[m].addedNodes[n]); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + matches.forEach(function(match) { |
| 104 | + $(match).each(msobserver.callback); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + // Observe the target element. |
| 109 | + observer.observe(options.target, {childList: true, subtree: true, attributes: true}); |
| 110 | + }; |
51 | 111 |
|
52 | 112 | // Deprecated API (does not work with jQuery >= 3.1.1): |
53 | | - $.fn.initialize = function (callback) { |
54 | | - msobservers.initialize(this.selector, callback); |
| 113 | + $.fn.initialize = function (callback, options) { |
| 114 | + msobservers.initialize(this.selector, callback, $.extend({}, $.initialize.defaults, options)); |
55 | 115 | }; |
56 | | - $.initialize = function (selector, callback) { |
57 | | - msobservers.initialize(selector, callback); |
| 116 | + |
| 117 | + // Supported API |
| 118 | + $.initialize = function (selector, callback, options) { |
| 119 | + msobservers.initialize(selector, callback, $.extend({}, $.initialize.defaults, options)); |
58 | 120 | }; |
| 121 | + |
| 122 | + $.initialize.defaults = { |
| 123 | + scanMode: 'target', // Can be either: 'target', 'descendants', or 'exact' |
| 124 | + target: document.documentElement // Defaults observe the entire document. |
| 125 | + } |
| 126 | + |
59 | 127 | })(jQuery); |
0 commit comments