Made SSL validation in Curl HTTP requests the default.
[lhc/web/wiklou.git] / resources / mediawiki.page / mediawiki.page.watch.ajax.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 ( function ( mw, $ ) {
6 /**
7 * The name of the page to watch or unwatch.
8 */
9 var title = mw.config.get( 'wgRelevantPageName', mw.config.get( 'wgPageName' ) );
10
11 /**
12 * Update the link text, link href attribute and (if applicable)
13 * "loading" class.
14 *
15 * @param $link {jQuery} Anchor tag of (un)watch link.
16 * @param action {String} One of 'watch', 'unwatch'.
17 * @param state {String} [optional] 'idle' or 'loading'. Default is 'idle'.
18 */
19 function updateWatchLink( $link, action, state ) {
20 var accesskeyTip, msgKey, $li, otherAction;
21
22 // message keys 'watch', 'watching', 'unwatch' or 'unwatching'.
23 msgKey = state === 'loading' ? action + 'ing' : action;
24 otherAction = action === 'watch' ? 'unwatch' : 'watch';
25 accesskeyTip = $link.attr( 'title' ).match( mw.util.tooltipAccessKeyRegexp );
26 $li = $link.closest( 'li' );
27 /**
28 * Trigger a 'watchpage' event for this List item.
29 * Announce the otherAction value as the first param.
30 * Used to monitor the state of watch link.
31 * TODO: Revise when system wide hooks are implemented
32 */
33 if( state === undefined ) {
34 $li.trigger( 'watchpage.mw', otherAction );
35 }
36
37 $link
38 .text( mw.msg( msgKey ) )
39 .attr( 'title', mw.msg( 'tooltip-ca-' + action ) +
40 ( accesskeyTip ? ' ' + accesskeyTip[0] : '' )
41 )
42 .attr( 'href', mw.util.wikiScript() + '?' + $.param({
43 title: title,
44 action: action
45 })
46 );
47
48 // Most common ID style
49 if ( $li.prop( 'id' ) === 'ca-' + otherAction ) {
50 $li.prop( 'id', 'ca-' + action );
51 }
52
53 // Special case for vector icon
54 if ( $li.hasClass( 'icon' ) ) {
55 if ( state === 'loading' ) {
56 $link.addClass( 'loading' );
57 } else {
58 $link.removeClass( 'loading' );
59 }
60 }
61 }
62
63 /**
64 * @todo This should be moved somewhere more accessible.
65 * @param url {String}
66 * @return {String} The extracted action, defaults to 'view'.
67 */
68 function mwUriGetAction( url ) {
69 var action, actionPaths, key, i, m, parts;
70
71 actionPaths = mw.config.get( 'wgActionPaths' );
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 for ( key in actionPaths ) {
81 if ( actionPaths.hasOwnProperty( key ) ) {
82 parts = actionPaths[key].split( '$1' );
83 for ( i = 0; i < parts.length; i += 1 ) {
84 parts[i] = $.escapeRE( parts[i] );
85 }
86 m = new RegExp( parts.join( '(.+)' ) ).exec( url );
87 if ( m && m[1] ) {
88 return key;
89 }
90
91 }
92 }
93
94 return 'view';
95 }
96
97 // Expose local methods
98 mw.page.watch = {
99 'updateWatchLink': updateWatchLink
100 };
101
102 $( document ).ready( function () {
103 var $links = $( '.mw-watchlink a, a.mw-watchlink, ' +
104 '#ca-watch a, #ca-unwatch a, #mw-unwatch-link1, ' +
105 '#mw-unwatch-link2, #mw-watch-link2, #mw-watch-link1' );
106
107 // Allowing people to add inline animated links is a little scary
108 $links = $links.filter( ':not( #bodyContent *, #content * )' );
109
110 $links.click( function ( e ) {
111 var action, api, $link;
112
113 action = mwUriGetAction( this.href );
114
115 if ( action !== 'watch' && action !== 'unwatch' ) {
116 // Could not extract target action from link url,
117 // let native browsing handle it further
118 return true;
119 }
120 e.preventDefault();
121 e.stopPropagation();
122
123 $link = $( this );
124
125 updateWatchLink( $link, action, 'loading' );
126
127 api = new mw.Api();
128 api[action](
129 title,
130 // Success
131 function ( watchResponse ) {
132 var $li, otherAction;
133
134 otherAction = action === 'watch' ? 'unwatch' : 'watch';
135 $li = $link.closest( 'li' );
136
137 mw.notify( $.parseHTML( watchResponse.message ), { tag: 'watch-self' } );
138
139 // Set link to opposite
140 updateWatchLink( $link, otherAction );
141
142 // Bug 12395 - update the watch checkbox on edit pages when the
143 // page is watched or unwatched via the tab.
144 if ( watchResponse.watched !== undefined ) {
145 $( '#wpWatchthis' ).prop( 'checked', true );
146 } else {
147 $( '#wpWatchthis' ).removeProp( 'checked' );
148 }
149 },
150 // Error
151 function () {
152 var cleanTitle, msg, link;
153
154 // Reset link to non-loading mode
155 updateWatchLink( $link, action );
156
157 // Format error message
158 cleanTitle = title.replace( /_/g, ' ' );
159 link = mw.html.element(
160 'a', {
161 href: mw.util.wikiGetlink( title ),
162 title: cleanTitle
163 }, cleanTitle
164 );
165 msg = mw.messsage( 'watcherrortext', link );
166
167 // Report to user about the error
168 mw.notify( msg, { tag: 'watch-self' } );
169
170 }
171 );
172 });
173 });
174
175 }( mediaWiki, jQuery ) );