Merging resourceloader branch into trunk. Full documentation is at http://www.mediawi...
[lhc/web/wiklou.git] / resources / mediawiki / legacy / mediawiki.legacy.ajaxwatch.js
1 /*
2 * Legacy emulation for the now depricated skins/common/ajaxwatch.js
3 *
4 * AJAX functionality for the watch/unwatch link
5 *
6 * @depends mw.legacy.jsMsg() from mw.legacy.wikibits.js
7 */
8
9 ( function( $, mw ) {
10
11 /* Extension */
12
13 $.extend( true, mw.legacy, {
14
15 /* Global Variables */
16
17 'wgAjaxWatch': {
18
19 /* Global Variables */
20
21 'watchMsg': 'Watch',
22 'unwatchMsg': 'Unwatch',
23 'watchingMsg': 'Watching...',
24 'unwatchingMsg': 'Unwatching...',
25 'tooltip-ca-watchMsg': 'Add this page to your watchlist',
26 'tooltip-ca-unwatchMsg': 'Remove this page from your watchlist',
27
28 /* Functions */
29
30 /**
31 * Sets the text of the watch/unwatch link
32 *
33 * @param object link DOM node or jQuery selection of link to set text of
34 * @param string action message to use ('watch', 'unwatch', 'watching' or 'unwatching')
35 */
36 'setLinkText': function( link, action ) {
37 var $link = $( link );
38 if ( action == 'watch' || action == 'unwatch' ) {
39 // save the accesskey from the title
40 var keyCommand = $link.attr( 'title' ).match( /\[.*?\]$/ ) ?
41 $link.attr( 'title' ).match( /\[.*?\]$/ )[0] : '';
42 $link.attr( 'title', wgAjaxWatch['tooltip-ca-' + action + 'Msg'] + ' ' + keyCommand );
43 }
44 if ( $link.data( 'icon' ) ) {
45 $link.attr( 'alt', wgAjaxWatch[action + 'Msg'] );
46 if ( action == 'watching' || action == 'unwatching' ) {
47 $link.addClass( 'loading' );
48 } else {
49 $link.removeClass( 'loading' );
50 }
51 } else {
52 $link.html( wgAjaxWatch[action+'Msg'] );
53 }
54 },
55 /**
56 * Processes responses from the server
57 *
58 * @param object response data from server
59 */
60 'processResult': function( response ) {
61 response = response.watch;
62 var $link = $(this);
63 // To ensure we set the same status for all watch links with the same target we trigger a custom event on
64 // *all* watch links.
65 if ( response.watched !== undefined ) {
66 wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'watch'] );
67 } else if ( response.unwatched !== undefined ){
68 wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'unwatch'] );
69 } else {
70 // Either we got an error code or it just plain broke.
71 window.location.href = $link.attr( 'href' );
72 return;
73 }
74 mw.legacy.jsMsg( response.message, 'watch' );
75 // Bug 12395 - update the watch checkbox on edit pages when the page is watched or unwatched via the tab.
76 if ( response.watched !== undefined ) {
77 $j( '#wpWatchthis' ).attr( 'checked', '1' );
78 } else {
79 $j( '#wpWatchthis' ).removeAttr( 'checked' );
80 }
81 }
82 } );
83 } );
84
85 /* Initialization */
86
87 $( document ).ready( function() {
88 var $links = $( '.mw-watchlink a, a.mw-watchlink' );
89 // BC with older skins...
90 $links = $links
91 .add( $( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1' ) )
92 .add( $( 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' ) );
93 // ...allowing people to add inline animated links is a little scary
94 $links = $links.filter( ':not( #bodyContent *, #content * )' );
95 $links.each( function() {
96 var $link = $(this);
97 $link
98 .data( 'icon', $link.parent().hasClass( 'icon' ) )
99 .data( 'action', $link.attr( 'href' ).match( /[\?\&]action=unwatch/i ) ? 'unwatch' : 'watch' );
100 var title = $link.attr( 'href' ).match( /[\?\&]title=(.*?)&/i )[1];
101 $link.data( 'target', decodeURIComponent( title ).replace( /_/g, ' ' ) );
102 } );
103 $links.click( function( event ) {
104 var $link = $(this);
105 if ( mw.legacy.wgAjaxWatch.supported === false || !mw.legacy.wgEnableWriteAPI || !mw.legacy.wfSupportsAjax() ) {
106 // Lazy initialization so we don't toss up ActiveX warnings on initial page load for IE 6 users with
107 // security settings.
108 mw.legacy.wgAjaxWatch.$links.unbind( 'click' );
109 return true;
110 }
111 mw.legacy.wgAjaxWatch.setLinkText( $link, $link.data( 'action' ) + 'ing' );
112 var url = mw.legacy.wgScriptPath + '/api' + mw.legacy.wgScriptExtension + '?action=watch&format=json&title='
113 + encodeURIComponent( $link.data( 'target' ) ) + ( $link.data( 'action' ) == 'unwatch' ? '&unwatch' : '' );
114 $.get( url, {}, mw.legacy.wgAjaxWatch.processResult, 'json' );
115 return false;
116 } );
117 // When a request returns, a custom event 'mw-ajaxwatch' is triggered on *all* watch links, so they can be updated
118 // if necessary
119 $links.bind( 'mw-ajaxwatch', function( event, target, action ) {
120 var $link = $(this);
121 var foo = $link.data( 'target' );
122 if ( $link.data( 'target' ) == target ) {
123 var otheraction = action == 'watch' ? 'unwatch' : 'watch';
124 $link.data( 'action', otheraction );
125 wgAjaxWatch.setLinkText( $link, otheraction );
126 $link.attr( 'href', $link.attr( 'href' ).replace( '/&action=' + action + '/', '&action=' + otheraction ) );
127 if ( $link.parent().attr( 'id' ) == 'ca-' + action ){
128 $link.parent().attr( 'id', 'ca-' + otheraction );
129 }
130 }
131 return false;
132 } );
133 mw.legacy.wgAjaxWatch.$links = $links;
134 } );
135
136 } )( jQuery, mediaWiki );