r113894 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113893‎ | r113894 | r113895 >
Date:06:04, 15 March 2012
Author:krinkle
Status:reverted
Tags:gerritmigration 
Comment:
[mediawiki.action.watch] clean up
* Fix implied global variable 'i' in mwUriGetAction()
* Indention nearly everything an extra level (svn di -x-uw)
* other minor js coding style
Modified paths:
  • /trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js
@@ -4,152 +4,158 @@
55 */
66 ( function ( $, mw, undefined ) {
77
8 -/**
9 - * The name of the page to watch or unwatch.
10 - */
11 -var title = mw.config.get( 'wgRelevantPageName', mw.config.get( 'wgPageName' ) );
 8+ /**
 9+ * The name of the page to watch or unwatch.
 10+ */
 11+ var title = mw.config.get( 'wgRelevantPageName', mw.config.get( 'wgPageName' ) );
1212
13 -/**
14 - * Update the link text, link href attribute and (if applicable)
15 - * "loading" class.
16 - *
17 - * @param $link {jQuery} Anchor tag of (un)watch link
18 - * @param action {String} One of 'watch', 'unwatch'.
19 - * @param state {String} [optional] 'idle' or 'loading'. Default is 'idle'.
20 - */
21 -function updateWatchLink( $link, action, state ) {
22 - // message keys 'watch', 'watching', 'unwatch' or 'unwatching'.
23 - var msgKey = state === 'loading' ? action + 'ing' : action,
24 - accesskeyTip = $link.attr( 'title' ).match( mw.util.tooltipAccessKeyRegexp ),
 13+ /**
 14+ * Update the link text, link href attribute and (if applicable)
 15+ * "loading" class.
 16+ *
 17+ * @param $link {jQuery} Anchor tag of (un)watch link.
 18+ * @param action {String} One of 'watch', 'unwatch'.
 19+ * @param state {String} [optional] 'idle' or 'loading'. Default is 'idle'.
 20+ */
 21+ function updateWatchLink( $link, action, state ) {
 22+ var accesskeyTip, msgKey, $li;
 23+
 24+ // message keys 'watch', 'watching', 'unwatch' or 'unwatching'.
 25+ msgKey = state === 'loading' ? action + 'ing' : action;
 26+ accesskeyTip = $link.attr( 'title' ).match( mw.util.tooltipAccessKeyRegexp );
2527 $li = $link.closest( 'li' );
2628
27 - $link
28 - .text( mw.msg( msgKey ) )
29 - .attr( 'title', mw.msg( 'tooltip-ca-' + action ) +
30 - ( accesskeyTip ? ' ' + accesskeyTip[0] : '' )
31 - )
32 - .attr( 'href', mw.util.wikiScript() + '?' + $.param({
33 - title: title,
34 - action: action
35 - })
36 - );
 29+ $link
 30+ .text( mw.msg( msgKey ) )
 31+ .attr( 'title', mw.msg( 'tooltip-ca-' + action ) +
 32+ ( accesskeyTip ? ' ' + accesskeyTip[0] : '' )
 33+ )
 34+ .attr( 'href', mw.util.wikiScript() + '?' + $.param({
 35+ title: title,
 36+ action: action
 37+ })
 38+ );
3739
38 - // Special case for vector icon
39 - if ( $li.hasClass( 'icon' ) ) {
40 - if ( state === 'loading' ) {
41 - $link.addClass( 'loading' );
42 - } else {
43 - $link.removeClass( 'loading' );
 40+ // Special case for vector icon
 41+ if ( $li.hasClass( 'icon' ) ) {
 42+ if ( state === 'loading' ) {
 43+ $link.addClass( 'loading' );
 44+ } else {
 45+ $link.removeClass( 'loading' );
 46+ }
4447 }
4548 }
46 -}
4749
48 -/**
49 - * @todo This should be moved somewhere more accessible.
50 - * @param url {String}
51 - * @return {String} The extracted action, defaults to 'view'.
52 - */
53 -function mwUriGetAction( url ) {
54 - var actionPaths = mw.config.get( 'wgActionPaths' ),
55 - key, parts, m, action;
 50+ /**
 51+ * @todo This should be moved somewhere more accessible.
 52+ * @param url {String}
 53+ * @return {String} The extracted action, defaults to 'view'.
 54+ */
 55+ function mwUriGetAction( url ) {
 56+ var action, actionPaths, key, i, m, parts;
5657
57 - // @todo: Does MediaWiki give action path or query param
58 - // precedence ? If the former, move this to the bottom
59 - action = mw.util.getParamValue( 'action', url );
60 - if ( action !== null ) {
61 - return action;
62 - }
 58+ actionPaths = mw.config.get( 'wgActionPaths' );
6359
64 - for ( key in actionPaths ) {
65 - if ( actionPaths.hasOwnProperty( key ) ) {
66 - parts = actionPaths[key].split( '$1' );
67 - for ( i = 0; i < parts.length; i += 1 ) {
68 - parts[i] = $.escapeRE( parts[i] );
 60+ // @todo: Does MediaWiki give action path or query param
 61+ // precedence ? If the former, move this to the bottom
 62+ action = mw.util.getParamValue( 'action', url );
 63+ if ( action !== null ) {
 64+ return action;
 65+ }
 66+
 67+ for ( key in actionPaths ) {
 68+ if ( actionPaths.hasOwnProperty( key ) ) {
 69+ parts = actionPaths[key].split( '$1' );
 70+ for ( i = 0; i < parts.length; i += 1 ) {
 71+ parts[i] = $.escapeRE( parts[i] );
 72+ }
 73+ m = new RegExp( parts.join( '(.+)' ) ).exec( url );
 74+ if ( m && m[1] ) {
 75+ return key;
 76+ }
 77+
6978 }
70 - m = new RegExp( parts.join( '(.+)' ) ).exec( url );
71 - if ( m && m[1] ) {
72 - return key;
73 - }
74 -
7579 }
 80+
 81+ return 'view';
7682 }
7783
78 - return 'view';
79 -}
 84+ $( document ).ready( function () {
 85+ var $links = $( '.mw-watchlink a, a.mw-watchlink, ' +
 86+ '#ca-watch a, #ca-unwatch a, #mw-unwatch-link1, ' +
 87+ '#mw-unwatch-link2, #mw-watch-link2, #mw-watch-link1' );
8088
81 -$( document ).ready( function() {
82 - var $links = $( '.mw-watchlink a, a.mw-watchlink, ' +
83 - '#ca-watch a, #ca-unwatch a, #mw-unwatch-link1, ' +
84 - '#mw-unwatch-link2, #mw-watch-link2, #mw-watch-link1' );
 89+ // Allowing people to add inline animated links is a little scary
 90+ $links = $links.filter( ':not( #bodyContent *, #content * )' );
8591
86 - // Allowing people to add inline animated links is a little scary
87 - $links = $links.filter( ':not( #bodyContent *, #content * )' );
 92+ $links.click( function ( e ) {
 93+ var action, api, $link;
8894
89 - $links.click( function( e ) {
90 - var $link, api,
9195 action = mwUriGetAction( this.href );
9296
93 - if ( action !== 'watch' && action !== 'unwatch' ) {
94 - // Could not extract target action from link url,
95 - // let native browsing handle it further
96 - return true;
97 - }
98 - e.preventDefault();
99 - e.stopPropagation();
100 -
101 - $link = $( this );
 97+ if ( action !== 'watch' && action !== 'unwatch' ) {
 98+ // Could not extract target action from link url,
 99+ // let native browsing handle it further
 100+ return true;
 101+ }
 102+ e.preventDefault();
 103+ e.stopPropagation();
102104
103 - updateWatchLink( $link, action, 'loading' );
 105+ $link = $( this );
104106
105 - api = new mw.Api();
106 - api[action](
107 - title,
108 - // Success
109 - function( watchResponse ) {
110 - var otherAction = action === 'watch' ? 'unwatch' : 'watch',
 107+ updateWatchLink( $link, action, 'loading' );
 108+
 109+ api = new mw.Api();
 110+ api[action](
 111+ title,
 112+ // Success
 113+ function ( watchResponse ) {
 114+ var $li, otherAction;
 115+
 116+ otherAction = action === 'watch' ? 'unwatch' : 'watch';
111117 $li = $link.closest( 'li' );
112118
113 - mw.util.jsMessage( watchResponse.message, 'ajaxwatch' );
 119+ mw.util.jsMessage( watchResponse.message, 'ajaxwatch' );
114120
115 - // Set link to opposite
116 - updateWatchLink( $link, otherAction );
 121+ // Set link to opposite
 122+ updateWatchLink( $link, otherAction );
117123
118 - // Most common ID style
119 - if ( $li.prop( 'id' ) === 'ca-' + otherAction || $li.prop( 'id' ) === 'ca-' + action ) {
120 - $li.prop( 'id', 'ca-' + otherAction );
121 - }
122 -
123 - // Bug 12395 - update the watch checkbox on edit pages when the
124 - // page is watched or unwatched via the tab.
125 - if ( watchResponse.watched !== undefined ) {
126 - $( '#wpWatchthis' ).prop( 'checked', true );
127 - } else {
128 - $( '#wpWatchthis' ).removeProp( 'checked' );
129 - }
130 - },
131 - // Error
132 - function(){
 124+ // Most common ID style
 125+ if ( $li.prop( 'id' ) === 'ca-' + otherAction || $li.prop( 'id' ) === 'ca-' + action ) {
 126+ $li.prop( 'id', 'ca-' + otherAction );
 127+ }
133128
134 - // Reset link to non-loading mode
135 - updateWatchLink( $link, action );
136 -
137 - // Format error message
138 - var cleanTitle = title.replace( /_/g, ' ' );
139 - var link = mw.html.element(
140 - 'a', {
141 - 'href': mw.util.wikiGetlink( title ),
142 - 'title': cleanTitle
143 - }, cleanTitle
144 - );
145 - var html = mw.msg( 'watcherrortext', link );
146 -
147 - // Report to user about the error
148 - mw.util.jsMessage( html, 'ajaxwatch' );
 129+ // Bug 12395 - update the watch checkbox on edit pages when the
 130+ // page is watched or unwatched via the tab.
 131+ if ( watchResponse.watched !== undefined ) {
 132+ $( '#wpWatchthis' ).prop( 'checked', true );
 133+ } else {
 134+ $( '#wpWatchthis' ).removeProp( 'checked' );
 135+ }
 136+ },
 137+ // Error
 138+ function () {
 139+ var cleanTitle, html, link;
149140
150 - }
151 - );
 141+ // Reset link to non-loading mode
 142+ updateWatchLink( $link, action );
 143+
 144+ // Format error message
 145+ cleanTitle = title.replace( /_/g, ' ' );
 146+ link = mw.html.element(
 147+ 'a', {
 148+ href: mw.util.wikiGetlink( title ),
 149+ title: cleanTitle
 150+ }, cleanTitle
 151+ );
 152+ html = mw.msg( 'watcherrortext', link );
 153+
 154+ // Report to user about the error
 155+ mw.util.jsMessage( html, 'ajaxwatch' );
 156+
 157+ }
 158+ );
 159+ });
152160 });
153161
154 -});
155 -
156 -})( jQuery, mediaWiki );
 162+}( jQuery, mediaWiki ) );

Follow-up revisions

RevisionCommit summaryAuthorDate
r114335Revert r107309, r113601, r113704, r113742, r113792, r113838, r113859, r113893......catrope00:16, 21 March 2012

Status & tagging log