5 // The #mw-notification-area div that all notifications are contained inside.
8 preReadyNotifQueue
= [];
11 * A Notification object for 1 message.
13 * The "_" in the name is to avoid a bug (http://github.com/senchalabs/jsduck/issues/304).
14 * It is not part of the actual class name.
16 * @class mw.Notification_
17 * @alternateClassName mw.Notification
19 * @constructor The constructor is not publicly accessible; use mw.notification#notify instead.
20 * This does not insert anything into the document (see #start).
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. Called automatically by mw.notification#notify
79 * (possibly asynchronously on document-ready).
81 * This inserts the notification into the page, closes any matching tagged notifications,
82 * handles the fadeIn animations and replacement transitions, and starts autoHide timers.
86 Notification
.prototype.start = function () {
89 $notification
, options
,
90 // Original opacity so that we can animate back to it later
92 // Other notification elements matching the same tag
105 options
= this.options
;
106 $notification
= this.$notification
;
108 opacity
= this.$notification
.css( 'opacity' );
110 // Set the opacity to 0 so we can fade in later.
111 $notification
.css( 'opacity', 0 );
114 // Check to see if there are any tagged notifications with the same tag as the new one
115 $tagMatches
= $area
.find( '.mw-notification-tag-' + options
.tag
);
118 // If we found a tagged notification use the replacement pattern instead of the new
119 // notification fade-in pattern.
120 if ( options
.tag
&& $tagMatches
.length
) {
122 // Iterate over the tag matches to find the outerHeight we should use
123 // for the placeholder.
125 $tagMatches
.each( function () {
126 var notif
= $( this ).data( 'mw.notification' );
128 // Use the notification's height + padding + border + margins
129 // as the placeholder height.
130 outerHeight
= notif
.$notification
.outerHeight( true );
131 if ( notif
.$replacementPlaceholder
) {
132 // Grab the height of a placeholder that has not finished animating.
133 placeholderHeight
= notif
.$replacementPlaceholder
.height();
134 // Remove any placeholders added by a previous tagged
135 // notification that was in the middle of replacing another.
136 // This also makes sure that we only grab the placeholderHeight
137 // for the most recent notification.
138 notif
.$replacementPlaceholder
.remove();
139 delete notif
.$replacementPlaceholder
;
141 // Close the previous tagged notification
142 // Since we're replacing it do this with a fast speed and don't output a placeholder
143 // since we're taking care of that transition ourselves.
144 notif
.close( { speed
: 'fast', placeholder
: false } );
147 if ( placeholderHeight
!== undefined ) {
148 // If the other tagged notification was in the middle of replacing another
149 // tagged notification, continue from the placeholder's height instead of
150 // using the outerHeight of the notification.
151 outerHeight
= placeholderHeight
;
155 // Insert the new notification before the tagged notification(s)
156 .insertBefore( $tagMatches
.first() )
158 // Use an absolute position so that we can use a placeholder to gracefully push other notifications
159 // into the right spot.
160 position
: 'absolute',
161 width
: $notification
.width()
163 // Fade-in the notification
164 .animate( { opacity
: opacity
},
167 complete: function () {
168 // After we've faded in clear the opacity and let css take over
169 $( this ).css( { opacity
: '' } );
175 // Create a clear placeholder we can use to make the notifications around the notification that is being
176 // replaced expand or contract gracefully to fit the height of the new notification.
177 notif
.$replacementPlaceholder
= $( '<div>' )
178 // Set the height to the space the previous notification or placeholder took
179 .css( 'height', outerHeight
)
180 // Make sure that this placeholder is at the very end of this tagged notification group
181 .insertAfter( $tagMatches
.eq( -1 ) )
182 // Animate the placeholder height to the space that this new notification will take up
183 .animate( { height
: $notification
.outerHeight( true ) },
185 // Do space animations fast
187 complete: function () {
188 // Reset the notification position after we've finished the space animation
189 // However do not do it if the placeholder was removed because another tagged
190 // notification went and closed this one.
191 if ( notif
.$replacementPlaceholder
) {
192 $notification
.css( 'position', '' );
194 // Finally, remove the placeholder from the DOM
199 // Append to the notification area and fade in to the original opacity.
202 .animate( { opacity
: opacity
},
205 complete: function () {
206 // After we've faded in clear the opacity and let css take over
207 $( this ).css( 'opacity', '' );
213 // By default a notification is paused.
214 // If this notification is within the first {autoHideLimit} notifications then
215 // start the auto-hide timer as soon as it's created.
216 autohideCount
= $area
.find( '.mw-notification-autohide' ).length
;
217 if ( autohideCount
<= notification
.autoHideLimit
) {
223 * Pause any running auto-hide timer for this notification
225 Notification
.prototype.pause = function () {
226 if ( this.isPaused
) {
229 this.isPaused
= true;
231 if ( this.timeout
) {
232 clearTimeout( this.timeout
);
238 * Start autoHide timer if not already started.
239 * Does nothing if autoHide is disabled.
240 * Either to resume from pause or to make the first start.
242 Notification
.prototype.resume = function () {
244 if ( !notif
.isPaused
) {
247 // Start any autoHide timeouts
248 if ( notif
.options
.autoHide
) {
249 notif
.isPaused
= false;
250 notif
.timeout
= setTimeout( function () {
251 // Already finished, so don't try to re-clear it
252 delete notif
.timeout
;
254 }, notification
.autoHideSeconds
* 1000 );
259 * Close/hide the notification.
261 * @param {Object} options An object containing options for the closing of the notification.
263 * - speed: Use a close speed different than the default 'slow'.
264 * - placeholder: Set to false to disable the placeholder transition.
266 Notification
.prototype.close = function ( options
) {
267 if ( !this.isOpen
) {
271 // Clear any remaining timeout on close
274 options
= $.extend( {
279 // Remove the mw-notification-autohide class from the notification to avoid
280 // having a half-closed notification counted as a notification to resume
281 // when handling {autoHideLimit}.
282 this.$notification
.removeClass( 'mw-notification-autohide' );
284 // Now that a notification is being closed. Start auto-hide timers for any
285 // notification that has now become one of the first {autoHideLimit} notifications.
286 notification
.resume();
290 // Don't trigger any mouse events while fading out, just in case the cursor
291 // happens to be right above us when we transition upwards.
292 pointerEvents
: 'none',
293 // Set an absolute position so we can move upwards in the animation.
294 // Notification replacement doesn't look right unless we use an animation like this.
295 position
: 'absolute',
296 // We must fix the width to avoid it shrinking horizontally.
297 width
: this.$notification
.width()
299 // Fix the top/left position to the current computed position from which we
300 // can animate upwards.
301 .css( this.$notification
.position() );
303 // This needs to be done *after* notification's position has been made absolute.
304 if ( options
.placeholder
) {
305 // Insert a placeholder with a height equal to the height of the
306 // notification plus it's vertical margins in place of the notification
307 var $placeholder
= $( '<div>' )
308 .css( 'height', this.$notification
.outerHeight( true ) )
309 .insertBefore( this.$notification
);
312 // Animate opacity and top to create fade upwards animation for notification closing
318 duration
: options
.speed
,
319 complete: function () {
320 // Remove the notification
322 if ( options
.placeholder
) {
323 // Use a fast slide up animation after closing to make it look like the notifications
324 // below slide up into place when the notification disappears
325 $placeholder
.slideUp( 'fast', function () {
326 // Remove the placeholder
335 * Helper function, take a list of notification divs and call
336 * a function on the Notification instance attached to them.
340 * @param {jQuery} $notifications A jQuery object containing notification divs
341 * @param {string} fn The name of the function to call on the Notification instance
343 function callEachNotification( $notifications
, fn
) {
344 $notifications
.each( function () {
345 var notif
= $( this ).data( 'mw.notification' );
354 * Must only be called once, and not before the document is ready.
358 var offset
, $window
= $( window
);
360 $area
= $( '<div id="mw-notification-area" class="mw-notification-area mw-notification-area-layout"></div>' )
361 // Pause auto-hide timers when the mouse is in the notification area.
363 mouseenter
: notification
.pause
,
364 mouseleave
: notification
.resume
366 // When clicking on a notification close it.
367 .on( 'click', '.mw-notification', function () {
368 var notif
= $( this ).data( 'mw.notification' );
373 // Stop click events from <a> tags from propogating to prevent clicking.
374 // on links from hiding a notification.
375 .on( 'click', 'a', function ( e
) {
379 // Prepend the notification area to the content area and save it's object.
380 mw
.util
.$content
.prepend( $area
);
381 offset
= $area
.offset();
383 function updateAreaMode() {
384 var isFloating
= $window
.scrollTop() > offset
.top
;
386 .toggleClass( 'mw-notification-area-floating', isFloating
)
387 .toggleClass( 'mw-notification-area-layout', !isFloating
);
390 $window
.on( 'scroll', updateAreaMode
);
397 * @class mw.notification
402 * Pause auto-hide timers for all notifications.
403 * Notifications will not auto-hide until resume is called.
404 * @see mw.Notification#pause
407 callEachNotification(
408 $area
.children( '.mw-notification' ),
414 * Resume any paused auto-hide timers from the beginning.
415 * Only the first #autoHideLimit timers will be resumed.
417 resume: function () {
418 callEachNotification(
419 // Only call resume on the first #autoHideLimit notifications.
420 // Exclude noautohide notifications to avoid bugs where #autoHideLimit
421 // `{ autoHide: false }` notifications are at the start preventing any
422 // auto-hide notifications from being autohidden.
423 $area
.children( '.mw-notification-autohide' ).slice( 0, notification
.autoHideLimit
),
429 * Display a notification message to the user.
431 * @param {HTMLElement|jQuery|mw.Message|string} message
432 * @param {Object} options The options to use for the notification.
433 * See #defaults for details.
434 * @return {mw.Notification} Notification object
436 notify: function ( message
, options
) {
438 options
= $.extend( {}, notification
.defaults
, options
);
440 notif
= new Notification( message
, options
);
445 preReadyNotifQueue
.push( notif
);
453 * The defaults for #notify options parameter.
456 * A boolean indicating whether the notifification should automatically
457 * be hidden after shown. Or if it should persist.
460 * An optional string. When a notification is tagged only one message
461 * with that tag will be displayed. Trying to display a new notification
462 * with the same tag as one already being displayed will cause the other
463 * notification to be closed and this new notification to open up inside
464 * the same place as the previous notification.
467 * An optional title for the notification. Will be displayed above the
468 * content. Usually in bold.
478 * Number of seconds to wait before auto-hiding notifications.
484 * Maximum number of notifications to count down auto-hide timers for.
485 * Only the first #autoHideLimit notifications being displayed will
486 * auto-hide. Any notifications further down in the list will only start
487 * counting down to auto-hide after the first few messages have closed.
489 * This basically represents the number of notifications the user should
490 * be able to process in #autoHideSeconds time.
500 // Handle pre-ready queue.
502 while ( preReadyNotifQueue
.length
) {
503 notif
= preReadyNotifQueue
.shift();
508 mw
.notification
= notification
;
510 }( mediaWiki
, jQuery
) );