Merge "mediawiki.page.watch: Remove obsolete CSS selectors"
[lhc/web/wiklou.git] / resources / src / mediawiki / page / watch.js
1 /**
2 * Animate watch/unwatch links to use asynchronous API requests to
3 * watch pages, rather than navigating to a different URI.
4 *
5 * @class mw.page.watch.ajax
6 */
7 ( function ( mw, $ ) {
8 // The name of the page to watch or unwatch
9 var title = mw.config.get( 'wgRelevantPageName' );
10
11 /**
12 * Update the link text, link href attribute and (if applicable)
13 * "loading" class.
14 *
15 * @param {jQuery} $link Anchor tag of (un)watch link
16 * @param {string} action One of 'watch', 'unwatch'
17 * @param {string} [state="idle"] 'idle' or 'loading'. Default is 'idle'
18 */
19 function updateWatchLink( $link, action, state ) {
20 var msgKey, $li, otherAction;
21
22 // A valid but empty jQuery object shouldn't throw a TypeError
23 if ( !$link.length ) {
24 return;
25 }
26
27 // Invalid actions shouldn't silently turn the page in an unrecoverable state
28 if ( action !== 'watch' && action !== 'unwatch' ) {
29 throw new Error( 'Invalid action' );
30 }
31
32 // message keys 'watch', 'watching', 'unwatch' or 'unwatching'.
33 msgKey = state === 'loading' ? action + 'ing' : action;
34 otherAction = action === 'watch' ? 'unwatch' : 'watch';
35 $li = $link.closest( 'li' );
36
37 // Trigger a 'watchpage' event for this List item.
38 // Announce the otherAction value as the first param.
39 // Used to monitor the state of watch link.
40 // TODO: Revise when system wide hooks are implemented
41 if ( state === undefined ) {
42 $li.trigger( 'watchpage.mw', otherAction );
43 }
44
45 $link
46 .text( mw.msg( msgKey ) )
47 .attr( 'title', mw.msg( 'tooltip-ca-' + action ) )
48 .updateTooltipAccessKeys()
49 .attr( 'href', mw.util.wikiScript() + '?' + $.param( {
50 title: title,
51 action: action
52 } )
53 );
54
55 // Most common ID style
56 if ( $li.prop( 'id' ) === 'ca-' + otherAction ) {
57 $li.prop( 'id', 'ca-' + action );
58 }
59
60 if ( state === 'loading' ) {
61 $link.addClass( 'loading' );
62 } else {
63 $link.removeClass( 'loading' );
64 }
65 }
66
67 /**
68 * TODO: This should be moved somewhere more accessible.
69 *
70 * @private
71 * @param {string} url
72 * @return {string} The extracted action, defaults to 'view'
73 */
74 function mwUriGetAction( url ) {
75 var action, actionPaths, key, i, m, parts;
76
77 // TODO: Does MediaWiki give action path or query param
78 // precedence? If the former, move this to the bottom
79 action = mw.util.getParamValue( 'action', url );
80 if ( action !== null ) {
81 return action;
82 }
83
84 actionPaths = mw.config.get( 'wgActionPaths' );
85 for ( key in actionPaths ) {
86 if ( actionPaths.hasOwnProperty( key ) ) {
87 parts = actionPaths[ key ].split( '$1' );
88 for ( i = 0; i < parts.length; i++ ) {
89 parts[ i ] = mw.RegExp.escape( parts[ i ] );
90 }
91 m = new RegExp( parts.join( '(.+)' ) ).exec( url );
92 if ( m && m[ 1 ] ) {
93 return key;
94 }
95
96 }
97 }
98
99 return 'view';
100 }
101
102 // Expose public methods
103 mw.page.watch = {
104 updateWatchLink: updateWatchLink
105 };
106
107 $( function () {
108 var $links = $( '.mw-watchlink a, a.mw-watchlink' );
109 // Restrict to core interfaces, ignore user-generated content
110 $links = $links.filter( ':not( #bodyContent *, #content * )' );
111
112 $links.click( function ( e ) {
113 var action, api, $link;
114
115 // Start preloading the notification module (normally loaded by mw.notify())
116 mw.loader.load( 'mediawiki.notification' );
117
118 action = mwUriGetAction( this.href );
119
120 if ( action !== 'watch' && action !== 'unwatch' ) {
121 // Could not extract target action from link url,
122 // let native browsing handle it further
123 return true;
124 }
125 e.preventDefault();
126 e.stopPropagation();
127
128 $link = $( this );
129
130 if ( $link.hasClass( 'loading' ) ) {
131 return;
132 }
133
134 updateWatchLink( $link, action, 'loading' );
135
136 api = new mw.Api();
137
138 api[ action ]( title )
139 .done( function ( watchResponse ) {
140 var otherAction = action === 'watch' ? 'unwatch' : 'watch';
141
142 mw.notify( $.parseHTML( watchResponse.message ), {
143 tag: 'watch-self'
144 } );
145
146 // Set link to opposite
147 updateWatchLink( $link, otherAction );
148
149 // Update the "Watch this page" checkbox on action=edit when the
150 // page is watched or unwatched via the tab (bug 12395).
151 $( '#wpWatchthis' ).prop( 'checked', watchResponse.watched !== undefined );
152 } )
153 .fail( function () {
154 var cleanTitle, msg, link;
155
156 // Reset link to non-loading mode
157 updateWatchLink( $link, action );
158
159 // Format error message
160 cleanTitle = title.replace( /_/g, ' ' );
161 link = mw.html.element(
162 'a', {
163 href: mw.util.getUrl( title ),
164 title: cleanTitle
165 }, cleanTitle
166 );
167 msg = mw.message( 'watcherrortext', link );
168
169 // Report to user about the error
170 mw.notify( msg, {
171 tag: 'watch-self',
172 type: 'error'
173 } );
174 } );
175 } );
176 } );
177
178 }( mediaWiki, jQuery ) );