|
13 | 13 |
|
14 | 14 | "use strict"; |
15 | 15 |
|
16 | | - // List of mutation types that are observable. |
17 | | - var mtypes = ['childList', 'attributes']; |
18 | | - |
19 | 16 | var combinators = [' ', '>', '+', '~']; // https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors#Combinators |
20 | | - var fraternisers = ['+', '~']; |
21 | | - var complexTypes = ['ATTR', 'PSEUDO', 'ID', 'CLASS']; |
| 17 | + var fraternisers = ['+', '~']; // These combinators involve siblings. |
| 18 | + var complexTypes = ['ATTR', 'PSEUDO', 'ID', 'CLASS']; // These selectors are based upon attributes. |
22 | 19 |
|
| 20 | + // Understand what kind of selector the initializer is based upon. |
23 | 21 | function grok(msobserver) { |
24 | 22 | if (!$.find.tokenize) { |
25 | 23 | // This is an old version of jQuery, so cannot parse the selector. |
|
32 | 30 | return; |
33 | 31 | } |
34 | 32 |
|
| 33 | + // Parse the selector. |
35 | 34 | msobserver.isCombinatorial = false; |
36 | 35 | msobserver.isFraternal = false; |
37 | 36 | msobserver.isComplex = false; |
38 | | - |
39 | | - // Search for combinators. |
40 | 37 | let token = $.find.tokenize(msobserver.selector); |
41 | 38 | for (let i = 0; i < token.length; i++) { |
42 | 39 | for (let j = 0; j < token[i].length; j++) { |
43 | 40 | if (combinators.indexOf(token[i][j].type) != -1) |
44 | | - msobserver.isCombinatorial = true; |
| 41 | + msobserver.isCombinatorial = true; // This selector uses combinators. |
45 | 42 |
|
46 | 43 | if (fraternisers.indexOf(token[i][j].type) != -1) |
47 | | - msobserver.isFraternal = true; |
| 44 | + msobserver.isFraternal = true; // This selector uses sibling combinators. |
48 | 45 |
|
49 | 46 | if (complexTypes.indexOf(token[i][j].type) != -1) |
50 | | - msobserver.isComplex = true; |
| 47 | + msobserver.isComplex = true; // This selector is based on attributes. |
51 | 48 | } |
52 | 49 | } |
53 | 50 | } |
|
92 | 89 | // For each mutation. |
93 | 90 | for (var m = 0; m < mutations.length; m++) { |
94 | 91 |
|
95 | | - // Do we observe this mutation type? |
96 | | - if (mtypes.indexOf(mutations[m].type) == -1) continue; |
97 | | - |
98 | 92 | // If this is an attributes mutation, then the target is the node upon which the mutation occurred. |
99 | 93 | if (mutations[m].type == 'attributes') { |
100 | 94 | // Check if the mutated node matchs. |
|
108 | 102 | // If the selector is combinatorial, query descendants of the mutated node for matches. |
109 | 103 | else if (msobserver.isCombinatorial) |
110 | 104 | mutations[m].target.querySelectorAll(msobserver.selector).forEach(push); |
111 | | - } else if (mutations[m].type == 'childList') { |
| 105 | + } |
| 106 | + |
| 107 | + // If this is an childList mutation, then inspect added nodes. |
| 108 | + if (mutations[m].type == 'childList') { |
112 | 109 |
|
113 | 110 | // Search added nodes for matching selectors. |
114 | 111 | for (var n = 0; n < mutations[m].addedNodes.length; n++) { |
|
129 | 126 | } |
130 | 127 | } |
131 | 128 |
|
| 129 | + // For each match, call the callback using jQuery.each() to initialize the element (once only.) |
132 | 130 | matches.forEach(function(match) { |
133 | 131 | $(match).each(msobserver.callback); |
134 | 132 | }); |
|
149 | 147 | msobservers.initialize(selector, callback, $.extend({}, $.initialize.defaults, options)); |
150 | 148 | }; |
151 | 149 |
|
| 150 | + // Options |
152 | 151 | $.initialize.defaults = { |
153 | | - target: document.documentElement, // Defaults observe the entire document. |
154 | | - observer: null |
| 152 | + target: document.documentElement, // Defaults to observe the entire document. |
| 153 | + observer: null // MutationObserverInit: Defaults to internal configuration if not provided. |
155 | 154 | } |
156 | 155 |
|
157 | 156 | })(jQuery); |
0 commit comments