Skip to content

Commit 572f1aa

Browse files
committed
Added detach methods and fixed update method.
1 parent 45f922f commit 572f1aa

File tree

3 files changed

+146
-14
lines changed

3 files changed

+146
-14
lines changed

src/ElementQueries.js

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
* @constructor
1212
*/
1313
var ElementQueries = this.ElementQueries = function() {
14+
15+
this.withTracking = false;
16+
var elements = [];
17+
1418
/**
1519
*
1620
* @param element
@@ -28,9 +32,8 @@
2832
*
2933
* @copyright https://github.com/Mr0grog/element-query/blob/master/LICENSE
3034
*
31-
* @param element
32-
* @param value
33-
* @param units
35+
* @param {HTMLElement} element
36+
* @param {*} value
3437
* @returns {*}
3538
*/
3639
function convertToPx(element, value) {
@@ -139,11 +142,15 @@
139142
} else {
140143
element.elementQueriesSetupInformation = new SetupInformation(element);
141144
element.elementQueriesSetupInformation.addOption(options);
142-
new ResizeSensor(element, function() {
145+
element.elementQueriesSensor = new ResizeSensor(element, function() {
143146
element.elementQueriesSetupInformation.call();
144147
});
145148
}
146149
element.elementQueriesSetupInformation.call();
150+
151+
if (this.withTracking) {
152+
elements.push(element);
153+
}
147154
}
148155

149156
/**
@@ -218,23 +225,72 @@
218225

219226
/**
220227
* Searches all css rules and setups the event listener to all elements with element query rules..
228+
*
229+
* @param {Boolean} withTracking allows and requires you to use detach, since we store internally all used elements
230+
* (no garbage collection possible if you don not call .detach() first)
221231
*/
222-
this.init = function() {
232+
this.init = function(withTracking) {
233+
this.withTracking = withTracking;
223234
for (var i = 0, j = document.styleSheets.length; i < j; i++) {
224235
readRules(document.styleSheets[i].cssText || document.styleSheets[i].cssRules || document.styleSheets[i].rules);
225236
}
226237
};
227238

228-
this.update = function() {
239+
/**
240+
*
241+
* @param {Boolean} withTracking allows and requires you to use detach, since we store internally all used elements
242+
* (no garbage collection possible if you don not call .detach() first)
243+
*/
244+
this.update = function(withTracking) {
245+
this.withTracking = withTracking;
229246
this.init();
230247
};
248+
249+
this.detach = function() {
250+
if (!this.withTracking) {
251+
throw 'withTracking is not enabled. We can not detach elements since we don not store it.' +
252+
'Use ElementQueries.withTracking = true; before domready.';
253+
}
254+
255+
var element;
256+
while (element = elements.pop()) {
257+
ElementQueries.detach(element);
258+
}
259+
260+
elements = [];
261+
};
231262
};
232263

233-
function init() {
234-
ElementQueries.instance = new ElementQueries().init();
235-
ElementQueries.update = function() {
236-
ElementQueries.instance.update();
264+
/**
265+
*
266+
* @param {Boolean} withTracking allows and requires you to use detach, since we store internally all used elements
267+
* (no garbage collection possible if you don not call .detach() first)
268+
*/
269+
ElementQueries.update = function(withTracking) {
270+
ElementQueries.instance.update(withTracking);
271+
};
272+
273+
/**
274+
* Removes all sensor and elementquery information from the element.
275+
*
276+
* @param {HTMLElement} element
277+
*/
278+
ElementQueries.detach = function(element) {
279+
if (element.elementQueriesSetupInformation) {
280+
element.elementQueriesSensor.detach();
281+
delete element.elementQueriesSetupInformation;
282+
delete element.elementQueriesSensor;
283+
console.log('detached');
284+
} else {
285+
console.log('detached already', element);
237286
}
287+
};
288+
289+
ElementQueries.withTracking = false;
290+
291+
function init() {
292+
ElementQueries.instance = new ElementQueries();
293+
ElementQueries.instance.init(ElementQueries.withTracking);
238294
}
239295

240296
if (window.addEventListener) {

src/ResizeSensor.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@
102102
reset();
103103

104104
var changed = function() {
105-
element.resizedAttached.call();
105+
if (element.resizedAttached) {
106+
element.resizedAttached.call();
107+
}
106108
};
107109

108110
var addEvent = function(el, name, cb) {
@@ -139,6 +141,18 @@
139141
} else {
140142
attachResizeEvent(element, callback);
141143
}
142-
}
143144

144-
})();
145+
this.detach = function() {
146+
ResizeSensor.detach(element);
147+
};
148+
};
149+
150+
this.ResizeSensor.detach = function(element) {
151+
if (element.resizeSensor) {
152+
element.removeChild(element.resizeSensor);
153+
delete element.resizeSensor;
154+
delete element.resizedAttached;
155+
}
156+
};
157+
158+
})();

test/index.html

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,29 @@
327327
}
328328
}
329329

330+
.dynamicElement {
331+
margin: 50px;
332+
width: 50px;
333+
334+
-moz-animation-name: animHeight;
335+
animation-name: animHeight;
336+
-webkit-animation-name: animHeight;
337+
338+
animation-duration: 3s;
339+
-moz-animation-duration: 3s;
340+
-webkit-animation-duration: 3s;
341+
animation-iteration-count: infinite;
342+
-webkit-animation-iteration-count: infinite;
343+
344+
display: inline-block;
345+
border: 1px solid #eee;
346+
background-color: #f9f9f9;
347+
}
348+
349+
.dynamic {
350+
margin: 15px;
351+
}
352+
330353
</style>
331354
Drag the gray line at the right to see it in action.
332355
</head>
@@ -378,7 +401,7 @@ <h2>Demo 4 - height<button id="startStop4">Start/Stop</button></h2>
378401
</div>
379402
</div>
380403

381-
<div style="height: 150px">
404+
<div style="height: 200px">
382405
<div class="examplesResizerDemosXY">
383406
<div id="example-5">
384407
0 changes
@@ -426,5 +449,44 @@ <h2>Demo 4 - height<button id="startStop4">Start/Stop</button></h2>
426449
});
427450
})();
428451
</script>
452+
<div class="dynamic">
453+
<input type="text" value="10" id="dynamicCount" />
454+
<button onclick="addDynamic()">Add</button>
455+
<button onclick="detachDynamic()">Detach</button>
456+
<button onclick="removeDynamic()">Remove</button>
457+
<div id="dynamicCounter"></div>
458+
<div id="dynamicContainer"></div>
459+
</div>
460+
<script>
461+
var container = $('dynamicContainer');
462+
var dynamicCount = $('dynamicCount');
463+
var dynamicCounter = $('dynamicCounter');
464+
465+
window.detachDynamic = function() {
466+
container.getChildren().each(function(element) {
467+
ResizeSensor.detach(element);
468+
});
469+
};
470+
471+
window.removeDynamic = function() {
472+
container.empty();
473+
};
474+
475+
window.addDynamic = function() {
476+
container.empty();
477+
var i = 0, to = dynamicCount.value, div, counter = 0;
478+
for (; i < to; i++) {
479+
div = new Element('div', {
480+
'class': 'dynamicElement',
481+
text: '#' + i
482+
}).inject(container);
483+
484+
new ResizeSensor(div, function(){
485+
counter++;
486+
dynamicCounter.set('text', counter + ' changes.');
487+
});
488+
}
489+
}
490+
</script>
429491
</div>
430492
</body>

0 commit comments

Comments
 (0)