mediawiki-extensions-Cookie.../resources/ext.CookieWarning/ext.CookieWarning.js
Ostrzyciel edd0a7d949 Move the warning outside sitenotice, tidy up code
There's a lot of things going on here, so a quick summary:
* Moved the warning outside sitenotice to avoid reflows and all
  kinds of other issues, including those with dismissable notices.
* Placed the warning on the bottom of the page, on all skins, for
  consistency and to avoid obstructing vital UI elements such as
  site name, user tools, search box.
* Changed the UI to use OOUI for, again, consistency and to avoid
  reinventing the wheel with button styling.
* Removed the cookie emoji as it was only taking up valuable space
  on mobile and making designing a sensible layout much harder.
* Merged the mobile and desktop RL modules into a single, responsive
  one. The warning will work correctly for any skin and screen width.
* The integration tests are truly horrible, I tried to at least
  make them work. They deserve a proper rewrite, but that should be
  done after the extension switches to the new hook system.

I will post screenshots of this in action in the first of the linked
tasks.

Bug: T271047
Bug: T173335
Change-Id: I6e2a3d7aeccc0d4df1b3238e52c67e72099d27d8
2021-02-01 11:23:53 +01:00

39 lines
1.3 KiB
JavaScript

( function ( mw, $ ) {
/**
* Sets the cookie, that the cookiewarning is dismissed. Called,
* when the api query to save this information in the user preferences,
* failed for any reason, or the user is not logged-in.
*/
function setCookie() {
mw.cookie.set( 'cookiewarning_dismissed', true );
}
$( function () {
if ( mw.cookie.get( 'cookiewarning_dismissed' ) ) {
$( '.mw-cookiewarning-container' ).detach();
} else {
// Click handler for the "Ok" element in the cookiewarning information bar
$( '.mw-cookiewarning-container button' ).on( 'click', function ( ev ) {
// an anonymous user doesn't have preferences, so don't try to save this in
// the user preferences.
if ( !mw.user.isAnon() ) {
// try to save, that the cookiewarning was disabled, in the user preferences
new mw.Api().saveOption( 'cookiewarning_dismissed', '1' )
.fail( function ( code, result ) {
// if it fails, fall back to the cookie
mw.log.warn( 'Failed to save dismissed CookieWarning: ' + code + '\n' + result.error + '. Using cookie now.' );
setCookie();
} );
} else {
// use cookies for anonymous users
setCookie();
}
// always remove the cookiewarning element
$( '.mw-cookiewarning-container' ).detach();
ev.preventDefault();
} );
}
} );
}( mediaWiki, jQuery ) );