Commit | Line | Data |
---|---|---|
539d0787 TD |
1 | /** |
2 | * Allows to be informed when the DOM may have changed and | |
04c681d2 | 3 | * new elements that are relevant to you may have been added. |
539d0787 TD |
4 | * |
5 | * @author Tim Duesterhus | |
c839bd49 | 6 | * @copyright 2001-2018 WoltLab GmbH |
539d0787 | 7 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
58d7e8f8 | 8 | * @module WoltLabSuite/Core/Dom/Change/Listener |
539d0787 TD |
9 | */ |
10 | define(['CallbackList'], function(CallbackList) { | |
11 | "use strict"; | |
12 | ||
13 | var _callbackList = new CallbackList(); | |
14 | var _hot = false; | |
15 | ||
16 | /** | |
58d7e8f8 | 17 | * @exports WoltLabSuite/Core/Dom/Change/Listener |
539d0787 | 18 | */ |
b0ecc62e | 19 | return { |
539d0787 | 20 | /** |
58d7e8f8 | 21 | * @see WoltLabSuite/Core/CallbackList#add |
539d0787 TD |
22 | */ |
23 | add: _callbackList.add.bind(_callbackList), | |
24 | ||
25 | /** | |
58d7e8f8 | 26 | * @see WoltLabSuite/Core/CallbackList#remove |
539d0787 TD |
27 | */ |
28 | remove: _callbackList.remove.bind(_callbackList), | |
29 | ||
30 | /** | |
31 | * Triggers the execution of all the listeners. | |
32 | * Use this function when you added new elements to the DOM that might | |
33 | * be relevant to others. | |
34 | * While this function is in progress further calls to it will be ignored. | |
35 | */ | |
36 | trigger: function() { | |
37 | if (_hot) return; | |
38 | ||
39 | try { | |
40 | _hot = true; | |
41 | _callbackList.forEach(null, function(callback) { | |
42 | callback(); | |
43 | }); | |
44 | } | |
45 | finally { | |
46 | _hot = false; | |
47 | } | |
48 | } | |
49 | }; | |
539d0787 | 50 | }); |