Index: trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js |
— | — | @@ -2,12 +2,10 @@ |
3 | 3 | * Animate watch/unwatch links to use asynchronous API requests to |
4 | 4 | * watch pages, rather than clicking on links. Requires jQuery. |
5 | 5 | */ |
| 6 | +( function( $ ) { |
| 7 | +var $links; |
6 | 8 | |
7 | | -if ( typeof wgAjaxWatch === 'undefined' || !wgAjaxWatch ) { |
8 | | - window.wgAjaxWatch = { }; |
9 | | -} |
10 | | - |
11 | | -wgAjaxWatch.setLinkText = function( $link, action ) { |
| 9 | +var setLinkText = function( $link, action ) { |
12 | 10 | if ( action == 'watch' || action == 'unwatch' ) { |
13 | 11 | // save the accesskey from the title |
14 | 12 | var keyCommand = $link.attr( 'title' ).match( /\[.*?\]$/ ) ? $link.attr( 'title' ).match( /\[.*?\]$/ )[0] : ''; |
— | — | @@ -25,26 +23,26 @@ |
26 | 24 | } |
27 | 25 | }; |
28 | 26 | |
29 | | -wgAjaxWatch.processResult = function( response, $link ) { |
30 | | - response = response.watch; |
| 27 | +var processResult = function( response, $link ) { |
| 28 | + watchResponse = response.watch; |
31 | 29 | |
32 | 30 | // To ensure we set the same status for all watch links with the |
33 | 31 | // same target we trigger a custom event on *all* watch links. |
34 | | - if( response.watched !== undefined ) { |
35 | | - wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'watch', $link] ); |
36 | | - } else if ( response.unwatched !== undefined ) { |
37 | | - wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'unwatch', $link] ); |
| 32 | + if ( watchResponse.watched !== undefined ) { |
| 33 | + $links.trigger( 'mw-ajaxwatch', [watchResponse.title, 'watch', $link] ); |
| 34 | + } else if ( watchResponse.unwatched !== undefined ) { |
| 35 | + $links.trigger( 'mw-ajaxwatch', [watchResponse.title, 'unwatch', $link] ); |
38 | 36 | } else { |
39 | 37 | // Either we got an error code or it just plain broke. |
40 | 38 | window.location.href = $link[0].href; |
41 | 39 | return; |
42 | 40 | } |
43 | 41 | |
44 | | - mw.util.jsMessage( response.message, 'watch' ); |
| 42 | + mw.util.jsMessage( watchResponse.message, 'watch' ); |
45 | 43 | |
46 | 44 | // Bug 12395 - update the watch checkbox on edit pages when the |
47 | 45 | // page is watched or unwatched via the tab. |
48 | | - if( response.watched !== undefined ) { |
| 46 | + if ( watchResponse.watched !== undefined ) { |
49 | 47 | $( '#wpWatchthis' ).attr( 'checked', 'checked' ); |
50 | 48 | } else { |
51 | 49 | $( '#wpWatchthis' ).removeAttr( 'checked' ); |
— | — | @@ -52,7 +50,7 @@ |
53 | 51 | }; |
54 | 52 | |
55 | 53 | $( document ).ready( function() { |
56 | | - var $links = $( '.mw-watchlink a, a.mw-watchlink' ); |
| 54 | + $links = $( '.mw-watchlink a, a.mw-watchlink' ); |
57 | 55 | // BC with older skins |
58 | 56 | $links = $links |
59 | 57 | .add( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1, ' + |
— | — | @@ -73,15 +71,15 @@ |
74 | 72 | $links.click( function( event ) { |
75 | 73 | var $link = $( this ); |
76 | 74 | |
77 | | - if( wgAjaxWatch.supported === false || !mw.config.get( 'wgEnableWriteAPI' ) ) { |
| 75 | + if ( !mw.config.get( 'wgEnableWriteAPI' ) ) { |
78 | 76 | // Lazy initialization so we don't toss up |
79 | 77 | // ActiveX warnings on initial page load |
80 | 78 | // for IE 6 users with security settings. |
81 | | - wgAjaxWatch.$links.unbind( 'click' ); |
| 79 | + $links.unbind( 'click' ); |
82 | 80 | return true; |
83 | 81 | } |
84 | 82 | |
85 | | - wgAjaxWatch.setLinkText( $link, $link.data( 'action' ) + 'ing' ); |
| 83 | + setLinkText( $link, $link.data( 'action' ) + 'ing' ); |
86 | 84 | |
87 | 85 | var reqData = { |
88 | 86 | 'action': 'watch', |
— | — | @@ -95,7 +93,7 @@ |
96 | 94 | + '/api' + mw.config.get( 'wgScriptExtension' ), |
97 | 95 | reqData, |
98 | 96 | function( data, textStatus, xhr ) { |
99 | | - wgAjaxWatch.processResult( data, $link ); |
| 97 | + processResult( data, $link ); |
100 | 98 | } |
101 | 99 | ); |
102 | 100 | |
— | — | @@ -106,19 +104,19 @@ |
107 | 105 | // on *all* watch links, so they can be updated if necessary |
108 | 106 | $links.bind( 'mw-ajaxwatch', function( event, target, action, $link ) { |
109 | 107 | var foo = $link.data( 'target' ); |
110 | | - if( $link.data( 'target' ) == target ) { |
| 108 | + if ( $link.data( 'target' ) == target ) { |
111 | 109 | var otheraction = action == 'watch' |
112 | 110 | ? 'unwatch' |
113 | 111 | : 'watch'; |
114 | 112 | |
115 | 113 | $link.data( 'action', otheraction ); |
116 | | - wgAjaxWatch.setLinkText( $link, otheraction ); |
| 114 | + setLinkText( $link, otheraction ); |
117 | 115 | $link.attr( 'href', |
118 | 116 | mw.config.get( 'wgScript' ) |
119 | 117 | + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) ) |
120 | 118 | + '&action=' + otheraction |
121 | 119 | ); |
122 | | - if( $link.closest( 'li' ).attr( 'id' ) == 'ca-' + action ) { |
| 120 | + if ( $link.closest( 'li' ).attr( 'id' ) == 'ca-' + action ) { |
123 | 121 | $link.closest( 'li' ).attr( 'id', 'ca-' + otheraction ); |
124 | 122 | // update the link text with the new message |
125 | 123 | $link.text( mw.msg( otheraction ) ); |
— | — | @@ -128,5 +126,6 @@ |
129 | 127 | return false; |
130 | 128 | }); |
131 | 129 | |
132 | | - wgAjaxWatch.$links = $links; |
133 | 130 | }); |
| 131 | + |
| 132 | +})( jQuery ); |