From 539d0787c69876c20b1d1457d80108e77732ccfa Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 22 May 2015 21:32:11 +0200 Subject: [PATCH] Add WoltLab/WCF/DOM/Change/Listener --- .../js/WoltLab/WCF/DOM/Change/Listener.js | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 wcfsetup/install/files/js/WoltLab/WCF/DOM/Change/Listener.js 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; +}); -- 2.20.1