r100793 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100792‎ | r100793 | r100794 >
Date:06:02, 26 October 2011
Author:werdna
Status:resolved (Comments)
Tags:
Comment:
FeedbackDashboard: Rearrange to allow showing/hiding by AJAX. Also make sure that we test everything correctly in AJAX responses
Modified paths:
  • /trunk/extensions/MoodBar/ApiFeedbackDashboard.php (added) (history)
  • /trunk/extensions/MoodBar/MoodBar.php (modified) (history)
  • /trunk/extensions/MoodBar/SpecialFeedbackDashboard.php (modified) (history)
  • /trunk/extensions/MoodBar/modules/ext.moodBar.dashboard/ext.moodBar.dashboard.css (modified) (history)
  • /trunk/extensions/MoodBar/modules/ext.moodBar.dashboard/ext.moodBar.dashboard.js (modified) (history)

Diff [purge]

Index: trunk/extensions/MoodBar/SpecialFeedbackDashboard.php
@@ -278,7 +278,7 @@
279279 $permalink = $GLOBALS['wgUser']->getSkin()
280280 ->link( $permalinkTitle, $permalinkText,
281281 array(), array('hide-feedback' => '1') );
282 - return Xml::tags( 'div', array( 'class' => 'fbd-item-permalink' ), "($permalink)" );
 282+ return Xml::tags( 'div', array( 'class' => 'fbd-item-hide' ), "($permalink)" );
