r72118 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72117‎ | r72118 | r72119 >
Date:16:51, 1 September 2010
Author:adam
Status:deferred
Tags:
Comment:
ArticleAssessment - Lots of javascript improvements. Finished porting the stars plugin..all api calls are now working.
Modified paths:
  • /trunk/extensions/ArticleAssessmentPilot/css/ArticleAssessment.css (modified) (history)
  • /trunk/extensions/ArticleAssessmentPilot/js/ArticleAssessment.js (modified) (history)
  • /trunk/extensions/ArticleAssessmentPilot/js/jquery.stars.js (modified) (history)
  • /trunk/extensions/ArticleAssessmentPilot/test.html (deleted) (history)

Diff [purge]

Index: trunk/extensions/ArticleAssessmentPilot/test.html
@@ -1,33 +0,0 @@
2 -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4 -
5 -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6 -<head>
7 - <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
8 - <script type="text/javascript" charset="utf-8">
9 - var wgScriptPath = "";
10 - </script>
11 - <script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript" charset="utf-8"></script>
12 - <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js" type="text/javascript" charset="utf-8"></script>
13 - <script src="js/jquery.stars.js" type="text/javascript" charset="utf-8"></script>
14 - <script src="js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>
15 - <script src="js/jquery.tipsy.js" type="text/javascript" charset="utf-8"></script>
16 -
17 - <script src="js/ArticleAssessment.js" type="text/javascript" charset="utf-8"></script>
18 - <style type="text/css" media="screen">
19 - body,html{ background: #fff; }
20 - </style>
21 - <title>ArticleAssessment JS Test</title>
22 - <link rel="stylesheet" href="css/ArticleAssessment.css" type="text/css" media="screen" title="CSS" charset="utf-8" />
23 -</head>
24 -
25 -<body>
26 -<div class="article-assessment-wrapper">
27 - <div id="catlinks">
28 -
29 - </div>
30 -</div>
31 -
32 -
33 -</body>
34 -</html>
Index: trunk/extensions/ArticleAssessmentPilot/css/ArticleAssessment.css
@@ -28,8 +28,9 @@
2929 .article-assessment-wrapper .ui-stars-star a {
3030 background-position: 0 0px;
3131 }
 32+
3233 .article-assessment-wrapper .ui-stars-star-on a {
33 - background-position: 0 -42px;
 34+ background-position: 0 -21px;
3435 }
3536 .article-assessment-wrapper .ui-stars-star-rated a {
3637 background-position: 0 -42px;
Index: trunk/extensions/ArticleAssessmentPilot/js/ArticleAssessment.js
@@ -84,7 +84,7 @@
8585 if ( ! wgUserName ) {
8686 config.userID = cookieSettings.uid;
8787 }
88 - // setup our markup
 88+ // setup our markup using the template varibales in settings
8989 var $output = $( settings.structureHTML
9090 .replace( /\{INSTRUCTIONS\}/g, mw.usability.getMsg('articleassessment-pleaserate') )
9191 .replace( /\{FEEDBACK\}/g, mw.usability.getMsg('articleassessment-featurefeedback')
@@ -109,8 +109,9 @@
110110 .replace( /\{COUNT\}/g, mw.usability.getMsg( 'articleassessment-noratings', [0, 0] ) ) )
111111 );
112112 }
 113+ // store our settings and configuration for later
113114 $output.find( '#article-assessment' ).data( 'articleAssessment-context', { 'settings': settings, 'config': config } );
114 - // hook up the ratings show/hide
 115+ // bind the ratings show/hide handlers
115116 $output
116117 .find( '.article-assessment-show-ratings a' )
117118 .click( function() {
@@ -159,9 +160,13 @@
160161 .stars( {
161162 inputType: 'select',
162163 callback: function( value, link ) {
163 - // remove any stale classes
 164+ // remove any stale or rated classes
164165 value.$stars.each( function() {
165 - $( this ).removeClass( 'ui-stars-star-stale' );
 166+ $( this )
 167+ .removeClass( 'ui-stars-star-stale' )
 168+ .removeClass( 'ui-stars-star-rated' );
 169+ // enable our submit button if it's still disabled
 170+ $( '#article-assessment input:disabled' ).removeAttr( "disabled" );
166171 } );
167172 }
168173 } );
@@ -174,19 +179,24 @@
175180 .tipsy( { gravity : 'se', opacity: '0.9', } ) );
176181 } );
177182 // bind submit event to the form
178 - $( '#article-assessment' ).submit( function() { $.ArticleAssessment.fn.submitRating(); return false; } );
 183+ $( '#article-assessment' )
 184+ .submit( function() { $.ArticleAssessment.fn.submitRating(); return false; } );
