Index: trunk/extensions/Reviews/resources/reviews.pager.js |
— | — | @@ -0,0 +1,97 @@ |
| 2 | +/** |
| 3 | + * JavasSript for the Reviews MediaWiki extension. |
| 4 | + * @see https://www.mediawiki.org/wiki/Extension:Reviews |
| 5 | + * |
| 6 | + * @licence GNU GPL v3 or later |
| 7 | + * @author Jeroen De Dauw <jeroendedauw at gmail dot com> |
| 8 | + */ |
| 9 | + |
| 10 | +(function( $, mw ) { |
| 11 | + |
| 12 | + var _this = this; |
| 13 | + |
| 14 | + this.updateStateLinks = function( $container, id, state ) { |
| 15 | + $container.text( '' ); |
| 16 | + |
| 17 | + $container.attr( { |
| 18 | + 'data-review-state': state |
| 19 | + } ); |
| 20 | + |
| 21 | + var linksForState = { |
| 22 | + 'new': ['flagged', 'reviewed'], |
| 23 | + 'flagged': ['new', 'reviewed'], |
| 24 | + 'reviewed': ['flagged'] |
| 25 | + }; |
| 26 | + |
| 27 | + for ( i in linksForState[state] ) { |
| 28 | + if ( linksForState[state].hasOwnProperty( i ) ) { |
| 29 | + var targetState = linksForState[state][i]; |
| 30 | + var isFirst = $container.text() === ''; |
| 31 | + |
| 32 | + if ( isFirst ) { |
| 33 | + $container.append( mw.msg( 'reviews-state-' + state ), ' (' ); |
| 34 | + } |
| 35 | + else { |
| 36 | + $container.append( ' | ' ); |
| 37 | + } |
| 38 | + |
| 39 | + $container.append( $( '<a>' ).attr( { |
| 40 | + 'class': 'review-flag-link', |
| 41 | + 'data-target': targetState |
| 42 | + } ).text( mw.msg( 'reviews-pager-change-' + targetState ) ).click( _this.onLinkClick ) ); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + $container.append( ')' ); |
| 47 | + }; |
| 48 | + |
| 49 | + this.onLinkClick = function() { |
| 50 | + var $this = $( this ); |
| 51 | + var $container = $this.closest( 'div' ); |
| 52 | + var state = $container.attr( 'data-review-state' ); |
| 53 | + var targetState = $this.attr( 'data-target' ); |
| 54 | + var id = $container.attr( 'data-review-id' ); |
| 55 | + |
| 56 | + if ( confirm( mw.msg( 'reviews-pager-confirm-' + targetState ) ) ) { |
| 57 | + var requestArgs = { |
| 58 | + 'action': 'flagreviews', |
| 59 | + 'format': 'json', |
| 60 | + 'token': mw.user.tokens.get( 'editToken' ), |
| 61 | + 'state': targetState, |
| 62 | + 'ids': id |
| 63 | + }; |
| 64 | + |
| 65 | + $container.text( mw.msg( 'reviews-pager-updating' ) ); |
| 66 | + |
| 67 | + $.post( |
| 68 | + wgScriptPath + '/api.php', |
| 69 | + requestArgs, |
| 70 | + function( data ) { |
| 71 | + if ( data.hasOwnProperty( 'success' ) && data.success ) { |
| 72 | + _this.updateStateLinks( $container, id, targetState ); |
| 73 | + } |
| 74 | + else { |
| 75 | + // TODO |
| 76 | + alert( 'Could not change the state of the review' ); |
| 77 | + |
| 78 | + _this.updateStateLinks( $container, id, state ); |
| 79 | + } |
| 80 | + } |
| 81 | + ); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + $( document ).ready( function() { |
| 86 | + |
| 87 | + $.each( $( '.reviews-state-controls' ), function( i, element ) { |
| 88 | + $e = $( element ); |
| 89 | + _this.updateStateLinks( |
| 90 | + $e, |
| 91 | + $e.attr( 'data-review-id' ), |
| 92 | + $e.attr( 'data-review-state' ) |
| 93 | + ); |
| 94 | + } ); |
| 95 | + |
| 96 | + } ); |
| 97 | + |
| 98 | +})( window.jQuery, window.mediaWiki ); |