|
11 | 11 | * @constructor |
12 | 12 | */ |
13 | 13 | var ElementQueries = this.ElementQueries = function() { |
| 14 | + |
| 15 | + this.withTracking = false; |
| 16 | + var elements = []; |
| 17 | + |
14 | 18 | /** |
15 | 19 | * |
16 | 20 | * @param element |
|
28 | 32 | * |
29 | 33 | * @copyright https://github.com/Mr0grog/element-query/blob/master/LICENSE |
30 | 34 | * |
31 | | - * @param element |
32 | | - * @param value |
33 | | - * @param units |
| 35 | + * @param {HTMLElement} element |
| 36 | + * @param {*} value |
34 | 37 | * @returns {*} |
35 | 38 | */ |
36 | 39 | function convertToPx(element, value) { |
|
139 | 142 | } else { |
140 | 143 | element.elementQueriesSetupInformation = new SetupInformation(element); |
141 | 144 | element.elementQueriesSetupInformation.addOption(options); |
142 | | - new ResizeSensor(element, function() { |
| 145 | + element.elementQueriesSensor = new ResizeSensor(element, function() { |
143 | 146 | element.elementQueriesSetupInformation.call(); |
144 | 147 | }); |
145 | 148 | } |
146 | 149 | element.elementQueriesSetupInformation.call(); |
| 150 | + |
| 151 | + if (this.withTracking) { |
| 152 | + elements.push(element); |
| 153 | + } |
147 | 154 | } |
148 | 155 |
|
149 | 156 | /** |
|
218 | 225 |
|
219 | 226 | /** |
220 | 227 | * 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) |
221 | 231 | */ |
222 | | - this.init = function() { |
| 232 | + this.init = function(withTracking) { |
| 233 | + this.withTracking = withTracking; |
223 | 234 | for (var i = 0, j = document.styleSheets.length; i < j; i++) { |
224 | 235 | readRules(document.styleSheets[i].cssText || document.styleSheets[i].cssRules || document.styleSheets[i].rules); |
225 | 236 | } |
226 | 237 | }; |
227 | 238 |
|
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; |
229 | 246 | this.init(); |
230 | 247 | }; |
| 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 | + }; |
231 | 262 | }; |
232 | 263 |
|
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); |
237 | 286 | } |
| 287 | + }; |
| 288 | + |
| 289 | + ElementQueries.withTracking = false; |
| 290 | + |
| 291 | + function init() { |
| 292 | + ElementQueries.instance = new ElementQueries(); |
| 293 | + ElementQueries.instance.init(ElementQueries.withTracking); |
238 | 294 | } |
239 | 295 |
|
240 | 296 | if (window.addEventListener) { |
|
0 commit comments