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
27 function Notification( message, options ) {
28 var $notification, $notificationTitle, $notificationContent;
30 $notification = $( '<div class="mw-notification"></div>' )
31 .data( 'mw.notification', this )
32 .addClass( options.autoHide ? 'mw-notification-autohide' : 'mw-notification-noautohide' );
35 // Sanitize options.tag before it is used by any code. (Including Notification class methods)
36 options.tag = options.tag.replace( /[ _\-]+/g, '-' ).replace( /[^\-a-z0-9]+/ig, '' );
38 $notification.addClass( 'mw-notification-tag-' + options.tag );
45 // Sanitize options.type
46 options.type = options.type.replace( /[ _\-]+/g, '-' ).replace( /[^\-a-z0-9]+/ig, '' );
47 $notification.addClass( 'mw-notification-type-' + options.type );
50 if ( options.title ) {
51 $notificationTitle = $( '<div class="mw-notification-title"></div>' )
52 .text( options.title )
53 .appendTo( $notification );
56 $notificationContent = $( '<div class="mw-notification-content"></div>' );
58 if ( typeof message === 'object' ) {
59 // Handle mw.Message objects separately from DOM nodes and jQuery objects
60 if ( message instanceof mw.Message ) {
61 $notificationContent.html( message.parse() );
63 $notificationContent.append( message );
66 $notificationContent.text( message );
69 $notificationContent.appendTo( $notification );
71 // Private state parameters, meant for internal use only
72 // isOpen: Set to true after .start() is called to avoid double calls.
73 // Set back to false after .close() to avoid duplicating the close animation.
74 // isPaused: false after .resume(), true after .pause(). Avoids duplicating or breaking the hide timeouts.
75 // Set to true initially so .start() can call .resume().
76 // message: The message passed to the notification. Unused now but may be used in the future
77 // to stop replacement of a tagged notification with another notification using the same message.
78 // options: The options passed to the notification with a little sanitization. Used by various methods.
79 // $notification: jQuery object containing the notification DOM node.
82 this.message = message;
83 this.options = options;
84 this.$notification = $notification;
88 * Start the notification. Called automatically by mw.notification#notify
89 * (possibly asynchronously on document-ready).
91 * This inserts the notification into the page, closes any matching tagged notifications,
92 * handles the fadeIn animations and replacement transitions, and starts autoHide timers.
96 Notification.prototype.start = function () {
97 var options, $notification, $tagMatches, autohideCount;
106 openNotificationCount++;
108 options = this.options;
109 $notification = this.$notification;
112 // Find notifications with the same tag
113 $tagMatches = $area.find( '.mw-notification-tag-' + options.tag );
116 // If we found existing notification with the same tag, replace them
117 if ( options.tag && $tagMatches.length ) {
119 // While there can be only one "open" notif with a given tag, there can be several
120 // matches here because they remain in the DOM until the animation is finished.
121 $tagMatches.each( function () {
122 var notif = $( this ).data( 'mw.notification' );
123 if ( notif && notif.isOpen ) {
124 // Detach from render flow with position absolute so that the new tag can
125 // occupy its space instead.
128 position: 'absolute',
129 width: notif.$notification.width()
131 .css( notif.$notification.position() )
132 .addClass( 'mw-notification-replaced' );
138 .insertBefore( $tagMatches.first() )
139 .addClass( 'mw-notification-visible' );
141 $area.append( $notification );
143 // This frame renders the element in the area (invisible)
145 $notification.addClass( 'mw-notification-visible' );
150 // By default a notification is paused.
151 // If this notification is within the first {autoHideLimit} notifications then
152 // start the auto-hide timer as soon as it's created.
153 autohideCount = $area.find( '.mw-notification-autohide' ).length;
154 if ( autohideCount <= notification.autoHideLimit ) {
160 * Pause any running auto-hide timer for this notification
162 Notification.prototype.pause = function () {
163 if ( this.isPaused ) {
166 this.isPaused = true;
168 if ( this.timeout ) {
169 clearTimeout( this.timeout );
175 * Start autoHide timer if not already started.
176 * Does nothing if autoHide is disabled.
177 * Either to resume from pause or to make the first start.
179 Notification.prototype.resume = function () {
181 if ( !notif.isPaused ) {
184 // Start any autoHide timeouts
185 if ( notif.options.autoHide ) {
186 notif.isPaused = false;
187 notif.timeout = setTimeout( function () {
188 // Already finished, so don't try to re-clear it
189 delete notif.timeout;
191 }, notification.autoHideSeconds * 1000 );
196 * Close the notification.
198 Notification.prototype.close = function () {
201 if ( !this.isOpen ) {
206 openNotificationCount--;
208 // Clear any remaining timeout on close
211 // Remove the mw-notification-autohide class from the notification to avoid
212 // having a half-closed notification counted as a notification to resume
213 // when handling {autoHideLimit}.
214 this.$notification.removeClass( 'mw-notification-autohide' );
216 // Now that a notification is being closed. Start auto-hide timers for any
217 // notification that has now become one of the first {autoHideLimit} notifications.
218 notification.resume();
221 notif.$notification.removeClass( 'mw-notification-visible' );
223 setTimeout( function () {
224 if ( openNotificationCount === 0 ) {
225 // Hide the area after the last notification closes. Otherwise, the padding on
226 // the area can be obscure content, despite the area being empty/invisible (T54659). // FIXME
228 notif.$notification.remove();
230 notif.$notification.slideUp( 'fast', function () {
239 * Helper function, take a list of notification divs and call
240 * a function on the Notification instance attached to them.
244 * @param {jQuery} $notifications A jQuery object containing notification divs
245 * @param {string} fn The name of the function to call on the Notification instance
247 function callEachNotification( $notifications, fn ) {
248 $notifications.each( function () {
249 var notif = $( this ).data( 'mw.notification' );
258 * Must only be called once, and not before the document is ready.
263 var offset, $window = $( window );
265 $area = $( '<div id="mw-notification-area" class="mw-notification-area mw-notification-area-layout"></div>' )
266 // Pause auto-hide timers when the mouse is in the notification area.
268 mouseenter: notification.pause,
269 mouseleave: notification.resume
271 // When clicking on a notification close it.
272 .on( 'click', '.mw-notification', function () {
273 var notif = $( this ).data( 'mw.notification' );
278 // Stop click events from <a> tags from propogating to prevent clicking.
279 // on links from hiding a notification.
280 .on( 'click', 'a', function ( e ) {
284 // Prepend the notification area to the content area and save it's object.
285 mw.util.$content.prepend( $area );
286 offset = $area.offset();
289 function updateAreaMode() {
290 var isFloating = $window.scrollTop() > offset.top;
292 .toggleClass( 'mw-notification-area-floating', isFloating )
293 .toggleClass( 'mw-notification-area-layout', !isFloating );
296 $window.on( 'scroll', updateAreaMode );
303 * @class mw.notification
308 * Pause auto-hide timers for all notifications.
309 * Notifications will not auto-hide until resume is called.
311 * @see mw.Notification#pause
314 callEachNotification(
315 $area.children( '.mw-notification' ),
321 * Resume any paused auto-hide timers from the beginning.
322 * Only the first #autoHideLimit timers will be resumed.
324 resume: function () {
325 callEachNotification(
326 // Only call resume on the first #autoHideLimit notifications.
327 // Exclude noautohide notifications to avoid bugs where #autoHideLimit
328 // `{ autoHide: false }` notifications are at the start preventing any
329 // auto-hide notifications from being autohidden.
330 $area.children( '.mw-notification-autohide' ).slice( 0, notification.autoHideLimit ),
336 * Display a notification message to the user.
338 * @param {HTMLElement|HTMLElement[]|jQuery|mw.Message|string} message
339 * @param {Object} options The options to use for the notification.
340 * See #defaults for details.
341 * @return {mw.Notification} Notification object
343 notify: function ( message, options ) {
345 options = $.extend( {}, notification.defaults, options );
347 notif = new Notification( message, options );
352 preReadyNotifQueue.push( notif );
360 * The defaults for #notify options parameter.
363 * A boolean indicating whether the notifification should automatically
364 * be hidden after shown. Or if it should persist.
367 * An optional string. When a notification is tagged only one message
368 * with that tag will be displayed. Trying to display a new notification
369 * with the same tag as one already being displayed will cause the other
370 * notification to be closed and this new notification to open up inside
371 * the same place as the previous notification.
374 * An optional title for the notification. Will be displayed above the
375 * content. Usually in bold.
378 * An optional string for the type of the message used for styling:
379 * Examples: 'info', 'warn', 'error'.
390 * Number of seconds to wait before auto-hiding notifications.
396 * Maximum number of notifications to count down auto-hide timers for.
397 * Only the first #autoHideLimit notifications being displayed will
398 * auto-hide. Any notifications further down in the list will only start
399 * counting down to auto-hide after the first few messages have closed.
401 * This basically represents the number of notifications the user should
402 * be able to process in #autoHideSeconds time.
412 // Handle pre-ready queue.
414 while ( preReadyNotifQueue.length ) {
415 notif = preReadyNotifQueue.shift();
420 mw.notification = notification;
422 }( mediaWiki, jQuery ) );