Index: trunk/extensions/Ratings/Ratings.php |
— | — | @@ -91,6 +91,13 @@ |
92 | 92 | 'remoteBasePath' => $egRatingsScriptPath |
93 | 93 | ); |
94 | 94 | |
| 95 | + $wgResourceModules['ext.ratings.common'] = $moduleTemplate + array( |
| 96 | + 'scripts' => array( |
| 97 | + 'js/ext.ratings.common.js' |
| 98 | + ), |
| 99 | + 'messages' => $egRatingsStarsJSMessages |
| 100 | + ); |
| 101 | + |
95 | 102 | $wgResourceModules['ext.ratings.stars'] = $moduleTemplate + array( |
96 | 103 | 'styles' => array( |
97 | 104 | 'starrating/star-rating/jquery.rating.css' |
— | — | @@ -99,8 +106,7 @@ |
100 | 107 | 'starrating/star-rating/jquery.rating.js', |
101 | 108 | 'starrating/ext.ratings.stars.js' |
102 | 109 | ), |
103 | | - 'dependencies' => array(), |
104 | | - 'messages' => $egRatingsStarsJSMessages |
| 110 | + 'dependencies' => array( 'ext.ratings.common' ), |
105 | 111 | ); |
106 | 112 | |
107 | 113 | $wgResourceModules['ext.ratings.allrating'] = $moduleTemplate + array( |
— | — | @@ -111,8 +117,7 @@ |
112 | 118 | 'allrating/js/jquery.allRating.js', |
113 | 119 | 'allrating/ext.ratings.allrating.js' |
114 | 120 | ), |
115 | | - 'dependencies' => array(), |
116 | | - 'messages' => $egRatingsStarsJSMessages |
| 121 | + 'dependencies' => array( 'ext.ratings.common' ), |
117 | 122 | ); |
118 | 123 | } |
119 | 124 | |
Index: trunk/extensions/Ratings/Ratings.class.php |
— | — | @@ -176,4 +176,20 @@ |
177 | 177 | return $tags; |
178 | 178 | } |
179 | 179 | |
| 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 | + |
180 | 196 | } |
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 |
1 | 74 | + native |
Index: trunk/extensions/Ratings/allrating/RatingsAllRating.php |
— | — | @@ -166,6 +166,8 @@ |
167 | 167 | 'jQuery' |
168 | 168 | ); |
169 | 169 | |
| 170 | + Ratings::loadJs( $this->parser ); |
| 171 | + |
170 | 172 | $this->parser->getOutput()->addHeadItem( |
171 | 173 | Html::linkedScript( $egRatingsScriptPath . '/allrating/js/jquery.allRating.js' ) |
172 | 174 | . Html::linkedStyle( $egRatingsScriptPath . '/allrating/css/allRating.css' ), |
Index: trunk/extensions/Ratings/allrating/js/jquery.allRating.js |
— | — | @@ -71,6 +71,21 @@ |
72 | 72 | |
73 | 73 | /**Main Code**/ |
74 | 74 | $.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 | + |
75 | 90 | var config = $.extend({}, $.rating.defaults, options); |
76 | 91 | |
77 | 92 | this.each( function() { |
— | — | @@ -106,6 +121,7 @@ |
107 | 122 | $.fn.allRating.fetchInput = function(item, config, index ) { |
108 | 123 | |
109 | 124 | var input = {}; |
| 125 | + this.config = config; |
110 | 126 | |
111 | 127 | //fetch input details |
112 | 128 | input.item = $(item); |
— | — | @@ -184,6 +200,7 @@ |
185 | 201 | } |
186 | 202 | |
187 | 203 | $.fn.allRating.addActiveStars = function(input, config, original) { |
| 204 | + |
188 | 205 | if(original) { |
189 | 206 | $('#' + input.ratingId + ' .'+config.segmentContainerClass).find('a[rel="' + input.selectedValue + '"]').addClass('original'); |
190 | 207 | } |
— | — | @@ -193,7 +210,15 @@ |
194 | 211 | $(this).removeClass(config.segmentClassNonActiveClass).addClass(config.segmentActiveClass); |
195 | 212 | }); |
196 | 213 | }; |
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 | + |
198 | 223 | $.fn.allRating.initEvents = function(input, config) { |
199 | 224 | $.fn.allRating.initClickEvents(input, config); |
200 | 225 | $.fn.allRating.initHoverEvents(input, config); |
— | — | @@ -231,6 +256,7 @@ |
232 | 257 | $.fn.allRating.updateInput = function(inputId, newValue, config){ |
233 | 258 | switch(config.input) { |
234 | 259 | case 'select' : |
| 260 | + alert(inputId); |
235 | 261 | $(inputId).val(newValue); |
236 | 262 | return $(inputId); //return the original input |
237 | 263 | break; |
— | — | @@ -246,7 +272,6 @@ |
247 | 273 | } |
248 | 274 | }; |
249 | 275 | |
250 | | - |
251 | 276 | $.fn.allRating.findTextFromValue = function(input, value, config) { |
252 | 277 | switch(config.input) { |
253 | 278 | case 'select': |
Index: trunk/extensions/Ratings/allrating/ext.ratings.allrating.js |
— | — | @@ -17,19 +17,50 @@ |
18 | 18 | } |
19 | 19 | |
20 | 20 | /** |
| 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 | + /** |
21 | 39 | * Self executing function to setup the allrating rating elements on the page. |
22 | 40 | */ |
23 | 41 | (function setupRatingElements() { |
| 42 | + var ratings = {}; |
| 43 | + |
24 | 44 | $.each($(".allrating"), function(i,v) { |
25 | 45 | var self = $(this); |
26 | 46 | |
27 | 47 | self.allRating({ |
28 | 48 | onClickEvent: function(input) { |
29 | | - alert(input.val()); |
| 49 | + window.ratings.submitRating( self.attr( 'page' ), self.attr( 'tag' ), input.val() ); |
30 | 50 | }, |
31 | 51 | 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' ) ); |
33 | 59 | }); |
| 60 | + |
| 61 | + for ( i in ratings ) { |
| 62 | + window.ratings.getRatingsForPage( i, $.unique( ratings[i] ), initRatingElementsForPage ); |
| 63 | + } |
| 64 | + |
34 | 65 | })(); |
35 | 66 | |
36 | 67 | } ); })(jQuery); |
\ No newline at end of file |
Index: trunk/extensions/Ratings/starrating/RatingsStars.php |
— | — | @@ -163,6 +163,8 @@ |
164 | 164 | 'jQuery' |
165 | 165 | ); |
166 | 166 | |
| 167 | + Ratings::loadJs( $this->parser ); |
| 168 | + |
167 | 169 | $this->parser->getOutput()->addHeadItem( |
168 | 170 | Html::linkedScript( $egRatingsScriptPath . '/starrating/star-rating/jquery.rating.js' ) |
169 | 171 | . Html::linkedStyle( $egRatingsScriptPath . '/starrating/star-rating/jquery.rating.css' ), |
Index: trunk/extensions/Ratings/starrating/ext.ratings.stars.js |
— | — | @@ -34,7 +34,7 @@ |
35 | 35 | $( "input[name='" + groups[i] + "']" ).rating({ |
36 | 36 | callback: function( value, link ){ |
37 | 37 | var self = $(this); |
38 | | - submitRating( self.attr( 'page' ), self.attr( 'tag' ), value ); |
| 38 | + ratings.submitRating( self.attr( 'page' ), self.attr( 'tag' ), value ); |
39 | 39 | } |
40 | 40 | }); |
41 | 41 | } |
— | — | @@ -53,7 +53,6 @@ |
54 | 54 | } |
55 | 55 | }); |
56 | 56 | } |
57 | | - |
58 | 57 | })(); |
59 | 58 | |
60 | 59 | /** |
— | — | @@ -78,39 +77,11 @@ |
79 | 78 | }); |
80 | 79 | |
81 | 80 | for ( i in ratings ) { |
82 | | - getRatingsForPage( i, $.unique( ratings[i] ) ); |
| 81 | + window.ratings.getRatingsForPage( i, $.unique( ratings[i] ), initRatingElementsForPage ); |
83 | 82 | } |
84 | 83 | } |
85 | 84 | |
86 | 85 | /** |
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 | | - /** |
115 | 86 | * Loop over all rating elements for the page and set their value when available. |
116 | 87 | * |
117 | 88 | * @param {string} page |
— | — | @@ -128,36 +99,4 @@ |
129 | 100 | }); |
130 | 101 | } |
131 | 102 | |
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 | | - |
164 | 103 | } ); })(jQuery); |
\ No newline at end of file |