-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (53 loc) · 1.37 KB
/
index.js
File metadata and controls
64 lines (53 loc) · 1.37 KB
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
(function(){
// constructor
// give it the element to insert html into
// html that should be inserted
// method - html, prepend or append
// returns a promise that is resolved after insertion
function InsertHTML(el, html, method){
this.rAF = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame;
var promise = this.insert(el, html, method);
return promise;
}
// set up the inserting
// return a promise
InsertHTML.prototype.insert = function(el, html, method){
var $el = $(el);
var insertMethod = method || 'html';
var promise = $.Deferred();
var observer = new WebKitMutationObserver(function(mutations){
observer.disconnect();
this.requestAnimationFrame(
function(){
promise.resolve()
}
)
}.bind(this));
observer.observe(
$el[0],
{
'childList': true
}
);
this.requestAnimationFrame(
function(){
$el[insertMethod](html);
}
);
return promise;
}
// use requestAnimation to do
// the actual insertion
InsertHTML.prototype.requestAnimationFrame = function(fn){
var r = this.rAF;
r(fn.bind(this));
}
// check if we've got require
if(typeof module !== "undefined"){
module.exports = InsertHTML;
}
else{
window.InsertHTML = InsertHTML;
}
}()); // end wrapper