From: Tim Düsterhus Date: Fri, 22 May 2015 19:32:11 +0000 (+0200) Subject: Add WoltLab/WCF/DOM/Change/Listener X-Git-Tag: 3.0.0_Beta_1~2350^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=539d0787c69876c20b1d1457d80108e77732ccfa;p=GitHub%2FWoltLab%2FWCF.git Add WoltLab/WCF/DOM/Change/Listener --- 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 index 0000000000..d2b85769c8 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLab/WCF/DOM/Change/Listener.js @@ -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 + * @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; +});