This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjquery.mutation-summary.js
177 lines (140 loc) · 6.02 KB
/
jquery.mutation-summary.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*!
* @license jquery-mutation-summary
* Copyright © 2012, 2013, 2014, 2015, 2016, 2017, Joel Purra <https://joelpurra.com/>
* Released under MIT, BSD and GPL license. Comply with at least one.
*
* A jQuery wrapper/plugin for mutation-summary, the DOM mutation-observers wrapper.
* https://joelpurra.com/projects/jquery-mutation-summary
*
* "Mutation Summary is a JavaScript library that makes observing changes to the DOM fast, easy and safe."
* https://github.com/rafaelw/mutation-summary
*/
/* global
jQuery,
MutationSummary,
*/
(function($, global) {
"use strict";
var tag = "JqueryMutationSummary",
JqueryMutationSummary = function(element, options) {
this.$element = $(element);
this.options = $.extend(true, {}, this.internalDefaults, $.fn.mutationSummary.defaults, options);
},
JqueryMutationSummaryInner = function($element, configuration) {
this.$element = $element;
this.configuration = configuration;
},
privateFunctions = {};
$.extend(true, privateFunctions, {
getConfiguration: function(callback, observeOwnChanges, queries) {
var configuration;
if ($.isFunction(callback)) {
if ($.isArray(observeOwnChanges)) {
queries = observeOwnChanges;
observeOwnChanges = false;
}
configuration = {
callback: callback,
observeOwnChanges: observeOwnChanges === true,
queries: queries || [],
};
} else {
configuration = callback;
}
return configuration;
},
});
JqueryMutationSummary.prototype = {
constructor: JqueryMutationSummary,
internalDefaults: {
mutationSummaries: [],
},
connect: function(callback, observeOwnChanges, queries) {
var configuration = privateFunctions.getConfiguration(callback, observeOwnChanges, queries),
inner = new JqueryMutationSummaryInner(this.$element, configuration);
this.options.mutationSummaries.push(inner);
inner.start();
},
disconnect: function(callback, observeOwnChanges, queries) {
// Pass as reference to inner function
var summaries = this.options.mutationSummaries;
// If any parameters were passed, only disconnect any matching summaries
$.each(summaries, function(index) {
// Take care of deleted summaries
if (this === undefined) {
return;
}
if (this.configurationMatches(callback, observeOwnChanges, queries)) {
this.stop();
delete summaries[index];
}
});
},
};
JqueryMutationSummaryInner.prototype = {
constructor: JqueryMutationSummaryInner,
getCallbackWrapper: function() {
function callbackWrapper(summaries) {
// Pass extra info in the callback, since it's so wrapped
summaries.observer = this.observer;
summaries.configuration = $.extend(true, {}, this.configuration);
this.originalCallback(summaries);
};
return $.proxy(callbackWrapper, this);
},
configurationMatches: function(callback, observeOwnChanges, queries) {
var matchWith = privateFunctions.getConfiguration(callback, observeOwnChanges, queries),
isMatch = true;
isMatch = isMatch && (callback === undefined || this.configuration.callback === matchWith.callback);
isMatch = isMatch && (observeOwnChanges === undefined || this.configuration.observeOwnChanges === matchWith.observeOwnChanges);
isMatch = isMatch && (queries === undefined || this.configuration.queries === matchWith.queries);
return isMatch;
},
start: function() {
var rawElement = this.$element.get(0);
this.originalCallback = this.configuration.callback;
this.wrappedCallback = this.getCallbackWrapper();
this.wrappedConfiguration = $.extend(true, {}, this.configuration);
if (this.$element.length === 1) {
// mutation-summary fails if passing global
if (rawElement !== global) {
this.wrappedConfiguration.rootNode = rawElement;
}
}
this.wrappedConfiguration.callback = this.wrappedCallback;
this.observer = new MutationSummary(this.wrappedConfiguration);
},
stop: function() {
// Any changes from the last callback will be passed here
// https://github.com/rafaelw/mutation-summary/blob/master/APIReference.md#methods
var finalSummary = this.observer.disconnect();
if (finalSummary !== undefined) {
this.wrappedCallback(finalSummary);
}
delete this.observer;
},
};
// Add jQuery method
// $("#element").mutationSummary();
// $("#element").mutationSummary("method", arguments);
$.fn.extend({
mutationSummary: function(option) {
var callArguments = arguments;
return this.each(function() {
var $this = $(this),
data = $this.data(tag),
options = typeof option === "object" && option;
// Store javascript object as element data
if (!data) {
$this.data(tag, (data = new JqueryMutationSummary(this, options)));
}
// Pass arguments to methods
if (typeof option === "string") {
data[option].apply(data, Array.prototype.slice.call(callArguments, 1));
}
});
},
});
$.fn.mutationSummary.defaults = {};
$.fn.mutationSummary.Constructor = JqueryMutationSummary;
}(jQuery, this));