5 // The #mw-notification-area div that all notifications are contained inside.
8 preReadyNotifQueue = [];
11 * Creates a Notification object for 1 message.
12 * Does not insert anything into the document (see #start).
14 * The "_" in the name is to avoid a bug (http://github.com/senchalabs/jsduck/issues/304)
15 * It is not part of the actual class name.
17 * @class mw.Notification_
18 * @alternateClassName mw.Notification
23 function Notification( message, options ) {
24 var $notification, $notificationTitle, $notificationContent;
26 $notification = $( '<div class="mw-notification"></div>' )
27 .data( 'mw.notification', this )
28 .addClass( options.autoHide ? 'mw-notification-autohide' : 'mw-notification-noautohide' );
31 // Sanitize options.tag before it is used by any code. (Including Notification class methods)
32 options.tag = options.tag.replace( /[ _\-]+/g, '-' ).replace( /[^\-a-z0-9]+/ig, '' );
34 $notification.addClass( 'mw-notification-tag-' + options.tag );
40 if ( options.title ) {
41 $notificationTitle = $( '<div class="mw-notification-title"></div>' )
42 .text( options.title )
43 .appendTo( $notification );
46 $notificationContent = $( '<div class="mw-notification-content"></div>' );
48 if ( typeof message === 'object' ) {
49 // Handle mw.Message objects separately from DOM nodes and jQuery objects
50 if ( message instanceof mw.Message ) {
51 $notificationContent.html( message.parse() );
53 $notificationContent.append( message );
56 $notificationContent.text( message );
59 $notificationContent.appendTo( $notification );
61 // Private state parameters, meant for internal use only
62 // isOpen: Set to true after .start() is called to avoid double calls.
63 // Set back to false after .close() to avoid duplicating the close animation.
64 // isPaused: false after .resume(), true after .pause(). Avoids duplicating or breaking the hide timeouts.
65 // Set to true initially so .start() can call .resume().
66 // message: The message passed to the notification. Unused now but may be used in the future
67 // to stop replacement of a tagged notification with another notification using the same message.
68 // options: The options passed to the notification with a little sanitization. Used by various methods.
69 // $notification: jQuery object containing the notification DOM node.
72 this.message = message;
73 this.options = options;
74 this.$notification = $notification;
78 * Start the notification.
79 * This inserts it into the page, closes any matching tagged notifications,
80 * handles the fadeIn animations and repacement transitions, and starts autoHide timers.
82 Notification.prototype.start = function () {
85 $notification, options,
86 // Original opacity so that we can animate back to it later
88 // Other notification elements matching the same tag
101 options = this.options;
102 $notification = this.$notification;
104 opacity = this.$notification.css( 'opacity' );
106 // Set the opacity to 0 so we can fade in later.
107 $notification.css( 'opacity', 0 );
110 // Check to see if there are any tagged notifications with the same tag as the new one
111 $tagMatches = $area.find( '.mw-notification-tag-' + options.tag );
114 // If we found a tagged notification use the replacement pattern instead of the new
115 // notification fade-in pattern.
116 if ( options.tag && $tagMatches.length ) {
118 // Iterate over the tag matches to find the outerHeight we should use
119 // for the placeholder.
121 $tagMatches.each( function () {
122 var notif = $( this ).data( 'mw.notification' );
124 // Use the notification's height + padding + border + margins
125 // as the placeholder height.
126 outerHeight = notif.$notification.outerHeight( true );
127 if ( notif.$replacementPlaceholder ) {
128 // Grab the height of a placeholder that has not finished animating.
129 placeholderHeight = notif.$replacementPlaceholder.height();
130 // Remove any placeholders added by a previous tagged
131 // notification that was in the middle of replacing another.
132 // This also makes sure that we only grab the placeholderHeight
133 // for the most recent notification.
134 notif.$replacementPlaceholder.remove();
135 delete notif.$replacementPlaceholder;
137 // Close the previous tagged notification
138 // Since we're replacing it do this with a fast speed and don't output a placeholder
139 // since we're taking care of that transition ourselves.
140 notif.close( { speed: 'fast', placeholder: false } );
143 if ( placeholderHeight !== undefined ) {
144 // If the other tagged notification was in the middle of replacing another
145 // tagged notification, continue from the placeholder's height instead of
146 // using the outerHeight of the notification.
147 outerHeight = placeholderHeight;
151 // Insert the new notification before the tagged notification(s)
152 .insertBefore( $tagMatches.first() )
154 // Use an absolute position so that we can use a placeholder to gracefully push other notifications
155 // into the right spot.
156 position: 'absolute',
157 width: $notification.width()
159 // Fade-in the notification
160 .animate( { opacity: opacity },
163 complete: function () {
164 // After we've faded in clear the opacity and let css take over
165 $( this ).css( { opacity: '' } );
171 // Create a clear placeholder we can use to make the notifications around the notification that is being
172 // replaced expand or contract gracefully to fit the height of the new notification.
173 notif.$replacementPlaceholder = $( '<div>' )
174 // Set the height to the space the previous notification or placeholder took
175 .css( 'height', outerHeight )
176 // Make sure that this placeholder is at the very end of this tagged notification group
177 .insertAfter( $tagMatches.eq( -1 ) )
178 // Animate the placeholder height to the space that this new notification will take up
179 .animate( { height: $notification.outerHeight( true ) },
181 // Do space animations fast
183 complete: function () {
184 // Reset the notification position after we've finished the space animation
185 // However do not do it if the placeholder was removed because another tagged
186 // notification went and closed this one.
187 if ( notif.$replacementPlaceholder ) {
188 $notification.css( 'position', '' );
190 // Finally, remove the placeholder from the DOM
195 // Append to the notification area and fade in to the original opacity.
198 .animate( { opacity: opacity },
201 complete: function () {
202 // After we've faded in clear the opacity and let css take over
203 $( this ).css( 'opacity', '' );
209 // By default a notification is paused.
210 // If this notification is within the first {autoHideLimit} notifications then
211 // start the auto-hide timer as soon as it's created.
212 autohideCount = $area.find( '.mw-notification-autohide' ).length;
213 if ( autohideCount <= notification.autoHideLimit ) {
219 * Pause any running auto-hide timer for this notification
221 Notification.prototype.pause = function () {
222 if ( this.isPaused ) {
225 this.isPaused = true;
227 if ( this.timeout ) {
228 clearTimeout( this.timeout );
234 * Start autoHide timer if not already started.
235 * Does nothing if autoHide is disabled.
236 * Either to resume from pause or to make the first start.
238 Notification.prototype.resume = function () {
240 if ( !notif.isPaused ) {
243 // Start any autoHide timeouts
244 if ( notif.options.autoHide ) {
245 notif.isPaused = false;
246 notif.timeout = setTimeout( function () {
247 // Already finished, so don't try to re-clear it
248 delete notif.timeout;
250 }, notification.autoHideSeconds * 1000 );
255 * Close/hide the notification.
257 * @param {Object} options An object containing options for the closing of the notification.
258 * These are typically only used internally.
260 * - speed: Use a close speed different than the default 'slow'.
261 * - placeholder: Set to false to disable the placeholder transition.
263 Notification.prototype.close = function ( options ) {
264 if ( !this.isOpen ) {
268 // Clear any remaining timeout on close
271 options = $.extend( {
276 // Remove the mw-notification-autohide class from the notification to avoid
277 // having a half-closed notification counted as a notification to resume
278 // when handling {autoHideLimit}.
279 this.$notification.removeClass( 'mw-notification-autohide' );
281 // Now that a notification is being closed. Start auto-hide timers for any
282 // notification that has now become one of the first {autoHideLimit} notifications.
283 notification.resume();
287 // Don't trigger any mouse events while fading out, just in case the cursor
288 // happens to be right above us when we transition upwards.
289 pointerEvents: 'none',
290 // Set an absolute position so we can move upwards in the animation.
291 // Notification replacement doesn't look right unless we use an animation like this.
292 position: 'absolute',
293 // We must fix the width to avoid it shrinking horizontally.
294 width: this.$notification.width()
296 // Fix the top/left position to the current computed position from which we
297 // can animate upwards.
298 .css( this.$notification.position() );
300 // This needs to be done *after* notification's position has been made absolute.
301 if ( options.placeholder ) {
302 // Insert a placeholder with a height equal to the height of the
303 // notification plus it's vertical margins in place of the notification
304 var $placeholder = $( '<div>' )
305 .css( 'height', this.$notification.outerHeight( true ) )
306 .insertBefore( this.$notification );
309 // Animate opacity and top to create fade upwards animation for notification closing
315 duration: options.speed,
316 complete: function () {
317 // Remove the notification
319 if ( options.placeholder ) {
320 // Use a fast slide up animation after closing to make it look like the notifications
321 // below slide up into place when the notification disappears
322 $placeholder.slideUp( 'fast', function () {
323 // Remove the placeholder
332 * Helper function, take a list of notification divs and call
333 * a function on the Notification instance attached to them.
335 * @param {jQuery} $notifications A jQuery object containing notification divs
336 * @param {string} fn The name of the function to call on the Notification instance
338 function callEachNotification( $notifications, fn ) {
339 $notifications.each( function () {
340 var notif = $( this ).data( 'mw.notification' );
349 * Must only be called once, and not before the document is ready.
353 var offset, $window = $( window );
355 $area = $( '<div id="mw-notification-area" class="mw-notification-area mw-notification-area-layout"></div>' )
356 // Pause auto-hide timers when the mouse is in the notification area.
358 mouseenter: notification.pause,
359 mouseleave: notification.resume
361 // When clicking on a notification close it.
362 .on( 'click', '.mw-notification', function () {
363 var notif = $( this ).data( 'mw.notification' );
368 // Stop click events from <a> tags from propogating to prevent clicking.
369 // on links from hiding a notification.
370 .on( 'click', 'a', function ( e ) {
374 // Prepend the notification area to the content area and save it's object.
375 mw.util.$content.prepend( $area );
376 offset = $area.offset();
378 function updateAreaMode() {
379 var isFloating = $window.scrollTop() > offset.top;
381 .toggleClass( 'mw-notification-area-floating', isFloating )
382 .toggleClass( 'mw-notification-area-layout', !isFloating );
385 $window.on( 'scroll', updateAreaMode );
392 * @class mw.notification
397 * Pause auto-hide timers for all notifications.
398 * Notifications will not auto-hide until resume is called.
399 * @see mw.Notification#pause
402 callEachNotification(
403 $area.children( '.mw-notification' ),
409 * Resume any paused auto-hide timers from the beginning.
410 * Only the first #autoHideLimit timers will be resumed.
412 resume: function () {
413 callEachNotification(
414 // Only call resume on the first #autoHideLimit notifications.
415 // Exclude noautohide notifications to avoid bugs where #autoHideLimit
416 // `{ autoHide: false }` notifications are at the start preventing any
417 // auto-hide notifications from being autohidden.
418 $area.children( '.mw-notification-autohide' ).slice( 0, notification.autoHideLimit ),
424 * Display a notification message to the user.
426 * @param {HTMLElement|jQuery|mw.Message|string} message
427 * @param {Object} options The options to use for the notification.
428 * See #defaults for details.
430 notify: function ( message, options ) {
432 options = $.extend( {}, notification.defaults, options );
434 notif = new Notification( message, options );
439 preReadyNotifQueue.push( notif );
445 * The defaults for #notify options parameter.
448 * A boolean indicating whether the notifification should automatically
449 * be hidden after shown. Or if it should persist.
452 * An optional string. When a notification is tagged only one message
453 * with that tag will be displayed. Trying to display a new notification
454 * with the same tag as one already being displayed will cause the other
455 * notification to be closed and this new notification to open up inside
456 * the same place as the previous notification.
459 * An optional title for the notification. Will be displayed above the
460 * content. Usually in bold.
470 * Number of seconds to wait before auto-hiding notifications.
476 * Maximum number of notifications to count down auto-hide timers for.
477 * Only the first #autoHideLimit notifications being displayed will
478 * auto-hide. Any notifications further down in the list will only start
479 * counting down to auto-hide after the first few messages have closed.
481 * This basically represents the number of notifications the user should
482 * be able to process in #autoHideSeconds time.
492 // Handle pre-ready queue.
494 while ( preReadyNotifQueue.length ) {
495 notif = preReadyNotifQueue.shift();
500 mw.notification = notification;
502 }( mediaWiki, jQuery ) );