-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
76 lines (64 loc) · 1.73 KB
/
index.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
(function($){
// Replace easy ones
$.fn.html = readWrite($.fn.html);
$.fn.addClass = write($.fn.addClass);
$.fn.append = write($.fn.append);
$.fn.prepend = write($.fn.prepend);
$.fn.before = write($.fn.before);
$.fn.after = write($.fn.after);
$.fn.replaceWith = write($.fn.replaceWith);
// Wrap similar methods
$.each({
appendTo: 'append',
prependTo: 'prepend',
insertBefore: 'before',
insertAfter: 'after',
replaceAll: 'replaceWith'
}, function(name, original) {
$.fn[name] = function(selector, done) {
done = done || function(){};
var insert = $(selector);
var l = insert.length;
var last = l - 1;
var ret = [];
var elems;
for (var i = 0; i < l; i++) {
elems = i === last ? this : this.clone(true);
$(insert[i])[original](elems, done);
}
};
});
function write(original) {
return function(param, done) {
var ctx = this;
fastdom.write(function() {
original.call(ctx, param);
if (done) done();
});
};
}
function readWrite(original) {
return function(str, done) {
// Accept callback as first
// or second argument
if (typeof str === 'function') {
done = str;
str = null;
}
// Callback is optional
done = done || function(){};
var ctx = this;
var type = str ? 'write' : 'read';
// If a string is not provided
// we don't want to call the
// original method with any args
var args = str ? [str] : [];
// Perform the original jquery
// method, but wrapped inside
// a fastdom callback
fastdom[type](function() {
done(original.apply(ctx, args));
});
};
}
})(jQuery);