Index: trunk/extensions/CentralNotice/newCentralNotice.js |
— | — | @@ -36,6 +36,7 @@ |
37 | 37 | if ( timestamp ) { |
38 | 38 | listURL = "TBD" |
39 | 39 | } else { |
| 40 | + // http://geoiplookup.wikimedia.org/ |
40 | 41 | var geoLocation = 'US'; // Hard-coding for now |
41 | 42 | var bannerListPage = 'Special:BannerListLoader?language='+wgContentLanguage+'&project='+wgNoticeProject+'&location='+geoLocation; |
42 | 43 | //centralized version: |
— | — | @@ -45,19 +46,40 @@ |
46 | 47 | var request = $.ajax( { |
47 | 48 | url: bannerListURL, |
48 | 49 | dataType: 'json', |
49 | | - success: function( data ) { |
50 | | - $.centralNotice.fn.chooseBanner( data ); |
51 | | - } |
| 50 | + success: $.centralNotice.fn.chooseBanner |
52 | 51 | } ); |
53 | 52 | }, |
54 | 53 | 'chooseBanner': function( bannerList ) { |
55 | | - // pick a banner based on logged-in status and geotargetting |
56 | | - var bannerHTML = bannerList[0].html; |
57 | | - $.centralNotice.fn.displayBanner( bannerHTML ); |
| 54 | + // convert the json object to a true array |
| 55 | + bannerList = Array.prototype.slice.call( bannerList ); |
| 56 | + |
| 57 | + // Make sure there are some banners to choose from |
| 58 | + if ( bannerList.length == 0 ) return false; |
| 59 | + |
| 60 | + var groomedBannerList = []; |
| 61 | + |
| 62 | + for( var i = 0; i < bannerList.length; i++ ) { |
| 63 | + // only include this banner if it's inteded for the current user |
| 64 | + if( ( wgUserName ? bannerList[i].display_account == 1 : bannerList.display_anon == 1 ) ) { |
| 65 | + // add the banner to our list once per weight |
| 66 | + for( var j=0; j < bannerList[i].weight; j++ ) { |
| 67 | + groomedBannerList.push( bannerList[i] ); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // return if there's nothing left after the grooming |
| 73 | + if( groomedBannerList.length == 0 ) return false; |
| 74 | + // load a random banner from our groomed list |
| 75 | + |
| 76 | + $.centralNotice.fn.loadBanner( |
| 77 | + groomedBannerList[ Math.floor( Math.random() * groomedBannerList.length ) ].name |
| 78 | + ); |
58 | 79 | }, |
59 | 80 | 'displayBanner': function( bannerHTML ) { |
60 | 81 | // inject the banner html into the page |
61 | | - $( '#siteNotice' ).prepend('<div id="centralnotice" class="'+(wgNoticeToggleState ? 'expanded' : 'collapsed')+'">'+bannerHTML+'</div>'); |
| 82 | + $( '#siteNotice' ) |
| 83 | + .prepend( '<div id="centralnotice" class="' + ( wgNoticeToggleState ? 'expanded' : 'collapsed' ) + '">' + bannerHTML + '</div>' ); |
62 | 84 | }, |
63 | 85 | 'getQueryStringVariables': function() { |
64 | 86 | document.location.search.replace( /\??(?:([^=]+)=([^&]*)&?)/g, function () { |