r86783 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86782‎ | r86783 | r86784 >
Date:17:24, 23 April 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
some work on the js, still not functioning properly though
Modified paths:
  • /trunk/extensions/Ratings/Ratings.class.php (modified) (history)
  • /trunk/extensions/Ratings/Ratings.php (modified) (history)
  • /trunk/extensions/Ratings/allrating/RatingsAllRating.php (modified) (history)
  • /trunk/extensions/Ratings/allrating/ext.ratings.allrating.js (modified) (history)
  • /trunk/extensions/Ratings/allrating/js/jquery.allRating.js (modified) (history)
  • /trunk/extensions/Ratings/js (added) (history)
  • /trunk/extensions/Ratings/js/ext.ratings.common.js (added) (history)
  • /trunk/extensions/Ratings/starrating/RatingsStars.php (modified) (history)
  • /trunk/extensions/Ratings/starrating/ext.ratings.stars.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Ratings/Ratings.php
@@ -91,6 +91,13 @@
9292 'remoteBasePath' => $egRatingsScriptPath
9393 );
9494
 95+ $wgResourceModules['ext.ratings.common'] = $moduleTemplate + array(
 96+ 'scripts' => array(
 97+ 'js/ext.ratings.common.js'
 98+ ),
 99+ 'messages' => $egRatingsStarsJSMessages
 100+ );
 101+
95102 $wgResourceModules['ext.ratings.stars'] = $moduleTemplate + array(
96103 'styles' => array(
97104 'starrating/star-rating/jquery.rating.css'
@@ -99,8 +106,7 @@
100107 'starrating/star-rating/jquery.rating.js',
101108 'starrating/ext.ratings.stars.js'
102109 ),
103 - 'dependencies' => array(),
104 - 'messages' => $egRatingsStarsJSMessages
 110+ 'dependencies' => array( 'ext.ratings.common' ),
105111 );
106112
107113 $wgResourceModules['ext.ratings.allrating'] = $moduleTemplate + array(
@@ -111,8 +117,7 @@
112118 'allrating/js/jquery.allRating.js',
113119 'allrating/ext.ratings.allrating.js'
114120 ),
115 - 'dependencies' => array(),
116 - 'messages' => $egRatingsStarsJSMessages
 121+ 'dependencies' => array( 'ext.ratings.common' ),
117122 );
118123 }
119124
Index: trunk/extensions/Ratings/Ratings.class.php
@@ -176,4 +176,20 @@
177177 return $tags;
178178 }
179179
 180+ protected static function loadJs( Parser $parser ) {
 181+ static $loadedJs = false;
 182+
 183+ if ( $loadedJs ) {
 184+ return;
 185+ }
 186+
 187+ $loadedJs = true;
 188+ global $egRatingsScriptPath;
 189+
 190+ $parser->getOutput()->addHeadItem(
 191+ Html::linkedScript( $egRatingsScriptPath . '/js/ext.ratings.common.js' ),
 192+ 'ext.ratings.common'
 193+ );
 194+ }
 195+
180196 }
Index: trunk/extensions/Ratings/js/ext.ratings.common.js
@@ -0,0 +1,72 @@
 2+/**
 3+ * Common JavasSript for the Ratings extension.
 4+ * @see http://www.mediawiki.org/wiki/Extension:Ratings
 5+ *
 6+ * @licence GNU GPL v3 or later
 7+ * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
 8+ */
 9+
 10+window.ratings = new ( function( $ ) {
 11+
 12+ /**
 13+ * Obtain the vote values for a set of tags of a single page,
 14+ * and then find and update the corresponding rating stars.
 15+ *
 16+ * @param {string} page
 17+ * @param {Array} tags
 18+ * @param {callback} callback
 19+ */
 20+ this.getRatingsForPage = function( page, tags, callback ) {
 21+ $.getJSON(
 22+ wgScriptPath + '/api.php',
 23+ {
 24+ 'action': 'query',
 25+ 'format': 'json',
 26+ 'list': 'ratings',
 27+ 'qrpage': page,
 28+ 'qrtags': tags.join( '|' )
 29+ },
 30+ function( data ) {
 31+ if ( data.userratings ) {
 32+ callback( page, data.userratings );
 33+ }
 34+ else {
 35+ // TODO
 36+ }
 37+ }
 38+ );
 39+ };
 40+
 41+ /**
 42+ * Submit a rating.
 43+ *
 44+ * @param {string} page
 45+ * @param {string} tag
 46+ * @param {integer} value
 47+ */
 48+ this.submitRating = function( page, tag, value ) {
 49+ $.post(
 50+ wgScriptPath + '/api.php',
 51+ {
 52+ 'action': 'dorating',
 53+ 'format': 'json',
 54+ 'pagename': page,
 55+ 'tag': tag,
 56+ 'value': value
 57+ },
 58+ function( data ) {
 59+ if ( data.error && data.error.info ) {
 60+ alert( data.error.info );
 61+ }
 62+ else if ( data.result.success ) {
 63+ // TODO
 64+ }
 65+ else {
 66+ alert( 'Failed to submit rating' ) // TODO
 67+ }
 68+ },
 69+ 'json'
 70+ );
 71+ };
 72+
 73+} )( jQuery );