179185 // prevent the submit button for being active until all ratings are filled out
180 -
 186+ $( '#article-assessment input[type=submit]' )
 187+ .attr( 'disabled', 'disabled' );
181188 },
 189+ // Request the ratings data for the current article
182190 'getRatingData': function() {
 191+ var config = $( '#article-assessment' ).data( 'articleAssessment-context' ).config;
183192 var request = $.ajax( {
184193 url: wgScriptPath + '/api.php',
185194 data: {
186195 'action': 'query',
187196 'list': 'articleassessment',
188 - 'aarevid': wgCurRevisionId,
189 - 'aapageid': wgArticleId,
 197+ 'aarevid': config.revID,
 198+ 'aapageid': config.pageID,
190199 'aauserrating': 1,
 200+ 'aauserid': config.userID,
191201 'format': 'json'
192202 },
193203 dataType: 'json',
@@ -205,31 +215,32 @@
206216 for( rating in data.query.articleassessment[0].ratings) {
207217 var rating = data.query.articleassessment[0].ratings[rating],
208218 $rating = $( '#' + rating.ratingdesc ),
209 - label = mw.usability.getMsg( 'articleassessment-noratings', [rating.total, rating.count] );
 219+ count = rating.count,
 220+ total = rating.total / count,
 221+ label = mw.usability.getMsg( 'articleassessment-noratings', [total, count] );
210222 $rating
211223 .find( '.article-assessment-rating-field-value' )
212 - .text( rating.total )
 224+ .text( total )
213225 .end()
214226 .find( '.article-assessment-rating-count' )
215227 .text( label );
216228 if( rating.userrating ) {
217229 var $rateControl = $( '#' + rating.ratingdesc.replace( 'rating', 'rate' ) + ' .rating-field' );
218 - $rateControl.children( '.ui-stars-star' ).eq( rating.userrating - 1 ).click();
 230+ $rateControl.stars( 'select', rating.userrating );
219231 }
220232 }
221233 // if the rating is stale, add the stale class
222 - if( true /* replace with conditional based on returned results of past user ratings */ ) {
 234+ if( data.query.articleassessment ) {
223235 // add the stale star class to each on star
224236 $( '.ui-stars-star-on' )
225237 .addClass( 'ui-stars-star-stale' );
226238 // add the stale message
227 - $( '.article-assessment-submit' )
228 - .append( settings.staleMSG.replace( /\{MSG\}/g, mw.usability.getMsg( 'articleassessment-stalemessage-revisioncount' )
229 - .replace( /'''([^']*)'''/g, '<strong>$1</strong >' )
230 - .replace( /''([^']*)''/, '<em>$1</em>' ) ) );
 239+ var msg = mw.usability.getMsg( 'articleassessment-stalemessage-revisioncount' )
 240+ .replace( /'''([^']*)'''/g, '<strong>$1</strong>' )
 241+ .replace( /''([^']*)''/g, '<em>$1</em>' );
 242+ $.ArticleAssessment.fn.flashNotice( msg, { 'class': 'article-assessment-stale-msg' } );
231243 }
232244 }
233 -
234245 // initialize the ratings
235246 $( '.article-assessment-rating-field-value' ).each( function() {
236247 $( this )
@@ -241,6 +252,11 @@
242253 'submitRating': function() {
243254 var config = $( '#article-assessment' ).data( 'articleAssessment-context' ).config;
244255 // clear out the stale message
 256+ $.ArticleAssessment.fn.flashNotice( );
 257+
 258+ //lock the star inputs
 259+
 260+ // get our results for submitting
245261 var results = {};
246262 $( '.rating-field input' ).each( function() {
247263 // expects the hidden inputs to have names like 'rating[field-name]' which we use to
@@ -263,12 +279,38 @@
264280 },
265281 dataType: 'json',
266282 success: function( data ) {
267 - console.log(data);
 283+ // set the stars to rated status
 284+ $j('.ui-stars-star-on').addClass('ui-stars-star-rated');
 285+ // unlock the stars
 286+
 287+ // update the results
 288+
 289+ // show the results
 290+ $( '#article-assessment .article-assessment-show-ratings a' ).click();
268291 }
269292 } );
270293 },
 294+ // places a message on the interface
271295 'flashNotice': function( text, options ) {
272 -
 296+ if ( arguments.length == 0 ) {
 297+ // clear existing messages, but don't add a new one
 298+ $( '#article-assessment .article-assessment-flash' ).remove();
 299+ } else {
 300+ // clear and add a new message
 301+ $( '#article-assessment .article-assessment-flash' ).remove();
 302+ var className = options['class'];
 303+ // create our new message
 304+ $msg = $( '<span />' )
 305+ .addClass( 'article-assessment-flash' )
 306+ .html( text );
 307+ // if the class option was passed, add it
 308+ if( options['class'] ) {
 309+ $msg.addClass( options['class'] );
 310+ }
 311+ // place our new message on the page
 312+ $( '#article-assessment .article-assessment-submit' )
 313+ .append( $msg );
 314+ }
273315 }
274316 }
275317 };
Index: trunk/extensions/ArticleAssessmentPilot/js/jquery.stars.js
@@ -1,12 +1,11 @@
22 /*!
33 * jQuery Stars v1
4 - * adapted by Adam Miller (acm6603@gmail.com) 2010
 4+ * adapted by Adam Miller (acm6603@gmail.com)
55 *
66 * Adapted from jQuery UI Stars v3.0.1
77 * Marek "Orkan" Zajac (orkans@gmail.com)
88 * http://plugins.jquery.com/project/Star_Rating_widget
99 *
10 - * TODO: Non of the API functions are working right now. Need to rewrite more of this so they will
1110 */
1211 (function($) {
1312 $.stars = {
@@ -34,13 +33,6 @@
3534 cancelHoverClass: 'ui-stars-cancel-hover',
3635 cancelDisabledClass: 'ui-stars-cancel-disabled'
3736 },
38 - init: function( element, options ) {
39 - var context = $.extend( {}, {
40 - element: $( element ),
41 - options: $.extend( {}, this.defaults, options )
42 - }, this );
43 - this.create.call( context );
44 - },
4537 create: function( ) {
4638 var self = this, o = this.options, starId = 0;
4739 this.element.data('former.stars', this.element.html());
@@ -270,36 +262,43 @@
271263 }
272264 }
273265 $.fn.stars = function ( ) {
274 - var args = arguments;
 266+ // convert the arguments to an array
 267+ var args = Array.prototype.slice.call(arguments);
275268
276 - // var context = $( this ).data( 'stars-context' );
277 - // if ( typeof context == 'undefined' || context == null ) {
278 - // context = {
279 - // options: {
280 - // }
281 - // };
282 - // }
283 - // // // Handle various calling styles
284 - // if ( args.length > 0 ) {
285 - // if ( typeof args[0] == 'object' ) {
286 - // // Apply set of properties
287 - // // for ( var key in args[0] ) {
288 - // // $.suggestions.configure( context, key, args[0][key] );
289 - // // }
290 - // return $( this ).each( function() {
291 - // $.stars.init( this, args[0] );
292 - // } );
293 - // } else if ( typeof args[0] == 'string' ) {
294 - // // call the function
295 - // var funcName = args[0];
296 - // delete args[0];
297 - // return $( this ).each( function() {
298 - // $.stars[funcName]( args );
299 - // } );
300 - // }
301 - // }
302 - return $( this ).each( function() {
303 - $.stars.init( this, args[0] );
 269+ // default value to return -- overwritten by api calls
 270+ var out = $( this );
 271+
 272+ $( this ).each( function() {
 273+ // get the context if it's already been initialized
 274+ var context = $( this ).data( 'stars-context' );
 275+ if ( typeof context == 'undefined' || context == null ) {
 276+ // setup the context if it hasn't been yet
 277+ context = $.extend( {}, {
 278+ element: $( this ),
 279+ options: $.stars.defaults
 280+ }, $.stars );
 281+ }
 282+ // Handle various calling styles
 283+ if ( args.length > 0 ) {
 284+ if ( typeof args[0] == 'object' ) {
 285+ // merge the passed options into defaults
 286+ context.options = $.extend( {}, context.options, args[0] );
 287+ // initialize
 288+ $.stars.create.call( context );
 289+ } else if ( typeof args[0] == 'string' ) {
 290+ // API call
 291+ var funcName = args.shift();
 292+ // call the function, and if it returns something, store the output in our return var
 293+ out = $.stars[funcName].call( context, args[0] ) || out;
 294+ }
 295+ } else {
 296+ // initialize with the defaults
 297+ $.stars.create.call( context );
 298+ }
 299+ // save our context, bay-bee
 300+ $( this ).data( 'stars-context', context );
304301 } );
 302+
 303+ return out;
305304 };
306305 } )( jQuery );

Status & tagging log