SECURITY: Better controls for logout interface buttons
[lhc/web/wiklou.git] / resources / src / mediawiki.page.ready / ready.js
1 var checkboxShift = require( './checkboxShift.js' );
2 mw.hook( 'wikipage.content' ).add( function ( $content ) {
3 var $sortable, $collapsible;
4
5 $collapsible = $content.find( '.mw-collapsible' );
6 if ( $collapsible.length ) {
7 // Preloaded by Skin::getDefaultModules()
8 mw.loader.using( 'jquery.makeCollapsible', function () {
9 $collapsible.makeCollapsible();
10 } );
11 }
12
13 $sortable = $content.find( 'table.sortable' );
14 if ( $sortable.length ) {
15 // Preloaded by Skin::getDefaultModules()
16 mw.loader.using( 'jquery.tablesorter', function () {
17 $sortable.tablesorter();
18 } );
19 }
20
21 checkboxShift( $content.find( 'input[type="checkbox"]:not(.noshiftselect)' ) );
22 } );
23
24 // Handle elements outside the wikipage content
25 $( function () {
26 var $nodes;
27
28 // Add accesskey hints to the tooltips
29 $( '[accesskey]' ).updateTooltipAccessKeys();
30
31 $nodes = $( '.catlinks[data-mw="interface"]' );
32 if ( $nodes.length ) {
33 /**
34 * Fired when categories are being added to the DOM
35 *
36 * It is encouraged to fire it before the main DOM is changed (when $content
37 * is still detached). However, this order is not defined either way, so you
38 * should only rely on $content itself.
39 *
40 * This includes the ready event on a page load (including post-edit loads)
41 * and when content has been previewed with LivePreview.
42 *
43 * @event wikipage_categories
44 * @member mw.hook
45 * @param {jQuery} $content The most appropriate element containing the content,
46 * such as .catlinks
47 */
48 mw.hook( 'wikipage.categories' ).fire( $nodes );
49 }
50
51 $( '#t-print a' ).on( 'click', function ( e ) {
52 window.print();
53 e.preventDefault();
54 } );
55
56 // Turn logout to a POST action
57 $( '#pt-logout a[data-mw="interface"]' ).on( 'click', function ( e ) {
58 var api = new mw.Api(),
59 url = this.href;
60 mw.notify(
61 mw.message( 'logging-out-notify' ),
62 { tag: 'logout', autoHide: false }
63 );
64 api.postWithToken( 'csrf', {
65 action: 'logout'
66 } ).then(
67 function () {
68 location.href = url;
69 },
70 function ( err ) {
71 mw.notify(
72 mw.message( 'logout-failed', err ),
73 { type: 'error', tag: 'logout', autoHide: false }
74 );
75 }
76 );
77 e.preventDefault();
78 } );
79 } );