-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.waitforChild.js
113 lines (96 loc) · 3.08 KB
/
jquery.waitforChild.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*!
* jQuery waitforChild Plugin v0.0.1
* https://github.com/amenadiel/jquery.waitforChild
*
* Copyright 2015 Felipe Figueroa
* Released under the MIT license
*/
(function (root, factory) {
if (typeof define === "function" && define.amd) {
// AMD (+ global for extensions)
define(['jquery'], factory);
} else if (typeof module !== 'undefined' && typeof exports === "object") {
// CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser
root.jQuery = factory(root.jQuery);
}
}(this, function ($) {
'use strict';
/**
* Will execute a function on matching child elements, or set a MutationObserver to detect if they are appended afterwards
* @param {function} onFound function to execute on matching elements once they exist
* @param {String} [querySelector] optional CSS type selector to filter which elements should receive the onFound function
* @param {Boolean} [once] optional flag to execute the onFound function only on the first matching child
* @return {object} the element, as to keep the return chainable
*/
$.fn.waitforChild = function (onFound, querySelector, once) {
// allows for an object single parameter
if (typeof arguments[0] === 'object') {
once = arguments[0].once || false;
querySelector = arguments[0].querySelector || null;
onFound = arguments[0].onFound;
}
if (!onFound) {
onFound = function () {};
}
var $this = this;
// If no querySelector was asked, and the element has children, apply the onFound function either to the first or to all of them
if (!querySelector && $this.children().length) {
if (once) {
onFound($this.children().first());
} else {
$this.children().each(function (key, element) {
onFound($(element));
});
}
// If the element already has matching children, apply the onFound function either to the first or to all of them
} else if ($this.find(querySelector).length !== 0) {
if (once) {
onFound($this.find(querySelector).first());
} else {
$this.find(querySelector).each(function (key, element) {
onFound($(element));
});
}
} else {
if ($this.length === 0) {
console.warn("Can't attach an observer to a null node", $this);
} else {
// Otherwise, set a new MutationObserver and inspect each new inserted child from now on.
var observer = new MutationObserver(function (mutations) {
var _this = this;
mutations.forEach(function (mutation) {
if (mutation.addedNodes) {
if (!querySelector) {
onFound($(mutation.addedNodes[0]));
if (once) {
_this.disconnect();
}
} else {
for (var i = 0; i < mutation.addedNodes.length; ++i) {
var addedNode = mutation.addedNodes[i];
if ($(addedNode).is(querySelector)) {
onFound($(addedNode));
if (once) {
_this.disconnect();
break;
}
}
}
}
}
});
});
observer.observe($this[0], {
childList: true,
subtree: true,
attributes: false,
characterData: false
});
}
}
return $this;
};
}));