Merge "resources: Strip '$' and 'mw' from file closures"
[lhc/web/wiklou.git] / resources / src / mediawiki.special.unwatchedPages / unwatchedPages.js
1 /*!
2 * JavaScript for Special:UnwatchedPages
3 */
4 ( function () {
5 $( function () {
6 $( 'a.mw-watch-link' ).click( function ( e ) {
7 var promise,
8 api = new mw.Api(),
9 $link = $( this ),
10 $subjectLink = $link.closest( 'li' ).children( 'a' ).eq( 0 ),
11 title = mw.util.getParamValue( 'title', $link.attr( 'href' ) );
12 // nice format
13 title = mw.Title.newFromText( title ).toText();
14 $link.addClass( 'mw-watch-link-disabled' );
15
16 // Preload the notification module for mw.notify
17 mw.loader.load( 'mediawiki.notification' );
18
19 // Use the class to determine whether to watch or unwatch
20 if ( !$subjectLink.hasClass( 'mw-watched-item' ) ) {
21 $link.text( mw.msg( 'watching' ) );
22 promise = api.watch( title ).done( function () {
23 $subjectLink.addClass( 'mw-watched-item' );
24 $link.text( mw.msg( 'unwatch' ) );
25 mw.notify( mw.msg( 'addedwatchtext-short', title ) );
26 } ).fail( function () {
27 $link.text( mw.msg( 'watch' ) );
28 mw.notify( mw.msg( 'watcherrortext', title ), { type: 'error' } );
29 } );
30 } else {
31 $link.text( mw.msg( 'unwatching' ) );
32 promise = api.unwatch( title ).done( function () {
33 $subjectLink.removeClass( 'mw-watched-item' );
34 $link.text( mw.msg( 'watch' ) );
35 mw.notify( mw.msg( 'removedwatchtext-short', title ) );
36 } ).fail( function () {
37 $link.text( mw.msg( 'unwatch' ) );
38 mw.notify( mw.msg( 'watcherrortext', title ), { type: 'error' } );
39 } );
40 }
41
42 promise.always( function () {
43 $link.removeClass( 'mw-watch-link-disabled' );
44 } );
45
46 e.preventDefault();
47 } );
48 } );
49 }() );