Rebinding event handlers after certain actions took place
authorAlexander Ebert <ebert@woltlab.com>
Thu, 8 Jan 2015 20:45:18 +0000 (21:45 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 8 Jan 2015 20:45:18 +0000 (21:45 +0100)
Undo/Redo are two examples which cause issues because they internally work by saving the DOM and replacing it with the stored content. Given the fact that this works on a string, all event handlers are lost and must be reattached once the action has completed.

Luckily we can hook into this.observe.load() which is always invoked after such an action took place.

wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js

index 677a77d115392e5b06dce4773efa1db6d676287f..bf310099699646b279e94a6831b57fc610689b12 100644 (file)
@@ -143,6 +143,12 @@ RedactorPlugins.wbbcode = function() {
                        if ($index > -1) {
                                this.opts.verifiedTags.splice($index, 1);
                        }
+                       
+                       // reattach event listeners
+                       WCF.System.Event.addListener('com.woltlab.wcf.redactor', 'observe_load_' + $identifier, (function(data) {
+                               this.wbbcode.observeCodeListings();
+                               this.wbbcode.observeQuotes();
+                       }).bind(this));
                },
                
                /**
@@ -1558,13 +1564,7 @@ RedactorPlugins.wbbcode = function() {
                 * Initializes source editing for quotes.
                 */
                observeQuotes: function() {
-                       var $editHeader = this.$editor.find('.redactorQuoteEdit:not(.jsRedactorQuoteEdit)');
-                       if ($editHeader.length) {
-                               $editHeader.each((function(index, editHeader) {
-                                       var $editHeader = $(editHeader);
-                                       $editHeader.addClass('jsRedactorQuoteEdit').click($.proxy(this.wbbcode._observeQuotesClick, this));
-                               }).bind(this));
-                       }
+                       this.$editor.find('.redactorQuoteEdit').off('click.wbbcode').on('click.wbbcode', $.proxy(this.wbbcode._observeQuotesClick, this));
                },
                
                /**
@@ -1600,10 +1600,14 @@ RedactorPlugins.wbbcode = function() {
                 * Initializes editing for code listings.
                 */
                observeCodeListings: function() {
-                       this.$editor.find('.codeBox:not(.jsRedactorCodeBox)').each((function(index, codeBox) {
-                               var $codeBox = $(codeBox).addClass('jsRedactorCodeBox');
-                               var $editBox = $('<div class="redactorEditCodeBox"><div>' + WCF.Language.get('wcf.bbcode.code.edit') + '</div></div>').insertAfter($codeBox.find('> div > div > h3'));
-                               $editBox.click((function() {
+                       this.$editor.find('.codeBox').each((function(index, codeBox) {
+                               var $codeBox = $(codeBox);
+                               var $editBox = $codeBox.find('.redactorEditCodeBox');
+                               if (!$editBox.length) {
+                                       $editBox = $('<div class="redactorEditCodeBox"><div>' + WCF.Language.get('wcf.bbcode.code.edit') + '</div></div>').insertAfter($codeBox.find('> div > div > h3'));
+                               }
+                               
+                               $editBox.off('click.wbbcode').on('click.wbbcode', (function() {
                                        this.wbbcode._handleInsertCode($codeBox, false);
                                }).bind(this));
                        }).bind(this));
index 78bf3f02e334f57e00dd057a281fe15f849d4026..d3aff35eb96ecd1cdace7a652b98c70619361ee1 100644 (file)
@@ -788,6 +788,14 @@ RedactorPlugins.wmonkeypatch = function() {
                                $toggleButtons(parent, 'sup', 'a.re-superscript', false, 'redactor-act');
                        }).bind(this);
                        
+                       // observe.load
+                       var $mpLoad = this.observe.load;
+                       this.observe.load = (function() {
+                               $mpLoad.call(this);
+                               
+                               WCF.System.Event.fireEvent('com.woltlab.wcf.redactor', 'observe_load_' + this.$textarea.wcfIdentify());
+                       }).bind(this);
+                       
                        // observe.showTooltip
                        var $mpShowTooltip = this.observe.showTooltip;
                        this.observe.showTooltip = (function(e) {