r110100 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110099‎ | r110100 | r110101 >
Date:01:14, 27 January 2012
Author:rsterbin
Status:ok
Tags:aft 
Comment:
Mark as helpful is now sticky ($.articleFeedbackv5special):
- New method getActivity()
- Updated setBinds() to check for a previously set value and send the
appropriate requests, if any
- Updated flagFeedback():
- Accepts a third parameter for direction (1/-1)
- Sends 'increase' or 'decrease' to the api
- On success, adds or removes the helpful class on the button
- Updated loadFeedback() to loop through the divs coming in and, if there's
activity, set the helpful class
* NB: Each set of feedback is now in a div; this shouldn't affect much of
anything.
Modified paths:
  • /trunk/extensions/ArticleFeedbackv5/modules/jquery.articleFeedbackv5/jquery.articleFeedbackv5.special.js (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleFeedbackv5/modules/jquery.articleFeedbackv5/jquery.articleFeedbackv5.special.js
@@ -156,16 +156,28 @@
157157 // Helpful and unhelpful have their own special logic, so break those out.
158158 $.each( ['helpful', 'unhelpful' ], function ( index, value ) {
159159 $( '.articleFeedbackv5-' + value + '-link' ).live( 'click', function( e ) {
160 - id = $.articleFeedbackv5special.stripID( this, 'articleFeedbackv5-' + value + '-link-' );
161 - $.articleFeedbackv5special.flagFeedback( id, value );
162 - // add highlighted class
163 - $( this ).addClass( 'helpful-active' );
 160+ e.preventDefault();
 161+ var $l = $( e.target );
 162+ var id = $l.parents( '.articleFeedbackv5-feedback' ).attr( 'rel' );
 163+ var activity = $.articleFeedbackv5special.getActivity( id );
 164+ if ( activity[value] ) {
 165+ return false;
 166+ }
 167+ if ( 'helpful' == value && activity.unhelpful ) {
 168+ $.articleFeedbackv5special.flagFeedback( id, 'unhelpful', -1 );
 169+ $.articleFeedbackv5special.flagFeedback( id, 'helpful', 1 );
 170+ } else if ( 'unhelpful' == value && activity.helpful ) {
 171+ $.articleFeedbackv5special.flagFeedback( id, 'helpful', -1 );
 172+ $.articleFeedbackv5special.flagFeedback( id, 'unhelpful', 1 );
 173+ } else {
 174+ $.articleFeedbackv5special.flagFeedback( id, value, 1 );
 175+ }
164176 } )
165177 } );
166178
167179 $.each( ['unhide', 'undelete', 'oversight', 'hide', 'abuse', 'delete', 'unoversight'], function ( index, value ) {
168180 $( '.articleFeedbackv5-' + value + '-link' ).live( 'click', function( e ) {
169 - $.articleFeedbackv5special.flagFeedback( $.articleFeedbackv5special.stripID( this, 'articleFeedbackv5-' + value + '-link-' ), value );
 181+ $.articleFeedbackv5special.flagFeedback( $.articleFeedbackv5special.stripID( this, 'articleFeedbackv5-' + value + '-link-' ), value, 1 );
170182 } )
171183 } );
172184 }
@@ -309,8 +321,24 @@
310322 };
311323
312324 // }}}
 325+ // {{{ getActivity
313326
 327+ /**
 328+ * Utility method: Gets the activity for a feedback ID
 329+ *
 330+ * @param fid int the feedback ID
 331+ * @return object the activity object
 332+ */
 333+ $.articleFeedbackv5special.getActivity = function ( fid ) {
 334+ if ( !( fid in $.articleFeedbackv5special.activity ) ) {
 335+ $.articleFeedbackv5special.activity[fid] = { helpful: false, unhelpful: false, abuse: false, hide: false, delete: false };
 336+ }
 337+ return $.articleFeedbackv5special.activity[fid];
 338+ };
 339+
314340 // }}}
 341+
 342+ // }}}