Property changes on: trunk/extensions/Ratings/js/ext.ratings.common.js
___________________________________________________________________
Added: svn:eol-style
174 + native
Index: trunk/extensions/Ratings/allrating/RatingsAllRating.php
@@ -166,6 +166,8 @@
167167 'jQuery'
168168 );
169169
 170+ Ratings::loadJs( $this->parser );
 171+
170172 $this->parser->getOutput()->addHeadItem(
171173 Html::linkedScript( $egRatingsScriptPath . '/allrating/js/jquery.allRating.js' )
172174 . Html::linkedStyle( $egRatingsScriptPath . '/allrating/css/allRating.css' ),
Index: trunk/extensions/Ratings/allrating/js/jquery.allRating.js
@@ -71,6 +71,21 @@
7272
7373 /**Main Code**/
7474 $.fn.allRating = function(options) {
 75+ // Handle API methods
 76+ if(typeof arguments[0]=='string'){
 77+ // Perform API methods on individual elements
 78+ if(this.length>1){
 79+ var args = arguments;
 80+ return this.each(function(){
 81+ $.fn.rating.apply($(this), args);
 82+ });
 83+ };
 84+ // Invoke API method handler
 85+ $.fn.allRating[arguments[0]].apply(this, $.makeArray(arguments).slice(1) || []);
 86+ // Quick exit...
 87+ return this;
 88+ };
 89+
7590 var config = $.extend({}, $.rating.defaults, options);
7691
7792 this.each( function() {
@@ -106,6 +121,7 @@
107122 $.fn.allRating.fetchInput = function(item, config, index ) {
108123
109124 var input = {};
 125+ this.config = config;
110126
111127 //fetch input details
112128 input.item = $(item);
@@ -184,6 +200,7 @@
185201 }
186202
187203 $.fn.allRating.addActiveStars = function(input, config, original) {
 204+
188205 if(original) {
189206 $('#' + input.ratingId + ' .'+config.segmentContainerClass).find('a[rel="' + input.selectedValue + '"]').addClass('original');
190207 }
@@ -193,7 +210,15 @@
194211 $(this).removeClass(config.segmentClassNonActiveClass).addClass(config.segmentActiveClass);
195212 });
196213 };
197 -
 214+
 215+ $.extend($.fn.allRating, {
 216+ setValue: function( value ) {
 217+ var inputId = $(this).attr('id');
 218+ var config = $.extend({}, $.rating.defaults, this.options);
 219+ //var input = $.fn.allRating.updateInput('#'+inputId, value, config);
 220+ }
 221+ });
 222+
198223 $.fn.allRating.initEvents = function(input, config) {
199224 $.fn.allRating.initClickEvents(input, config);
200225 $.fn.allRating.initHoverEvents(input, config);
@@ -231,6 +256,7 @@
232257 $.fn.allRating.updateInput = function(inputId, newValue, config){
233258 switch(config.input) {
234259 case 'select' :
 260+ alert(inputId);
235261 $(inputId).val(newValue);
236262 return $(inputId); //return the original input
237263 break;
@@ -246,7 +272,6 @@
247273 }
248274 };
249275
250 -
251276 $.fn.allRating.findTextFromValue = function(input, value, config) {
252277 switch(config.input) {
253278 case 'select':
Index: trunk/extensions/Ratings/allrating/ext.ratings.allrating.js
@@ -17,19 +17,50 @@
1818 }
1919
2020 /**
 21+ * Loop over all rating elements for the page and set their value when available.
 22+ *
 23+ * @param {string} page
 24+ * @param {Array} tagValues
 25+ */
 26+ function initRatingElementsForPage( page, tagValues ) {
 27+ $.each($(".allrating"), function(i,v) {
 28+ var self = $(this);
 29+
 30+ if ( typeof self.attr( 'page' ) != 'undefined' && self.attr( 'page' ) == page ) {
 31+ if ( typeof tagValues[self.attr( 'tag' )] != 'undefined' ) {
 32+ self.allRating( 'setValue', tagValues[self.attr( 'tag' )] );
 33+ }
 34+ }
 35+ });
 36+ }
 37+
 38+ /**
2139 * Self executing function to setup the allrating rating elements on the page.
2240 */
2341 (function setupRatingElements() {
 42+ var ratings = {};
 43+
2444 $.each($(".allrating"), function(i,v) {
2545 var self = $(this);
2646
2747 self.allRating({
2848 onClickEvent: function(input) {
29 - alert(input.val());
 49+ window.ratings.submitRating( self.attr( 'page' ), self.attr( 'tag' ), input.val() );
3050 },
3151 showHover: false
32 - });
 52+ });
 53+
 54+ if ( !ratings[self.attr( 'page' )] ) {
 55+ ratings[self.attr( 'page' )] = [];
 56+ }
 57+
 58+ ratings[self.attr( 'page' )].push( self.attr( 'tag' ) );
3359 });
 60+
 61+ for ( i in ratings ) {
 62+ window.ratings.getRatingsForPage( i, $.unique( ratings[i] ), initRatingElementsForPage );
 63+ }
 64+
3465 })();
3566
3667 } ); })(jQuery);
\ No newline at end of file
Index: trunk/extensions/Ratings/starrating/RatingsStars.php
@@ -163,6 +163,8 @@
164164 'jQuery'
165165 );
166166
 167+ Ratings::loadJs( $this->parser );
 168+