283283 }
284284
285285
Index: trunk/extensions/MoodBar/modules/ext.moodBar.dashboard/ext.moodBar.dashboard.css
@@ -224,7 +224,8 @@
225225 }
226226
227227 .fbd-item-permalink,
228 -.fbd-item-show {
 228+.fbd-item-show,
 229+.fbd-item-hide {
229230 float: right;
230231 font-size: 0.8em;
231232 }
Index: trunk/extensions/MoodBar/modules/ext.moodBar.dashboard/ext.moodBar.dashboard.js
@@ -194,27 +194,31 @@
195195 }
196196
197197 /**
198 - * Show a hidden comment
 198+ * Reload a comment, showing hidden comments if necessary
 199+ * @param $item jQuery item containing the .fbd-item
 200+ * @param show Set to true to show the comment despite its hidden status
199201 */
200 - function showHiddenComment(e) {
201 - var $item = $(this).closest('.fbd-item');
 202+ function reloadItem( $item, show ) {
202203 var cont = $item.data('mbccontinue');
203204
204205 var request = {
205206 'action' : 'query',
206207 'list' : 'moodbarcomments',
207208 'format' : 'json',
208 - 'mbcprop' : 'formatted|hidden',
 209+ 'mbcprop' : 'formatted',
209210 'mbclimit' : 1,
210211 'mbccontinue' : cont
211212 };
212213
213 - var $spinner = $('<span class="mw-ajax-loader">&nbsp;</span>');
214 - $item.find('.fbd-item-show').empty().append( $spinner );
 214+ if ( show ) {
 215+ request.mbcprop = 'formatted|hidden';
 216+ }
215217
216218 $.post( mw.util.wikiScript('api'), request,
217219 function( data ) {
218 - if ( data && data.query && data.query.moodbarcomments ) {
 220+ if ( data && data.query && data.query.moodbarcomments &&
 221+ data.query.moodbarcomments.length > 0
 222+ ) {
219223 var $content = $j(data.query.moodbarcomments[0].formatted);
220224 $item.replaceWith($content);
221225 } else {
@@ -222,14 +226,84 @@
223227 $item.find('.fbd-item-show').remove();
224228 }
225229 }, 'json' );
 230+ }
 231+
 232+ /**
 233+ * Show a hidden comment
 234+ */
 235+ function showHiddenItem(e) {
 236+ var $item = $(this).closest('.fbd-item');
226237
 238+ var $spinner = $('<span class="mw-ajax-loader">&nbsp;</span>');
 239+ $item.find('.fbd-item-show').empty().append( $spinner );
 240+
 241+ reloadItem( $item, true );
 242+
227243 e.preventDefault();
228244 }
229245
 246+ /**
 247+ * Execute an action on an item
 248+ */
 249+ function doAction( params, $item ) {
 250+ var item_id = $item.data('mbccontinue').split('|')[1];
 251+
 252+ $.post( mw.util.wikiScript('api'),
 253+ $.extend( {
 254+ 'action' : 'feedbackdashboard',
 255+ 'token' : mw.user.tokens.get('editToken'),
 256+ 'item' : item_id,
 257+ 'format' : 'json'
 258+ }, params ),
 259+ function(response) {
 260+ if ( response && response.feedbackdashboard ) {
 261+ if ( response.feedbackdashboard.result == 'success' ) {
 262+ reloadItem( $item );
 263+ } else {
 264+ alert( response.feedbackdashboard.error );
 265+ }
 266+ } else {
 267+ alert('Unknown error');
 268+ }
 269+ } );
 270+ }
 271+
 272+ /**
 273+ * Restore a hidden item to full visibility (event handler)
 274+ */
 275+ function restoreItem(e) {
 276+ var $item = $(this).closest('.fbd-item');
 277+
 278+ var $spinner = $('<span class="mw-ajax-loader">&nbsp;</span>');
 279+ $item.find('.fbd-item-restore').empty().append( $spinner );
 280+
 281+ doAction( { 'mbaction' : 'restore' }, $item );
 282+
 283+ e.preventDefault();
 284+ }
 285+
 286+ /**
 287+ * Hide a item from view (event handler)
 288+ */
 289+ function hideItem(e) {
 290+ var $item = $(this).closest('.fbd-item');
 291+
 292+ var $spinner = $('<span class="mw-ajax-loader">&nbsp;</span>');
 293+ $item.find('.fbd-item-hide').empty().append( $spinner );
 294+
 295+ doAction( { 'mbaction' : 'hide' }, $item );
 296+
 297+ e.preventDefault();
 298+ }
 299+
230300 // On-load stuff
231301
232 - $('.fbd-item-show a').live( 'click', showHiddenComment );
 302+ $('.fbd-item-show a').live( 'click', showHiddenItem );
233303
 304+ $('.fbd-item-hide a').live( 'click', hideItem );
 305+
 306+ $('.fbd-item-restore').live( 'click', restoreItem );
 307+
234308 $( '#fbd-filters' ).children( 'form' ).submit( function( e ) {
235309 e.preventDefault();
236310 saveFormState();
Index: trunk/extensions/MoodBar/MoodBar.php
@@ -22,6 +22,8 @@
2323 $wgAPIModules['moodbar'] = 'ApiMoodBar';
2424 $wgAutoloadClasses['ApiQueryMoodBarComments'] = dirname( __FILE__ ). '/ApiQueryMoodBarComments.php';
2525 $wgAPIListModules['moodbarcomments'] = 'ApiQueryMoodBarComments';
 26+$wgAutoloadClasses['ApiFeedbackDashboard'] = dirname(__FILE__).'/ApiFeedbackDashboard.php';
 27+$wgAPIModules['feedbackdashboard'] = 'ApiFeedbackDashboard';
2628
2729 // Hooks
2830 $wgAutoloadClasses['MoodBarHooks'] = dirname(__FILE__).'/MoodBar.hooks.php';
Index: trunk/extensions/MoodBar/ApiFeedbackDashboard.php
@@ -0,0 +1,90 @@
 2+<?php
 3+
 4+class ApiFeedbackDashboard extends ApiBase {
 5+ public function execute() {
 6+ global $wgUser;
 7+ if ( ! $wgUser->isAllowed('moodbar-admin') ) {
 8+ $this->dieUsage( "You don't have permission to do that", 'permission-denied' );
 9+ }
 10+
 11+ $params = $this->extractRequestParams();
 12+ $form = null;
 13+
 14+ if ( $params['mbaction'] == 'hide' ) {
 15+ $form = new MBHideForm( $params['item'] );
 16+ } elseif ( $params['mbaction'] == 'restore' ) {
 17+ $form = new MBRestoreForm( $params['item'] );
 18+ } else {
 19+ throw new MWException( "Action {$params['action']} not implemented" );
 20+ }
 21+
 22+ $data = array(
 23+ 'reason' => $params['reason'],
 24+ 'item' => $params['item'],
 25+ );
 26+
 27+ $result = null;
 28+ $output = $form->submit( $data );
 29+ if ( $output === true ) {
 30+ $result = array( 'result' => 'success' );
 31+ } else {
 32+ $result = array( 'result' => 'error', 'error' => $output );
 33+ }
 34+
 35+ $this->getResult()->addValue( null, $this->getModuleName(), $result );
 36+ }
 37+
 38+ public function needsToken() {
 39+ return true;
 40+ }
 41+
 42+ public function getTokenSalt() {
 43+ return '';
 44+ }
 45+
 46+ public function getAllowedParams() {
 47+ return array(
 48+ 'mbaction' => array(
 49+ ApiBase::PARAM_REQUIRED => true,
 50+ ApiBase::PARAM_TYPE => array(
 51+ 'hide',
 52+ 'restore',
 53+ ),
 54+ ),
 55+
 56+ 'item' => array(
 57+ ApiBase::PARAM_REQUIRED => true,
 58+ ApiBase::PARAM_TYPE => 'integer',
 59+ ),
 60+
 61+ 'reason' => null,
 62+ 'token' => array(
 63+ ApiBase::PARAM_REQUIRED => true,
 64+ ),
 65+ );
 66+ }
 67+
 68+ public function mustBePosted() {
 69+ return true;
 70+ }
 71+
 72+ public function isWriteMode() {
 73+ return true;
 74+ }
 75+
 76+ public function getVersion() {
 77+ return __CLASS__ . ': $Id$';
 78+ }
 79+
 80+ public function getParamDescription() {
 81+ return array(
 82+ 'mbaction' => 'The action to take',
 83+ 'item' => 'The feedback item to apply it to',
 84+ 'reason' => 'The reason to specify in the log',
 85+ );
 86+ }
 87+
 88+ public function getDescription() {
 89+ return 'Allows administrators to manage submissions to the feedback dashboard';
 90+ }
 91+}
Property changes on: trunk/extensions/MoodBar/ApiFeedbackDashboard.php
___________________________________________________________________
Added: svn:keywords
192 + Id

Follow-up revisions

RevisionCommit summaryAuthorDate
r100802rename variable to avoid confusionwerdna12:25, 26 October 2011
r100803Improve error handlingwerdna12:45, 26 October 2011

Comments

#Comment by Catrope (talk | contribs)   12:28, 26 October 2011
+						alert( response.feedbackdashboard.error );

Per our IRC chat, error handling should use something nicer than alert(). OK otherwise.

Status & tagging log