// to stop replacement of a tagged notification with another notification using the same message.
// options: The options passed to the notification with a little sanitization. Used by various methods.
// $notification: jQuery object containing the notification DOM node.
+ // timeout: Holds appropriate methods to set/clear timeouts
this.autoHideSeconds = options.autoHideSeconds &&
notification.autoHideSeconds[ options.autoHideSeconds ] ||
notification.autoHideSeconds.short;
this.message = message;
this.options = options;
this.$notification = $notification;
+ if ( options.visibleTimeout ) {
+ this.timeout = require( 'mediawiki.visibleTimeout' );
+ } else {
+ this.timeout = {
+ set: setTimeout,
+ clear: clearTimeout
+ };
+ }
}
/**
}
this.isPaused = true;
- if ( this.timeout ) {
- clearTimeout( this.timeout );
- delete this.timeout;
+ if ( this.timeoutId ) {
+ this.timeout.clear( this.timeoutId );
+ delete this.timeoutId;
}
};
*/
Notification.prototype.resume = function () {
var notif = this;
+
if ( !notif.isPaused ) {
return;
}
// Start any autoHide timeouts
if ( notif.options.autoHide ) {
notif.isPaused = false;
- notif.timeout = setTimeout( function () {
+ notif.timeoutId = notif.timeout.set( function () {
// Already finished, so don't try to re-clear it
- delete notif.timeout;
+ delete notif.timeoutId;
notif.close();
}, this.autoHideSeconds * 1000 );
}
.toggleClass( 'mw-notification-area-layout', !isFloating );
}
+ // Write to the DOM:
// Prepend the notification area to the content area and save its object.
$area = $( '<div id="mw-notification-area" class="mw-notification-area mw-notification-area-layout"></div>' )
// Pause auto-hide timers when the mouse is in the notification area.
} );
mw.util.$content.prepend( $area );
- offset = $area.offset();
- $area.css( 'display', 'none' );
- $( window ).on( 'scroll', updateAreaMode );
+ // Read from the DOM:
+ // Must be in the next frame to avoid synchronous layout
+ // computation from offset()/getBoundingClientRect().
+ rAF( function () {
+ offset = $area.offset();
- // Initial mode
- updateAreaMode();
+ // Initial mode (reads, and then maybe writes)
+ updateAreaMode();
- // Handle pre-ready queue.
- isPageReady = true;
- while ( preReadyNotifQueue.length ) {
- notif = preReadyNotifQueue.shift();
- notif.start();
- }
+ // Once we have the offset for where it would normally render, set the
+ // initial state of the (currently empty) notification area to be hidden.
+ $area.css( 'display', 'none' );
+
+ $( window ).on( 'scroll', updateAreaMode );
+
+ // Handle pre-ready queue.
+ isPageReady = true;
+ while ( preReadyNotifQueue.length ) {
+ notif = preReadyNotifQueue.shift();
+ notif.start();
+ }
+ } );
}
/**
* - type:
* An optional string for the type of the message used for styling:
* Examples: 'info', 'warn', 'error'.
+ *
+ * - visibleTimeout:
+ * A boolean indicating if the autoHide timeout should be based on
+ * time the page was visible to user. Or if it should use wall clock time.
*/
defaults: {
autoHide: true,
autoHideSeconds: 'short',
tag: null,
title: null,
- type: null
+ type: null,
+ visibleTimeout: true
},
/**