Merge "refreshLinks: Queue non-recursive updates"
[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.getUrl( title, { action: action } ) );
50
51 // Most common ID style
52 if ( $li.prop( 'id' ) === 'ca-' + otherAction ) {
53 $li.prop( 'id', 'ca-' + action );
54 }
55
56 if ( state === 'loading' ) {
57 $link.addClass( 'loading' );
58 } else {
59 $link.removeClass( 'loading' );
60 }
61 }
62
63 /**
64 * TODO: This should be moved somewhere more accessible.
65 *
66 * @private
67 * @param {string} url
68 * @return {string} The extracted action, defaults to 'view'
69 */
70 function mwUriGetAction( url ) {
71 var action, actionPaths, key, i, m, parts;
72
73 // TODO: Does MediaWiki give action path or query param
74 // precedence? If the former, move this to the bottom
75 action = mw.util.getParamValue( 'action', url );
76 if ( action !== null ) {
77 return action;
78 }
79
80 actionPaths = mw.config.get( 'wgActionPaths' );
81 for ( key in actionPaths ) {
82 if ( actionPaths.hasOwnProperty( key ) ) {
83 parts = actionPaths[ key ].split( '$1' );
84 for ( i = 0; i < parts.length; i++ ) {
85 parts[ i ] = mw.RegExp.escape( parts[ i ] );
86 }
87 m = new RegExp( parts.join( '(.+)' ) ).exec( url );
88 if ( m && m[ 1 ] ) {
89 return key;
90 }
91
92 }
93 }
94
95 return 'view';
96 }
97
98 // Expose public methods
99 mw.page.watch = {
100 updateWatchLink: updateWatchLink
101 };
102
103 $( function () {
104 var $links = $( '.mw-watchlink a, a.mw-watchlink' );
105 // Restrict to core interfaces, ignore user-generated content
106 $links = $links.filter( ':not( #bodyContent *, #content * )' );
107
108 $links.click( function ( e ) {
109 var mwTitle, action, api, $link;
110
111 mwTitle = mw.Title.newFromText( title );
112 action = mwUriGetAction( this.href );
113
114 if ( !mwTitle || ( action !== 'watch' && action !== 'unwatch' ) ) {
115 // Let native browsing handle the link
116 return true;
117 }
118 e.preventDefault();
119 e.stopPropagation();
120
121 $link = $( this );
122
123 if ( $link.hasClass( 'loading' ) ) {
124 return;
125 }
126
127 updateWatchLink( $link, action, 'loading' );
128
129 // Preload the notification module for mw.notify
130 mw.loader.load( 'mediawiki.notification' );
131
132 api = new mw.Api();
133
134 api[ action ]( title )
135 .done( function ( watchResponse ) {
136 var message, otherAction = action === 'watch' ? 'unwatch' : 'watch';
137
138 if ( mwTitle.getNamespaceId() > 0 && mwTitle.getNamespaceId() % 2 === 1 ) {
139 message = action === 'watch' ? 'addedwatchtext-talk' : 'removedwatchtext-talk';
140 } else {
141 message = action === 'watch' ? 'addedwatchtext' : 'removedwatchtext';
142 }
143
144 mw.notify( mw.message( message, mwTitle.getPrefixedText() ).parseDom(), {
145 tag: 'watch-self'
146 } );
147
148 // Set link to opposite
149 updateWatchLink( $link, otherAction );
150
151 // Update the "Watch this page" checkbox on action=edit when the
152 // page is watched or unwatched via the tab (T14395).
153 $( '#wpWatchthis' ).prop( 'checked', watchResponse.watched === true );
154 } )
155 .fail( function () {
156 var msg, link;
157
158 // Reset link to non-loading mode
159 updateWatchLink( $link, action );
160
161 // Format error message
162 link = mw.html.element(
163 'a', {
164 href: mw.util.getUrl( title ),
165 title: mwTitle.getPrefixedText()
166 }, mwTitle.getPrefixedText()
167 );
168 msg = mw.message( 'watcherrortext', link );
169
170 // Report to user about the error
171 mw.notify( msg, {
172 tag: 'watch-self',
173 type: 'error'
174 } );
175 } );
176 } );
177 } );
178
179 }( mediaWiki, jQuery ) );