Add WoltLab/WCF/DOM/Change/Listener
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 22 May 2015 19:32:11 +0000 (21:32 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 22 May 2015 19:32:11 +0000 (21:32 +0200)
wcfsetup/install/files/js/WoltLab/WCF/DOM/Change/Listener.js [new file with mode: 0644]

diff --git a/wcfsetup/install/files/js/WoltLab/WCF/DOM/Change/Listener.js b/wcfsetup/install/files/js/WoltLab/WCF/DOM/Change/Listener.js
new file mode 100644 (file)
index 0000000..d2b8576
--- /dev/null
@@ -0,0 +1,53 @@
+/**
+ * Allows to be informed when the DOM may have changed and
+ * new elements that are relevant to may you have been added.
+ * 
+ * @author     Tim Duesterhus
+ * @copyright  2001-2015 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module     WoltLab/WCF/DOM/Change/Listener
+ */
+define(['CallbackList'], function(CallbackList) {
+       "use strict";
+       
+       var _callbackList = new CallbackList();
+       var _hot = false;
+       
+       /**
+        * @constructor
+        */
+       function Listener() { };
+       Listener.prototype = {
+               /**
+                * @see WoltLab/WCF/CallbackList#add
+                */
+               add: _callbackList.add.bind(_callbackList),
+               
+               /**
+                * @see WoltLab/WCF/CallbackList#remove
+                */
+               remove: _callbackList.remove.bind(_callbackList),
+               
+               /**
+                * Triggers the execution of all the listeners.
+                * Use this function when you added new elements to the DOM that might
+                * be relevant to others.
+                * While this function is in progress further calls to it will be ignored.
+                */
+               trigger: function() {
+                       if (_hot) return;
+                       
+                       try {
+                               _hot = true;
+                               _callbackList.forEach(null, function(callback) {
+                                       callback();
+                               });
+                       }
+                       finally {
+                               _hot = false;
+                       }
+               }
+       };
+       
+       return Listener;
+});