167169 $this->parser->getOutput()->addHeadItem(
168170 Html::linkedScript( $egRatingsScriptPath . '/starrating/star-rating/jquery.rating.js' )
169171 . Html::linkedStyle( $egRatingsScriptPath . '/starrating/star-rating/jquery.rating.css' ),
Index: trunk/extensions/Ratings/starrating/ext.ratings.stars.js
@@ -34,7 +34,7 @@
3535 $( "input[name='" + groups[i] + "']" ).rating({
3636 callback: function( value, link ){
3737 var self = $(this);
38 - submitRating( self.attr( 'page' ), self.attr( 'tag' ), value );
 38+ ratings.submitRating( self.attr( 'page' ), self.attr( 'tag' ), value );
3939 }
4040 });
4141 }
@@ -53,7 +53,6 @@
5454 }
5555 });
5656 }
57 -
5857 })();
5958
6059 /**
@@ -78,39 +77,11 @@
7978 });
8079
8180 for ( i in ratings ) {
82 - getRatingsForPage( i, $.unique( ratings[i] ) );
 81+ window.ratings.getRatingsForPage( i, $.unique( ratings[i] ), initRatingElementsForPage );
8382 }
8483 }
8584
8685 /**
87 - * Obtain the vote values for a set of tags of a single page,
88 - * and then find and update the corresponding rating stars.
89 - *
90 - * @param {string} page
91 - * @param {Array} tags
92 - */
93 - function getRatingsForPage( page, tags ) {
94 - $.getJSON(
95 - wgScriptPath + '/api.php',
96 - {
97 - 'action': 'query',
98 - 'format': 'json',
99 - 'list': 'ratings',
100 - 'qrpage': page,
101 - 'qrtags': tags.join( '|' )
102 - },
103 - function( data ) {
104 - if ( data.userratings ) {
105 - initRatingElementsForPage( page, data.userratings );
106 - }
107 - else {
108 - // TODO
109 - }
110 - }
111 - );
112 - }
113 -
114 - /**
11586 * Loop over all rating elements for the page and set their value when available.
11687 *
11788 * @param {string} page
@@ -128,36 +99,4 @@
129100 });
130101 }
131102
132 - /**
133 - * Submit a rating.
134 - *
135 - * @param {string} page
136 - * @param {string} tag
137 - * @param {integer} value
138 - */
139 - function submitRating( page, tag, value ) {
140 - $.post(
141 - wgScriptPath + '/api.php',
142 - {
143 - 'action': 'dorating',
144 - 'format': 'json',
145 - 'pagename': page,
146 - 'tag': tag,
147 - 'value': value
148 - },
149 - function( data ) {
150 - if ( data.error && data.error.info ) {
151 - alert( data.error.info );
152 - }
153 - else if ( data.result.success ) {
154 - // TODO
155 - }
156 - else {
157 - alert( 'Failed to submit rating' ) // TODO
158 - }
159 - },
160 - 'json'
161 - );
162 - }
163 -
164103 } ); })(jQuery);
\ No newline at end of file

Status & tagging log