Index: trunk/extensions/CentralNotice/special/SpecialBannerController.php |
— | — | @@ -50,77 +50,88 @@ |
51 | 51 | ( function( $ ) { |
52 | 52 | $.ajaxSetup({ cache: true }); |
53 | 53 | $.centralNotice = { |
54 | | - 'data': { |
55 | | - 'getVars': {}, |
56 | | - 'bannerType': 'default' |
| 54 | + data: { |
| 55 | + getVars: {}, |
| 56 | + bannerType: 'default' |
57 | 57 | }, |
58 | | - 'fn': { |
59 | | - 'loadBanner': function( bannerName, campaign, bannerType ) { |
| 58 | + fn: { |
| 59 | + loadBanner: function( bannerName, campaign, bannerType ) { |
| 60 | + var bannerPageQuery, bannerPage, bannerScript; |
| 61 | + |
60 | 62 | // Store the bannerType in case we need to set a banner hiding cookie later |
61 | 63 | $.centralNotice.data.bannerType = bannerType; |
62 | 64 | // Get the requested banner |
63 | | - var bannerPageQuery = $.param( { |
64 | | - 'banner': bannerName, 'campaign': campaign, 'userlang': wgUserLanguage, |
65 | | - 'db': wgDBname, 'sitename': wgSiteName, 'country': Geo.country |
| 65 | + bannerPageQuery = $.param( { |
| 66 | + banner: bannerName, |
| 67 | + campaign: campaign, |
| 68 | + userlang: mw.config.get( 'wgUserLanguage' ), |
| 69 | + db: mw.config.get( 'wgDBname' ), |
| 70 | + sitename: mw.config.get( 'wgSiteName' ), |
| 71 | + country: Geo.country |
66 | 72 | } ); |
67 | | - var bannerPage = '?title=Special:BannerLoader&' + bannerPageQuery; |
| 73 | + bannerPage = '?title=Special:BannerLoader&' + bannerPageQuery; |
68 | 74 | JAVASCRIPT; |
69 | | - $js .= "\n\t\t\t\tvar bannerScript = '<script type=\"text/javascript\" src=\"" . |
| 75 | + $js .= "\n\t\t\t\tbannerScript = '<script type=\"text/javascript\" src=\"" . |
70 | 76 | Xml::escapeJsString( $wgCentralPagePath ) . |
71 | 77 | "' + bannerPage + '\"></script>';\n"; |
72 | 78 | $js .= <<<JAVASCRIPT |
73 | | - if ( document.cookie.indexOf( 'centralnotice_'+bannerType+'=hide' ) == -1 ) { |
| 79 | + if ( document.cookie.indexOf( 'centralnotice_' + bannerType + '=hide' ) === -1 ) { |
74 | 80 | jQuery( '#siteNotice' ).prepend( '<div id="centralNotice" class="' + |
75 | 81 | ( wgNoticeToggleState ? 'expanded' : 'collapsed' ) + |
76 | 82 | ' cn-' + bannerType + '">'+bannerScript+'</div>' ); |
77 | 83 | } |
78 | 84 | }, |
79 | | - 'loadBannerList': function( geoOverride ) { |
| 85 | + loadBannerList: function( geoOverride ) { |
| 86 | + var geoLocation, bannerListQuery, bannerListURL; |
| 87 | + |
80 | 88 | if ( geoOverride ) { |
81 | | - var geoLocation = geoOverride; // override the geo info |
| 89 | + geoLocation = geoOverride; // override the geo info |
82 | 90 | } else { |
83 | | - var geoLocation = Geo.country; // pull the geo info |
| 91 | + geoLocation = Geo.country; // pull the geo info |
84 | 92 | } |
85 | | - var bannerListQuery = $.param( { 'language': wgContentLanguage, 'project': wgNoticeProject, 'country': geoLocation } ); |
| 93 | + bannerListQuery = $.param( { |
| 94 | + language: mw.config.get( 'wgContentLanguage' ), |
| 95 | + project: mw.config.get( 'wgNoticeProject' ), |
| 96 | + country: geoLocation |
| 97 | + } ); |
86 | 98 | JAVASCRIPT; |
87 | | - $js .= "\n\t\t\t\tvar bannerListURL = wgScript + '?title=' + encodeURIComponent('" . |
| 99 | + $js .= "\n\t\t\t\tbannerListURL = mw.util.wikiScript() + '?title=' + encodeURIComponent('" . |
88 | 100 | $wgContLang->specialPage( 'BannerListLoader' ) . |
89 | 101 | "') + '&cache=/cn.js&' + bannerListQuery;\n"; |
90 | 102 | $js .= <<<JAVASCRIPT |
91 | | - if ( wgNamespaceNumber !== -1 ) { // disable loading banners on Special pages |
92 | | - var request = $.ajax( { |
93 | | - url: bannerListURL, |
94 | | - dataType: 'json', |
95 | | - success: $.centralNotice.fn.chooseBanner |
96 | | - } ); |
97 | | - } |
| 103 | + $.ajax( { |
| 104 | + url: bannerListURL, |
| 105 | + dataType: 'json', |
| 106 | + success: $.centralNotice.fn.chooseBanner |
| 107 | + } ); |
98 | 108 | }, |
99 | | - 'chooseBanner': function( bannerList ) { |
100 | | - // Convert the json object to a true array |
101 | | - bannerList = Array.prototype.slice.call( bannerList ); |
| 109 | + chooseBanner: function( bannerList ) { |
| 110 | + var groomedBannerList = [], i, j, pointer; |
102 | 111 | |
103 | 112 | // Make sure there are some banners to choose from |
104 | | - if ( bannerList.length == 0 ) return false; |
| 113 | + if ( bannerList.length === 0 ) { |
| 114 | + return false; |
| 115 | + } |
105 | 116 | |
106 | | - var groomedBannerList = []; |
107 | | - |
108 | | - for( var i = 0; i < bannerList.length; i++ ) { |
| 117 | + for( i = 0; i < bannerList.length; i++ ) { |
109 | 118 | // Only include this banner if it's intended for the current user |
110 | | - if( ( wgUserName && bannerList[i].display_account ) || |
111 | | - ( !wgUserName && bannerList[i].display_anon == 1 ) ) |
| 119 | + if( ( !mw.user.anonymous() && bannerList[i].display_account === 1 ) || |
| 120 | + ( mw.user.anonymous() && bannerList[i].display_anon === 1 ) ) |
112 | 121 | { |
113 | | - // add the banner to our list once per weight |
114 | | - for( var j=0; j < bannerList[i].weight; j++ ) { |
| 122 | + // Add the banner to our list once per weight |
| 123 | + for( j = 0; j < bannerList[i].weight; j++ ) { |
115 | 124 | groomedBannerList.push( bannerList[i] ); |
116 | 125 | } |
117 | 126 | } |
118 | 127 | } |
119 | 128 | |
120 | 129 | // Return if there's nothing left after the grooming |
121 | | - if( groomedBannerList.length == 0 ) return false; |
| 130 | + if ( groomedBannerList.length === 0 ) { |
| 131 | + return false; |
| 132 | + } |
122 | 133 | |
123 | 134 | // Choose a random key |
124 | | - var pointer = Math.floor( Math.random() * groomedBannerList.length ); |
| 135 | + pointer = Math.floor( Math.random() * groomedBannerList.length ); |
125 | 136 | |
126 | 137 | // Load a random banner from our groomed list |
127 | 138 | $.centralNotice.fn.loadBanner( |
— | — | @@ -129,27 +140,29 @@ |
130 | 141 | ( groomedBannerList[pointer].fundraising ? 'fundraising' : 'default' ) |
131 | 142 | ); |
132 | 143 | }, |
133 | | - 'getQueryStringVariables': function() { |
134 | | - document.location.search.replace( /\??(?:([^=]+)=([^&]*)&?)/g, function () { |
135 | | - function decode( s ) { |
136 | | - return decodeURIComponent( s.split( "+" ).join( " " ) ); |
137 | | - } |
138 | | - $.centralNotice.data.getVars[decode( arguments[1] )] = decode( arguments[2] ); |
| 144 | + getQueryStringVariables: function() { |
| 145 | + function decode( s ) { |
| 146 | + return decodeURIComponent( s.split( '+' ).join( ' ' ) ); |
| 147 | + } |
| 148 | + document.location.search.replace( /\??(?:([^=]+)=([^&]*)&?)/g, function ( str, p1, p2 ) { |
| 149 | + $.centralNotice.data.getVars[decode( p1 )] = decode( p2 ); |
139 | 150 | } ); |
140 | 151 | } |
141 | 152 | } |
142 | | - } |
143 | | - jQuery( document ).ready( function ( $ ) { |
| 153 | + }; |
| 154 | + |
| 155 | + $( document ).ready( function ( $ ) { |
144 | 156 | // Initialize the query string vars |
145 | 157 | $.centralNotice.fn.getQueryStringVariables(); |
146 | | - if( $.centralNotice.data.getVars['banner'] ) { |
| 158 | + if( $.centralNotice.data.getVars.banner ) { |
147 | 159 | // if we're forcing one banner |
148 | | - $.centralNotice.fn.loadBanner( $.centralNotice.data.getVars['banner'] ); |
| 160 | + $.centralNotice.fn.loadBanner( $.centralNotice.data.getVars.banner ); |
149 | 161 | } else { |
150 | 162 | // Look for banners ready to go NOW |
151 | | - $.centralNotice.fn.loadBannerList( $.centralNotice.data.getVars['country'] ); |
| 163 | + $.centralNotice.fn.loadBannerList( $.centralNotice.data.getVars.country ); |
152 | 164 | } |
153 | | - } ); //document ready |
| 165 | + } ); |
| 166 | + |
154 | 167 | } )( jQuery ); |
155 | 168 | JAVASCRIPT; |
156 | 169 | return $js; |
— | — | @@ -157,7 +170,7 @@ |
158 | 171 | } |
159 | 172 | |
160 | 173 | function getToggleScripts() { |
161 | | - $script = "var wgNoticeToggleState = (document.cookie.indexOf('hidesnmessage=1')==-1);\n\n"; |
| 174 | + $script = "var wgNoticeToggleState = document.cookie.indexOf( 'hidesnmessage=1' ) === -1;\n\n"; |
162 | 175 | return $script; |
163 | 176 | } |
164 | 177 | |
— | — | @@ -165,38 +178,44 @@ |
166 | 179 | global $wgNoticeFundraisingUrl; |
167 | 180 | $script = <<<JAVASCRIPT |
168 | 181 | function insertBanner( bannerJson ) { |
| 182 | + var url, targets; |
| 183 | + |
169 | 184 | jQuery( 'div#centralNotice' ).prepend( bannerJson.bannerHtml ); |
170 | 185 | if ( bannerJson.autolink ) { |
171 | 186 | JAVASCRIPT; |
172 | | - $script .= "\n\t\tvar url = '" . |
| 187 | + $script .= "\n\t\turl = '" . |
173 | 188 | Xml::escapeJsString( $wgNoticeFundraisingUrl ) . "';\n"; |
174 | 189 | $script .= <<<JAVASCRIPT |
175 | 190 | if ( ( bannerJson.landingPages !== null ) && bannerJson.landingPages.length ) { |
176 | 191 | targets = String( bannerJson.landingPages ).split(','); |
177 | 192 | url += "?" + jQuery.param( { |
178 | | - 'landing_page': targets[Math.floor( Math.random() * targets.length )].replace( /^\s+|\s+$/, '' ) |
| 193 | + landing_page: targets[Math.floor( Math.random() * targets.length )].replace( /^\s+|\s+$/, '' ) |
179 | 194 | } ); |
180 | 195 | url += "&" + jQuery.param( { |
181 | | - 'utm_medium': 'sitenotice', 'utm_campaign': bannerJson.campaign, |
182 | | - 'utm_source': bannerJson.bannerName, 'language': wgUserLanguage, |
183 | | - 'country': Geo.country |
| 196 | + utm_medium: 'sitenotice', |
| 197 | + utm_campaign: bannerJson.campaign, |
| 198 | + utm_source: bannerJson.bannerName, |
| 199 | + language: mw.config.get( 'wgUserLanguage' ), |
| 200 | + country: Geo.country |
184 | 201 | } ); |
185 | 202 | jQuery( '#cn-landingpage-link' ).attr( 'href', url ); |
186 | 203 | } |
187 | 204 | } |
188 | 205 | } |
189 | | -function hideBanner() { |
190 | | - jQuery( '#centralNotice' ).hide(); // Hide current banner |
191 | | - var bannerType = $.centralNotice.data.bannerType; |
192 | | - if ( bannerType === undefined ) bannerType = 'default'; |
193 | | - setBannerHidingCookie( bannerType ); // Hide future banners of the same type |
194 | | -} |
195 | 206 | function setBannerHidingCookie( bannerType ) { |
196 | 207 | var e = new Date(); |
197 | 208 | e.setTime( e.getTime() + (14*24*60*60*1000) ); // two weeks |
198 | | - var work='centralnotice_'+bannerType+'=hide; expires=' + e.toGMTString() + '; path=/'; |
| 209 | + var work = 'centralnotice_' + bannerType + '=hide; expires=' + e.toGMTString() + '; path=/'; |
199 | 210 | document.cookie = work; |
200 | 211 | } |
| 212 | +function hideBanner() { |
| 213 | + jQuery( '#centralNotice' ).hide(); // Hide current banner |
| 214 | + var bannerType = jQuery.centralNotice.data.bannerType; |
| 215 | + if ( bannerType === undefined ) { |
| 216 | + bannerType = 'default'; |
| 217 | + } |
| 218 | + setBannerHidingCookie( bannerType ); // Hide future banners of the same type |
| 219 | +} |
201 | 220 | // This function is deprecated |
202 | 221 | function toggleNotice() { |
203 | 222 | hideBanner(); |