r78147 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78146‎ | r78147 | r78148 >
Date:21:59, 9 December 2010
Author:krinkle
Status:ok
Tags:
Comment:
start resourceloadify mw.legacy.ajaxwatch
Modified paths:
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/resources/Resources.php (modified) (history)
  • /trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.js (added) (history)
  • /trunk/phase3/skins/common/ajaxwatch.js (deleted) (history)

Diff [purge]

Index: trunk/phase3/skins/common/ajaxwatch.js
@@ -1,120 +0,0 @@
2 -/**
3 - * Animate watch/unwatch links to use asynchronous API requests to
4 - * watch pages, rather than clicking on links. Requires jQuery.
5 - * Uses jsMsg() from wikibits.js.
6 - */
7 -
8 -if ( typeof wgAjaxWatch === 'undefined' || !wgAjaxWatch ) {
9 - window.wgAjaxWatch = { };
10 -}
11 -
12 -wgAjaxWatch.setLinkText = function( $link, action ) {
13 - if ( action == 'watch' || action == 'unwatch' ) {
14 - // save the accesskey from the title
15 - var keyCommand = $link.attr( 'title' ).match( /\[.*?\]$/ ) ? $link.attr( 'title' ).match( /\[.*?\]$/ )[0] : '';
16 - $link.attr( 'title', mediaWiki.msg( 'tooltip-ca-' + action ) + ' ' + keyCommand );
17 - }
18 - if ( $link.data( 'icon' ) ) {
19 - $link.attr( 'alt', mediaWiki.msg( action ) );
20 - if ( action == 'watching' || action == 'unwatching' ) {
21 - $link.addClass( 'loading' );
22 - } else {
23 - $link.removeClass( 'loading' );
24 - }
25 - } else {
26 - $link.html( mediaWiki.msg( action ) );
27 - }
28 -};
29 -
30 -wgAjaxWatch.processResult = function( response ) {
31 - response = response.watch;
32 - var $link = $( this );
33 - // To ensure we set the same status for all watch links with the
34 - // same target we trigger a custom event on *all* watch links.
35 - if( response.watched !== undefined ) {
36 - wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'watch'] );
37 - } else if ( response.unwatched !== undefined ) {
38 - wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'unwatch'] );
39 - } else {
40 - // Either we got an error code or it just plain broke.
41 - window.location.href = $link.attr( 'href' );
42 - return;
43 - }
44 -
45 - jsMsg( response.message, 'watch' );
46 -
47 - // Bug 12395 - update the watch checkbox on edit pages when the
48 - // page is watched or unwatched via the tab.
49 - if( response.watched !== undefined ) {
50 - $( '#wpWatchthis' ).attr( 'checked', '1' );
51 - } else {
52 - $( '#wpWatchthis' ).removeAttr( 'checked' );
53 - }
54 -};
55 -
56 -$( document ).ready( function() {
57 - var $links = $( '.mw-watchlink a, a.mw-watchlink' );
58 - // BC with older skins
59 - $links = $links
60 - .add( $( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1' ) )
61 - .add( $( 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' ) );
62 - // allowing people to add inline animated links is a little scary
63 - $links = $links.filter( ':not( #bodyContent *, #content * )' );
64 -
65 - $links.each( function() {
66 - var $link = $( this );
67 - $link
68 - .data( 'icon', $link.parents( 'li' ).hasClass( 'icon' ) )
69 - .data( 'action', $link.attr( 'href' ).match( /[\?&]action=unwatch/i ) ? 'unwatch' : 'watch' );
70 - var title = $link.attr( 'href' ).match( /[\?&]title=(.*?)&/i )[1];
71 - $link.data( 'target', decodeURIComponent( title ).replace( /_/g, ' ' ) );
72 - });
73 -
74 - $links.click( function( event ) {
75 - var $link = $( this );
76 -
77 - if( wgAjaxWatch.supported === false || !wgEnableWriteAPI || !wfSupportsAjax() ) {
78 - // Lazy initialization so we don't toss up
79 - // ActiveX warnings on initial page load
80 - // for IE 6 users with security settings.
81 - wgAjaxWatch.$links.unbind( 'click' );
82 - return true;
83 - }
84 -
85 - wgAjaxWatch.setLinkText( $link, $link.data( 'action' ) + 'ing' );
86 - $.get( wgScriptPath
87 - + '/api' + wgScriptExtension + '?action=watch&format=json&title='
88 - + encodeURIComponent( $link.data( 'target' ) )
89 - + ( $link.data( 'action' ) == 'unwatch' ? '&unwatch' : '' ),
90 - {},
91 - wgAjaxWatch.processResult,
92 - 'json'
93 - );
94 -
95 - return false;
96 - });
97 -
98 - // When a request returns, a custom event 'mw-ajaxwatch' is triggered
99 - // on *all* watch links, so they can be updated if necessary
100 - $links.bind( 'mw-ajaxwatch', function( event, target, action ) {
101 - var $link = $( this );
102 - var foo = $link.data( 'target' );
103 - if( $link.data( 'target' ) == target ) {
104 - var otheraction = action == 'watch'
105 - ? 'unwatch'
106 - : 'watch';
107 -
108 - $link.data( 'action', otheraction );
109 - wgAjaxWatch.setLinkText( $link, otheraction );
110 - $link.attr( 'href', $link.attr( 'href' ).replace( '/&action=' + action + '/', '&action=' + otheraction ) );
111 - if( $link.parents( 'li' ).attr( 'id' ) == 'ca-' + action ) {
112 - $link.parents( 'li' ).attr( 'id', 'ca-' + otheraction );
113 - // update the link text with the new message
114 - $link.text( mediaWiki.msg( otheraction ) );
115 - }
116 - }
117 - return false;
118 - });
119 -
120 - wgAjaxWatch.$links = $links;
121 -});
Index: trunk/phase3/includes/OutputPage.php
@@ -1627,7 +1627,7 @@
16281628 wfRunHooks( 'AjaxAddScript', array( &$this ) );
16291629
16301630 if( $wgAjaxWatch && $wgUser->isLoggedIn() ) {
1631 - $this->addModules( 'mediawiki.legacy.ajaxwatch' );
 1631+ $this->addModules( 'mediawiki.action.watch.ajax' );
16321632 }
16331633
16341634 if ( $wgEnableMWSuggest && !$wgUser->getOption( 'disablesuggest', false ) ) {
Index: trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.js
@@ -0,0 +1,120 @@
 2+/**
 3+ * Animate watch/unwatch links to use asynchronous API requests to
 4+ * watch pages, rather than clicking on links. Requires jQuery.
 5+ * Uses jsMsg() from wikibits.js.
 6+ */
 7+
 8+if ( typeof wgAjaxWatch === 'undefined' || !wgAjaxWatch ) {
 9+ window.wgAjaxWatch = { };
 10+}
 11+
 12+wgAjaxWatch.setLinkText = function( $link, action ) {
 13+ if ( action == 'watch' || action == 'unwatch' ) {
 14+ // save the accesskey from the title
 15+ var keyCommand = $link.attr( 'title' ).match( /\[.*?\]$/ ) ? $link.attr( 'title' ).match( /\[.*?\]$/ )[0] : '';
 16+ $link.attr( 'title', mediaWiki.msg( 'tooltip-ca-' + action ) + ' ' + keyCommand );
 17+ }
 18+ if ( $link.data( 'icon' ) ) {
 19+ $link.attr( 'alt', mediaWiki.msg( action ) );
 20+ if ( action == 'watching' || action == 'unwatching' ) {
 21+ $link.addClass( 'loading' );
 22+ } else {
 23+ $link.removeClass( 'loading' );
 24+ }
 25+ } else {
 26+ $link.html( mediaWiki.msg( action ) );
 27+ }
 28+};
 29+
 30+wgAjaxWatch.processResult = function( response ) {
 31+ response = response.watch;
 32+ var $link = $( this );
 33+ // To ensure we set the same status for all watch links with the
 34+ // same target we trigger a custom event on *all* watch links.
 35+ if( response.watched !== undefined ) {
 36+ wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'watch'] );
 37+ } else if ( response.unwatched !== undefined ) {
 38+ wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'unwatch'] );
 39+ } else {
 40+ // Either we got an error code or it just plain broke.
 41+ window.location.href = $link.attr( 'href' );
 42+ return;
 43+ }
 44+
 45+ jsMsg( response.message, 'watch' );
 46+
 47+ // Bug 12395 - update the watch checkbox on edit pages when the
 48+ // page is watched or unwatched via the tab.
 49+ if( response.watched !== undefined ) {
 50+ $( '#wpWatchthis' ).attr( 'checked', '1' );
 51+ } else {
 52+ $( '#wpWatchthis' ).removeAttr( 'checked' );
 53+ }
 54+};
 55+
 56+$( document ).ready( function() {
 57+ var $links = $( '.mw-watchlink a, a.mw-watchlink' );
 58+ // BC with older skins
 59+ $links = $links
 60+ .add( $( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1' ) )
 61+ .add( $( 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' ) );
 62+ // allowing people to add inline animated links is a little scary
 63+ $links = $links.filter( ':not( #bodyContent *, #content * )' );
 64+
 65+ $links.each( function() {
 66+ var $link = $( this );
 67+ $link
 68+ .data( 'icon', $link.parents( 'li' ).hasClass( 'icon' ) )
 69+ .data( 'action', $link.attr( 'href' ).match( /[\?&]action=unwatch/i ) ? 'unwatch' : 'watch' );
 70+ var title = $link.attr( 'href' ).match( /[\?&]title=(.*?)&/i )[1];
 71+ $link.data( 'target', decodeURIComponent( title ).replace( /_/g, ' ' ) );
 72+ });
 73+
 74+ $links.click( function( event ) {
 75+ var $link = $( this );
 76+
 77+ if( wgAjaxWatch.supported === false || !wgEnableWriteAPI || !wfSupportsAjax() ) {
 78+ // Lazy initialization so we don't toss up
 79+ // ActiveX warnings on initial page load
 80+ // for IE 6 users with security settings.
 81+ wgAjaxWatch.$links.unbind( 'click' );
 82+ return true;
 83+ }
 84+
 85+ wgAjaxWatch.setLinkText( $link, $link.data( 'action' ) + 'ing' );
 86+ $.get( wgScriptPath
 87+ + '/api' + wgScriptExtension + '?action=watch&format=json&title='
 88+ + encodeURIComponent( $link.data( 'target' ) )
 89+ + ( $link.data( 'action' ) == 'unwatch' ? '&unwatch' : '' ),
 90+ {},
 91+ wgAjaxWatch.processResult,
 92+ 'json'
 93+ );
 94+
 95+ return false;
 96+ });
 97+
 98+ // When a request returns, a custom event 'mw-ajaxwatch' is triggered
 99+ // on *all* watch links, so they can be updated if necessary
 100+ $links.bind( 'mw-ajaxwatch', function( event, target, action ) {
 101+ var $link = $( this );
 102+ var foo = $link.data( 'target' );
 103+ if( $link.data( 'target' ) == target ) {
 104+ var otheraction = action == 'watch'
 105+ ? 'unwatch'
 106+ : 'watch';
 107+
 108+ $link.data( 'action', otheraction );
 109+ wgAjaxWatch.setLinkText( $link, otheraction );
 110+ $link.attr( 'href', $link.attr( 'href' ).replace( '/&action=' + action + '/', '&action=' + otheraction ) );
 111+ if( $link.parents( 'li' ).attr( 'id' ) == 'ca-' + action ) {
 112+ $link.parents( 'li' ).attr( 'id', 'ca-' + otheraction );
 113+ // update the link text with the new message
 114+ $link.text( mediaWiki.msg( otheraction ) );
 115+ }
 116+ }
 117+ return false;
 118+ });
 119+
 120+ wgAjaxWatch.$links = $links;
 121+});
Property changes on: trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.js
___________________________________________________________________
Added: svn:eol-style
1122 + native
Index: trunk/phase3/resources/Resources.php
@@ -334,9 +334,17 @@
335335 'dependencies' => array( 'jquery.checkboxShiftClick', 'jquery.client', 'jquery.placeholder' ),
336336 'debugScripts' => 'resources/mediawiki.util/mediawiki.util.test.js',
337337 ),
 338+ 'mediawiki.action.history' => array(
 339+ 'scripts' => 'resources/mediawiki.action/mediawiki.action.history.js',
 340+ 'dependencies' => 'mediawiki.legacy.history',
 341+ ),
338342 'mediawiki.action.view.rightClickEdit' => array(
339343 'scripts' => 'resources/mediawiki.action/mediawiki.action.view.rightClickEdit.js',
340344 ),
 345+ 'mediawiki.action.watch' => array(
 346+ 'scripts' => 'resources/mediawiki.action/mediawiki.action.watch.js',
 347+ 'dependencies' => 'mediawiki.legacy.wikibits',
 348+ ),
341349 'mediawiki.special.preferences' => array(
342350 'scripts' => 'resources/mediawiki.special/mediawiki.special.preferences.js',
343351 'styles' => 'resources/mediawiki.special/mediawiki.special.preferences.css',
@@ -344,10 +352,6 @@
345353 'mediawiki.special.search' => array(
346354 'scripts' => 'resources/mediawiki.special/mediawiki.special.search.js',
347355 ),
348 - 'mediawiki.action.history' => array(
349 - 'scripts' => 'resources/mediawiki.action/mediawiki.action.history.js',
350 - 'dependencies' => 'mediawiki.legacy.history',
351 - ),
352356 'mediawiki.language' => array(
353357 'scripts' => 'resources/mediawiki.language/mediawiki.language.js',
354358 'languageScripts' => array(
@@ -409,10 +413,6 @@
410414 ),
411415 'dependencies' => 'mediawiki.legacy.wikibits',
412416 ),
413 - 'mediawiki.legacy.ajaxwatch' => array(
414 - 'scripts' => 'skins/common/ajaxwatch.js',
415 - 'dependencies' => 'mediawiki.legacy.wikibits',
416 - ),
417417 'mediawiki.legacy.block' => array(
418418 'scripts' => 'skins/common/block.js',
419419 'dependencies' => 'mediawiki.legacy.wikibits',

Follow-up revisions

RevisionCommit summaryAuthorDate
r88527Fixing mediawiki.action.watch.ajax...krinkle18:33, 21 May 2011
r107354[mediawiki.action.watch.ajax.js] Rewrite using mw.Api and fixes bug 27146...krinkle01:21, 27 December 2011

Status & tagging log