315343 // {{{ Process methods
316344
317345 // {{{ flagFeedback
@@ -320,8 +348,9 @@
321349 *
322350 * @param id int the feedback id
323351 * @param type string the type of mark (valid values: hide, abuse, delete, helpful, unhelpful)
 352+ * @param dir int the direction of the mark (-1 = tick down; 1 = tick up)
324353 */
325 - $.articleFeedbackv5special.flagFeedback = function ( id, type ) {
 354+ $.articleFeedbackv5special.flagFeedback = function ( id, type, dir ) {
326355 $.ajax( {
327356 'url' : $.articleFeedbackv5special.apiUrl,
328357 'type' : 'POST',
@@ -330,6 +359,7 @@
331360 'pageid' : $.articleFeedbackv5special.page,
332361 'feedbackid': id,
333362 'flagtype' : type,
 363+ 'direction' : dir > 0 ? 'increase' : 'decrease',
334364 'format' : 'json',
335365 'action' : 'articlefeedbackv5-flag-feedback'
336366 },
@@ -342,11 +372,19 @@
343373 if ( 'helpful' in data['articlefeedbackv5-flag-feedback'] ) {
344374 $( '#articleFeedbackv5-helpful-votes-' + id ).text( data['articlefeedbackv5-flag-feedback'].helpful );
345375 }
 376+ if ( 'helpful' == type || 'unhelpful' == type ) {
 377+ var $l = $( '#articleFeedbackv5-' + type + '-link-' + id );
 378+ if ( dir > 0 ) {
 379+ $l.addClass( 'helpful-active' );
 380+ } else {
 381+ $l.removeClass( 'helpful-active' );
 382+ }
 383+ }
346384 // Save activity
347385 if ( !( id in $.articleFeedbackv5special.activity ) ) {
348386 $.articleFeedbackv5special.activity[id] = { helpful: false, unhelpful: false, abuse: false, hide: false, delete: false };
349387 }
350 - $.articleFeedbackv5special.activity[id][type] = true;
 388+ $.articleFeedbackv5special.activity[id][type] = dir > 0 ? true : false;
351389 $.articleFeedbackv5special.storeActivity();
352390 } else if ( data['articlefeedbackv5-flag-feedback'].result == 'Error' ) {
353391 msg = data['articlefeedbackv5-flag-feedback'].reason;
@@ -395,10 +433,22 @@
396434 'success': function ( data ) {
397435 if ( 'articlefeedbackv5-view-feedback' in data ) {
398436 if ( resetContents ) {
399 - $( '#articleFeedbackv5-show-feedback' ).html( data['articlefeedbackv5-view-feedback'].feedback);
400 - } else {
401 - $( '#articleFeedbackv5-show-feedback' ).append( data['articlefeedbackv5-view-feedback'].feedback);
 437+ $( '#articleFeedbackv5-show-feedback' ).empty();
402438 }
 439+ var $newList = $( '<div></div>' ).html( data['articlefeedbackv5-view-feedback'].feedback );
 440+ $newList.find( '.articleFeedbackv5-feedback' ).each( function () {
 441+ var id = $( this ).attr( 'rel' );
 442+ if ( id in $.articleFeedbackv5special.activity ) {
 443+ var activity = $.articleFeedbackv5special.getActivity( id );
 444+ if ( activity.helpful ) {
 445+ $( this ).find( '#articleFeedbackv5-helpful-link-' + id ).addClass( 'helpful-active' );
 446+ }
 447+ if ( activity.unhelpful ) {
 448+ $( this ).find( '#articleFeedbackv5-unhelpful-link-' + id ).addClass( 'helpful-active' );
 449+ }
 450+ }
 451+ } );
 452+ $( '#articleFeedbackv5-show-feedback' ).append( $newList );
403453 $( '#articleFeedbackv5-feedback-count-total' ).text( data['articlefeedbackv5-view-feedback'].count );
404454 $.articleFeedbackv5special.listControls.continue = data['articlefeedbackv5-view-feedback'].continue;
405455 // set effects on toolboxes

Status & tagging log