mootools / mootools-core

MooTools Core Repository
https://mootools.net
2.65k stars 510 forks source link

Add native event "visibilitychange" #2779

Closed leonardfischer closed 7 years ago

leonardfischer commented 7 years ago

Hi, right now it is not possible to observe "visibilitychange" events with MooTools.

I used this code to implement it (found here https://davidwalsh.name/page-visibility)

// Adapted slightly from Sam Dutton
// Set name of hidden property and visibility change event
// since some browsers only offer vendor-prefixed support
var hidden, state, visibilityChange; 
if (typeof document.hidden !== "undefined") {
    visibilityChange = "visibilitychange";
    state = "visibilityState";
} else if (typeof document.mozHidden !== "undefined") {
    visibilityChange = "mozvisibilitychange";
    state = "mozVisibilityState";
} else if (typeof document.msHidden !== "undefined") {
    visibilityChange = "msvisibilitychange";
    state = "msVisibilityState";
} else if (typeof document.webkitHidden !== "undefined") {
    visibilityChange = "webkitvisibilitychange";
    state = "webkitVisibilityState";
}

// Set it up!
Element.NativeEvents[visibilityChange] = 2;
Element.Events[visibilityChange] = {
    base: visibilityChange,
    condition: function(event) {
        event[state] = document[state];
        event.visibilityState = document[state];
        return true;
    }
};

// Now use it!
document.addEvent(visibilityChange, function(e) {
    document.title = document[state];
});

But because of the use of the "visibilityChange" variable, I can't use this as part of addEvents

DimitarChristoff commented 7 years ago

since support is not universal (IE10+) and some older versions need vendor prefixes, we probably cannot standardise the event universally enough for it to be part of mootools-core. As such, I think your code is fine as is (just put it in a closure) - or use something like this:

https://github.com/ai/visibilityjs - which does several things, like fallback to blur / focus and hides the events name in case they need prefixing.

unrelated. are you the solidsnek from QL? :D

leonardfischer commented 7 years ago

Hi, thats too bad, but I understand your point. I'll take a look at visibilityjs, thanks! I guess this ticket can be closed then?

And nope, just chose this nick many years ago because I'm a huge fan of "Metal Gear Solid"