5 // The .mw-notification-area div that all notifications are contained inside.
7 // Number of open notification boxes at any time
8 openNotificationCount
= 0,
10 preReadyNotifQueue
= [],
11 rAF
= window
.requestAnimationFrame
|| setTimeout
;
14 * A Notification object for 1 message.
16 * The underscore in the name is to avoid a bug <https://github.com/senchalabs/jsduck/issues/304>.
17 * It is not part of the actual class name.
19 * The constructor is not publicly accessible; use mw.notification#notify instead.
20 * This does not insert anything into the document (see #start).
22 * @class mw.Notification_
23 * @alternateClassName mw.Notification
26 * @param {mw.Message|jQuery|HTMLElement|string} message
27 * @param {Object} options
29 function Notification( message
, options
) {
30 var $notification
, $notificationContent
;
32 $notification
= $( '<div class="mw-notification"></div>' )
33 .data( 'mw-notification', this )
34 .addClass( options
.autoHide
? 'mw-notification-autohide' : 'mw-notification-noautohide' );
37 // Sanitize options.tag before it is used by any code. (Including Notification class methods)
38 options
.tag
= options
.tag
.replace( /[ _-]+/g, '-' ).replace( /[^-a-z0-9]+/ig, '' );
40 $notification
.addClass( 'mw-notification-tag-' + options
.tag
);
47 // Sanitize options.type
48 options
.type
= options
.type
.replace( /[ _-]+/g, '-' ).replace( /[^-a-z0-9]+/ig, '' );
49 $notification
.addClass( 'mw-notification-type-' + options
.type
);
52 if ( options
.title
) {
53 $( '<div class="mw-notification-title"></div>' )
54 .text( options
.title
)
55 .appendTo( $notification
);
58 $notificationContent
= $( '<div class="mw-notification-content"></div>' );
60 if ( typeof message
=== 'object' ) {
61 // Handle mw.Message objects separately from DOM nodes and jQuery objects
62 if ( message
instanceof mw
.Message
) {
63 $notificationContent
.html( message
.parse() );
65 $notificationContent
.append( message
);
68 $notificationContent
.text( message
);
71 $notificationContent
.appendTo( $notification
);
73 // Private state parameters, meant for internal use only
74 // autoHideSeconds: String alias for number of seconds for timeout of auto-hiding notifications.
75 // isOpen: Set to true after .start() is called to avoid double calls.
76 // Set back to false after .close() to avoid duplicating the close animation.
77 // isPaused: false after .resume(), true after .pause(). Avoids duplicating or breaking the hide timeouts.
78 // Set to true initially so .start() can call .resume().
79 // message: The message passed to the notification. Unused now but may be used in the future
80 // to stop replacement of a tagged notification with another notification using the same message.
81 // options: The options passed to the notification with a little sanitization. Used by various methods.
82 // $notification: jQuery object containing the notification DOM node.
83 // timeout: Holds appropriate methods to set/clear timeouts
84 this.autoHideSeconds
= options
.autoHideSeconds
&&
85 notification
.autoHideSeconds
[ options
.autoHideSeconds
] ||
86 notification
.autoHideSeconds
.short;
89 this.message
= message
;
90 this.options
= options
;
91 this.$notification
= $notification
;
92 if ( options
.visibleTimeout
) {
93 this.timeout
= require( 'mediawiki.visibleTimeout' );
103 * Start the notification. Called automatically by mw.notification#notify
104 * (possibly asynchronously on document-ready).
106 * This inserts the notification into the page, closes any matching tagged notifications,
107 * handles the fadeIn animations and replacement transitions, and starts autoHide timers.
111 Notification
.prototype.start = function () {
112 var options
, $notification
, $tagMatches
, autohideCount
;
114 $area
.css( 'display', '' );
121 openNotificationCount
++;
123 options
= this.options
;
124 $notification
= this.$notification
;
127 // Find notifications with the same tag
128 $tagMatches
= $area
.find( '.mw-notification-tag-' + options
.tag
);
131 // If we found existing notification with the same tag, replace them
132 if ( options
.tag
&& $tagMatches
.length
) {
134 // While there can be only one "open" notif with a given tag, there can be several
135 // matches here because they remain in the DOM until the animation is finished.
136 $tagMatches
.each( function () {
137 var notif
= $( this ).data( 'mw-notification' );
138 if ( notif
&& notif
.isOpen
) {
139 // Detach from render flow with position absolute so that the new tag can
140 // occupy its space instead.
143 position
: 'absolute',
144 width
: notif
.$notification
.width()
146 .css( notif
.$notification
.position() )
147 .addClass( 'mw-notification-replaced' );
153 .insertBefore( $tagMatches
.first() )
154 .addClass( 'mw-notification-visible' );
156 $area
.append( $notification
);
158 // This frame renders the element in the area (invisible)
160 $notification
.addClass( 'mw-notification-visible' );
165 // By default a notification is paused.
166 // If this notification is within the first {autoHideLimit} notifications then
167 // start the auto-hide timer as soon as it's created.
168 autohideCount
= $area
.find( '.mw-notification-autohide' ).length
;
169 if ( autohideCount
<= notification
.autoHideLimit
) {
175 * Pause any running auto-hide timer for this notification
177 Notification
.prototype.pause = function () {
178 if ( this.isPaused
) {
181 this.isPaused
= true;
183 if ( this.timeoutId
) {
184 this.timeout
.clear( this.timeoutId
);
185 delete this.timeoutId
;
190 * Start autoHide timer if not already started.
191 * Does nothing if autoHide is disabled.
192 * Either to resume from pause or to make the first start.
194 Notification
.prototype.resume = function () {
197 if ( !notif
.isPaused
) {
200 // Start any autoHide timeouts
201 if ( notif
.options
.autoHide
) {
202 notif
.isPaused
= false;
203 notif
.timeoutId
= notif
.timeout
.set( function () {
204 // Already finished, so don't try to re-clear it
205 delete notif
.timeoutId
;
207 }, this.autoHideSeconds
* 1000 );
212 * Close the notification.
214 Notification
.prototype.close = function () {
217 if ( !this.isOpen
) {
222 openNotificationCount
--;
224 // Clear any remaining timeout on close
227 // Remove the mw-notification-autohide class from the notification to avoid
228 // having a half-closed notification counted as a notification to resume
229 // when handling {autoHideLimit}.
230 this.$notification
.removeClass( 'mw-notification-autohide' );
232 // Now that a notification is being closed. Start auto-hide timers for any
233 // notification that has now become one of the first {autoHideLimit} notifications.
234 notification
.resume();
237 notif
.$notification
.removeClass( 'mw-notification-visible' );
239 setTimeout( function () {
240 if ( openNotificationCount
=== 0 ) {
241 // Hide the area after the last notification closes. Otherwise, the padding on
242 // the area can be obscure content, despite the area being empty/invisible (T54659). // FIXME
243 $area
.css( 'display', 'none' );
244 notif
.$notification
.remove();
246 notif
.$notification
.slideUp( 'fast', function () {
255 * Helper function, take a list of notification divs and call
256 * a function on the Notification instance attached to them.
260 * @param {jQuery} $notifications A jQuery object containing notification divs
261 * @param {string} fn The name of the function to call on the Notification instance
263 function callEachNotification( $notifications
, fn
) {
264 $notifications
.each( function () {
265 var notif
= $( this ).data( 'mw-notification' );
274 * Must only be called once, and not before the document is ready.
282 function updateAreaMode() {
283 var shouldFloat
= window
.pageYOffset
> offset
.top
;
284 if ( isFloating
=== shouldFloat
) {
287 isFloating
= shouldFloat
;
289 .toggleClass( 'mw-notification-area-floating', isFloating
)
290 .toggleClass( 'mw-notification-area-layout', !isFloating
);
293 // Look for a preset notification area in the skin.
294 // 'data-mw*' attributes are banned from user content in Sanitizer.
295 $area
= $( '.mw-notification-area[data-mw="interface"]' ).first();
296 if ( !$area
.length
) {
297 $area
= $( '<div>' ).addClass( 'mw-notification-area' );
298 // Prepend the notification area to the content area
299 mw
.util
.$content
.prepend( $area
);
302 .addClass( 'mw-notification-area-layout' )
303 // The ID attribute here is deprecated.
304 .attr( 'id', 'mw-notification-area' )
305 // Pause auto-hide timers when the mouse is in the notification area.
307 mouseenter
: notification
.pause
,
308 mouseleave
: notification
.resume
310 // When clicking on a notification close it.
311 .on( 'click', '.mw-notification', function () {
312 var notif
= $( this ).data( 'mw-notification' );
317 // Stop click events from <a> tags from propogating to prevent clicking.
318 // on links from hiding a notification.
319 .on( 'click', 'a', function ( e
) {
323 // Read from the DOM:
324 // Must be in the next frame to avoid synchronous layout
325 // computation from offset()/getBoundingClientRect().
327 offset
= $area
.offset();
329 // Initial mode (reads, and then maybe writes)
332 // Once we have the offset for where it would normally render, set the
333 // initial state of the (currently empty) notification area to be hidden.
334 $area
.css( 'display', 'none' );
336 $( window
).on( 'scroll', updateAreaMode
);
338 // Handle pre-ready queue.
340 while ( preReadyNotifQueue
.length
) {
341 notif
= preReadyNotifQueue
.shift();
348 * @class mw.notification
353 * Pause auto-hide timers for all notifications.
354 * Notifications will not auto-hide until resume is called.
356 * @see mw.Notification#pause
359 callEachNotification(
360 $area
.children( '.mw-notification' ),
366 * Resume any paused auto-hide timers from the beginning.
367 * Only the first #autoHideLimit timers will be resumed.
369 resume: function () {
370 callEachNotification(
371 // Only call resume on the first #autoHideLimit notifications.
372 // Exclude noautohide notifications to avoid bugs where #autoHideLimit
373 // `{ autoHide: false }` notifications are at the start preventing any
374 // auto-hide notifications from being autohidden.
375 $area
.children( '.mw-notification-autohide' ).slice( 0, notification
.autoHideLimit
),
381 * Display a notification message to the user.
383 * @param {HTMLElement|HTMLElement[]|jQuery|mw.Message|string} message
384 * @param {Object} options The options to use for the notification.
385 * See #defaults for details.
386 * @return {mw.Notification} Notification object
388 notify: function ( message
, options
) {
390 options
= $.extend( {}, notification
.defaults
, options
);
392 notif
= new Notification( message
, options
);
397 preReadyNotifQueue
.push( notif
);
405 * The defaults for #notify options parameter.
408 * A boolean indicating whether the notifification should automatically
409 * be hidden after shown. Or if it should persist.
412 * Key to #autoHideSeconds for number of seconds for timeout of auto-hide
416 * An optional string. When a notification is tagged only one message
417 * with that tag will be displayed. Trying to display a new notification
418 * with the same tag as one already being displayed will cause the other
419 * notification to be closed and this new notification to open up inside
420 * the same place as the previous notification.
423 * An optional title for the notification. Will be displayed above the
424 * content. Usually in bold.
427 * An optional string for the type of the message used for styling:
428 * Examples: 'info', 'warn', 'error'.
431 * A boolean indicating if the autoHide timeout should be based on
432 * time the page was visible to user. Or if it should use wall clock time.
436 autoHideSeconds
: 'short',
454 * Maximum number of simultaneous notifications to start auto-hide timers for.
455 * Only this number of notifications being displayed will be auto-hidden at one time.
456 * Any additional notifications in the list will only start counting their timeout for
457 * auto-hiding after the previous messages have been closed.
459 * This basically represents the minimal number of notifications the user should
460 * be able to process during the {@link #defaults default} #autoHideSeconds time.
467 mw
.notification
= notification
;