Index: trunk/extensions/SemanticImageInput/resources/jquery.instantImage.js |
— | — | @@ -7,9 +7,16 @@ |
8 | 8 | * @author Jeroen De Dauw <jeroendedauw at gmail dot com> |
9 | 9 | */ |
10 | 10 | |
11 | | -(function( $, mw ) { |
| 11 | +(function( $, mw ) { $.fn.instantImage = function( options ) { |
| 12 | + |
| 13 | + var settings = $.extend( { |
| 14 | + 'imagename': 'Beatles', |
| 15 | + 'inputname': '', |
| 16 | + 'apipath': 'https://en.wikipedia.org/w/api.php?callback=?', |
| 17 | + 'imagewidth': 200 |
| 18 | + }, options ); |
12 | 19 | |
13 | | - $.fn.instantImage = function( opts ) { |
| 20 | + return this.each( function() { |
14 | 21 | |
15 | 22 | var _this = this; |
16 | 23 | var $this = $( this ); |
— | — | @@ -18,25 +25,18 @@ |
19 | 26 | this.images = null; |
20 | 27 | this.raw = null; |
21 | 28 | |
22 | | - this.options = { |
23 | | - 'imagename': 'Beatles', |
24 | | - 'inputname': '', |
25 | | - 'apipath': 'https://en.wikipedia.org/w/api.php?callback=?', |
26 | | - 'imagewidth': 200 |
27 | | - }; |
28 | | - |
29 | 29 | this.getMainTitle = function( callback ) { |
30 | 30 | $.getJSON( |
31 | | - this.options.apipath, |
| 31 | + settings.apipath, |
32 | 32 | { |
33 | 33 | 'action': 'query', |
34 | 34 | 'format': 'json', |
35 | | - 'titles': this.options.imagename, |
| 35 | + 'titles': settings.imagename, |
36 | 36 | 'redirects': 1 |
37 | 37 | }, |
38 | 38 | function( data ) { |
39 | 39 | if ( data.query && data.query.redirects ) { |
40 | | - _this.options.imagename = data.query.redirects[0].to; |
| 40 | + settings.imagename = data.query.redirects[0].to; |
41 | 41 | } |
42 | 42 | |
43 | 43 | callback(); |
— | — | @@ -46,12 +46,12 @@ |
47 | 47 | |
48 | 48 | this.getImages = function( callback ) { |
49 | 49 | $.getJSON( |
50 | | - this.options.apipath, |
| 50 | + settings.apipath, |
51 | 51 | { |
52 | 52 | 'action': 'query', |
53 | 53 | 'format': 'json', |
54 | 54 | 'prop': 'images', |
55 | | - 'titles': this.options.imagename, |
| 55 | + 'titles': settings.imagename, |
56 | 56 | 'redirects': 1, |
57 | 57 | 'imlimit': 500 |
58 | 58 | }, |
— | — | @@ -81,13 +81,13 @@ |
82 | 82 | |
83 | 83 | this.getRaw = function( callback ) { |
84 | 84 | $.getJSON( |
85 | | - this.options.apipath, |
| 85 | + settings.apipath, |
86 | 86 | { |
87 | 87 | 'action': 'query', |
88 | 88 | 'format': 'json', |
89 | 89 | 'prop': 'revisions', |
90 | 90 | 'rvprop': 'content', |
91 | | - 'titles': this.options.imagename |
| 91 | + 'titles': settings.imagename |
92 | 92 | }, |
93 | 93 | function( data ) { |
94 | 94 | if ( data.query ) { |
— | — | @@ -133,14 +133,14 @@ |
134 | 134 | } |
135 | 135 | |
136 | 136 | $.getJSON( |
137 | | - this.options.apipath, |
| 137 | + settings.apipath, |
138 | 138 | { |
139 | 139 | 'action': 'query', |
140 | 140 | 'format': 'json', |
141 | 141 | 'prop': 'imageinfo', |
142 | 142 | 'iiprop': 'url', |
143 | 143 | 'titles': image, |
144 | | - 'iiurlwidth': this.options.imagewidth |
| 144 | + 'iiurlwidth': settings.imagewidth |
145 | 145 | }, |
146 | 146 | function( data ) { |
147 | 147 | if ( data.query && data.query.pages ) { |
— | — | @@ -150,11 +150,11 @@ |
151 | 151 | var info = pages[p].imageinfo; |
152 | 152 | for ( i in info ) { |
153 | 153 | if ( info[i].thumburl.indexOf( '/wikipedia/commons/' ) !== -1 ) { |
154 | | - $( 'input[name="' + _this.options.inputname + '"]' ).val( image ); |
| 154 | + $( 'input[name="' + settings.inputname + '"]' ).val( image ); |
155 | 155 | |
156 | 156 | $this.html( $( '<img />' ).attr( { |
157 | 157 | 'src': info[i].thumburl, |
158 | | - 'width': _this.options.imagewidth + 'px' |
| 158 | + 'width': settings.imagewidth + 'px' |
159 | 159 | } ) ); |
160 | 160 | |
161 | 161 | return; |
— | — | @@ -180,11 +180,11 @@ |
181 | 181 | this.start = function() { |
182 | 182 | this.loadedFirstReq = false; |
183 | 183 | |
184 | | - if ( this.options.iteminput ) { |
185 | | - this.options.imagename = this.options.iteminput.val(); |
| 184 | + if ( settings.iteminput ) { |
| 185 | + settings.imagename = settings.iteminput.val(); |
186 | 186 | } |
187 | 187 | |
188 | | - if ( this.options.imagename.trim() === '' ) { |
| 188 | + if ( settings.imagename.trim() === '' ) { |
189 | 189 | $this.html( '' ); |
190 | 190 | } |
191 | 191 | else { |
— | — | @@ -196,18 +196,14 @@ |
197 | 197 | }; |
198 | 198 | |
199 | 199 | this.init = function() { |
200 | | - $.extend( this.options, opts ); |
201 | | - |
202 | | - if ( this.options.iteminput ) { |
203 | | - this.options.iteminput.change( function() { _this.start(); } ); |
| 200 | + if ( settings.iteminput ) { |
| 201 | + settings.iteminput.change( function() { _this.start(); } ); |
204 | 202 | } |
205 | 203 | |
206 | 204 | this.start(); |
207 | 205 | }; |
208 | 206 | |
209 | 207 | this.init(); |
210 | | - |
211 | | - return this; |
212 | | - }; |
| 208 | + } ); |
213 | 209 | |
214 | | -})( window.jQuery, window.mediaWiki ); |
| 210 | +}; })( window.jQuery, window.mediaWiki ); |