material icon indicating copy to clipboard operation
material copied to clipboard

firefox hangs when targetEvent in $mddialog is not present.

Open lucasmartinez opened this issue 11 years ago • 3 comments

When $event is not sent on ng-click, firefox hangs. You can see an example in http://jsfiddle.net/Ly4n3d0q/6/. Press open dialog. After dialog is shown, press esc to close and press Open dialog again to re open dialog. In chrom it works fine, but firefox hangs. Is this a bug ? is targetEvent optional ?

lucasmartinez avatar Apr 06 '15 14:04 lucasmartinez

This can also be seen directly with the Angular Material Design Demo: http://codepen.io/anon/pen/JdXbOz

(I've commented out passing the event argument to the showDialog methods for the 2nd and 3rd examples).

I just tried the codepen and the jsfiddle in FireFox version 38 and everything works.

However, we need to support older versions of FireFox and both the codepen and the fiddle break at least on FireFox version 31.

nkoterba avatar May 13 '15 11:05 nkoterba

So I've started to track down where this may be occurring...I actually don't think the inclusion or exclusion of the $event object causes the issue...I think it may actually have to do with timing and/or promises.

What I've noticed is that when the dialog is closed, the following code is executed:

https://github.com/angular/material/blob/master/src/components/dialog/dialog.js#L559-566:

      return dialogPopOut(
        element,
        options.parent,
        options.popInTarget && options.popInTarget.length && options.popInTarget
      ).then(function() {
        element.remove();
        options.popInTarget && options.popInTarget.focus();
      });

Putting a breakpoint at the dialogPopOut line and then at the first line in the promise element.remove() and loading the JSFiddle example above, I've observed that the promise callback is sometimes never called.

Diving into the dialogPopOut method: https://github.com/angular/material/blob/master/src/components/dialog/dialog.js#L655-662

    function dialogPopOut(container, parentElement, clickElement) {
      var dialogEl = container.find('md-dialog');

      dialogEl.addClass('transition-out').removeClass('transition-in');
      transformToClickElement(dialogEl, clickElement);

      return $mdUtil.transitionEndPromise(dialogEl);
    }

We see that we are returning a transition promise: https://github.com/angular/material/blob/master/src/core/util/util.js#L202-215

    transitionEndPromise: function(element, opts) {
      opts = opts || {};
      var deferred = $q.defer();
      element.on($mdConstant.CSS.TRANSITIONEND, finished);
      function finished(ev) {
        // Make sure this transitionend didn't bubble up from a child
        if (!ev || ev.target === element[0]) {
          element.off($mdConstant.CSS.TRANSITIONEND, finished);
          deferred.resolve();
        }
      }
      if (opts.timeout) $timeout(finished, opts.timeout);
      return deferred.promise;
    },

My general understanding of transitionEndPromise is to return once the transition of the dialog is complete. However, the deferred.resolve() statement is not always being called in Firefox, thereby preventing the initial promise from ever resolving and returning the focus to the web page.

I'm fairly certain it has to do with Firefox internals and timing because putting a break point at various places ensures 100% successful promise resolution.

I did find this on the Mozilla site:

Note: The transitionend event doesn't fire if the transition is aborted before the transition 
is completed because either the element is made display: none or the animating 
property's value is changed.

I noticed that the css class transition-out AND the transformToClickElement functions both modify the "transform" CSS style and thought that perhaps the transitioned event wasn't firing because the animating property's value is changing, e.g. transform.

However, the deferred.resolve() method is being occasionally being called in transitionEndPromise the function. But then the promise call back in the very first code snippet is never executing.

Ultimately, what we did to get things to at least work in FireFox 31 was to update the dialogPopOut function to somewhat "match" the dialogPopIn function:

function dialogPopOut(container, parentElement, clickElement) {
    var dialogEl = container.find('md-dialog');

    dialogEl.addClass('transition-out').removeClass('transition-in');

    $$rAF(function() {
       transformToClickElement(dialogEl, clickElement);
    });

    return $mdUtil.transitionEndPromise(dialogEl);
}

By wrapping the transformToClickElement in $$rAF or even a $timeout call, the promises always successfully resolve. The animation, both pop in and pop out, works perfectly on Chrome. Unfortunately, on FireFox 31, the dialog does not "pop" back to the element that was clicked, it just fades away. It isn't ideal but at least it doesn't freeze the entire application requiring a refresh of the browser page to continue using the application.

I think there is some race or cancellation condition going on between Firefox emitting the "transitionend" event, the addition of the 'transition-out' class, the transform back to the clicked element via transformToClickElement, and/or registering the "on(transitioned)" event handler in transitionEndPromise.

As an aside, I also noticed there was a dialogTransitionEnd in dialog.js that never seems to be referenced in the entire angular material design code base. https://github.com/angular/material/blob/master/src/components/dialog/dialog.js#L680-691.

Not sure if this is "leftover" merge code or was originally intended to be called when the dialog is being transitioned out of the UI.

nkoterba avatar May 13 '15 14:05 nkoterba

As a follow up to my edit above, I was able to prevent all md-dialog popups from freezing FireFox 31, by updating the code that calls dialogPopOut to pass the parent object if no popInTarget was provided or exists...

In other words, if someone calls md-dialog without providing a .targetEvent(event), this code will just use the parent of the dialog (which is most often the document body), as the popInTarget, which then seems to ensure the promise returned by dialogPopOut always occurs (does not error perhaps?) and focus can return to the popInTarget or the body:

return dialogPopOut(
        element,
        options.parent,
        (options.popInTarget && options.popInTarget.length && options.popInTarget) ||
     // Add a fallback option in case popInTarget is null or empty. popInTarget is provided by .targetEvent(event) md-dialog API call
          options.parent && options.parent.length && options.parent
      ).then(function() {
        options.scope.$destroy();
        element.remove();
        options.popInTarget && options.popInTarget.focus();
      });

nkoterba avatar Jun 26 '15 22:06 nkoterba